2019-03-14 14:07:24 +00:00
|
|
|
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;
|
2019-06-12 15:31:45 +00:00
|
|
|
const tx = await Self.beginTransaction({});
|
2019-03-14 14:07:24 +00:00
|
|
|
|
2019-06-12 15:31:45 +00:00
|
|
|
try {
|
|
|
|
let options = {transaction: tx};
|
2019-05-28 10:55:50 +00:00
|
|
|
|
2019-06-12 15:31:45 +00:00
|
|
|
// Find original zone
|
|
|
|
const zone = await models.Zone.findOne({
|
|
|
|
fields: [
|
|
|
|
'name',
|
|
|
|
'hour',
|
|
|
|
'warehouseFk',
|
|
|
|
'agencyModeFk',
|
|
|
|
'travelingDays',
|
|
|
|
'price',
|
|
|
|
'bonus',
|
|
|
|
'isVolumetric'],
|
|
|
|
where: {id}
|
|
|
|
}, options);
|
2019-03-14 14:07:24 +00:00
|
|
|
|
2019-06-12 15:31:45 +00:00
|
|
|
// Find all original included geolocations
|
|
|
|
const includedGeo = await models.ZoneIncluded.find({
|
|
|
|
fields: ['geoFk', 'isIncluded'],
|
|
|
|
where: {zoneFk: id}
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
// Find all original selected days
|
|
|
|
const calendarDays = await models.ZoneCalendar.find({
|
|
|
|
where: {zoneFk: id}
|
|
|
|
}, options);
|
2019-03-14 14:07:24 +00:00
|
|
|
|
2019-05-28 10:55:50 +00:00
|
|
|
const newZone = await Self.create(zone, options);
|
2019-03-14 14:07:24 +00:00
|
|
|
const newIncludedGeo = includedGeo.map(included => {
|
|
|
|
included.zoneFk = newZone.id;
|
|
|
|
return included;
|
|
|
|
});
|
2019-05-28 10:55:50 +00:00
|
|
|
const newCalendayDays = calendarDays.map(day => {
|
|
|
|
day.zoneFk = newZone.id;
|
|
|
|
return day;
|
|
|
|
});
|
2019-03-14 14:07:24 +00:00
|
|
|
|
2019-05-28 10:55:50 +00:00
|
|
|
await models.ZoneIncluded.create(newIncludedGeo, options);
|
|
|
|
await models.ZoneCalendar.create(newCalendayDays, options);
|
2019-06-12 15:31:45 +00:00
|
|
|
await tx.commit();
|
2019-03-14 14:07:24 +00:00
|
|
|
|
|
|
|
return newZone;
|
|
|
|
} catch (e) {
|
2019-06-12 15:31:45 +00:00
|
|
|
await tx.rollback();
|
2019-03-14 14:07:24 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|