63 lines
1.7 KiB
JavaScript
63 lines
1.7 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethod('clone', {
|
|
description: 'Clone zone and all its properties including geolocations',
|
|
accessType: 'WRITE',
|
|
accepts: {
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'Zone id',
|
|
http: {source: 'path'}
|
|
},
|
|
returns: {
|
|
root: true,
|
|
type: 'Object'
|
|
},
|
|
http: {
|
|
path: '/:id/clone',
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.clone = async id => {
|
|
const models = Self.app.models;
|
|
const transaction = await Self.beginTransaction({});
|
|
|
|
// Find original zone
|
|
const zone = await models.Zone.findOne({
|
|
fields: [
|
|
'name',
|
|
'hour',
|
|
'warehouseFk',
|
|
'agencyModeFk',
|
|
'travelingDays',
|
|
'price',
|
|
'bonus',
|
|
'isVolumetric'],
|
|
where: {id}
|
|
});
|
|
|
|
// Find all original included geolocations
|
|
const includedGeo = await models.ZoneIncluded.find({
|
|
fields: ['geoFk', 'isIncluded'],
|
|
where: {zoneFk: id}
|
|
});
|
|
|
|
try {
|
|
const newZone = await Self.create(zone, {transaction});
|
|
const newIncludedGeo = includedGeo.map(included => {
|
|
included.zoneFk = newZone.id;
|
|
return included;
|
|
});
|
|
|
|
await models.ZoneIncluded.create(newIncludedGeo, {transaction});
|
|
await transaction.commit();
|
|
|
|
return newZone;
|
|
} catch (e) {
|
|
await transaction.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|