salix/modules/ticket/back/methods/sale/refund.js

63 lines
1.5 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
2023-06-01 06:32:06 +00:00
Self.remoteMethodCtx('refund', {
description: 'Create refund tickets with sales and services if provided',
accessType: 'WRITE',
accepts: [
{
arg: 'salesIds',
type: ['number'],
required: true
},
{
arg: 'servicesIds',
type: ['number']
},
{
arg: 'withWarehouse',
type: 'boolean',
required: true
2023-05-03 12:12:26 +00:00
}
],
returns: {
2023-11-20 06:56:15 +00:00
type: ['object'],
root: true
},
http: {
path: `/refund`,
verb: 'post'
}
});
2023-07-28 13:21:43 +00:00
Self.refund = async(ctx, salesIds, servicesIds, withWarehouse, options) => {
const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const refundsTicket = await models.Sale.clone(
ctx,
2023-08-24 08:51:34 +00:00
salesIds,
2023-07-31 08:33:51 +00:00
servicesIds,
2023-07-28 13:21:43 +00:00
withWarehouse,
true,
2023-07-28 13:21:43 +00:00
myOptions
);
2023-09-21 14:08:55 +00:00
if (tx) await tx.commit();
2023-07-28 13:21:43 +00:00
2023-11-20 06:56:15 +00:00
return refundsTicket;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};