64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
module.exports = Self => {
|
|
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
|
|
}
|
|
],
|
|
returns: {
|
|
type: ['number'],
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/refund`,
|
|
verb: 'post'
|
|
}
|
|
});
|
|
|
|
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,
|
|
salesIds,
|
|
servicesIds,
|
|
withWarehouse,
|
|
false,
|
|
true,
|
|
myOptions
|
|
);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return refundsTicket[0];
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|