salix/modules/worker/back/methods/operator/add.js

37 lines
957 B
JavaScript
Raw Normal View History

2024-01-19 13:35:10 +00:00
const UserError = require('vn-loopback/util/user-error');
2023-11-02 15:25:25 +00:00
module.exports = Self => {
Self.remoteMethodCtx('add', {
description: 'Add a new operator',
accessType: 'WRITE',
http: {
path: `/add`,
verb: 'POST'
}
});
2024-01-19 13:35:10 +00:00
Self.add = async(ctx, options) => {
2023-11-02 15:25:25 +00:00
const userId = ctx.req.accessToken.userId;
2024-01-19 13:35:10 +00:00
const myOptions = {};
const $t = ctx.req.__;
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
2023-11-02 15:25:25 +00:00
await Self.create({
workerFk: userId
});
2024-01-19 13:35:10 +00:00
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw new UserError($t('This worker does not exist'));
2023-11-02 15:25:25 +00:00
}
};
};