salix/modules/worker/back/methods/worker/deallocatePDA.js

54 lines
1.4 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('deallocatePDA', {
description: 'Deallocate the worker\'s PDA',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'The worker id',
http: {source: 'path'}
}, {
arg: 'pda',
type: 'number',
required: true,
description: 'The pda id'
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/:id/deallocatePDA`,
verb: 'POST'
}
});
Self.deallocatePDA = async(ctx, options) => {
const models = Self.app.models;
const args = ctx.args;
let tx;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const pda = await models.DeviceProduction.findById(args.pda, myOptions);
await pda.updateAttributes({stateFk: 'idle'}, myOptions);
await models.DeviceProductionUser.destroyAll({userFk: args.id, deviceProductionFk: args.pda}, myOptions);
if (tx) await tx.commit();
return pda;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};