refs #6184 saveCmr #1788
|
@ -1,4 +1,5 @@
|
||||||
const {Email} = require('vn-print');
|
const {Email} = require('vn-print');
|
||||||
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethodCtx('cmrEmail', {
|
Self.remoteMethodCtx('cmrEmail', {
|
||||||
|
@ -6,22 +7,10 @@ module.exports = Self => {
|
||||||
accessType: 'WRITE',
|
accessType: 'WRITE',
|
||||||
accepts: [
|
accepts: [
|
||||||
{
|
{
|
||||||
arg: 'ticketId',
|
arg: 'tickets',
|
||||||
type: 'number',
|
type: ['number'],
|
||||||
required: true,
|
required: true,
|
||||||
description: 'The ticket id',
|
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: [
|
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 models = Self.app.models;
|
||||||
const myOptions = {};
|
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')
|
if (typeof options == 'object')
|
||||||
Object.assign(myOptions, options);
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
if (!recipient)
|
if (!myOptions.transaction) {
|
||||||
params.recipient = (await models.Client.findById(recipientId, {fields: ['email']}, myOptions)).email;
|
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({
|
const dms = await models.TicketDms.findOne({
|
||||||
where: {
|
where: {
|
||||||
|
@ -84,15 +86,24 @@ module.exports = Self => {
|
||||||
]
|
]
|
||||||
}, myOptions);
|
}, myOptions);
|
||||||
|
|
||||||
|
if (!dms) throw new UserError('Cmr file does not exist');
|
||||||
|
|
||||||
const response = await models.Dms.downloadFile(ctx, dms.id);
|
const response = await models.Dms.downloadFile(ctx, dms.id);
|
||||||
const email = new Email('cmr', params);
|
const email = new Email('cmr', params);
|
||||||
|
|
||||||
return email.send({
|
return email.send({
|
||||||
overrideAttachments: true,
|
overrideAttachments: true,
|
||||||
attachments: [{
|
attachments: [{
|
||||||
filename: `${cmr}.pdf`,
|
filename: `${ticket.cmrFk}.pdf`,
|
||||||
content: response[0]
|
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;
|
const models = require('vn-loopback/server/server').models;
|
||||||
|
|
||||||
fdescribe('Ticket saveCmr()', () => {
|
describe('Ticket saveCmr()', () => {
|
||||||
let ctx = {req: {
|
let ctx = {req: {
|
||||||
accessToken: {userId: 9}
|
accessToken: {userId: 9}
|
||||||
}};
|
}};
|
||||||
|
|
|
@ -4,8 +4,7 @@ module.exports = Self => {
|
||||||
Self.remoteMethodCtx('saveCmr', {
|
Self.remoteMethodCtx('saveCmr', {
|
||||||
description: 'Save cmr',
|
description: 'Save cmr',
|
||||||
accessType: 'WRITE',
|
accessType: 'WRITE',
|
||||||
accepts:
|
accepts: [
|
||||||
[
|
|
||||||
{
|
{
|
||||||
arg: 'tickets',
|
arg: 'tickets',
|
||||||
type: ['number'],
|
type: ['number'],
|
||||||
|
@ -23,6 +22,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;
|
||||||
|
|
||||||
if (typeof options == 'object')
|
if (typeof options == 'object')
|
||||||
Object.assign(myOptions, options);
|
Object.assign(myOptions, options);
|
||||||
|
@ -56,7 +56,7 @@ module.exports = Self => {
|
||||||
if (!hasDmsCmr?.dms()) {
|
if (!hasDmsCmr?.dms()) {
|
||||||
ctx.args.id = ticket.cmrFk;
|
ctx.args.id = ticket.cmrFk;
|
||||||
const response = await models.Route.cmr(ctx, myOptions);
|
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 = {
|
const data = {
|
||||||
workerFk: ctx.req.accessToken.userId,
|
workerFk: ctx.req.accessToken.userId,
|
||||||
dmsTypeFk: dmsTypeCmr.id,
|
dmsTypeFk: dmsTypeCmr.id,
|
||||||
|
@ -68,7 +68,7 @@ module.exports = Self => {
|
||||||
hasFile: true
|
hasFile: true
|
||||||
};
|
};
|
||||||
|
|
||||||
const dms = await models.Dms.createFromStream(data, 'pdf', pdfStream, myOptions);
|
dms = await models.Dms.createFromStream(data, 'pdf', pdfStream);
|
||||||
await models.TicketDms.create({
|
await models.TicketDms.create({
|
||||||
ticketFk: ticketId,
|
ticketFk: ticketId,
|
||||||
dmsFk: dms.id
|
dmsFk: dms.id
|
||||||
|
@ -80,6 +80,7 @@ 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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -128,23 +128,42 @@ module.exports = Self => {
|
||||||
if (location) setLocation(ticketId);
|
if (location) setLocation(ticketId);
|
||||||
if (!gestDocCreated) await createGestDoc(ticketId);
|
if (!gestDocCreated) await createGestDoc(ticketId);
|
||||||
await models.TicketDms.create({ticketFk: ticketId, dmsFk: dms[0].id}, myOptions);
|
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);
|
await ticket.updateAttribute('isSigned', true, myOptions);
|
||||||
|
|
||||||
const deliveryState = await models.State.find({
|
const deliveryState = await models.State.findOne({
|
||||||
where: {
|
where: {
|
||||||
code: 'DELIVERED'
|
code: 'DELIVERED'
|
||||||
}
|
}
|
||||||
}, options);
|
}, 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') {
|
||||||
await models.Ticket.saveCmr(ctx, [ticketId], myOptions);
|
await models.Ticket.saveCmr(ctx, [ticketId], myOptions);
|
||||||
await models.Route.cmrEmail(ctx, [ticketId], myOptions);
|
await models.Route.cmrEmail(ctx, [ticketId], myOptions);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (tx) await tx.commit();
|
if (tx) await tx.commit();
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
const models = require('vn-loopback/server/server').models;
|
const models = require('vn-loopback/server/server').models;
|
||||||
|
|
||||||
describe('Ticket saveSign()', () => {
|
describe('Ticket saveSign()', () => {
|
||||||
const FormData = require('form-data');
|
|
||||||
const data = new FormData();
|
|
||||||
let ctx = {req: {
|
let ctx = {req: {
|
||||||
accessToken: {userId: 9},
|
getLocale: () => {
|
||||||
headers: {
|
return 'en';
|
||||||
...data.getHeaders()
|
},
|
||||||
}
|
accessToken: {userId: 9}
|
||||||
|
|
||||||
}};
|
}};
|
||||||
|
|
||||||
it(`should throw error if the ticket's alert level is lower than 2`, async() => {
|
it(`should throw error if the ticket's alert level is lower than 2`, async() => {
|
||||||
|
@ -17,9 +14,9 @@ describe('Ticket saveSign()', () => {
|
||||||
let error;
|
let error;
|
||||||
try {
|
try {
|
||||||
const options = {transaction: tx};
|
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();
|
await tx.rollback();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
Loading…
Reference in New Issue