salix/modules/item/back/methods/item/new.js

99 lines
3.0 KiB
JavaScript
Raw Normal View History

2019-01-16 07:46:40 +00:00
let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
2023-06-01 06:32:06 +00:00
Self.remoteMethodCtx('new', {
2021-07-12 10:54:59 +00:00
description: 'returns the created item',
2019-01-16 07:46:40 +00:00
accessType: 'WRITE',
accepts: [{
arg: 'params',
type: 'object',
http: {source: 'body'}
}],
returns: {
type: 'number',
root: true
},
http: {
path: `/new`,
verb: 'post'
}
});
2023-06-01 06:32:06 +00:00
Self.new = async(ctx, params, options) => {
2021-07-12 10:54:59 +00:00
const models = Self.app.models;
2023-06-01 06:32:06 +00:00
const myOptions = {userId: ctx.req.accessToken.userId};
2021-07-12 10:54:59 +00:00
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
const validUpdateParams = [
2019-01-16 07:46:40 +00:00
'provisionalName',
'typeFk',
'intrastatFk',
'originFk',
2023-01-17 09:01:49 +00:00
'priority',
2023-02-20 12:15:47 +00:00
'tag'
2019-01-16 07:46:40 +00:00
];
for (const key in params) {
if (validUpdateParams.indexOf(key) === -1)
throw new UserError(`You don't have enough privileges to do that`);
}
try {
const itemConfig = await models.ItemConfig.findOne({fields: ['validPriorities']}, myOptions);
if (!itemConfig.validPriorities.includes(params.priority))
throw new UserError(`Valid priorities: ${[...itemConfig.validPriorities]}`);
2023-01-17 09:01:49 +00:00
2021-07-12 10:54:59 +00:00
const provisionalName = params.provisionalName;
2019-01-16 07:46:40 +00:00
delete params.provisionalName;
2023-01-17 09:01:49 +00:00
const itemType = await models.ItemType.findById(params.typeFk, {fields: ['isLaid']}, myOptions);
2022-10-31 13:43:31 +00:00
params.isLaid = itemType.isLaid;
2023-02-20 12:15:47 +00:00
params.isPhotoRequested = true;
2022-10-31 13:43:31 +00:00
const item = await models.Item.create(params, myOptions);
2022-10-31 13:43:31 +00:00
2021-07-12 10:54:59 +00:00
const typeTags = await models.ItemTypeTag.find({
where: {itemTypeFk: item.typeFk}
}, myOptions);
2019-01-16 07:46:40 +00:00
let query = `SET @isTriggerDisabled = TRUE`;
2021-07-12 10:54:59 +00:00
await Self.rawSql(query, null, myOptions);
2023-01-17 09:01:49 +00:00
const nameTag = await models.Tag.findById(params.tag, {fields: ['id']}, myOptions);
2019-01-16 07:46:40 +00:00
let newTags = [];
2023-01-17 09:01:49 +00:00
newTags.push({itemFk: item.id, tagFk: nameTag.id, value: provisionalName, priority: item.priority});
2019-01-16 07:46:40 +00:00
typeTags.forEach(typeTag => {
2023-01-17 09:01:49 +00:00
if (nameTag.id != typeTag.tagFk)
newTags.push({itemFk: item.id, tagFk: typeTag.tagFk, value: '', priority: typeTag.priority});
2019-01-16 07:46:40 +00:00
});
2021-07-12 10:54:59 +00:00
await models.ItemTag.create(newTags, myOptions);
2019-01-16 07:46:40 +00:00
query = `SET @isTriggerDisabled = FALSE`;
2021-07-12 10:54:59 +00:00
await Self.rawSql(query, null, myOptions);
2019-01-16 07:46:40 +00:00
query = `CALL vn.itemRefreshTags(?)`;
2021-07-12 10:54:59 +00:00
await Self.rawSql(query, [item.id], myOptions);
if (tx) await tx.commit();
2019-01-16 07:46:40 +00:00
return item;
} catch (e) {
2021-07-12 10:54:59 +00:00
if (tx) await tx.rollback();
2019-01-16 07:46:40 +00:00
throw e;
}
};
};