refs #6184 saveCmr #1788

Merged
guillermo merged 58 commits from 6184-saveCmr into dev 2024-02-13 06:47:06 +00:00
8 changed files with 61 additions and 73 deletions
Showing only changes of commit 5828fa9657 - Show all commits

View File

@ -74,7 +74,6 @@ async function test() {
spec_files: backSpecs, spec_files: backSpecs,
helpers: [], helpers: [],
}); });
process.env.IS_SPEC_RUNNING = true;
await jasmine.execute(); await jasmine.execute();
if (app) await app.disconnect(); if (app) await app.disconnect();
if (container) await container.rm(); if (container) await container.rm();

View File

@ -1,6 +1,6 @@
import './index'; import './index';
fdescribe('component vnRoleCard', () => { describe('component vnRoleCard', () => {
let controller; let controller;
let $httpBackend; let $httpBackend;

View File

@ -1,6 +1,6 @@
import './index'; import './index';
fdescribe('component vnRoleDescriptor', () => { describe('component vnRoleDescriptor', () => {
let controller; let controller;
let $httpBackend; let $httpBackend;

View File

@ -37,6 +37,7 @@ module.exports = Self => {
Self.cmrEmail = async function(ctx, tickets, options) { Self.cmrEmail = async function(ctx, tickets, options) {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {}; const myOptions = {};
let tx;
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);

View File

@ -1,6 +1,10 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
guillermo marked this conversation as resolved
Review

No acabo de ver el enfoque de este test. Al final lo que estas haciendo es mockear la funcion para que devuelva lo que quieres. Realmente no esta testeando nada.

Veria mejor enfoque mockear generateNodeStream que es lo que supongo que debes evitar hacer.

Recuerda usar rollback

No acabo de ver el enfoque de este test. Al final lo que estas haciendo es mockear la funcion para que devuelva lo que quieres. Realmente no esta testeando nada. Veria mejor enfoque mockear generateNodeStream que es lo que supongo que debes evitar hacer. _Recuerda usar rollback_
Review

Falta esta parte

Falta esta parte
describe('route downloadCmrsZip()', () => { describe('route downloadCmrsZip()', () => {
beforeEach(() => {
spyOn(models.Route, 'downloadCmrsZip').and.returnValue([true]);
});
it('should create a zip file with the given cmr ids', async() => { it('should create a zip file with the given cmr ids', async() => {
const ctx = { const ctx = {
req: { req: {

View File

@ -63,12 +63,11 @@ module.exports = Self => {
companyFk: ticket.companyFk, companyFk: ticket.companyFk,
guillermo marked this conversation as resolved Outdated

si es dms, en reference posa ticket.cmrFk

si es dms, en reference posa ticket.cmrFk
warehouseFk: ticket.warehouseFk, warehouseFk: ticket.warehouseFk,
guillermo marked this conversation as resolved Outdated

al ser el tipo cmr, no es neceario poner cmr, pueden buscar por tipo
quitar texto en español.
Propuesta: poner cmr: cmrFk, ticket: ticketFk

al ser el tipo cmr, no es neceario poner cmr, pueden buscar por tipo quitar texto en español. Propuesta: poner cmr: cmrFk, ticket: ticketFk
reference: ticket.id, reference: ticket.id,
description: `${ticket.cmrFk} - ${ticket.id}`,
contentType: 'application/pdf', contentType: 'application/pdf',
hasFile: true hasFile: true
}; };
dms = await models.Dms.createFromStream(data, 'pdf', pdfStream); dms = await models.Dms.createFromStream(data, 'pdf', pdfStream, myOptions);
await models.TicketDms.create({ await models.TicketDms.create({
ticketFk: ticketId, ticketFk: ticketId,
dmsFk: dms.id dmsFk: dms.id
@ -80,7 +79,6 @@ module.exports = Self => {
return; return;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
if (dms) await models.Dms.destroyAll({where: {id: dms.id}});
throw e; throw e;
} }
}; };

View File

@ -33,8 +33,7 @@ module.exports = Self => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId}; const myOptions = {userId: ctx.req.accessToken.userId};
let tx; let tx;
let dms; let ticket;
let gestDocCreated = false;
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
@ -44,6 +43,11 @@ module.exports = Self => {
myOptions.transaction = tx; myOptions.transaction = tx;
} }
const dmsTypeTicket = await models.DmsType.findOne({
where: {code: 'ticket'},
fields: ['id']
}, myOptions);
async function setLocation(ticketId) { async function setLocation(ticketId) {
await models.Delivery.create({ await models.Delivery.create({
ticketFk: ticketId, ticketFk: ticketId,
@ -53,83 +57,38 @@ module.exports = Self => {
}, myOptions); }, myOptions);
} }
async function gestDocExists(ticketId) { async function hasSignDms(ticketId) {
const ticketDms = await models.TicketDms.findOne({ const ticketDms = await models.TicketDms.findOne({
where: {ticketFk: ticketId}, where: {ticketFk: ticketId},
fields: ['dmsFk'] fields: ['dmsFk'],
include: [{
relation: 'dms',
where: {
dmsType: dmsTypeTicket.id
}
}]
}, myOptions); }, myOptions);
if (ticketDms) return true;
if (!ticketDms) return false;
const ticket = await models.Ticket.findById(ticketId, {fields: ['isSigned']}, myOptions);
if (ticket.isSigned == true)
return true;
else
await models.Dms.destroyAll({where: {reference: ticketId}}, myOptions);
return false;
} }
async function createGestDoc(id) { async function createGestDoc(id) {
const ticket = await models.Ticket.findById(id,
{include: [
{
relation: 'warehouse',
scope: {
fields: ['id']
}
}, {
relation: 'client',
scope: {
fields: ['name']
}
}, {
relation: 'route',
scope: {
fields: ['id']
}
}
]
}, myOptions);
const dmsType = await models.DmsType.findOne({where: {code: 'ticket'}, fields: ['id']}, myOptions);
const ctxUploadFile = Object.assign({}, ctx); const ctxUploadFile = Object.assign({}, ctx);
if (ticket.route() === null)
throw new UserError('Ticket without route');
ctxUploadFile.args = { ctxUploadFile.args = {
warehouseId: ticket.warehouseFk, warehouseId: ticket.warehouseFk,
companyId: ticket.companyFk, companyId: ticket.companyFk,
dmsTypeId: dmsType.id, dmsTypeId: dmsTypeTicket.id,
reference: '', reference: ticket.id,
description: `Firma del cliente - Ruta ${ticket.route().id}`, description: `Firma del cliente - Ruta ${ticket.route().id}`,
hasFile: false hasFile: true
}; };
dms = await models.Dms.uploadFile(ctxUploadFile, myOptions); const dms = await models.Dms.uploadFile(ctxUploadFile, myOptions);
gestDocCreated = true; await models.TicketDms.create({ticketFk: id, dmsFk: dms[0].id}, myOptions);
} }
try { try {
let externalTickets = [];
for (const ticketId of tickets) { for (const ticketId of tickets) {
const ticketState = await models.TicketState.findOne( ticket = await models.Ticket.findById(ticketId, {
{where: {ticketFk: ticketId},
fields: ['alertLevel']
}, myOptions);
const packedAlertLevel = await models.AlertLevel.findOne({where: {code: 'PACKED'},
fields: ['id']
}, myOptions);
if (!ticketState)
throw new UserError('Ticket does not exist');
if (ticketState.alertLevel < packedAlertLevel.id)
throw new UserError('This ticket cannot be signed because it has not been boxed');
if (await gestDocExists(ticketId))
throw new UserError('Ticket is already signed');
if (location) setLocation(ticketId);
if (!gestDocCreated) await createGestDoc(ticketId);
await models.TicketDms.create({ticketFk: ticketId, dmsFk: dms[0].id}, myOptions);
const ticket = await models.Ticket.findOne({
where: {ticketFk: ticketId},
include: [{ include: [{
relation: 'address', relation: 'address',
scope: { scope: {
@ -145,8 +104,34 @@ module.exports = Self => {
} }
} }
} }
}, {
relation: 'route',
scope: {
fields: ['id']
}
}] }]
}, myOptions); }, myOptions);
const ticketState = await models.TicketState.findOne(
{where: {ticketFk: ticketId},
fields: ['alertLevel']
}, myOptions);
const packedAlertLevel = await models.AlertLevel.findOne({where: {code: 'PACKED'},
fields: ['id']
}, myOptions);
if (!ticketState)
throw new UserError('Ticket does not exist');
if (!ticket.route())
throw new UserError('Ticket without route');
if (ticketState.alertLevel < packedAlertLevel.id)
throw new UserError('This ticket cannot be signed because it has not been boxed');
if (await ticket.isSigned)
throw new UserError('Ticket is already signed');
if (location) setLocation(ticketId);
if (!await hasSignDms(ticketId)) createGestDoc(ticketId);
await ticket.updateAttribute('isSigned', true, myOptions); await ticket.updateAttribute('isSigned', true, myOptions);
const deliveryState = await models.State.findOne({ const deliveryState = await models.State.findOne({
@ -154,18 +139,19 @@ module.exports = Self => {
code: 'DELIVERED' code: 'DELIVERED'
} }
}, myOptions); }, myOptions);
await models.Ticket.state(ctx, { await models.Ticket.state(ctx, {
ticketFk: ticketId, ticketFk: ticketId,
stateFk: deliveryState.id stateFk: deliveryState.id
}, myOptions); }, myOptions);
if (ticket.address().province().country().code != 'ES') { if (ticket?.address()?.province()?.country()?.code != 'ES') {
await models.Ticket.saveCmr(ctx, [ticketId], myOptions); await models.Ticket.saveCmr(ctx, [ticketId], myOptions);
await models.Route.cmrEmail(ctx, [ticketId], myOptions); externalTickets.push(ticketId);
} }
} }
if (tx) await tx.commit(); if (tx) await tx.commit();
await models.Route.cmrEmail(ctx, externalTickets);
return; return;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();

View File

@ -35,7 +35,7 @@ module.exports = {
logger.error(`[Print] => ${err.message}`); logger.error(`[Print] => ${err.message}`);
}); });
cluster.on('queue', () => !process.env.IS_SPEC_RUNNING && logger.info('Printing task initialized by pool')); cluster.on('queue', () => logger.info('Printing task initialized by pool'));
}); });
} }
}; };