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

137 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-05-15 10:57:46 +00:00
let UserError = require('vn-loopback/util/user-error');
2018-02-22 12:40:23 +00:00
module.exports = Self => {
Self.remoteMethod('clone', {
description: 'clone item',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'origin itemId',
http: {source: 'path'}
}],
returns: {
2021-07-12 10:54:59 +00:00
type: 'object',
2019-05-15 10:57:46 +00:00
description: 'new cloned itemId',
root: true,
2018-02-22 12:40:23 +00:00
},
http: {
path: `/:id/clone`,
verb: 'post'
}
});
2021-07-12 10:54:59 +00:00
Self.clone = async(itemId, options) => {
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
2018-02-22 12:40:23 +00:00
try {
2021-07-12 10:54:59 +00:00
const origin = await Self.findById(itemId, null, myOptions);
2019-05-15 10:57:46 +00:00
if (!origin)
2019-06-21 06:26:59 +00:00
throw new UserError(`This item doesn't exists`);
2019-05-15 10:57:46 +00:00
origin.itemTag = undefined;
origin.description = undefined;
origin.comment = undefined;
2019-05-23 06:03:31 +00:00
origin.size = undefined;
2021-07-12 10:54:59 +00:00
const newItem = await Self.create(origin, myOptions);
await cloneTaxes(origin.id, newItem.id, myOptions);
await cloneBotanical(origin.id, newItem.id, myOptions);
await cloneTags(origin.id, newItem.id, myOptions);
2021-07-12 10:54:59 +00:00
if (tx) await tx.commit();
2018-05-15 12:02:16 +00:00
2019-05-15 10:57:46 +00:00
return newItem;
2019-04-02 12:36:49 +00:00
} catch (e) {
2021-07-12 10:54:59 +00:00
if (tx) await tx.rollback();
2019-04-02 12:36:49 +00:00
throw e;
2018-02-22 12:40:23 +00:00
}
};
2019-05-15 10:57:46 +00:00
/**
* Clone original taxes to new item
* @param {Integer} originalId - Original item id
* @param {Integer} newId - New item id
* @param {Object} options - Transaction options
*/
2019-06-21 06:26:59 +00:00
async function cloneTaxes(originalId, newId, options) {
2019-05-15 10:57:46 +00:00
const models = Self.app.models;
const originalTaxes = await models.ItemTaxCountry.find({
where: {itemFk: originalId},
fields: ['countryFk', 'taxClassFk']
2019-05-15 10:57:46 +00:00
}, options);
2019-07-08 12:07:09 +00:00
const promises = [];
2019-06-21 06:26:59 +00:00
for (tax of originalTaxes) {
2019-05-15 10:57:46 +00:00
tax.itemFk = newId;
2019-07-08 12:07:09 +00:00
const newTax = models.ItemTaxCountry.upsertWithWhere({
2019-05-15 10:57:46 +00:00
itemFk: newId,
countryFk: tax.countryFk,
}, tax, options);
2019-07-08 12:07:09 +00:00
promises.push(newTax);
2019-06-21 06:26:59 +00:00
}
2019-07-08 12:07:09 +00:00
await Promise.all(promises);
2019-05-15 10:57:46 +00:00
}
/**
* Clone original botanical to new item
* @param {Integer} originalId - Original item id
* @param {Integer} newId - New item id
* @param {Object} options - Transaction options
*/
2019-06-21 06:26:59 +00:00
async function cloneBotanical(originalId, newId, options) {
2019-05-15 10:57:46 +00:00
const models = Self.app.models;
const botanical = await models.ItemBotanical.findOne({
where: {itemFk: originalId},
fields: ['genusFk', 'specieFk']
2019-05-15 10:57:46 +00:00
}, options);
if (botanical) {
botanical.itemFk = newId;
2019-06-21 06:26:59 +00:00
await models.ItemBotanical.create(botanical, options);
2019-05-15 10:57:46 +00:00
}
}
/**
* Clone original item tags to new item
* @param {Integer} originalId - Original item id
* @param {Integer} newId - New item id
* @param {Object} options - Transaction options
*/
2019-06-21 06:26:59 +00:00
async function cloneTags(originalId, newId, options) {
2019-05-15 10:57:46 +00:00
const models = Self.app.models;
const originalTags = await models.ItemTag.find({
where: {
itemFk: originalId
},
fields: ['tagFk', 'value', 'priority']
}, options);
2019-07-08 12:07:09 +00:00
const promises = [];
2019-06-21 06:26:59 +00:00
for (tag of originalTags) {
2019-05-15 10:57:46 +00:00
tag.itemFk = newId;
2019-07-08 12:07:09 +00:00
const newItemTag = models.ItemTag.create(tag, options);
2019-05-15 10:57:46 +00:00
2019-07-08 12:07:09 +00:00
promises.push(newItemTag);
2019-06-21 06:26:59 +00:00
}
2019-07-08 12:07:09 +00:00
await Promise.all(promises);
2019-05-15 10:57:46 +00:00
}
2018-02-22 12:40:23 +00:00
};