Skip to content

Commit

Permalink
[Feature Request] : Plugin Architecture for Server (#730)
Browse files Browse the repository at this point in the history
* Test : lib/resolvers/group_chat_query/groupChatMessages.js

* Add Test  : Added Valid JSON Check

* Create README.md

* Create README.md

* Update is-auth.js

* AddedDocs

Docs from withfilter pending

* Update index.js

* completed

* sample

* update/index.js

* update/index.js

* update/is-auth.js

* Update/readme.md

* update/image-readme

* Add/query-schema

* Update/Plugin-graphQL-schema

* Add/Plugin-MongoDB-Model

* Add/createPlugin-and-refractoring

* Add/getPlugins

* Add/Mutation/UpdatePluginStatus

* Add/Mutation/updateTempPluginInstalledOrgs

* Add/Mutation/Schema

* Add/import/mutations

* Add/plugins

* Fix/path-err

* Fix/Erros

* update/lockfile

* Add/plugin-model

* Update is-auth.js

* Removed extra queries

* Documentation added for plugin queries and models

* Fix/`delelte cr in prettier/prettier`

* updated : mutation

* Add/test `getPlugins`

* Fix/`lint error`

* test `updatePluginInstalledOrgs`

* Test/ `updateInstallStatus`

* Fix/Erros-1

* Fix/Erros-2

* Fix/Erros-3

* Test/`Queries.js`

* Test/`Mutation.js`

* remove extra `console.log`

* Delete admin-plugin-query.js

* Delete super-admin-plugin-query.js

* Fix/Package-lock
  • Loading branch information
SiddheshKukade authored Aug 8, 2022
1 parent e1b6161 commit d56bd8c
Show file tree
Hide file tree
Showing 23 changed files with 358 additions and 207 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"singleQuote": true
"singleQuote": true,
"endOfLine": "auto"
}
Empty file added image/README.md
Empty file.
1 change: 0 additions & 1 deletion lib/middleware/is-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,4 @@ const isAuth = (req) => {
userId,
};
};

module.exports = isAuth;
35 changes: 35 additions & 0 deletions lib/models/Plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
/**
* @name pluginSchema
* @description Schema for MongoDB database
* @param {string} pluginName Name of the plugin preferred having underscores "_"
* @param {string} pluginCreatedBy name of the plugin creator ex.John Doe
* @param {string} pluginDesc brief description of the plugin and it's features
* @param {Boolean} pluginInstallStatus shows if the plugin is enabled or not
* @param {String[]} installedOrgs list of orgIDs on which the plugin is enabled
*/
const pluginSchema = new Schema({
pluginName: {
type: String,
required: true,
},
pluginCreatedBy: {
type: String,
required: true,
},
pluginDesc: {
type: String,
required: true,
},
pluginInstallStatus: {
type: Boolean,
required: true,
default: false,
},
installedOrgs: [
{ type: Schema.Types.ObjectId, required: false, default: [] },
],
});

module.exports = mongoose.model('PluginTemp', pluginSchema);
49 changes: 0 additions & 49 deletions lib/models/Plugins.js

This file was deleted.

