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

91 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-06-01 13:01:38 +00:00
module.exports = Self => {
2023-06-02 11:31:41 +00:00
Self.remoteMethodCtx('new', {
2023-06-01 13:01:38 +00:00
description: 'Remplaza el registro o lo crea si no existe',
accessType: 'READ',
accepts: [
{
arg: 'saleFk',
type: 'number',
description: 'The sale id'
},
{
arg: 'isChecked',
type: 'boolean'
},
{
arg: 'quantity',
type: 'number'
},
{
arg: 'stateCode',
type: 'string'
}
],
returns: {
type: ['object'],
root: true
},
http: {
2023-06-02 11:31:41 +00:00
path: `/new`,
2023-06-01 13:01:38 +00:00
verb: 'POST'
}
});
2023-06-02 11:31:41 +00:00
Self.new = async(ctx, saleFk, isChecked, quantity, stateCode, options) => {
const models = Self.app.models;
2023-06-01 13:01:38 +00:00
const userId = ctx.req.accessToken.userId;
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 {
const state = await models.State.findOne({
where: {code: stateCode}
}, myOptions);
const saleTracking = await models.SaleTracking.findOne({
where: {
saleFk: saleFk,
stateFk: state.id,
workerFk: userId
}
}, myOptions);
let newSaleTracking;
if (saleTracking) {
newSaleTracking = await saleTracking.updateAttributes({
saleFk: saleFk,
stateFk: state.id,
workerFk: userId,
isChecked: isChecked,
originalQuantity: quantity,
isScanned: null
}, myOptions);
} else {
newSaleTracking = await models.SaleTracking.create({
saleFk: saleFk,
stateFk: state.id,
workerFk: userId,
isChecked: isChecked,
originalQuantity: quantity,
isScanned: null
});
}
if (tx) await tx.commit();
return newSaleTracking;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
2023-06-01 13:01:38 +00:00
};
};