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',
|
2023-06-01 13:01:38 +00:00
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'saleFk',
|
|
|
|
type: 'number',
|
|
|
|
description: 'The sale id'
|
|
|
|
},
|
|
|
|
{
|
2023-12-01 08:44:49 +00:00
|
|
|
arg: 'stateCodes',
|
|
|
|
type: ['string']
|
|
|
|
},
|
2023-06-01 13:01:38 +00:00
|
|
|
],
|
|
|
|
returns: {
|
2023-12-01 08:44:49 +00:00
|
|
|
type: 'boolean',
|
2023-06-01 13:01:38 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/delete`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-01 08:44:49 +00:00
|
|
|
Self.delete = async(saleFk, stateCodes, options) => {
|
2023-07-21 07:50:27 +00:00
|
|
|
const models = Self.app.models;
|
2023-06-01 13:01:38 +00:00
|
|
|
const myOptions = {};
|
2023-06-06 09:56:14 +00:00
|
|
|
let tx;
|
2023-06-01 13:01:38 +00:00
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2023-06-06 09:56:14 +00:00
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-12-01 08:44:49 +00:00
|
|
|
const itemShelvingSales = await models.ItemShelvingSale.find({where: {saleFk: saleFk}}, myOptions);
|
|
|
|
|
|
|
|
for (let itemShelvingSale of itemShelvingSales)
|
|
|
|
await itemShelvingSale.destroy(myOptions);
|
2023-07-21 07:50:27 +00:00
|
|
|
|
2023-12-01 08:44:49 +00:00
|
|
|
const states = await models.State.find({
|
|
|
|
fields: ['id'],
|
|
|
|
where: {
|
|
|
|
code: {inq: stateCodes}
|
|
|
|
}
|
2023-07-21 07:50:27 +00:00
|
|
|
}, myOptions);
|
|
|
|
|
2023-12-01 08:44:49 +00:00
|
|
|
const stateIds = states.map(state => state.id);
|
|
|
|
|
2023-07-21 07:50:27 +00:00
|
|
|
const filter = {
|
|
|
|
where: {
|
|
|
|
saleFk: saleFk,
|
2023-12-01 08:44:49 +00:00
|
|
|
stateFk: {inq: stateIds}
|
2023-07-21 07:50:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const saleTrackings = await models.SaleTracking.find(filter, myOptions);
|
|
|
|
for (let saleTracking of saleTrackings)
|
|
|
|
await saleTracking.destroy(myOptions);
|
2023-06-06 09:56:14 +00:00
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
2023-07-21 07:50:27 +00:00
|
|
|
return true;
|
2023-06-06 09:56:14 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2023-06-01 13:01:38 +00:00
|
|
|
};
|
|
|
|
};
|