69 lines
1.8 KiB
JavaScript
69 lines
1.8 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('deleteZone', {
|
|
description: 'Delete a zone',
|
|
accessType: 'WRITE',
|
|
accepts: {
|
|
arg: 'id',
|
|
type: 'number',
|
|
description: 'The zone id',
|
|
http: {source: 'path'}
|
|
},
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/deleteZone`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.deleteZone = async(ctx, id, options) => {
|
|
const models = Self.app.models;
|
|
const token = ctx.req.accessToken;
|
|
const userId = token.userId;
|
|
const today = Date.vnNew();
|
|
today.setHours(0, 0, 0, 0);
|
|
|
|
let tx;
|
|
const myOptions = {userId};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const filter = {
|
|
where: {
|
|
zoneFk: id
|
|
},
|
|
include: {
|
|
relation: 'ticketState',
|
|
scope: {
|
|
fields: ['id', 'alertLevel', 'code']
|
|
}
|
|
}
|
|
};
|
|
|
|
const ticketList = await models.Ticket.find(filter, myOptions);
|
|
const hasRefFk = ticketList.some(ticket => !ticket.refFk);
|
|
if (hasRefFk)
|
|
throw new UserError('There are tickets to be invoiced');
|
|
|
|
await models.Zone.destroyById(id, myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return id;
|
|
} catch (err) {
|
|
if (tx) await tx.rollback();
|
|
throw err;
|
|
}
|
|
};
|
|
};
|