refs #6184 saveCmr #1788

Merged
guillermo merged 58 commits from 6184-saveCmr into dev 2024-02-13 06:47:06 +00:00
5 changed files with 100 additions and 72 deletions
Showing only changes of commit 9005975f1c - Show all commits

View File

@ -1,4 +1,5 @@
const {Email} = require('vn-print');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('cmrEmail', {
@ -6,22 +7,10 @@ module.exports = Self => {
accessType: 'WRITE',
accepts: [
{
arg: 'ticketId',
type: 'number',
arg: 'tickets',
type: ['number'],
required: true,
description: 'The ticket id',
},
{
arg: 'recipientId',
type: 'number',
description: 'The client id',
required: true
},
{
arg: 'recipient',
type: 'string',
description: 'The recipient email',
required: false,
}
],
returns: [
@ -45,54 +34,76 @@ module.exports = Self => {
}
});
Self.cmrEmail = async function(ctx, ticketId, recipientId, recipient, options) {
Self.cmrEmail = async function(ctx, tickets, options) {
const models = Self.app.models;
const myOptions = {};
const args = Object.assign({}, ctx.args);
const params = {
recipient: args.recipient,
lang: ctx.req.getLocale()
};
delete args.ctx;
for (const param in args)
params[param] = args[param];
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!recipient)
params.recipient = (await models.Client.findById(recipientId, {fields: ['email']}, myOptions)).email;
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
const cmr = (await models.Ticket.findById(ticketId, {fields: ['cmrFk']}, myOptions)).cmrFk;
try {
guillermo marked this conversation as resolved Outdated
Outdated
Review

Ya esta la traduccion There is no assigned email for this client por si te vale y asi no tenemos 2 casi iguales

Ya esta la traduccion `There is no assigned email for this client` por si te vale y asi no tenemos 2 casi iguales
for (const ticketId of tickets) {
const ticket = await models.Ticket.findOne({
where: {
id: ticketId
},
include: [{
relation: 'client',
fields: ['email']
}]
}, myOptions);
const dms = await models.TicketDms.findOne({
where: {
ticketFk: ticketId
},
include: [
{
relation: 'dms',
fields: ['id'],
scope: {
relation: 'dmsType',
where: {
code: 'cmr'
const recipient = ticket.client().email;
if (!recipient)
throw new UserError('Client does not have an email');
const params = {
ticketId,
lang: ctx.req.getLocale(),
recipient
};
const dms = await models.TicketDms.findOne({
where: {
ticketFk: ticketId
},
guillermo marked this conversation as resolved Outdated
Outdated
Review

Si solo se usa una vez params se puede poner directamente el objeto o si se quiere variable poner inmediatamente arriba asi facilita la lectura

Si solo se usa una vez params se puede poner directamente el objeto o si se quiere variable poner inmediatamente arriba asi facilita la lectura
include: [
{
relation: 'dms',
fields: ['id'],
scope: {
relation: 'dmsType',
where: {
code: 'cmr'
}
}
}
}
}
]
}, myOptions);
]
}, myOptions);
const response = await models.Dms.downloadFile(ctx, dms.id);
const email = new Email('cmr', params);
if (!dms) throw new UserError('Cmr file does not exist');
return email.send({
overrideAttachments: true,
attachments: [{
filename: `${cmr}.pdf`,
content: response[0]
}]
});
const response = await models.Dms.downloadFile(ctx, dms.id);
const email = new Email('cmr', params);
return email.send({
overrideAttachments: true,
attachments: [{
filename: `${ticket.cmrFk}.pdf`,
content: response[0]
}]
});
}
if (tx) await tx.commit();
return;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

@ -1,6 +1,6 @@
const models = require('vn-loopback/server/server').models;
fdescribe('Ticket saveCmr()', () => {
describe('Ticket saveCmr()', () => {
let ctx = {req: {
accessToken: {userId: 9}
}};

View File

@ -4,8 +4,7 @@ module.exports = Self => {
Self.remoteMethodCtx('saveCmr', {
description: 'Save cmr',
accessType: 'WRITE',
accepts:
[
accepts: [
{
arg: 'tickets',
type: ['number'],
@ -23,6 +22,7 @@ module.exports = Self => {
const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId};
let tx;
let dms;
if (typeof options == 'object')
Object.assign(myOptions, options);
@ -56,7 +56,7 @@ module.exports = Self => {
if (!hasDmsCmr?.dms()) {
guillermo marked this conversation as resolved Outdated

const

const

En eixe cas te que ser let, fijat baix.
Per a que siga const he tingut que juntar-ho.

En eixe cas te que ser let, fijat baix. Per a que siga const he tingut que juntar-ho.
ctx.args.id = ticket.cmrFk;
const response = await models.Route.cmr(ctx, myOptions);
const pdfStream = Readable.from(Buffer.from(response));
const pdfStream = Readable.from(Buffer.from(response[0]));
const data = {
workerFk: ctx.req.accessToken.userId,
dmsTypeFk: dmsTypeCmr.id,
@ -68,7 +68,7 @@ module.exports = Self => {
hasFile: true
};
const dms = await models.Dms.createFromStream(data, 'pdf', pdfStream, myOptions);
dms = await models.Dms.createFromStream(data, 'pdf', pdfStream);
await models.TicketDms.create({
ticketFk: ticketId,
dmsFk: dms.id
@ -80,6 +80,7 @@ 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

@ -128,22 +128,41 @@ module.exports = Self => {
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.findById(ticketId, null, myOptions);
const ticket = await models.Ticket.findOne({
where: {ticketFk: ticketId},
include: [{
relation: 'address',
scope: {
include: {
relation: 'province',
scope: {
include: {
relation: 'country',
scope: {
fields: ['code']
}
}
}
}
}
}]
}, myOptions);
await ticket.updateAttribute('isSigned', true, myOptions);
const deliveryState = await models.State.find({
const deliveryState = await models.State.findOne({
where: {
code: 'DELIVERED'
}
}, options);
}, myOptions);
await models.Ticket.state(ctx, {
ticketFk: ticketId,
stateFk: deliveryState.id
}, myOptions);
await models.Ticket.saveCmr(ctx, [ticketId], myOptions);
await models.Route.cmrEmail(ctx, [ticketId], myOptions);
if (ticket.address().province().country().code != 'ES') {
await models.Ticket.saveCmr(ctx, [ticketId], myOptions);
await models.Route.cmrEmail(ctx, [ticketId], myOptions);
}
}
if (tx) await tx.commit();

View File

@ -1,14 +1,11 @@
const models = require('vn-loopback/server/server').models;
describe('Ticket saveSign()', () => {
const FormData = require('form-data');
const data = new FormData();
let ctx = {req: {
accessToken: {userId: 9},
headers: {
...data.getHeaders()
}
getLocale: () => {
return 'en';
},
accessToken: {userId: 9}
}};
it(`should throw error if the ticket's alert level is lower than 2`, async() => {
@ -17,9 +14,9 @@ describe('Ticket saveSign()', () => {
let error;
try {
const options = {transaction: tx};
ctx.args = {tickets: [ticketWithOkState]};
const tickets = [ticketWithOkState];
await models.Ticket.saveSign(ctx, options);
await models.Ticket.saveSign(ctx, tickets, options);
await tx.rollback();
} catch (e) {