salix/back/methods/user-config/getUserConfig.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-10-05 11:01:46 +00:00
module.exports = function(Self) {
Self.remoteMethodCtx('getUserConfig', {
description: 'returns the information from UserConfig model for the active user',
accepts: [],
returns: {
2018-10-08 06:14:25 +00:00
type: 'object',
root: true
2018-10-05 11:01:46 +00:00
},
http: {
path: `/getUserConfig`,
verb: 'get'
}
});
Self.getUserConfig = async(ctx, options) => {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
let userConfig = await models.UserConfig.findOne({
2019-01-24 14:50:21 +00:00
where: {userFk: ctx.req.accessToken.userId}
}, myOptions);
const companyFilter = {where: {code: 'VNL'}};
const company = await models.Company.findOne(companyFilter, myOptions);
const warehouseFilter = {where: {code: 'ALG'}};
const warehouse = await models.Warehouse.findOne(warehouseFilter, myOptions);
if (!userConfig) {
let newConfig = {
warehouseFk: warehouse.id,
companyFk: company.id,
userFk: ctx.req.accessToken.userId
};
userConfig = await models.UserConfig.create(newConfig, myOptions);
}
return userConfig;
2018-10-08 06:14:25 +00:00
};
2018-10-05 11:01:46 +00:00
};