10 changes: 8 additions & 2 deletions lib/resolvers/Mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ const removeUserFromGroupChat = require('./group_chat_mutations/removeUserFromGr
const updateLanguage = require('./language_mutation/updateLanguage');
const blockPluginCreationBySuperadmin = require('../resolvers/user_mutations/blockForPlugin');

const createPlugin = require('./plugin_mutations/createPlugin');
const createMessageChat = require('./message_chat_mutation/createMessageChat');
const addLanguageTranslation = require('./language_maintainer_mutation/addLanguageTranslation');

const createPlugin = require('./plugin_mutations/createPlugin');
const updatePluginStatus = require('./plugin_mutations/updatePluginStatus');
const updatePluginInstalledOrgs = require('./plugin_mutations/updatePluginInstalledOrgs');

const Mutation = {
signUp,
login,
Expand Down Expand Up @@ -148,10 +151,13 @@ const Mutation = {
addUserToGroupChat,
removeUserFromGroupChat,
blockPluginCreationBySuperadmin,
createPlugin,

createMessageChat,
addLanguageTranslation,

createPlugin,
updatePluginStatus,
updatePluginInstalledOrgs,
};

module.exports = Mutation;
7 changes: 2 additions & 5 deletions lib/resolvers/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ const postsByOrganizationConnection = require('../resolvers/post_organization_qu
const { users, user, me } = require('./user_query/users');
const { usersConnection } = require('./user_query/users');
const { organizationsMemberConnection } = require('./user_query/users');
const plugin = require('./plugin_query/super-admin-plugin-query');
const adminPlugin = require('./plugin_query/admin-plugin-query');
const myLanguage = require('../resolvers/user_query/myLanguage');
const userLanguage = require('../resolvers/user_query/userLanguage');
const getlanguage = require('../resolvers/language_maintainer_query/getlanguage');
const directChatsByUserID = require('./direct_chat_query/directChatsByUserID');
const directChatsMessagesByChatID = require('./direct_chat_query/directChatsMessagesByChatID');
const checkAuth = require('./auth_query/checkAuth');

const getPlugins = require('./plugin_query/getPlugins');
const Query = {
me,
user,
Expand Down Expand Up @@ -69,10 +67,9 @@ const Query = {

myLanguage,
userLanguage,
plugin,
adminPlugin,

getlanguage,
getPlugins,
};

module.exports = Query;
82 changes: 17 additions & 65 deletions lib/resolvers/plugin_mutations/createPlugin.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,23 @@
const User = require('../../models/User');
const Plugin = require('../../models/Plugins');
const PluginField = require('../../models/PluginsField');
const Organization = require('../../models/Organization');

const { UnauthorizedError } = require('errors');
const { NotFoundError } = require('errors');
const requestContext = require('talawa-request-context');

const Plugin = require('../../models/Plugin');
/**
* @name createPlugin creates a Plugin and return the same
* @description creates a document of Plugin type and stores it in database
* @param {any} parent parent of current request
* @param {object} args payload provided with the request
* @param {any} context context of entire application
*/
// eslint-disable-next-line no-unused-vars
module.exports = async (parent, args, context) => {
let org = await Organization.findOne({
_id: args.plugin.orgId,
});

if (!org) {
throw new NotFoundError(
requestContext.translate('organization.notFound'),
'organization.notFound',
'organization'
);
}

const user = await User.findOne({
_id: context.userId,
pluginCreationAllowed: true,
});

if (!user) {
throw new NotFoundError(
requestContext.translate('user.notFound'),
'user.notFound',
'user'
);
}

const isSuperAdmin = user.userType === 'SUPERADMIN';
if (!isSuperAdmin) {
const isAdmin = org.admins.includes(user.id);
if (!isAdmin) {
throw new UnauthorizedError(
requestContext.translate('user.notAuthorized'),
'user.notAuthorized',
'userAuthorization'
);
}
}

let pluginAddnFields = [];
if (args.plugin.fields.length > 0) {
for (let i = 0; i < args.plugin.fields.length; i++) {
let pluginField = new PluginField({
key: args.plugin.fields[i].key,
value: args.plugin.fields[i].value,
});

pluginAddnFields.push(pluginField);
}
}

//create MongoDB document
let plugin = new Plugin({
orgId: args.plugin.orgId,
pluginName: args.plugin.pluginName,
pluginKey: args.plugin.pluginKey,
additionalInfo: pluginAddnFields,
pluginName: args.pluginName,
pluginCreatedBy: args.pluginCreatedBy,
pluginDesc: args.pluginDesc,
pluginInstallStatus: args.pluginInstallStatus,
installedOrgs: args.installedOrgs,
});

plugin = await plugin.save();

//store the plugin
plugin = await Plugin.save();
return {
...plugin._doc,
};
Expand Down
47 changes: 47 additions & 0 deletions lib/resolvers/plugin_mutations/updatePluginInstalledOrgs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const Plugin = require('../../models/Plugin');
/**
* @name updatePluginInstalledOrgs
* @description updates the installedOrgs list of the specific plugin and adds or removes the current orgId from the list.
* @param {any} parent parent of current request
* @param {object} args payload provided with the request
* @param {any} context context of entire application
*/
// eslint-disable-next-line no-unused-vars
module.exports = async (parent, args, context) => {
let plug = await Plugin.findById(args.id);
const plugOrgList = plug?.installedOrgs;
const isDuplicate = plugOrgList?.includes(args.orgId);
// remove the entry if duplicate
if (isDuplicate) {
// eslint-disable-next-line no-unused-vars
const result = await Plugin.findByIdAndUpdate(
args.id,
{ $pull: { installedOrgs: args.orgId } },
{ new: true },
(err, res) => {
if (err) {
console.log(err);
} else {
console.log('Updated Plugin with installed orgs : ', res);
}
}
);
} else {
// eslint-disable-next-line no-unused-vars
const result = await Plugin.findByIdAndUpdate(
args.id,
{ $push: { installedOrgs: args.orgId } },
{ new: true },
(err, res) => {
if (err) {
console.log(err);
} else {
console.log('Updated Plugin with installed orgs : ', res);
}
}
);
}

plug = await Plugin.findById(args.id);
return plug;
};
27 changes: 27 additions & 0 deletions lib/resolvers/plugin_mutations/updatePluginStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const Plugin = require('../../models/Plugin');
/**
* @name updatePluginStatus
* @description toggles the installStatus of the plugin
* @param {any} parent parent of current request
* @param {object} args payload provided with the request
* @param {any} context context of entire application
*/
// eslint-disable-next-line no-unused-vars
module.exports = async (parent, args, context) => {
console.log('Argment s ', args);
// eslint-disable-next-line no-unused-vars
const result = await Plugin.findByIdAndUpdate(
args.id,
{ pluginInstallStatus: args.status },
{ new: true },
(err, res) => {
if (err) {
console.log(err);
} else {
console.log('Updated Plugin : ', res);
}
}
);
const plug = await Plugin.findById(args.id);
return plug;
};
29 changes: 0 additions & 29 deletions lib/resolvers/plugin_query/admin-plugin-query.js

This file was deleted.

8 changes: 8 additions & 0 deletions lib/resolvers/plugin_query/getPlugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Plugin = require('../../models/Plugin');
/**
* @name getPlugins a GraphQL Query
* @description returns list of plugin from database
*/
module.exports = async () => {
return await Plugin.find();
};
Loading

0 comments on commit d56bd8c

Please sign in to comment.