#6276 createNewWarehouse methods migrated from silex to salix #1850

Merged
jorgep merged 158 commits from 6276-createNewWarehouse into dev 2024-03-06 11:32:11 +00:00
17 changed files with 49 additions and 139 deletions
Showing only changes of commit d5a3413307 - Show all commits

View File

@ -1,10 +1,10 @@
const UserError = require('vn-loopback/util/user-error'); const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('assignCollection', { Self.remoteMethodCtx('assign', {
description: 'Assign a collection', description: 'Assign a collection',
accessType: 'WRITE', accessType: 'WRITE',
http: { http: {
path: `/assignCollection`, path: `/assign`,
verb: 'POST' verb: 'POST'
}, },
returns: { returns: {
@ -13,7 +13,7 @@ module.exports = Self => {
}, },
}); });
Self.assignCollection = async(ctx, options) => { Self.assign = async(ctx, options) => {
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
const myOptions = {userId}; const myOptions = {userId};

View File

@ -1,5 +1,5 @@
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('getSalesFromTicketOrCollection', { Self.remoteMethodCtx('getSales', {
description: 'Get sales from ticket or collection', description: 'Get sales from ticket or collection',
accessType: 'READ', accessType: 'READ',
accepts: [ accepts: [
@ -23,12 +23,12 @@ module.exports = Self => {
root: true root: true
}, },
http: { http: {
path: `/getSalesFromTicketOrCollection`, path: `/getSales`,
verb: 'GET' verb: 'GET'
}, },
}); });
Self.getSalesFromTicketOrCollection = async(ctx, collectionOrTicketFk, print, source, options) => { Self.getSales = async(ctx, collectionOrTicketFk, print, source, options) => {
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
const myOptions = {userId}; const myOptions = {userId};
const $t = ctx.req.__; const $t = ctx.req.__;
@ -62,11 +62,11 @@ module.exports = Self => {
let observations = ticket.observaciones.split(' '); let observations = ticket.observaciones.split(' ');
for (let observation of observations) { for (let observation of observations) {
const salesMan = ticket.salesPersonFk; const salesPerson = ticket.salesPersonFk;
if (!observation.startsWith('#') && !observation.startsWith('@')) return; if (!observation.startsWith('#') && !observation.startsWith('@')) return;
await models.Chat.send(ctx, await models.Chat.send(ctx,
observation, observation,
$t('ticketCommercial', {ticket: ticket.ticketFk, salesMan}) $t('ticketCommercial', {ticket: ticket.ticketFk, salesPerson})
); );
} }
} }
@ -92,9 +92,8 @@ module.exports = Self => {
if (sale.ticketFk == ticketFk) { if (sale.ticketFk == ticketFk) {
sale.placements = []; sale.placements = [];
for (const salePlacement of placements) { for (const salePlacement of placements) {
let placement;
if (salePlacement.saleFk == sale.saleFk && salePlacement.order) { if (salePlacement.saleFk == sale.saleFk && salePlacement.order) {
placement = { const placement = {
saleFk: salePlacement.saleFk, saleFk: salePlacement.saleFk,
itemFk: salePlacement.itemFk, itemFk: salePlacement.itemFk,
placement: salePlacement.placement, placement: salePlacement.placement,

View File

@ -1,7 +1,7 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context'); const LoopBackContext = require('loopback-context');
describe('ticket assignCollection()', () => { describe('ticket assign()', () => {
let ctx; let ctx;
let options; let options;
let tx; let tx;
@ -30,7 +30,7 @@ describe('ticket assignCollection()', () => {
it('should throw an error when there is not picking tickets', async() => { it('should throw an error when there is not picking tickets', async() => {
try { try {
await models.Collection.assignCollection(ctx, options); await models.Collection.assign(ctx, options);
} catch (e) { } catch (e) {
expect(e.message).toEqual('There are not picking tickets'); expect(e.message).toEqual('There are not picking tickets');
} }

View File

@ -1,6 +1,6 @@
const {models} = require('vn-loopback/server/server'); const {models} = require('vn-loopback/server/server');
describe('collection getSalesFromTicketOrCollection()', () => { describe('collection getSales()', () => {
const collectionOrTicketFk = 999999; const collectionOrTicketFk = 999999;
const print = true; const print = true;
const source = 'CHECKER'; const source = 'CHECKER';
@ -18,7 +18,7 @@ describe('collection getSalesFromTicketOrCollection()', () => {
const tx = await models.Collection.beginTransaction({}); const tx = await models.Collection.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};
try { try {
const collection = await models.Collection.getSalesFromTicketOrCollection(ctx, const collection = await models.Collection.getSales(ctx,
collectionOrTicketFk, print, source, options); collectionOrTicketFk, print, source, options);
const [firstTicket] = collection.tickets; const [firstTicket] = collection.tickets;
@ -47,7 +47,7 @@ describe('collection getSalesFromTicketOrCollection()', () => {
try { try {
const printQueueBefore = await models.Collection.rawSql( const printQueueBefore = await models.Collection.rawSql(
query, [], options); query, [], options);
await models.Collection.getSalesFromTicketOrCollection(ctx, await models.Collection.getSales(ctx,
collectionOrTicketFk, true, source, options); collectionOrTicketFk, true, source, options);
const printQueueAfter = await models.Collection.rawSql( const printQueueAfter = await models.Collection.rawSql(
query, [], options); query, [], options);

View File

@ -50,13 +50,13 @@ module.exports = Self => {
if (machineWorker) { if (machineWorker) {
const {maxHours} = await models.MachineWorkerConfig.findOne({fields: ['maxHours']}, myOptions); const {maxHours} = await models.MachineWorkerConfig.findOne({fields: ['maxHours']}, myOptions);
const hoursDifference = (Date.vnNow() - machineWorker.inTime.getTime()) / (60 * 60 * 1000); const hoursDifference = (Date.vnNow() - machineWorker.inTime.getTime()) / (60 * 60 * 1000);
const isHimSelf = userId == machineWorker.workerFk; const isHimself = userId == machineWorker.workerFk;
const isSameMachine = machine.id == machineWorker.machineFk; const isSameMachine = machine.id == machineWorker.machineFk;
if (hoursDifference < maxHours && !isHimSelf) if (hoursDifference < maxHours && !isHimself)
throw new UserError($t('This machine is already in use.')); throw new UserError($t('This machine is already in use.'));
if (hoursDifference < maxHours && isHimSelf && !isSameMachine) if (hoursDifference < maxHours && isHimself && !isSameMachine)
throw new UserError($t('You are already using a machine')); throw new UserError($t('You are already using a machine'));
await machineWorker.updateAttributes({ await machineWorker.updateAttributes({

View File

@ -3,6 +3,6 @@ module.exports = Self => {
require('../methods/collection/setSaleQuantity')(Self); require('../methods/collection/setSaleQuantity')(Self);
require('../methods/collection/previousLabel')(Self); require('../methods/collection/previousLabel')(Self);
require('../methods/collection/getTickets')(Self); require('../methods/collection/getTickets')(Self);
require('../methods/collection/assignCollection')(Self); require('../methods/collection/assign')(Self);
require('../methods/collection/getSalesFromTicketOrCollection')(Self); require('../methods/collection/getSales')(Self);
}; };

View File

@ -1,6 +1,6 @@
INSERT INTO `salix`.`ACL` (model, property, accessType, permission, principalType, principalId) INSERT INTO `salix`.`ACL` (model, property, accessType, permission, principalType, principalId)
VALUES VALUES
('Collection', 'assignCollection', 'WRITE', 'ALLOW', 'ROLE', 'production'), ('Collection', 'assign', 'WRITE', 'ALLOW', 'ROLE', 'production'),
('ExpeditionPallet', 'getPallet', 'READ', 'ALLOW', 'ROLE', 'production'), ('ExpeditionPallet', 'getPallet', 'READ', 'ALLOW', 'ROLE', 'production'),
('MachineWorker','updateInTime','WRITE','ALLOW','ROLE','production'), ('MachineWorker','updateInTime','WRITE','ALLOW','ROLE','production'),
('MobileAppVersionControl','getVersion','READ','ALLOW','ROLE','production'), ('MobileAppVersionControl','getVersion','READ','ALLOW','ROLE','production'),
@ -8,7 +8,4 @@ INSERT INTO `salix`.`ACL` (model, property, accessType, permission, principalTyp
('SaleTracking','updateTracking','WRITE','ALLOW','ROLE','production'), ('SaleTracking','updateTracking','WRITE','ALLOW','ROLE','production'),
('SaleTracking','mark','WRITE','ALLOW','ROLE','production'), ('SaleTracking','mark','WRITE','ALLOW','ROLE','production'),
('ExpeditionPallet', '*', 'READ', 'ALLOW', 'ROLE', 'production'), ('ExpeditionPallet', '*', 'READ', 'ALLOW', 'ROLE', 'production'),
('Collection', 'assignCollection', 'WRITE', 'ALLOW', 'ROLE', 'production'), ('Sale', 'getFromSectorCollection', 'READ', 'ALLOW', 'ROLE', 'production');
('Sale', 'getFromSectorCollection', 'READ', 'ALLOW', 'ROLE', 'production'),
('Ticket', 'addSaleByCode', 'WRITE', 'ALLOW', 'ROLE', 'production');

View File

@ -1,5 +1,5 @@
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('return', { Self.remoteMethod('getAlternative', {
description: 'Returns a list of items and possible alternative locations', description: 'Returns a list of items and possible alternative locations',
accessType: 'READ', accessType: 'READ',
accepts: [{ accepts: [{
@ -12,12 +12,12 @@ module.exports = Self => {
root: true root: true
}, },
http: { http: {
path: `/return`, path: `/getAlternative`,
verb: 'GET' verb: 'GET'
} }
}); });
Self.return = async(shelvingFk, options) => { Self.getAlternative = async(shelvingFk, options) => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {}; const myOptions = {};

View File

@ -1,6 +1,6 @@
const {models} = require('vn-loopback/server/server'); const {models} = require('vn-loopback/server/server');
describe('itemShelving return()', () => { describe('itemShelving getAlternative()', () => {
beforeAll(async() => { beforeAll(async() => {
ctx = { ctx = {
req: { req: {
@ -11,14 +11,14 @@ describe('itemShelving return()', () => {
it('should return a list of items without alternatives', async() => { it('should return a list of items without alternatives', async() => {
const shelvingFk = 'HEJ'; const shelvingFk = 'HEJ';
const itemShelvings = await models.ItemShelving.return(shelvingFk); const itemShelvings = await models.ItemShelving.getAlternative(shelvingFk);
expect(itemShelvings[0].carros.length).toEqual(0); expect(itemShelvings[0].carros.length).toEqual(0);
}); });
it('should return an empty list', async() => { it('should return an empty list', async() => {
const shelvingFk = 'ZZP'; const shelvingFk = 'ZZP';
const itemShelvings = await models.ItemShelving.return(shelvingFk); const itemShelvings = await models.ItemShelving.getAlternative(shelvingFk);
expect(itemShelvings.length).toEqual(0); expect(itemShelvings.length).toEqual(0);
}); });

View File

@ -1,14 +1,14 @@
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('card', { Self.remoteMethod('get', {
description: 'Get the data from an item', description: 'Get the data from an item',
accessType: 'READ', accessType: 'READ',
http: { http: {
path: `/card`, path: `/get`,
verb: 'GET' verb: 'GET'
}, },
accepts: [ accepts: [
{ {
arg: 'itemFk', arg: 'barcode',
type: 'number', type: 'number',
required: true, required: true,
}, },
@ -24,7 +24,7 @@ module.exports = Self => {
}, },
}); });
Self.card = async(itemFk, warehouseFk, options) => { Self.get = async(barcode, warehouseFk, options) => {
const myOptions = {}; const myOptions = {};
if (typeof options == 'object') if (typeof options == 'object')
@ -32,19 +32,17 @@ module.exports = Self => {
const models = Self.app.models; const models = Self.app.models;
const [[itemInfo]] = await Self.rawSql('CALL vn.item_getInfo(?, ?)', [itemFk, warehouseFk], myOptions); const [[itemInfo]] = await Self.rawSql('CALL vn.item_getInfo(?, ?)', [barcode, warehouseFk], myOptions);
const barcodeItems = await Self.rawSql('SELECT vn.barcodeToItem(?) as realIdItem', [itemFk], myOptions); if (itemInfo) {
const [realIdItem] = barcodeItems.map(barcodeItem => barcodeItem.realIdItem); itemInfo.barcodes = await models.ItemBarcode.find({
const barcodes = await models.ItemBarcode.find({
fields: ['code'], fields: ['code'],
where: { where: {
itemFk: realIdItem itemFk: itemInfo.id
} }
}); });
}
if (itemInfo) itemInfo.barcodes = barcodes;
return itemInfo; return itemInfo;
}; };
}; };

View File

@ -1,10 +1,10 @@
const {models} = require('vn-loopback/server/server'); const {models} = require('vn-loopback/server/server');
describe('item card()', () => { describe('item get()', () => {
const itemFk = 1; const barcode = 1;
const warehouseFk = 1; const warehouseFk = 1;
it('should get an item with several barcodes', async() => { it('should get an item with several barcodes', async() => {
const card = await models.Item.card(itemFk, warehouseFk); const card = await models.Item.get(barcode, warehouseFk);
expect(card).toBeDefined(); expect(card).toBeDefined();
expect(card.barcodes.length).toBeTruthy(); expect(card.barcodes.length).toBeTruthy();

View File

@ -1,6 +1,6 @@
module.exports = Self => { module.exports = Self => {
require('../methods/item-shelving/deleteItemShelvings')(Self); require('../methods/item-shelving/deleteItemShelvings')(Self);
require('../methods/item-shelving/getInventory')(Self); require('../methods/item-shelving/getInventory')(Self);
require('../methods/item-shelving/return')(Self); require('../methods/item-shelving/getAlternative')(Self);
require('../methods/item-shelving/updateFromSale')(Self); require('../methods/item-shelving/updateFromSale')(Self);
}; };

View File

@ -17,7 +17,7 @@ module.exports = Self => {
require('../methods/item/buyerWasteEmail')(Self); require('../methods/item/buyerWasteEmail')(Self);
require('../methods/item/labelPdf')(Self); require('../methods/item/labelPdf')(Self);
require('../methods/item/setVisibleDiscard')(Self); require('../methods/item/setVisibleDiscard')(Self);
require('../methods/item/card')(Self); require('../methods/item/get')(Self);
Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'}); Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'});

View File

@ -1,11 +1,10 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('addSaleByCode', { Self.remoteMethodCtx('addSaleByCode', {
description: 'Add a collection', description: 'Add a collection',
accessType: 'WRITE', accessType: 'WRITE',
accepts: [ accepts: [
{ {
arg: 'code', arg: 'barcode',
type: 'string', type: 'string',
required: true required: true
}, { }, {
@ -29,7 +28,7 @@ module.exports = Self => {
}, },
}); });
Self.addSaleByCode = async(ctx, code, quantity, ticketFk, warehouseFk, options) => { Self.addSaleByCode = async(ctx, barcode, quantity, ticketFk, warehouseFk, options) => {
const myOptions = {}; const myOptions = {};
let tx; let tx;
@ -42,10 +41,8 @@ module.exports = Self => {
} }
try { try {
const [[item]] = await Self.rawSql('CALL vn.item_getInfo(?,?)', [code, warehouseFk], myOptions); const [[item]] = await Self.rawSql('CALL vn.item_getInfo(?,?)', [barcode, warehouseFk], myOptions);
if (!item?.available) throw new UserError('We do not have availability for the selected item'); await Self.addSale(ctx, ticketFk, item.id, quantity, myOptions);
await Self.rawSql('CALL vn.collection_addItem(?, ?, ?)', [item.id, quantity, ticketFk], myOptions);
if (tx) await tx.commit(); if (tx) await tx.commit();
} catch (e) { } catch (e) {

View File

@ -1,21 +0,0 @@
module.exports = Self => {
Self.remoteMethodCtx('add', {
description: 'Add a new operator',
accessType: 'WRITE',
http: {
path: `/add`,
verb: 'POST'
}
});
Self.add = async(ctx, options) => {
const userId = ctx.req.accessToken.userId;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const isOperator = await Self.findById(userId, myOptions);
if (!isOperator) await Self.create({workerFk: userId}, myOptions);
};
};

View File

@ -1,58 +0,0 @@
const {models} = require('vn-loopback/server/server');
describe('operator add()', () => {
const noOperator = 104;
const operator = 9;
beforeAll(async() => {
ctx = {
req: {
accessToken: {},
headers: {origin: 'http://localhost'},
__: value => value
}
};
});
it('should not add an existent operator', async() => {
const tx = await models.Operator.beginTransaction({});
const options = {transaction: tx};
ctx.req.accessToken.userId = operator;
try {
const operatorBefore = await models.Operator.find(null, options);
const isOperator = await models.Operator.findOne(null, options);
expect(isOperator).toBeDefined();
await models.Operator.add(ctx, options);
const operatorAfter = await models.Operator.find(null, options);
expect(operatorBefore.length).toEqual(operatorAfter.length);
await tx.rollback();
} catch (e) {
await tx.rollback();
}
});
it('should add a new operator successfully', async() => {
const tx = await models.Operator.beginTransaction({});
const options = {transaction: tx};
ctx.req.accessToken.userId = noOperator;
try {
const operatorBefore = await models.Operator.find(null, options);
await models.Operator.add(ctx, options);
const operatorAfter = await models.Operator.find(null, options);
const isOperator = await models.Operator.findOne(null, options);
expect(operatorBefore.length).toEqual(operatorAfter.length - 1);
expect(isOperator).toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -1,6 +1,4 @@
module.exports = Self => { module.exports = Self => {
require('../methods/operator/add')(Self);
Self.observe('after save', async function(ctx) { Self.observe('after save', async function(ctx) {
const instance = ctx.data || ctx.instance; const instance = ctx.data || ctx.instance;
const models = Self.app.models; const models = Self.app.models;