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) => {
|
2023-07-21 07:50:27 +00:00
|
|
|
const models = Self.app.models;
|
2023-06-01 13:01:38 +00:00
|
|
|
const userId = ctx.req.accessToken.userId;
|
2023-06-06 09:56:14 +00:00
|
|
|
const myOptions = {};
|
|
|
|
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-07-21 07:50:27 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
2023-06-06 09:56:14 +00:00
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
2023-07-21 07:50:27 +00:00
|
|
|
return newSaleTracking;
|
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
|
|
|
};
|
|
|
|
};
|