UNPKG

cluedin-widget

Version:

This project contains all the pages needed for browsing entities and searching them. The aim is to replace the CluedIn.Webapp project with this one when all the pages ( including the Admin page ) will be ported to REACT.

143 lines (119 loc) 3.68 kB
var lodash = require('lodash'); var allDefaults = void 0; var allLayoutDefaults = void 0; var entityConfig = {}; var layoutConfig = {}; var widgetConfigurationModel = require('./models/WidgetConfiguration'); var layoutModel = require('./models/Layout'); var UserProfileModel = require('./models/Layout'); var getAllDefault = function (cb) { if (allDefaults) { return cb(null, allDefaults) } widgetConfigurationModel.find({ isDefault: true }).exec(function (err, defaults) { allDefaults = defaults; return cb(null, defaults); }); }; var getByClientId = function (clientId, cb) { widgetConfigurationModel.find({ owner: clientId }).exec(function (err, widgetsPerClientId) { if (err) { return cb(err); } return cb(null, widgetsPerClientId); }); }; var getAllDefaultLayout = function (cb) { if (allLayoutDefaults) { return cb(null, allLayoutDefaults) } layoutModel.find({ isDefault: true }).exec(function (err, defaults) { allLayoutDefaults = defaults; return cb(null, defaults); }); }; var getLayoutByClientId = function (clientId, cb) { layoutModel.find({ owner: clientId }).exec(function (err, layoutsPerClientId) { return cb(null, layoutsPerClientId); }); }; var getLayoutConfiguration = function (clientId, cb) { var result = []; if (layoutConfig[clientId]) { return cb(null, layoutConfig[clientId]); } getAllDefaultLayout(function (err, defaults) { getLayoutByClientId(clientId, function (err, layouts) { defaults.forEach(function (defaultLayout) { var customizeLayout = lodash.find(layouts, function (l) { return defaultLayout.code === l.code; }); if (customizeLayout) { result.push(customizeLayout); } else { result.push(defaultLayout); } }); layoutConfig[clientId] = result; return cb(null, result); }); }); }; var getWidgetConfiguration = function (clientId, cb) { var result = []; if (entityConfig[clientId]) { return cb(null, entityConfig[clientId]); } getAllDefault(function (err, defaults) { getByClientId(clientId, function (err, widgets) { defaults.forEach(function (defaultWidget) { var customizedWidget = lodash.find(widgets, function (w) { return defaultWidget.entityType === w.entityType; }); if (customizedWidget) { result.push(customizedWidget); } else { result.push(defaultWidget); } }); entityConfig[clientId] = result; return cb(null, result); }); }); }; module.exports = { getConfiguration: function (clientId, done) { getWidgetConfiguration(clientId, function (err, all) { if (err) { return done(err); } getLayoutConfiguration(clientId, function (err, allLayouts) { if (err) { return done(err); } return done(null, { widgets: all, layouts: allLayouts }); }); }); }, getUserProfile: function (userId, done) { UserProfileModel.find({ userId: userId }).exec(done); }, saveUserProfile: function (userProfile, done) { UserProfileModel.find({ userId: userProfile.userId }).exec(function (err, userProfileFound) { if (err) { return done(err); } if (!userProfileFound) { var newUserProfile = new UserProfileModel(userProfile); newUserProfile.save(done); } else { userProfileFound.isOnBoardingFinished = userProfile.isOnBoardingFinished; userProfileFound.widgetState = userProfile.widgetState; userProfileFound.save(done); } }); } };