55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethodCtx('clone', {
|
|
description: 'clone a ticket and return the new ticket id',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The ticket id',
|
|
http: {source: 'path'}
|
|
}, {
|
|
arg: 'shipped',
|
|
type: 'date',
|
|
}, {
|
|
arg: 'withWarehouse',
|
|
type: 'boolean',
|
|
}
|
|
],
|
|
returns: {
|
|
type: 'number',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/clone`,
|
|
verb: 'POST'
|
|
}
|
|
});
|
|
|
|
Self.clone = async(ctx, id, shipped, withWarehouse, options) => {
|
|
const myOptions = {userId: ctx.req.accessToken.userId};
|
|
let tx;
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const [, [{clonedTicketId}]] = await Self.rawSql(`
|
|
CALL vn.ticket_cloneAll(?, ?, ?, @clonedTicketId);
|
|
SELECT @clonedTicketId clonedTicketId;`,
|
|
[id, shipped, withWarehouse], myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
return clonedTicketId;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|