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'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-12 10:50:15 +00:00
|
|
|
Self.getUserConfig = async(ctx, options) => {
|
2021-05-24 15:41:22 +00:00
|
|
|
const models = Self.app.models;
|
2021-07-12 10:50:15 +00:00
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
2021-05-24 15:41:22 +00:00
|
|
|
|
|
|
|
let userConfig = await models.UserConfig.findOne({
|
2019-01-24 14:50:21 +00:00
|
|
|
where: {userFk: ctx.req.accessToken.userId}
|
2021-07-12 10:50:15 +00:00
|
|
|
}, myOptions);
|
2019-02-06 11:14:54 +00:00
|
|
|
|
2021-05-24 15:41:22 +00:00
|
|
|
const companyFilter = {where: {code: 'VNL'}};
|
2021-07-12 10:50:15 +00:00
|
|
|
const company = await models.Company.findOne(companyFilter, myOptions);
|
2021-05-24 15:41:22 +00:00
|
|
|
|
|
|
|
const warehouseFilter = {where: {code: 'ALG'}};
|
2021-07-12 10:50:15 +00:00
|
|
|
const warehouse = await models.Warehouse.findOne(warehouseFilter, myOptions);
|
2021-05-24 15:41:22 +00:00
|
|
|
|
2019-02-06 11:14:54 +00:00
|
|
|
if (!userConfig) {
|
|
|
|
let newConfig = {
|
2021-05-24 15:41:22 +00:00
|
|
|
warehouseFk: warehouse.id,
|
|
|
|
companyFk: company.id,
|
2019-02-06 11:14:54 +00:00
|
|
|
userFk: ctx.req.accessToken.userId
|
|
|
|
};
|
|
|
|
|
2021-07-12 10:50:15 +00:00
|
|
|
userConfig = await models.UserConfig.create(newConfig, myOptions);
|
2019-02-06 11:14:54 +00:00
|
|
|
}
|
|
|
|
return userConfig;
|
2018-10-08 06:14:25 +00:00
|
|
|
};
|
2018-10-05 11:01:46 +00:00
|
|
|
};
|