parent
d358f940a6
commit
11ae1dbe7c
|
@ -1,3 +1,5 @@
|
||||||
|
let UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('clone', {
|
Self.remoteMethod('clone', {
|
||||||
description: 'clone item',
|
description: 'clone item',
|
||||||
|
@ -10,8 +12,9 @@ module.exports = Self => {
|
||||||
http: {source: 'path'}
|
http: {source: 'path'}
|
||||||
}],
|
}],
|
||||||
returns: {
|
returns: {
|
||||||
arg: 'id',
|
type: 'Object',
|
||||||
description: 'new cloned itemId'
|
description: 'new cloned itemId',
|
||||||
|
root: true,
|
||||||
},
|
},
|
||||||
http: {
|
http: {
|
||||||
path: `/:id/clone`,
|
path: `/:id/clone`,
|
||||||
|
@ -20,76 +23,99 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.clone = async itemId => {
|
Self.clone = async itemId => {
|
||||||
let $ = Self.app.models;
|
|
||||||
let transaction = await Self.beginTransaction({});
|
let transaction = await Self.beginTransaction({});
|
||||||
let filter = {
|
let options = {transaction};
|
||||||
where: {
|
|
||||||
id: itemId
|
|
||||||
},
|
|
||||||
include: [
|
|
||||||
{relation: 'tags', scope: {order: 'priority ASC', include: {relation: 'tag'}}}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let origin = await Self.findOne(filter);
|
const origin = await Self.findById(itemId, options);
|
||||||
let copy = JSON.parse(JSON.stringify(origin));
|
if (!origin)
|
||||||
|
throw new UserError(`That item doesn't exists`);
|
||||||
|
|
||||||
delete copy.id;
|
const newItem = await Self.create(origin, options);
|
||||||
delete copy.itemTag;
|
let promises = [];
|
||||||
delete copy.description;
|
|
||||||
delete copy.image;
|
|
||||||
delete copy.comment;
|
|
||||||
|
|
||||||
let newItem = await Self.create(copy);
|
await cloneTaxes(origin.id, newItem.id, promises, options);
|
||||||
|
await cloneBotanical(origin.id, newItem.id, promises, options);
|
||||||
let botanical = await $.ItemBotanical.findOne({
|
await cloneTags(origin.id, newItem.id, promises, options);
|
||||||
where: {itemFk: origin.id},
|
await Promise.all(promises);
|
||||||
fields: ['botanical', 'genusFk', 'specieFk']
|
|
||||||
}, {options: transaction});
|
|
||||||
|
|
||||||
if (botanical) {
|
|
||||||
botanical.itemFk = newItem.id;
|
|
||||||
await $.ItemBotanical.create(botanical);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
let copyTags = await $.ItemTag.find({
|
|
||||||
where: {
|
|
||||||
itemFk: origin.id
|
|
||||||
},
|
|
||||||
fields: ['tagFk', 'value', 'priority']
|
|
||||||
}, {options: transaction});
|
|
||||||
|
|
||||||
for (let i = 0; i < copyTags.length; i++) {
|
|
||||||
copyTags[i].itemFk = newItem.id;
|
|
||||||
await $.ItemTag.upsert(copyTags[i], {options: transaction});
|
|
||||||
}
|
|
||||||
|
|
||||||
let copyTaxes = await Self.app.models.ItemTaxCountry.find({
|
|
||||||
where: {itemFk: origin.id},
|
|
||||||
fields: ['botanical', 'countryFk', 'taxClassFk']
|
|
||||||
}, {options: transaction});
|
|
||||||
|
|
||||||
for (let i = 0; i < copyTaxes.length; i++) {
|
|
||||||
let tax = copyTaxes[i];
|
|
||||||
tax.itemFk = newItem.id;
|
|
||||||
await Self.app.models.ItemTaxCountry.upsertWithWhere(
|
|
||||||
{
|
|
||||||
itemFk: tax.itemFk,
|
|
||||||
countryFk: tax.countryFk,
|
|
||||||
taxClassFk: tax.taxClassFk,
|
|
||||||
},
|
|
||||||
tax,
|
|
||||||
{options: transaction}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
await transaction.commit();
|
await transaction.commit();
|
||||||
return newItem.id;
|
|
||||||
|
return newItem;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await transaction.rollback();
|
await transaction.rollback();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clone original taxes to new item
|
||||||
|
* @param {Integer} originalId - Original item id
|
||||||
|
* @param {Integer} newId - New item id
|
||||||
|
* @param {Array} promises - Array of promises
|
||||||
|
* @param {Object} options - Transaction options
|
||||||
|
*/
|
||||||
|
async function cloneTaxes(originalId, newId, promises, options) {
|
||||||
|
const models = Self.app.models;
|
||||||
|
const originalTaxes = await models.ItemTaxCountry.find({
|
||||||
|
where: {itemFk: originalId},
|
||||||
|
fields: ['botanical', 'countryFk', 'taxClassFk']
|
||||||
|
}, options);
|
||||||
|
|
||||||
|
originalTaxes.forEach(tax => {
|
||||||
|
tax.itemFk = newId;
|
||||||
|
|
||||||
|
const newItemTax = models.ItemTaxCountry.upsertWithWhere({
|
||||||
|
itemFk: newId,
|
||||||
|
countryFk: tax.countryFk,
|
||||||
|
}, tax, options);
|
||||||
|
|
||||||
|
promises.push(newItemTax);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clone original botanical to new item
|
||||||
|
* @param {Integer} originalId - Original item id
|
||||||
|
* @param {Integer} newId - New item id
|
||||||
|
* @param {Array} promises - Array of promises
|
||||||
|
* @param {Object} options - Transaction options
|
||||||
|
*/
|
||||||
|
async function cloneBotanical(originalId, newId, promises, options) {
|
||||||
|
const models = Self.app.models;
|
||||||
|
const botanical = await models.ItemBotanical.findOne({
|
||||||
|
where: {itemFk: originalId},
|
||||||
|
fields: ['botanical', 'genusFk', 'specieFk']
|
||||||
|
}, options);
|
||||||
|
|
||||||
|
if (botanical) {
|
||||||
|
botanical.itemFk = newId;
|
||||||
|
const newBotanical = models.ItemBotanical.create(botanical, options);
|
||||||
|
promises.push(newBotanical);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clone original item tags to new item
|
||||||
|
* @param {Integer} originalId - Original item id
|
||||||
|
* @param {Integer} newId - New item id
|
||||||
|
* @param {Array} promises - Array of promises
|
||||||
|
* @param {Object} options - Transaction options
|
||||||
|
*/
|
||||||
|
async function cloneTags(originalId, newId, promises, options) {
|
||||||
|
const models = Self.app.models;
|
||||||
|
const originalTags = await models.ItemTag.find({
|
||||||
|
where: {
|
||||||
|
itemFk: originalId
|
||||||
|
},
|
||||||
|
fields: ['tagFk', 'value', 'priority']
|
||||||
|
}, options);
|
||||||
|
|
||||||
|
originalTags.forEach(tag => {
|
||||||
|
tag.itemFk = newId;
|
||||||
|
|
||||||
|
const newTag = models.ItemTag.create(tag, options);
|
||||||
|
promises.push(newTag);
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -16,7 +16,7 @@ describe('item clone()', () => {
|
||||||
let itemFk = 1;
|
let itemFk = 1;
|
||||||
let result = await app.models.Item.clone(itemFk);
|
let result = await app.models.Item.clone(itemFk);
|
||||||
|
|
||||||
expect(result).toEqual(nextItemId);
|
expect(result.id).toEqual(nextItemId);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should attempt to clone the given item but give an error as it doesnt exist', async() => {
|
it('should attempt to clone the given item but give an error as it doesnt exist', async() => {
|
||||||
|
@ -24,7 +24,7 @@ describe('item clone()', () => {
|
||||||
let itemFk = 999;
|
let itemFk = 999;
|
||||||
await app.models.Item.clone(itemFk)
|
await app.models.Item.clone(itemFk)
|
||||||
.catch(e => {
|
.catch(e => {
|
||||||
expect(e.message).toContain('Cannot convert undefined or null to object');
|
expect(e.message).toContain(`That item doesn't exists`);
|
||||||
error = e;
|
error = e;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue