82 lines
2.4 KiB
JavaScript
82 lines
2.4 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
module.exports = Self => {
|
|
Self.remoteMethod('clone', {
|
|
description: 'Clones the selected routes',
|
|
accessType: 'WRITE',
|
|
accepts: [
|
|
{
|
|
arg: 'ids',
|
|
type: ['number'],
|
|
required: true,
|
|
description: 'The routes ids to clone'
|
|
},
|
|
{
|
|
arg: 'etd',
|
|
type: 'date',
|
|
required: true,
|
|
description: 'The estimated time of departure for all roadmaps'
|
|
}
|
|
],
|
|
returns: {
|
|
type: ['Object'],
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/clone`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.clone = async(ids, etd) => {
|
|
const tx = await Self.beginTransaction({});
|
|
try {
|
|
const models = Self.app.models;
|
|
const options = {transaction: tx};
|
|
const originalRoadmaps = await models.Roadmap.find({
|
|
where: {id: {inq: ids}},
|
|
fields: [
|
|
'id',
|
|
'name',
|
|
'tractorPlate',
|
|
'trailerPlate',
|
|
'phone',
|
|
'supplierFk',
|
|
'etd',
|
|
'observations',
|
|
'price'],
|
|
include: [{
|
|
relation: 'expeditionTruck',
|
|
scope: {
|
|
fields: ['roadmapFk', 'warehouseFk', 'eta', 'description']
|
|
}
|
|
}]
|
|
|
|
}, options);
|
|
|
|
if (ids.length != originalRoadmaps.length)
|
|
throw new UserError(`The amount of roadmaps found don't match`);
|
|
|
|
for (const roadmap of originalRoadmaps) {
|
|
roadmap.id = undefined;
|
|
roadmap.etd = etd;
|
|
|
|
const clone = await models.Roadmap.create(roadmap, options);
|
|
|
|
const expeditionTrucks = roadmap.expeditionTruck();
|
|
expeditionTrucks.map(expeditionTruck => {
|
|
expeditionTruck.roadmapFk = clone.id;
|
|
return expeditionTruck;
|
|
});
|
|
await models.ExpeditionTruck.create(expeditionTrucks, options);
|
|
}
|
|
|
|
await tx.commit();
|
|
|
|
return true;
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|