refs #6184 saveCmr #1788

Merged
guillermo merged 58 commits from 6184-saveCmr into dev 2024-02-13 06:47:06 +00:00
3 changed files with 31 additions and 30 deletions
Showing only changes of commit 567e480e3b - Show all commits

View File

@ -336,5 +336,7 @@
"Incorrect pin": "Pin incorrecto.", "Incorrect pin": "Pin incorrecto.",
"You already have the mailAlias": "Ya tienes este alias de correo", "You already have the mailAlias": "Ya tienes este alias de correo",
"The alias cant be modified": "Este alias de correo no puede ser modificado", "The alias cant be modified": "Este alias de correo no puede ser modificado",
"No tickets to invoice": "No hay tickets para facturar" "No tickets to invoice": "No hay tickets para facturar",
"This ticket already has a cmr saved": "Este ticket ya tiene un cmr guardado",
"Client does not have an email": "El cliente no tiene correo"
} }

View File

@ -1,4 +1,5 @@
const {Readable} = require('stream'); const {Readable} = require('stream');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('saveCmr', { Self.remoteMethodCtx('saveCmr', {
@ -22,7 +23,6 @@ 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);
@ -33,11 +33,6 @@ module.exports = Self => {
} }
try { try {
const dmsTypeCmr = await models.DmsType.findOne({
where: {code: 'cmr'},
fields: ['id']
}, myOptions);
for (const ticketId of tickets) { for (const ticketId of tickets) {
const ticket = await models.Ticket.findById(ticketId, myOptions); const ticket = await models.Ticket.findById(ticketId, myOptions);
guillermo marked this conversation as resolved Outdated

Este el cree cada vegada pero es el mateix id per a tots. Pujal fora del for
El nom jo posaría dmsTypeCmr

Este el cree cada vegada pero es el mateix id per a tots. Pujal fora del for El nom jo posaría dmsTypeCmr
@ -48,12 +43,17 @@ module.exports = Self => {
relation: 'dms', relation: 'dms',
fields: ['dmsFk'], fields: ['dmsFk'],
scope: { scope: {
where: {dmsTypeFk: dmsTypeCmr.id} relation: 'dmsType',
scope: {
where: {code: 'cmr'}
}
} }
} }
}, myOptions); }, myOptions);
if (!hasDmsCmr?.dms()) { if (hasDmsCmr?.dms())
throw new UserError('This ticket already has a cmr saved');
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; 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[0])); const pdfStream = Readable.from(Buffer.from(response[0]));
@ -67,14 +67,13 @@ module.exports = Self => {
hasFile: true hasFile: true
}; };
dms = await models.Dms.createFromStream(data, 'pdf', pdfStream, myOptions); const dms = await models.Dms.createFromStream(data, 'pdf', pdfStream, myOptions);
await models.TicketDms.create({ await models.TicketDms.create({
ticketFk: ticketId, ticketFk: ticketId,
dmsFk: dms.id dmsFk: dms.id
}, myOptions); }, myOptions);
} }
} }
}
if (tx) await tx.commit(); if (tx) await tx.commit();
return; return;
} catch (e) { } catch (e) {

View File

@ -70,7 +70,7 @@ module.exports = Self => {
} }
] ]
}, myOptions); }, myOptions);
if (ticketDms.dms()?.id) return true; if (ticketDms?.dms()?.id) return true;
} }
async function createGestDoc(id) { async function createGestDoc(id) {