refs #6184 saveCmr #1788
|
@ -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,26 +34,39 @@ 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 {
|
||||
for (const ticketId of tickets) {
|
||||
const ticket = await models.Ticket.findOne({
|
||||
where: {
|
||||
id: ticketId
|
||||
},
|
||||
include: [{
|
||||
relation: 'client',
|
||||
fields: ['email']
|
||||
}]
|
||||
}, myOptions);
|
||||
|
||||
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: {
|
||||
|
@ -84,15 +86,24 @@ module.exports = Self => {
|
|||
]
|
||||
}, myOptions);
|
||||
|
||||
if (!dms) throw new UserError('Cmr file does not exist');
|
||||
|
||||
const response = await models.Dms.downloadFile(ctx, dms.id);
|
||||
const email = new Email('cmr', params);
|
||||
|
||||
return email.send({
|
||||
overrideAttachments: true,
|
||||
attachments: [{
|
||||
filename: `${cmr}.pdf`,
|
||||
filename: `${ticket.cmrFk}.pdf`,
|
||||
content: response[0]
|
||||
}]
|
||||
});
|
||||
}
|
||||
if (tx) await tx.commit();
|
||||
return;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const models = require('vn-loopback/server/server').models;
|
||||
|
||||
fdescribe('Ticket saveCmr()', () => {
|
||||
describe('Ticket saveCmr()', () => {
|
||||
let ctx = {req: {
|
||||
accessToken: {userId: 9}
|
||||
}};
|
||||
|
|
|
@ -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()) {
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -128,23 +128,42 @@ 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);
|
||||
|
||||
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();
|
||||
return;
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue