2022-11-23 09:41:32 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
2022-11-22 13:29:42 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('lock', {
|
|
|
|
description: 'Lock an app for the user',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'appName',
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
description: 'The app name',
|
|
|
|
http: {source: 'path'}
|
|
|
|
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: ['object'],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:appName/lock`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.lock = async(ctx, appName, options) => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
const myOptions = {};
|
2022-11-23 09:41:32 +00:00
|
|
|
const $t = ctx.req.__; // $translate
|
2022-11-22 13:29:42 +00:00
|
|
|
|
|
|
|
let tx;
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-12-01 06:22:13 +00:00
|
|
|
const mdbApp = await models.MdbApp.findById(appName, {fields: ['app', 'locked', 'userFk']}, myOptions);
|
2022-11-23 09:41:32 +00:00
|
|
|
|
2022-12-01 06:22:13 +00:00
|
|
|
if (mdbApp.locked) {
|
|
|
|
throw new UserError($t('App locked', {
|
|
|
|
userId: mdbApp.userFk
|
|
|
|
}));
|
|
|
|
}
|
2022-11-23 09:41:32 +00:00
|
|
|
|
2022-11-22 13:29:42 +00:00
|
|
|
const updatedMdbApp = await mdbApp.updateAttributes({
|
|
|
|
userFk: userId,
|
2023-01-16 14:18:24 +00:00
|
|
|
locked: Date.vnNew()
|
2022-11-22 13:29:42 +00:00
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return updatedMdbApp;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|