159 lines
5.2 KiB
JavaScript
159 lines
5.2 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethodCtx('regularizeClaim', {
|
|
description: 'Imports lines from claimBeginning to a new ticket with specific shipped, landed dates, agency and company',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'params',
|
|
type: 'object',
|
|
http: {source: 'body'}
|
|
}],
|
|
returns: {
|
|
type: ['Object'],
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/regularizeClaim`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.regularizeClaim = async (ctx, params) => {
|
|
const userId = ctx.req.accessToken.userId;
|
|
const models = Self.app.models;
|
|
const resolvedState = 3;
|
|
|
|
const claimEnds = await models.ClaimEnd.find({
|
|
include: {
|
|
relation: 'claimDestination',
|
|
fields: ['addressFk']
|
|
},
|
|
where: {claimFk: params.claimFk}
|
|
});
|
|
|
|
let transaction = await Self.beginTransaction({});
|
|
try {
|
|
for (let i = 0; i < claimEnds.length; i++) {
|
|
const claimEnd = claimEnds[i];
|
|
const destination = claimEnd.claimDestination();
|
|
const addressFk = destination && destination.addressFk;
|
|
|
|
if (!addressFk)
|
|
continue;
|
|
|
|
let sale = await getSale(claimEnd.saleFk);
|
|
let ticketFk = await getTicketId({
|
|
addressFk: addressFk,
|
|
companyFk: sale.ticket().companyFk,
|
|
warehouseFk: sale.ticket().warehouseFk
|
|
}, transaction);
|
|
|
|
let address = await models.Address.findOne({
|
|
where: {id: addressFk}
|
|
});
|
|
|
|
if (!ticketFk) {
|
|
ticketFk = await createTicket({
|
|
clientFk: address.clientFk,
|
|
addressFk: addressFk,
|
|
warehouseFk: sale.ticket().warehouseFk,
|
|
companyFk: sale.ticket().companyFk,
|
|
userId: userId
|
|
}, transaction);
|
|
}
|
|
|
|
await models.Sale.create({
|
|
ticketFk: ticketFk,
|
|
itemFk: sale.itemFk,
|
|
concept: sale.concept,
|
|
quantity: -sale.quantity,
|
|
price: sale.price,
|
|
discount: 100
|
|
}, {transaction: transaction});
|
|
|
|
await sendMessage(ctx, {
|
|
itemFk: sale.itemFk,
|
|
ticketFk: sale.ticketFk,
|
|
workerFk: sale.item().itemType().workerFk,
|
|
quantity: sale.quantity,
|
|
concept: sale.concept,
|
|
nickname: address.nickname
|
|
}, transaction);
|
|
}
|
|
|
|
let claim = await Self.findById(params.claimFk);
|
|
claim = await claim.updateAttributes({
|
|
claimStateFk: resolvedState
|
|
}, {transaction: transaction});
|
|
|
|
await transaction.commit();
|
|
|
|
return claim;
|
|
} catch (e) {
|
|
await transaction.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
async function getSale(saleFk) {
|
|
return await Self.app.models.Sale.findOne({
|
|
include: [
|
|
{
|
|
relation: 'ticket',
|
|
scope: {fields: ['warehouseFk', 'companyFk']}
|
|
},
|
|
{
|
|
relation: 'item',
|
|
scope: {
|
|
fields: ['typeFk'],
|
|
include: {
|
|
relation: 'itemType',
|
|
scope: {fields: ['workerFk']}
|
|
}
|
|
}
|
|
}],
|
|
where: {id: saleFk}
|
|
});
|
|
}
|
|
|
|
async function getTicketId(params, transaction) {
|
|
const currentDate = new Date();
|
|
currentDate.setHours(null, null, null);
|
|
|
|
let ticket = await Self.app.models.Ticket.findOne({
|
|
where: {
|
|
addressFk: params.addressFk,
|
|
companyFk: params.companyFk,
|
|
warehouseFk: params.warehouseFk,
|
|
shipped: currentDate,
|
|
landed: currentDate
|
|
}
|
|
}, {transaction: transaction});
|
|
|
|
return ticket && ticket.id;
|
|
}
|
|
|
|
async function createTicket(ctx, params, transaction) {
|
|
let ticket = await Self.app.models.Ticket.new(ctx,
|
|
{shipped: new Date(),
|
|
landed: new Date(),
|
|
clientFk: params.clientFk,
|
|
warehouseFk: params.warehouseFk,
|
|
companyFk: params.companyFk,
|
|
addressFk: params.addressFk,
|
|
userId: params.userId
|
|
}, {transaction: transaction});
|
|
|
|
return ticket.id;
|
|
}
|
|
|
|
async function sendMessage(ctx, params, transaction) {
|
|
const message = `Envio ${params.quantity} unidades de "${params.concept}" (#${params.itemFk}) a `
|
|
+ `"${params.nickname}" provenientes del ticket #${params.ticketFk}`;
|
|
|
|
await Self.app.models.Message.send(ctx, {
|
|
recipientFk: params.workerFk,
|
|
message: message
|
|
}, {transaction: transaction});
|
|
}
|
|
};
|