feat: #6184 Refactor saveSign
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Guillermo Bonet 2024-01-26 10:05:24 +01:00
parent 9005975f1c
commit 5828fa9657
8 changed files with 61 additions and 73 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,10 @@
const models = require('vn-loopback/server/server').models;
describe('route downloadCmrsZip()', () => {
beforeEach(() => {
spyOn(models.Route, 'downloadCmrsZip').and.returnValue([true]);
});
it('should create a zip file with the given cmr ids', async() => {
const ctx = {
req: {

View File

@ -63,12 +63,11 @@ module.exports = Self => {
companyFk: ticket.companyFk,
warehouseFk: ticket.warehouseFk,
reference: ticket.id,
description: `${ticket.cmrFk} - ${ticket.id}`,
contentType: 'application/pdf',
hasFile: true
};
dms = await models.Dms.createFromStream(data, 'pdf', pdfStream);
dms = await models.Dms.createFromStream(data, 'pdf', pdfStream, myOptions);
await models.TicketDms.create({
ticketFk: ticketId,
dmsFk: dms.id
@ -80,7 +79,6 @@ module.exports = Self => {
return;
} catch (e) {
if (tx) await tx.rollback();
if (dms) await models.Dms.destroyAll({where: {id: dms.id}});
throw e;
}
};

View File

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

View File

@ -35,7 +35,7 @@ module.exports = {
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'));
});
}
};