From d896e54edfacc81bd338d0a2514053b049bc4a9e Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Thu, 25 Feb 2021 16:22:45 +0530 Subject: [PATCH 01/15] Add Club Type --- server/schema/club/club.type.js | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/server/schema/club/club.type.js b/server/schema/club/club.type.js index e69de29b..668fe21a 100644 --- a/server/schema/club/club.type.js +++ b/server/schema/club/club.type.js @@ -0,0 +1,47 @@ +const { GraphQLString, GraphQLList } = require('graphql'); +const { GraphQLObjectType, GraphQLID, GraphQLDateTime, GraphQLInt } = require('../scalars'); + +const ExecutiveType = new GraphQLObjectType({ + name: 'Executive', + fields: () => ({ + user: { + type: GraphQLID, + }, + name: { + type: GraphQLString, + }, + picture: { + type: GraphQLID, + }, + nitrMail: { + type: GraphQLString, + }, + designation: { + type: GraphQLString, + }, + }), +}); +const ClubType = new GraphQLObjectType({ + name: 'Club', + fields: () => ({ + id: { type: GraphQLID }, + name: { type: GraphQLString }, + website: { type: GraphQLString }, + instagram: { type: GraphQLString }, + facebook: { type: GraphQLString }, + twitter: { type: GraphQLString }, + logo: { type: GraphQLString }, + //TODO: Mention the type of array for executive + executive: { type: GraphQLList(ExecutiveType) }, + society: { type: GraphQLString }, + description: { type: GraphQLString }, + facAd: { type: GraphQLString }, + createdAt: { type: GraphQLID }, + createdBy: { type: GraphQLID }, + updatedAt: { type: GraphQLDateTime }, + updatedBy: { type: GraphQLID }, + schemaVersion: { type: GraphQLInt }, + }), +}); + +module.exports = ClubType; From d74d07eeaa6cc461d22379c011be6ceb19209585 Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Thu, 4 Mar 2021 00:37:43 +0530 Subject: [PATCH 02/15] Merges ClubSchema into graphql schemas --- server/schema/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/schema/index.js b/server/schema/index.js index c2e8c2ca..ba9dda6b 100644 --- a/server/schema/index.js +++ b/server/schema/index.js @@ -12,8 +12,9 @@ */ const { mergeSchemas } = require('graphql-tools'); +const ClubSchema = require('./club/club.schema'); const UserSchema = require('./user/user.schema'); module.exports = mergeSchemas({ - schemas: [UserSchema], + schemas: [UserSchema, ClubSchema], }); From c767692ec2b2aa5913f98ba71a45d3986dc449d0 Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Thu, 4 Mar 2021 00:38:32 +0530 Subject: [PATCH 03/15] Adds facAd field to the model --- server/schema/club/club.model.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/schema/club/club.model.js b/server/schema/club/club.model.js index 0006dae5..e30a811d 100644 --- a/server/schema/club/club.model.js +++ b/server/schema/club/club.model.js @@ -34,6 +34,10 @@ const ClubSchema = new Schema( required: false, trim: true, }, + facAd: { + type: String, + required: true, + }, facebook: { type: String, required: false, From 6e962a6f33ac3503eecc1f2f7a350617f85ef6ce Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Thu, 4 Mar 2021 00:41:25 +0530 Subject: [PATCH 04/15] Migrates ExecutiveType to a unique file --- server/schema/club/club.type.js | 49 +++++++++++++++------------- server/schema/club/executive.type.js | 38 +++++++++++++++++++++ 2 files changed, 64 insertions(+), 23 deletions(-) create mode 100644 server/schema/club/executive.type.js diff --git a/server/schema/club/club.type.js b/server/schema/club/club.type.js index 668fe21a..0856ef23 100644 --- a/server/schema/club/club.type.js +++ b/server/schema/club/club.type.js @@ -1,26 +1,30 @@ -const { GraphQLString, GraphQLList } = require('graphql'); -const { GraphQLObjectType, GraphQLID, GraphQLDateTime, GraphQLInt } = require('../scalars'); +/** + * @module app.schema.ClubType + * @description User Type + * + * @requires module:app.schema.scalars + * + * @version v1 + * @since 0.1.0 + */ + +const { + GraphQLObjectType, + GraphQLString, + // GraphQLSchema, + GraphQLID, + GraphQLList, + // GraphQLBoolean, + GraphQLInt, + // GraphQLNonNull, + // GraphQLDate, + // GraphQLTime, + GraphQLDateTime, + // GraphQLJSON, + // GraphQLJSONObject, +} = require('../scalars'); +const ExecutiveType = require('./executive.type'); -const ExecutiveType = new GraphQLObjectType({ - name: 'Executive', - fields: () => ({ - user: { - type: GraphQLID, - }, - name: { - type: GraphQLString, - }, - picture: { - type: GraphQLID, - }, - nitrMail: { - type: GraphQLString, - }, - designation: { - type: GraphQLString, - }, - }), -}); const ClubType = new GraphQLObjectType({ name: 'Club', fields: () => ({ @@ -31,7 +35,6 @@ const ClubType = new GraphQLObjectType({ facebook: { type: GraphQLString }, twitter: { type: GraphQLString }, logo: { type: GraphQLString }, - //TODO: Mention the type of array for executive executive: { type: GraphQLList(ExecutiveType) }, society: { type: GraphQLString }, description: { type: GraphQLString }, diff --git a/server/schema/club/executive.type.js b/server/schema/club/executive.type.js new file mode 100644 index 00000000..271897fb --- /dev/null +++ b/server/schema/club/executive.type.js @@ -0,0 +1,38 @@ +const { + GraphQLObjectType, + GraphQLString, + // GraphQLSchema, + GraphQLID, + // GraphQLList, + // GraphQLBoolean, + // GraphQLInt, + // GraphQLNonNull, + // GraphQLDate, + // GraphQLTime, + // GraphQLDateTime, + // GraphQLJSON, + // GraphQLJSONObject, +} = require('../scalars'); + +const ExecutiveType = new GraphQLObjectType({ + name: 'Executive', + fields: () => ({ + user: { + type: GraphQLID, + }, + name: { + type: GraphQLString, + }, + picture: { + type: GraphQLID, + }, + nitrMail: { + type: GraphQLString, + }, + designation: { + type: GraphQLString, + }, + }), +}); + +module.exports = ExecutiveType; From 0c2820965bd6dce4671755d9ea77a13e99ce003b Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Thu, 4 Mar 2021 00:45:17 +0530 Subject: [PATCH 05/15] Adds club queries --- server/schema/club/club.query.js | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/server/schema/club/club.query.js b/server/schema/club/club.query.js index e69de29b..59c49ffd 100644 --- a/server/schema/club/club.query.js +++ b/server/schema/club/club.query.js @@ -0,0 +1,55 @@ +const { + GraphQLObjectType, + GraphQLString, + // GraphQLSchema, + GraphQLID, + GraphQLList, + // GraphQLBoolean, + // GraphQLInt, + GraphQLNonNull, + // GraphQLDate, + // GraphQLTime, + // GraphQLDateTime, + // GraphQLJSON, + // GraphQLJSONObject, +} = require('../scalars'); +const ClubType = require('./club.type'); +const { getClub, listClubs, searchClubs } = require('./club.resolver'); + +module.exports = new GraphQLObjectType({ + name: 'ClubQuery', + fields: { + getClub: { + description: 'Retrieves a single club', + type: ClubType, + args: { + id: { + type: GraphQLNonNull(GraphQLID), + description: "The club's mongo ID.", + }, + }, + resolve: getClub, + }, + listClubs: { + type: GraphQLList(ClubType), + args: { + ids: { + type: GraphQLList(GraphQLID), + description: 'The List of club mongo IDs.', + }, + }, + resolve: listClubs, + }, + searchClubs: { + description: 'Searches a club for keywords', + type: GraphQLList(ClubType), + args: { + keywords: { + description: 'The search keywords', + type: new GraphQLNonNull(GraphQLString), + }, + }, + resolve: searchClubs, + }, + }, +}); From 7897856e21706aa971522b9a34389c86b3cb7741 Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Thu, 4 Mar 2021 00:46:08 +0530 Subject: [PATCH 06/15] Adds updateClub and updateClubExecutive mutations --- server/schema/club/club.mutation.js | 63 +++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/server/schema/club/club.mutation.js b/server/schema/club/club.mutation.js index e69de29b..75e106e1 100644 --- a/server/schema/club/club.mutation.js +++ b/server/schema/club/club.mutation.js @@ -0,0 +1,63 @@ +const { + GraphQLObjectType, + GraphQLString, + // GraphQLSchema, + GraphQLID, + // GraphQLList, + // GraphQLBoolean, + // GraphQLInt, + // GraphQLNonNull, + // GraphQLDate, + // GraphQLTime, + // GraphQLDateTime, + // GraphQLJSON, + // GraphQLJSONObject, +} = require('../scalars'); +const { updateClub, updateClubExecutive } = require('./club.resolver'); +const ClubType = require('./club.type'); +const ExecutiveType = require('./executive.type'); + +module.exports = new GraphQLObjectType({ + name: 'ClubMutation', + fields: { + updateClub: { + description: 'Updates a single club', + type: ClubType, + args: { + id: { type: GraphQLID }, + name: { type: GraphQLString }, + website: { type: GraphQLString }, + instagram: { type: GraphQLString }, + facebook: { type: GraphQLString }, + twitter: { type: GraphQLString }, + logo: { type: GraphQLString }, + + society: { type: GraphQLString }, + description: { type: GraphQLString }, + facAd: { type: GraphQLString }, + }, + resolve: updateClub, + }, + updateClubExecutive: { + type: ExecutiveType, + args: { + user: { + type: GraphQLID, + }, + name: { + type: GraphQLString, + }, + picture: { + type: GraphQLID, + }, + nitrMail: { + type: GraphQLString, + }, + designation: { + type: GraphQLString, + }, + }, + resolve: updateClubExecutive, + }, + }, +}); From e0acda351ebf5c24c9bfbf28c91752b64c5584c4 Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Thu, 4 Mar 2021 00:48:18 +0530 Subject: [PATCH 07/15] adds club schema --- server/schema/club/club.schema.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 server/schema/club/club.schema.js diff --git a/server/schema/club/club.schema.js b/server/schema/club/club.schema.js new file mode 100644 index 00000000..85b37339 --- /dev/null +++ b/server/schema/club/club.schema.js @@ -0,0 +1,22 @@ +const { + // GraphQLObjectType, + // GraphQLString, + GraphQLSchema, + // GraphQLID, + // GraphQLList, + // GraphQLBoolean, + // GraphQLInt, + // GraphQLNonNull, + // GraphQLDate, + // GraphQLTime, + // GraphQLDateTime, + // GraphQLJSON, + // GraphQLJSONObject, +} = require('../scalars'); +const ClubMutation = require('./club.mutation'); +const ClubQuery = require('./club.query'); + +module.exports = new GraphQLSchema({ + query: ClubQuery, + mutation: ClubMutation, +}); From e6fdc740ef926f10c09b99009649ecb4d8f09840 Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Thu, 4 Mar 2021 00:48:37 +0530 Subject: [PATCH 08/15] Adds empty resolvers --- server/schema/club/club.resolver.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/server/schema/club/club.resolver.js b/server/schema/club/club.resolver.js index e69de29b..80341646 100644 --- a/server/schema/club/club.resolver.js +++ b/server/schema/club/club.resolver.js @@ -0,0 +1,17 @@ +const { HasPermission } = require('../../helpers/authorization'); +const ClubModel = require('./club.model'); + +module.exports = { + getClub: async (parent, args, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => {}, + listClubs: async (parent, args, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => {}, + searchClubs: async (parent, args, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => {}, + updateClub: async (parent, args, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => {}, + updateClubExecutive: async ( + parent, + args, + context, + info, + _ClubModel = ClubModel, + _HasPermission = HasPermission + ) => {}, +}; From b381267ac9103243a2743b5fcd79166973b23b52 Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Thu, 4 Mar 2021 00:49:35 +0530 Subject: [PATCH 09/15] grammer correction in HasPermission --- server/helpers/authorization.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/helpers/authorization.js b/server/helpers/authorization.js index a5eb6768..1e6f37aa 100644 --- a/server/helpers/authorization.js +++ b/server/helpers/authorization.js @@ -174,7 +174,7 @@ const Authorization = { * @param {String} permission * @returns {Boolean | GraphQLError} */ - HasPermmission: (session, jwt, permission) => { + HasPermission: (session, jwt, permission) => { if (!Authorization.CheckSession(session, jwt)) { session.destroy(); return APIError('UNAUTHORIZED', null, { From 60d796377051c4f6f19e2de0d66da895c5079093 Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Mon, 22 Mar 2021 20:51:52 +0530 Subject: [PATCH 10/15] Adds bsic documentation to club module --- server/schema/club/club.mutation.js | 10 ++++++++++ server/schema/club/club.query.js | 10 ++++++++++ server/schema/club/club.resolver.js | 12 ++++++++++++ server/schema/club/club.schema.js | 12 ++++++++++++ 4 files changed, 44 insertions(+) diff --git a/server/schema/club/club.mutation.js b/server/schema/club/club.mutation.js index 75e106e1..84186599 100644 --- a/server/schema/club/club.mutation.js +++ b/server/schema/club/club.mutation.js @@ -1,3 +1,13 @@ +/** + * @module app.schema.ClubMutation + * @description Club Mutation + * + * @requires module:app.schema.scalars + * + * @version v1 + * @since 0.1.0 + */ + const { GraphQLObjectType, GraphQLString, diff --git a/server/schema/club/club.query.js b/server/schema/club/club.query.js index 59c49ffd..6c5906df 100644 --- a/server/schema/club/club.query.js +++ b/server/schema/club/club.query.js @@ -1,3 +1,13 @@ +/** + * @module app.schema.ClubQuery + * @description Club Query + * + * @requires module:app.schema.scalars + * + * @version v1 + * @since 0.1.0 + */ + const { GraphQLObjectType, GraphQLString, diff --git a/server/schema/club/club.resolver.js b/server/schema/club/club.resolver.js index 80341646..706ce154 100644 --- a/server/schema/club/club.resolver.js +++ b/server/schema/club/club.resolver.js @@ -1,3 +1,15 @@ +/** + * @module app.schema.ClubResolver + * @description Club Resolver + * + * @requires module:app.schema.ClubType + * @requires module:app.schema.ClubModel + * @requires module:app.authorization + * + * @version v1 + * @since 0.1.0 + */ + const { HasPermission } = require('../../helpers/authorization'); const ClubModel = require('./club.model'); diff --git a/server/schema/club/club.schema.js b/server/schema/club/club.schema.js index 85b37339..e53b4963 100644 --- a/server/schema/club/club.schema.js +++ b/server/schema/club/club.schema.js @@ -1,3 +1,15 @@ +/** + * @module app.schema.Club + * @description Club Schema + * + * @requires module:app.schema.scalars + * @requires module:app.schema.ClubQuery + * @requires module:app.schema.ClubMutation + * + * @version v1 + * @since 0.1.0 + */ + const { // GraphQLObjectType, // GraphQLString, From eb51c827bcf8a544c6a0ea53c70fc4697e789111 Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Sat, 27 Mar 2021 02:08:16 +0530 Subject: [PATCH 11/15] Adds addClub mutation and getClub query Updates pm2, changes logo to ID type, sets required parameter of logo to false for development purposes, adds resolvers for addClub and getClub --- server/package.json | 2 +- server/schema/club/club.model.js | 3 +- server/schema/club/club.mutation.js | 20 ++++++++-- server/schema/club/club.resolver.js | 58 ++++++++++++++++++++++++++++- server/schema/club/club.type.js | 2 +- 5 files changed, 77 insertions(+), 8 deletions(-) diff --git a/server/package.json b/server/package.json index 4f4b9143..40c3c6b8 100644 --- a/server/package.json +++ b/server/package.json @@ -59,7 +59,7 @@ "graphql-type-json": "^0.3.2", "https": "^1.0.0", "mongoose": "^5.10.13", - "pm2": "^4.5.0", + "pm2": "^4.5.5", "winston": "^3.3.3" }, "devDependencies": { diff --git a/server/schema/club/club.model.js b/server/schema/club/club.model.js index e30a811d..066b7263 100644 --- a/server/schema/club/club.model.js +++ b/server/schema/club/club.model.js @@ -27,7 +27,8 @@ const ClubSchema = new Schema( logo: { type: Schema.Types.ObjectId, ref: 'Media', - required: true, + // TODO: set it to true in future. + required: false, }, bio: { type: String, diff --git a/server/schema/club/club.mutation.js b/server/schema/club/club.mutation.js index 84186599..9a1bfd29 100644 --- a/server/schema/club/club.mutation.js +++ b/server/schema/club/club.mutation.js @@ -23,13 +23,29 @@ const { // GraphQLJSON, // GraphQLJSONObject, } = require('../scalars'); -const { updateClub, updateClubExecutive } = require('./club.resolver'); +const { addClub, updateClub, updateClubExecutive } = require('./club.resolver'); const ClubType = require('./club.type'); const ExecutiveType = require('./executive.type'); module.exports = new GraphQLObjectType({ name: 'ClubMutation', fields: { + addClub: { + description: 'Adds a single club', + type: ClubType, + args: { + id: { type: GraphQLID }, + name: { type: GraphQLString }, + website: { type: GraphQLString }, + instagram: { type: GraphQLString }, + facebook: { type: GraphQLString }, + twitter: { type: GraphQLString }, + logo: { type: GraphQLID }, + description: { type: GraphQLString }, + facAd: { type: GraphQLString }, + }, + resolve: addClub, + }, updateClub: { description: 'Updates a single club', type: ClubType, @@ -41,8 +57,6 @@ module.exports = new GraphQLObjectType({ facebook: { type: GraphQLString }, twitter: { type: GraphQLString }, logo: { type: GraphQLString }, - - society: { type: GraphQLString }, description: { type: GraphQLString }, facAd: { type: GraphQLString }, }, diff --git a/server/schema/club/club.resolver.js b/server/schema/club/club.resolver.js index 706ce154..7053ea1e 100644 --- a/server/schema/club/club.resolver.js +++ b/server/schema/club/club.resolver.js @@ -10,12 +10,66 @@ * @since 0.1.0 */ +const { GraphQLError, APIError, FirebaseAuthError } = require('../../helpers/errorHandler'); +const { Model } = require('mongoose'); const { HasPermission } = require('../../helpers/authorization'); const ClubModel = require('./club.model'); module.exports = { - getClub: async (parent, args, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => {}, - listClubs: async (parent, args, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => {}, + getClub: async (parent, {id}, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => { + try { + if (!id) { + return APIError('BAD_REQUEST'); + } + const _club = await _ClubModel.findById(id); + + if (!_club) { + return APIError('NOT_FOUND'); + } + return _club; + } catch (e) { + if (e instanceof GraphQLError) { + return e; + } + return APIError(null, e); + } + }, + addClub: async ( + parent, + {name, website, instagram, facebook, description, facAd}, + context, + info, + _ClubModel = ClubModel, + ) => { + try { + if(!name){ + return APIError('BAD_REQUEST'); + } + if(!_HasPermission(context, 'club.write.all')){ + return APIError('FORBIDDEN'); + } + const _club = await _ClubModel.create({ + name: name, + website: website, + instagram: instagram, + facebook: facebook, + twitter: twitter, + // TODO: create an object in the media collection and use ID + // logo: args.logo, + description: description, + facAd: facAd, + }); + return _club; + } catch (error) { + if (error instanceof GraphQLError) { + return error; + } + return APIError(null, error); + } + }, + listClubs: async (parent, args, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => { + + }, searchClubs: async (parent, args, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => {}, updateClub: async (parent, args, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => {}, updateClubExecutive: async ( diff --git a/server/schema/club/club.type.js b/server/schema/club/club.type.js index 0856ef23..feb4da77 100644 --- a/server/schema/club/club.type.js +++ b/server/schema/club/club.type.js @@ -34,7 +34,7 @@ const ClubType = new GraphQLObjectType({ instagram: { type: GraphQLString }, facebook: { type: GraphQLString }, twitter: { type: GraphQLString }, - logo: { type: GraphQLString }, + logo: { type: GraphQLID}, executive: { type: GraphQLList(ExecutiveType) }, society: { type: GraphQLString }, description: { type: GraphQLString }, From 09df2541e3957b37b09bab15c968012bcd86b8f0 Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Sun, 28 Mar 2021 20:53:33 +0530 Subject: [PATCH 12/15] Adds resolvers for listCubs and updateClub --- server/schema/club/club.mutation.js | 4 + server/schema/club/club.query.js | 1 + server/schema/club/club.resolver.js | 114 ++++++++++++++++++++++++---- 3 files changed, 103 insertions(+), 16 deletions(-) diff --git a/server/schema/club/club.mutation.js b/server/schema/club/club.mutation.js index 9a1bfd29..bd1c090c 100644 --- a/server/schema/club/club.mutation.js +++ b/server/schema/club/club.mutation.js @@ -8,6 +8,7 @@ * @since 0.1.0 */ +const { GraphQLList } = require('graphql'); const { GraphQLObjectType, GraphQLString, @@ -43,6 +44,8 @@ module.exports = new GraphQLObjectType({ logo: { type: GraphQLID }, description: { type: GraphQLString }, facAd: { type: GraphQLString }, + society: { type: GraphQLString }, + executive: { type: GraphQLList(ExecutiveType) }, }, resolve: addClub, }, @@ -59,6 +62,7 @@ module.exports = new GraphQLObjectType({ logo: { type: GraphQLString }, description: { type: GraphQLString }, facAd: { type: GraphQLString }, + society: { type: GraphQLString }, }, resolve: updateClub, }, diff --git a/server/schema/club/club.query.js b/server/schema/club/club.query.js index 6c5906df..72929cd3 100644 --- a/server/schema/club/club.query.js +++ b/server/schema/club/club.query.js @@ -41,6 +41,7 @@ module.exports = new GraphQLObjectType({ resolve: getClub, }, listClubs: { + description: 'Retrieves a list of specified clubs', type: GraphQLList(ClubType), args: { ids: { diff --git a/server/schema/club/club.resolver.js b/server/schema/club/club.resolver.js index 7053ea1e..7cc6cab2 100644 --- a/server/schema/club/club.resolver.js +++ b/server/schema/club/club.resolver.js @@ -5,18 +5,23 @@ * @requires module:app.schema.ClubType * @requires module:app.schema.ClubModel * @requires module:app.authorization - * + * @version v1 * @since 0.1.0 */ const { GraphQLError, APIError, FirebaseAuthError } = require('../../helpers/errorHandler'); const { Model } = require('mongoose'); +const mongoose = require('mongoose'); +/** + * @type {Model} + */ const { HasPermission } = require('../../helpers/authorization'); const ClubModel = require('./club.model'); +const { GraphQLExtension } = require('graphql-extensions'); module.exports = { - getClub: async (parent, {id}, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => { + getClub: async (parent, { id }, context, info, _ClubModel = ClubModel) => { try { if (!id) { return APIError('BAD_REQUEST'); @@ -24,7 +29,7 @@ module.exports = { const _club = await _ClubModel.findById(id); if (!_club) { - return APIError('NOT_FOUND'); + return APIError('NOT_FOUND'); } return _club; } catch (e) { @@ -36,17 +41,17 @@ module.exports = { }, addClub: async ( parent, - {name, website, instagram, facebook, description, facAd}, + { name, website, instagram, facebook, description, facAd, society, executive }, context, info, - _ClubModel = ClubModel, + _ClubModel = ClubModel ) => { try { - if(!name){ + if (!name) { return APIError('BAD_REQUEST'); } - if(!_HasPermission(context, 'club.write.all')){ - return APIError('FORBIDDEN'); + if (!HasPermission(context, 'club.write.all')) { + return APIError('FORBIDDEN'); } const _club = await _ClubModel.create({ name: name, @@ -57,7 +62,9 @@ module.exports = { // TODO: create an object in the media collection and use ID // logo: args.logo, description: description, + society: society, facAd: facAd, + executive: executive, }); return _club; } catch (error) { @@ -67,17 +74,92 @@ module.exports = { return APIError(null, error); } }, - listClubs: async (parent, args, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => { - + listClubs: async (parent, { ids }, context, info, _ClubModel = ClubModel) => { + try { + // TODO: if length == 0, APIError doesn.t work + if (!ids || !(ids instanceof Array || ids.length <= 0)) { + return APIError('BAD_REQUEST'); + } + //TODO: set permission and error handling + // if (!HasPermmission(context, 'club.list.all') || !HasPermmission(context, 'club.read.public')) { + // return APIError('FORBIDDEN'); + // } + + const _clubs = await _ClubModel.find().where('_id').in(ids).exec(); + + return _clubs; + } catch (error) { + if (error instanceof GraphQLError) { + return error; + } + return APIError(null, error); + } + }, + searchClubs: async (parent, { keywords }, context, info, _ClubModel = ClubModel) => { + //TODO: non functional and incomplete as of now + if (!keywords) { + return APIError('BAD_REQUEST'); + } + + const _clubs = _ClubModel.find({}); + return _clubs; + }, + updateClub: async ( + parent, + { id, name, website, instagram, facebook, facAd, description }, + context, + info, + _ClubModel = ClubModel + ) => { + try { + if (!id) { + return APIError('BAD_REQUEST'); + } + //TODO: Permission handling and error handling needs to be improved. + if (!HasPermission(context, 'club.write.all')) { + return APIError('FORBIDDEN'); + } + + const _clubs = await _ClubModel.findByIdAndUpdate( + { _id: id }, + { + $set: { + name: name, + website: website, + facebook: facebook, + instagram: instagram, + facAd: facAd, + description: description, + }, + } + ); + + return _clubs; + } catch (error) { + if (error instanceof GraphQLError) { + return error; + } + return APIError(null, error); + } }, - searchClubs: async (parent, args, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => {}, - updateClub: async (parent, args, context, info, _ClubModel = ClubModel, _HasPermission = HasPermission) => {}, updateClubExecutive: async ( parent, - args, + { user, name, picture, nitrMail, description }, context, info, - _ClubModel = ClubModel, - _HasPermission = HasPermission - ) => {}, + _ClubModel = ClubModel + ) => { + const _clubExecutive = await _ClubModel.findByIdAndUpdate( + { _id: user }, + { + $set: { + name: name, + picture: picture, + nitrMail: nitrMail, + description: description, + }, + } + ); + return _clubExecutive; + }, }; From 3b320b95e23f4026532fb8af1e99449f30a1248f Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Mon, 29 Mar 2021 22:04:23 +0530 Subject: [PATCH 13/15] Adds resolver for searchClubs. Changes updateClub resolver --- server/schema/club/club.mutation.js | 36 +++++----------------- server/schema/club/club.resolver.js | 45 ++++++++++------------------ server/schema/club/executive.type.js | 1 + 3 files changed, 24 insertions(+), 58 deletions(-) diff --git a/server/schema/club/club.mutation.js b/server/schema/club/club.mutation.js index bd1c090c..5f99834a 100644 --- a/server/schema/club/club.mutation.js +++ b/server/schema/club/club.mutation.js @@ -8,25 +8,23 @@ * @since 0.1.0 */ -const { GraphQLList } = require('graphql'); const { GraphQLObjectType, GraphQLString, // GraphQLSchema, GraphQLID, - // GraphQLList, + GraphQLList, // GraphQLBoolean, // GraphQLInt, // GraphQLNonNull, // GraphQLDate, // GraphQLTime, // GraphQLDateTime, - // GraphQLJSON, - // GraphQLJSONObject, + GraphQLJSON, + GraphQLJSONObject, } = require('../scalars'); -const { addClub, updateClub, updateClubExecutive } = require('./club.resolver'); +const { addClub, updateClub } = require('./club.resolver'); const ClubType = require('./club.type'); -const ExecutiveType = require('./executive.type'); module.exports = new GraphQLObjectType({ name: 'ClubMutation', @@ -45,7 +43,7 @@ module.exports = new GraphQLObjectType({ description: { type: GraphQLString }, facAd: { type: GraphQLString }, society: { type: GraphQLString }, - executive: { type: GraphQLList(ExecutiveType) }, + executive: { type: GraphQLList(GraphQLJSONObject) }, }, resolve: addClub, }, @@ -59,33 +57,13 @@ module.exports = new GraphQLObjectType({ instagram: { type: GraphQLString }, facebook: { type: GraphQLString }, twitter: { type: GraphQLString }, - logo: { type: GraphQLString }, + logo: { type: GraphQLID }, description: { type: GraphQLString }, facAd: { type: GraphQLString }, society: { type: GraphQLString }, + executive: { type: GraphQLList(GraphQLJSONObject) }, }, resolve: updateClub, }, - updateClubExecutive: { - type: ExecutiveType, - args: { - user: { - type: GraphQLID, - }, - name: { - type: GraphQLString, - }, - picture: { - type: GraphQLID, - }, - nitrMail: { - type: GraphQLString, - }, - designation: { - type: GraphQLString, - }, - }, - resolve: updateClubExecutive, - }, }, }); diff --git a/server/schema/club/club.resolver.js b/server/schema/club/club.resolver.js index 7cc6cab2..a5a4dc53 100644 --- a/server/schema/club/club.resolver.js +++ b/server/schema/club/club.resolver.js @@ -41,7 +41,7 @@ module.exports = { }, addClub: async ( parent, - { name, website, instagram, facebook, description, facAd, society, executive }, + { name, website, instagram, twitter, facebook, description, facAd, society, executive }, context, info, _ClubModel = ClubModel @@ -50,9 +50,10 @@ module.exports = { if (!name) { return APIError('BAD_REQUEST'); } - if (!HasPermission(context, 'club.write.all')) { - return APIError('FORBIDDEN'); - } + //if (!HasPermission(context, 'club.write.all')) { + // return APIError('FORBIDDEN'); + //} + const _club = await _ClubModel.create({ name: name, website: website, @@ -101,12 +102,17 @@ module.exports = { return APIError('BAD_REQUEST'); } - const _clubs = _ClubModel.find({}); + const _clubs = _ClubModel.find({ + $text: { + $search: keywords, + $caseSensitive: true, + }, + }); return _clubs; }, updateClub: async ( parent, - { id, name, website, instagram, facebook, facAd, description }, + { id, name, website, executive, instagram, facebook, facAd, description }, context, info, _ClubModel = ClubModel @@ -116,9 +122,9 @@ module.exports = { return APIError('BAD_REQUEST'); } //TODO: Permission handling and error handling needs to be improved. - if (!HasPermission(context, 'club.write.all')) { - return APIError('FORBIDDEN'); - } + //if (!HasPermission(context, 'club.write.all')) { + // return APIError('FORBIDDEN'); + //} const _clubs = await _ClubModel.findByIdAndUpdate( { _id: id }, @@ -130,6 +136,7 @@ module.exports = { instagram: instagram, facAd: facAd, description: description, + executive: executive, }, } ); @@ -142,24 +149,4 @@ module.exports = { return APIError(null, error); } }, - updateClubExecutive: async ( - parent, - { user, name, picture, nitrMail, description }, - context, - info, - _ClubModel = ClubModel - ) => { - const _clubExecutive = await _ClubModel.findByIdAndUpdate( - { _id: user }, - { - $set: { - name: name, - picture: picture, - nitrMail: nitrMail, - description: description, - }, - } - ); - return _clubExecutive; - }, }; diff --git a/server/schema/club/executive.type.js b/server/schema/club/executive.type.js index 271897fb..db5105be 100644 --- a/server/schema/club/executive.type.js +++ b/server/schema/club/executive.type.js @@ -17,6 +17,7 @@ const { const ExecutiveType = new GraphQLObjectType({ name: 'Executive', fields: () => ({ + id: { type: GraphQLID }, user: { type: GraphQLID, }, From b6fbbf3e86ee55c57d171ee50e8040e465516c31 Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Wed, 31 Mar 2021 19:03:14 +0530 Subject: [PATCH 14/15] updates required fields to non-null of club mutation --- server/schema/club/club.mutation.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/server/schema/club/club.mutation.js b/server/schema/club/club.mutation.js index 5f99834a..cdc867f6 100644 --- a/server/schema/club/club.mutation.js +++ b/server/schema/club/club.mutation.js @@ -16,11 +16,11 @@ const { GraphQLList, // GraphQLBoolean, // GraphQLInt, - // GraphQLNonNull, + GraphQLNonNull, // GraphQLDate, // GraphQLTime, // GraphQLDateTime, - GraphQLJSON, + //GraphQLJSON, GraphQLJSONObject, } = require('../scalars'); const { addClub, updateClub } = require('./club.resolver'); @@ -34,15 +34,15 @@ module.exports = new GraphQLObjectType({ type: ClubType, args: { id: { type: GraphQLID }, - name: { type: GraphQLString }, + name: { type: GraphQLNonNull(GraphQLString) }, website: { type: GraphQLString }, instagram: { type: GraphQLString }, facebook: { type: GraphQLString }, twitter: { type: GraphQLString }, logo: { type: GraphQLID }, - description: { type: GraphQLString }, + description: { type: GraphQLNonNull(GraphQLString) }, facAd: { type: GraphQLString }, - society: { type: GraphQLString }, + society: { type: GraphQLNonNull(GraphQLString) }, executive: { type: GraphQLList(GraphQLJSONObject) }, }, resolve: addClub, From ffcae1f137439e20a705619c736247beb4c79a02 Mon Sep 17 00:00:00 2001 From: Sriram Patibanda Date: Sat, 24 Apr 2021 00:52:41 +0530 Subject: [PATCH 15/15] Adds deleteClub, changes updateClub resolver and fixes spelling errors --- server/schema/club/club.mutation.js | 10 +++- server/schema/club/club.query.js | 6 +- server/schema/club/club.resolver.js | 88 +++++++++++++++++------------ 3 files changed, 63 insertions(+), 41 deletions(-) diff --git a/server/schema/club/club.mutation.js b/server/schema/club/club.mutation.js index cdc867f6..6bb24b7b 100644 --- a/server/schema/club/club.mutation.js +++ b/server/schema/club/club.mutation.js @@ -23,7 +23,7 @@ const { //GraphQLJSON, GraphQLJSONObject, } = require('../scalars'); -const { addClub, updateClub } = require('./club.resolver'); +const { addClub, updateClub, deleteClub } = require('./club.resolver'); const ClubType = require('./club.type'); module.exports = new GraphQLObjectType({ @@ -65,5 +65,13 @@ module.exports = new GraphQLObjectType({ }, resolve: updateClub, }, + deleteClub: { + description: 'Deletes a single club', + type: ClubType, + args: { + id: { type: GraphQLID }, + }, + resolve: deleteClub, + }, }, }); diff --git a/server/schema/club/club.query.js b/server/schema/club/club.query.js index 72929cd3..4cb5fc1b 100644 --- a/server/schema/club/club.query.js +++ b/server/schema/club/club.query.js @@ -24,12 +24,12 @@ const { // GraphQLJSONObject, } = require('../scalars'); const ClubType = require('./club.type'); -const { getClub, listClubs, searchClubs } = require('./club.resolver'); +const { getClubByID, listClubs, searchClubs } = require('./club.resolver'); module.exports = new GraphQLObjectType({ name: 'ClubQuery', fields: { - getClub: { + getClubByID: { description: 'Retrieves a single club', type: ClubType, args: { @@ -38,7 +38,7 @@ module.exports = new GraphQLObjectType({ description: "The club's mongo ID.", }, }, - resolve: getClub, + resolve: getClubByID, }, listClubs: { description: 'Retrieves a list of specified clubs', diff --git a/server/schema/club/club.resolver.js b/server/schema/club/club.resolver.js index a5a4dc53..336e7ec5 100644 --- a/server/schema/club/club.resolver.js +++ b/server/schema/club/club.resolver.js @@ -12,7 +12,7 @@ const { GraphQLError, APIError, FirebaseAuthError } = require('../../helpers/errorHandler'); const { Model } = require('mongoose'); -const mongoose = require('mongoose'); + /** * @type {Model} */ @@ -21,11 +21,8 @@ const ClubModel = require('./club.model'); const { GraphQLExtension } = require('graphql-extensions'); module.exports = { - getClub: async (parent, { id }, context, info, _ClubModel = ClubModel) => { + getClubByID: async (parent, { id }, context, info, _ClubModel = ClubModel) => { try { - if (!id) { - return APIError('BAD_REQUEST'); - } const _club = await _ClubModel.findById(id); if (!_club) { @@ -47,25 +44,22 @@ module.exports = { _ClubModel = ClubModel ) => { try { - if (!name) { - return APIError('BAD_REQUEST'); - } //if (!HasPermission(context, 'club.write.all')) { // return APIError('FORBIDDEN'); //} const _club = await _ClubModel.create({ - name: name, - website: website, - instagram: instagram, - facebook: facebook, - twitter: twitter, + name, + website, + instagram, + facebook, + twitter, // TODO: create an object in the media collection and use ID // logo: args.logo, - description: description, - society: society, - facAd: facAd, - executive: executive, + description, + society, + facAd, + executive, }); return _club; } catch (error) { @@ -118,30 +112,50 @@ module.exports = { _ClubModel = ClubModel ) => { try { - if (!id) { - return APIError('BAD_REQUEST'); + if (!(await _ClubModel.exists({ id }))) { + return APIError('NOT_FOUND'); } - //TODO: Permission handling and error handling needs to be improved. - //if (!HasPermission(context, 'club.write.all')) { - // return APIError('FORBIDDEN'); - //} - const _clubs = await _ClubModel.findByIdAndUpdate( - { _id: id }, - { - $set: { - name: name, - website: website, - facebook: facebook, - instagram: instagram, - facAd: facAd, - description: description, - executive: executive, - }, + /** + * The getUpdateObject function returns an object that contains the + * key-value pairs of the club fields that are needed to be updated. + */ + const getUpdateObject = (propertiesObject) => { + /**propertiesObject + * Initialises an empty object that stores the updated fields. + */ + const updateObject = {}; + /** + * The propertiesObject(an object which contains the club fields that + * can be updated) is looped through and only the fields that are + * required to be updated are added to updateObject. + */ + for (key in propertiesObject) { + if (propertiesObject[key]) { + updateObject[key] = propertiesObject[key]; + } } - ); - return _clubs; + return updateObject; + }; + + const updateClub = getUpdateObject({ name, website, executive, instagram, facebook, facAd, description }); + const _club = await _ClubModel.findByIdAndUpdate(id, updateClub); + return _club; + } catch (error) { + if (error instanceof GraphQLError) { + return error; + } + return APIError(null, error); + } + }, + deleteClub: async (parent, { id }, context, info, _ClubModel = ClubModel) => { + try { + if (!(await _ClubModel.exists({ id }))) { + return APIError('NOT_FOUND'); + } + const _club = await _ClubModel.findByIdAndDelete(id); + return _club; } catch (error) { if (error instanceof GraphQLError) { return error;