81 lines
2.4 KiB
JavaScript
81 lines
2.4 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethodCtx('cloneAll', {
|
|
description: 'Clone tickets, sales, services and packages',
|
|
accessType: 'WRITE',
|
|
accepts: [
|
|
{
|
|
arg: 'ticketsIds',
|
|
type: ['number'],
|
|
required: true,
|
|
description: 'IDs of the tickets to clone'
|
|
},
|
|
{
|
|
arg: 'withWarehouse',
|
|
type: 'boolean',
|
|
required: true,
|
|
description: 'true: keep original warehouse; false: set to null'
|
|
},
|
|
{
|
|
arg: 'negative',
|
|
type: 'boolean',
|
|
required: true,
|
|
description: 'true: invert quantities; false: keep as is.'
|
|
}
|
|
],
|
|
returns: {
|
|
type: ['object'],
|
|
root: true,
|
|
description: 'The cloned tickets with associated data'
|
|
},
|
|
http: {
|
|
path: `/cloneAll`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.cloneAll = async(ctx, ticketsIds, withWarehouse, negative, options) => {
|
|
const models = Self.app.models;
|
|
let tx;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const filter = {where: {ticketFk: {inq: ticketsIds}}};
|
|
|
|
const [sales, services, ticketPackaging] = await Promise.all([
|
|
models.Sale.find(filter, myOptions),
|
|
models.TicketService.find(filter, myOptions),
|
|
models.TicketPackaging.find(filter, myOptions)
|
|
]);
|
|
|
|
const salesIds = sales.map(({id}) => id);
|
|
const servicesIds = services.map(({id}) => id);
|
|
const ticketPackagingIds = ticketPackaging.map(({id}) => id);
|
|
|
|
const clonedTickets = await models.Sale.clone(
|
|
ctx,
|
|
salesIds,
|
|
servicesIds,
|
|
ticketPackagingIds,
|
|
withWarehouse,
|
|
negative,
|
|
myOptions
|
|
);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return clonedTickets;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|