49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('deleteEntry', {
|
|
description: 'Clones an entry',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The entry id',
|
|
http: {source: 'path'}
|
|
}],
|
|
http: {
|
|
path: `/:id/deleteEntry`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.deleteEntry = async(ctx, id, options) => {
|
|
const userId = ctx.req.accessToken.userId;
|
|
const myOptions = {userId};
|
|
let tx;
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const entry = await Self.findById(id, null, myOptions);
|
|
await entry.updateAttribute('travelFk', null, myOptions);
|
|
await Self.rawSql('DELETE FROM vn.duaEntry WHERE entryFk = ?;', [id], myOptions);
|
|
await Self.rawSql(`
|
|
DELETE i.*
|
|
FROM vn.invoiceIn i
|
|
JOIN vn.entry e ON e.invoiceInFk = i.id
|
|
WHERE e.id = ?`, [id], myOptions
|
|
);
|
|
|
|
if (tx) await tx.commit();
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|