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

56 lines
1.5 KiB
JavaScript

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: 'created',
type: 'date',
required: true,
description: 'The created date for all routes'
}
],
returns: {
type: ['Object'],
root: true
},
http: {
path: `/clone`,
verb: 'POST'
}
});
Self.clone = async(ids, created) => {
const tx = await Self.beginTransaction({});
try {
const options = {transaction: tx};
const originalRoutes = await Self.find({
where: {id: {inq: ids}},
fields: ['workerFk', 'agencyModeFk', 'vehicleFk', 'description']
}, options);
if (ids.length != originalRoutes.length)
throw new Error(`The amount of routes found don't match`);
const routes = originalRoutes.map(route => {
route.created = created;
return route;
});
const clones = await Self.create(routes, options);
await tx.commit();
return clones;
} catch (e) {
await tx.rollback();
throw e;
}
};
};