salix/modules/ticket/back/methods/sale-tracking/delete.js

68 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2023-06-01 13:01:38 +00:00
module.exports = Self => {
Self.remoteMethod('delete', {
2023-07-27 05:58:55 +00:00
description: 'Delete sale trackings and item shelving sales',
2024-03-14 07:36:19 +00:00
accessType: 'WRITE',
2023-06-01 13:01:38 +00:00
accepts: [
{
arg: 'saleFk',
type: 'number',
description: 'The sale id'
},
{
2024-03-14 07:36:19 +00:00
arg: 'stateCodes',
type: ['string']
},
2023-06-01 13:01:38 +00:00
],
http: {
path: `/delete`,
verb: 'POST'
}
});
2024-03-14 07:36:19 +00:00
Self.delete = async(saleFk, stateCodes, options) => {
const models = Self.app.models;
2023-06-01 13:01:38 +00:00
const myOptions = {};
let tx;
2023-06-01 13:01:38 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
2024-03-14 07:36:19 +00:00
const itemShelvingSales = await models.ItemShelvingSale.find({where: {saleFk: saleFk}}, myOptions);
for (let itemShelvingSale of itemShelvingSales)
await itemShelvingSale.destroy(myOptions);
2024-03-14 07:36:19 +00:00
const states = await models.State.find({
fields: ['id'],
where: {
code: {inq: stateCodes}
}
}, myOptions);
2024-03-14 07:36:19 +00:00
const stateIds = states.map(state => state.id);
const filter = {
where: {
saleFk: saleFk,
2024-03-14 07:36:19 +00:00
stateFk: {inq: stateIds}
}
};
const saleTrackings = await models.SaleTracking.find(filter, myOptions);
for (let saleTracking of saleTrackings)
await saleTracking.destroy(myOptions);
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
2023-06-01 13:01:38 +00:00
};
};