2024-01-22 06:58:18 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2023-12-11 12:01:34 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('updateTracking', {
|
|
|
|
description: 'Modify a saleTracking record and, if applicable, add a corresponding record in saleBuy.',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'saleFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'originalQuantity',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'code',
|
|
|
|
type: 'string',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'isChecked',
|
2024-01-04 11:27:02 +00:00
|
|
|
type: 'boolean',
|
2023-12-11 12:01:34 +00:00
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'buyFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'isScanned',
|
2024-01-04 11:35:00 +00:00
|
|
|
type: 'boolean',
|
2023-12-11 12:01:34 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
http: {
|
|
|
|
path: `/updateTracking`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-22 06:49:21 +00:00
|
|
|
Self.updateTracking = async(ctx, saleFk, originalQuantity, code, isChecked, buyFk, isScanned = null, options) => {
|
2023-12-11 12:01:34 +00:00
|
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
const models = Self.app.models;
|
2024-02-08 09:08:16 +00:00
|
|
|
const myOptions = {userId};
|
2023-12-11 12:01:34 +00:00
|
|
|
let tx;
|
|
|
|
|
|
|
|
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},
|
|
|
|
}, myOptions);
|
|
|
|
|
2024-01-31 12:11:15 +00:00
|
|
|
if (!state) throw new UserError('this state does not exist');
|
2024-01-22 06:49:21 +00:00
|
|
|
const uniqueAttributes = {
|
2023-12-11 12:01:34 +00:00
|
|
|
saleFk,
|
2024-01-22 06:49:21 +00:00
|
|
|
workerFk: userId,
|
|
|
|
stateFk: state?.id,
|
|
|
|
};
|
|
|
|
const attributes = {
|
2023-12-11 12:01:34 +00:00
|
|
|
isChecked,
|
|
|
|
originalQuantity,
|
2024-01-22 06:49:21 +00:00
|
|
|
isScanned
|
2023-12-11 12:01:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const saleTracking = await models.SaleTracking.findOne({
|
2024-01-22 06:49:21 +00:00
|
|
|
where: uniqueAttributes,
|
2023-12-11 12:01:34 +00:00
|
|
|
}, myOptions);
|
|
|
|
|
2024-01-22 06:49:21 +00:00
|
|
|
if (!saleTracking) {
|
|
|
|
await models.SaleTracking.create({
|
|
|
|
...uniqueAttributes,
|
|
|
|
...attributes
|
|
|
|
}, myOptions);
|
|
|
|
} else {
|
|
|
|
await saleTracking.updateAttributes({
|
|
|
|
...attributes
|
|
|
|
}, myOptions);
|
|
|
|
}
|
2023-12-11 12:01:34 +00:00
|
|
|
|
|
|
|
let isBuy;
|
|
|
|
if (buyFk) {
|
|
|
|
isBuy = await models.Buy.findOne({
|
|
|
|
where: {
|
|
|
|
id: buyFk,
|
|
|
|
itemOriginalFk: {
|
|
|
|
neq: null
|
|
|
|
}
|
|
|
|
}
|
2024-01-25 07:13:47 +00:00
|
|
|
}, myOptions);
|
2023-12-11 12:01:34 +00:00
|
|
|
}
|
2024-02-01 11:56:11 +00:00
|
|
|
|
2023-12-11 12:01:34 +00:00
|
|
|
if (isBuy)
|
|
|
|
await models.SaleBuy.create({saleFk, buyFk}, myOptions);
|
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|