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.
221 lines (180 loc) • 5.82 kB
JavaScript
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/UserProfile');
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);
});
});
};
const 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
});
});
});
};
const getUserProfile = function (userId, done) {
UserProfileModel.findOne({ userId: userId }).exec(done);
};
const mergeCluedInWithOrgWidgets = function (base, override) {
let result = override;
base.filter(function (w) {
var isPresent = override.find(function (wFromOrg) {
return wFromOrg.name.toLowerCase() === w.name.toLowerCase();
});
if (!isPresent) {
result.push(w);
}
});
return result;
};
var removeUserProfile = function (userId, done) {
UserProfileModel.remove({ userId: userId }, done);
};
var saveUserProfile = function (userProfile, done) {
UserProfileModel.findOne({ userId: userProfile.userId }).exec(function (err, userProfileFound) {
if (err) {
return done(err);
}
if (!userProfileFound) {
var newUserProfile = new UserProfileModel(userProfile);
newUserProfile.save(function (errFromSave) {
done(err);
});
} else {
userProfileFound.widgets = userProfile.widgets;
userProfileFound.isOnBoardingFinished = userProfile.isOnBoardingFinished;
userProfileFound.widgetState = userProfile.widgetState;
userProfileFound.save(done);
}
});
};
module.exports = {
getConfiguration: getConfiguration,
getUserProfile: getUserProfile,
addWidgetToUserProfile: function (clientId, userId, widgetToAdd, done) {
getConfiguration(clientId, function (err, widgetConfiguration) {
if (err) {
done(err);
}
getUserProfile(clientId, function (errUserProfile, userProfile) {
let userProfileToSave = userProfile;
if (errUserProfile) {
done(errUserProfile);
}
if (!userProfile) {
userProfileToSave = new UserProfileModel({
userId: userId,
});
}
let widgetConfigForEntityType = widgetConfiguration.widgets.find(function (w) {
return w.entityType.toLowerCase() === widgetToAdd.entityType.toLowerCase();
});
if (!widgetConfigForEntityType) {
done('invalid entity type');
}
userProfileToSave.widgets = userProfileToSave.widgets || [];
const hasWidgetToAdd = lodash.find(userProfileToSave.widgets || [], function (widgetInUserProfile) {
return widgetInUserProfile.name.toLowerCase() === widgetToAdd.name.toLowerCase();
});
if (hasWidgetToAdd) {
done('you cannot add a widget already added');
}
userProfileToSave.widgets.push({
name: widgetToAdd.name,
place: widgetToAdd.place,
isAdmin: widgetToAdd.isAdmin,
});
saveUserProfile(userProfileToSave, function (errFromSave) {
done(errFromSave, userProfileToSave);
});
});
});
},
saveUserProfile: saveUserProfile,
removeUserProfile: removeUserProfile,
};