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,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 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 {
|
||||||
guillermo marked this conversation as resolved
Outdated
|
|||||||
|
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({
|
const recipient = ticket.client().email;
|
||||||
where: {
|
if (!recipient)
|
||||||
ticketFk: ticketId
|
throw new UserError('Client does not have an email');
|
||||||
},
|
|
||||||
include: [
|
const params = {
|
||||||
{
|
ticketId,
|
||||||
relation: 'dms',
|
lang: ctx.req.getLocale(),
|
||||||
fields: ['id'],
|
recipient
|
||||||
scope: {
|
};
|
||||||
relation: 'dmsType',
|
|
||||||
where: {
|
const dms = await models.TicketDms.findOne({
|
||||||
code: 'cmr'
|
where: {
|
||||||
|
ticketFk: ticketId
|
||||||
|
},
|
||||||
guillermo marked this conversation as resolved
Outdated
alexm
commented
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);
|
if (!dms) throw new UserError('Cmr file does not exist');
|
||||||
const email = new Email('cmr', params);
|
|
||||||
|
|
||||||
return email.send({
|
const response = await models.Dms.downloadFile(ctx, dms.id);
|
||||||
overrideAttachments: true,
|
const email = new Email('cmr', params);
|
||||||
attachments: [{
|
|
||||||
filename: `${cmr}.pdf`,
|
return email.send({
|
||||||
content: response[0]
|
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -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()) {
|
||||||
guillermo marked this conversation as resolved
Outdated
jgallego
commented
const const
guillermo
commented
En eixe cas te que ser let, fijat baix. En eixe cas te que ser let, fijat baix.
Per a que siga const he tingut que juntar-ho.
|
|||||||
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,22 +128,41 @@ 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);
|
||||||
|
|
||||||
await models.Ticket.saveCmr(ctx, [ticketId], myOptions);
|
if (ticket.address().province().country().code != 'ES') {
|
||||||
await models.Route.cmrEmail(ctx, [ticketId], myOptions);
|
await models.Ticket.saveCmr(ctx, [ticketId], myOptions);
|
||||||
|
await models.Route.cmrEmail(ctx, [ticketId], myOptions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tx) await tx.commit();
|
if (tx) await tx.commit();
|
||||||
|
|
|
@ -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
Ya esta la traduccion
There is no assigned email for this client
por si te vale y asi no tenemos 2 casi iguales