2018-10-02 07:52:38 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('importTicketSales', {
|
|
|
|
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: `/importTicketSales`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-29 13:42:29 +00:00
|
|
|
Self.importTicketSales = async(ctx, params, options) => {
|
2018-10-02 07:52:38 +00:00
|
|
|
let models = Self.app.models;
|
|
|
|
let userId = ctx.req.accessToken.userId;
|
2021-03-29 13:42:29 +00:00
|
|
|
|
|
|
|
let tx;
|
|
|
|
let myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const worker = await models.Worker.findOne({where: {userFk: userId}}, myOptions);
|
|
|
|
|
|
|
|
let ticketSales = await models.Sale.find({
|
|
|
|
where: {ticketFk: params.ticketFk}
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
let claimEnds = [];
|
|
|
|
ticketSales.forEach(sale => {
|
|
|
|
claimEnds.push({
|
|
|
|
saleFk: sale.id,
|
|
|
|
claimFk: params.claimFk,
|
|
|
|
workerFk: worker.id
|
|
|
|
});
|
2018-10-02 07:52:38 +00:00
|
|
|
});
|
|
|
|
|
2021-03-29 13:42:29 +00:00
|
|
|
const createdClaimEnds = await Self.create(claimEnds, myOptions);
|
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return createdClaimEnds;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2018-10-02 07:52:38 +00:00
|
|
|
};
|
|
|
|
};
|