-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add graphql operations for comments (#176)
* feat: add graphql operations for comment * feat: add roles for comments * chore: prettier and eslint check * fix: update roles for comment * feat: add operations for comment content * feat: add comment parent model enum * fix: remove redundant transactions, * fix: add null check for author * chore: prettier check * fix: refactor create comment * remove redundant transactions * add destructuring * add null check for author * fix: remove redundant authorID * fix: make required arguments non-null * fix: add null check in getting list of comments * fix: remove update author mutation * fix: remove update author import * feat: update permissions * chore: remove previous roles and permissions * chore: run eslint and prettier
- Loading branch information
1 parent
477d759
commit 8fd2962
Showing
14 changed files
with
545 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const DataLoader = require('dataloader'); | ||
const { APIError } = require('../../utils/exception'); | ||
const UserSession = require('../../utils/userAuth/session'); | ||
const userModel = require('../user/user.model'); | ||
const CommentModel = require('./comment.model'); | ||
|
||
const findByID = () => | ||
new DataLoader( | ||
async (data) => { | ||
try { | ||
const _comments = await CommentModel.find({ _id: { $in: data.map(({ id }) => id) } }); | ||
|
||
const _returnComments = data.map(({ id, permission, mid }) => { | ||
const _comment = _comments.find((comment) => comment._id.toString() === id.toString()); | ||
return _comment && _comment.approved ? _comment : permission || mid === _comment.createdBy ? _comment : null; | ||
}); | ||
return _returnComments; | ||
} catch (error) { | ||
throw APIError(null, error); | ||
} | ||
}, | ||
{ | ||
batchScheduleFn: (cb) => setTimeout(cb, 100), | ||
} | ||
); | ||
|
||
const findAll = (offset, limit, permission, mid) => { | ||
// Get approved comments if the user does not have permission to read unapproved comments and the user is not the author | ||
// Get all comments if the user has permission to read unapproved comments or the user is the author | ||
const query = permission ? {} : { $or: [{ approved: true }, { approved: false, createdBy: mid }] }; | ||
return CommentModel.find(query).sort({ createdAt: 'desc' }).skip(offset).limit(limit); | ||
}; | ||
|
||
const countNumberOfComments = (parentID, parentModel) => | ||
CommentModel.countDocuments({ | ||
'parent.reference': parentID, | ||
'parent.model': parentModel, | ||
approved: true, | ||
}); | ||
|
||
const create = async (authorID, content, parentID, parentType, session, authToken, mid, approved) => { | ||
try { | ||
const _author = await userModel.findById(authorID); | ||
if (!_author) { | ||
throw APIError('NOT FOUND', null, 'Invalid Author ID'); | ||
} | ||
|
||
const [_comment] = await CommentModel.create([ | ||
{ | ||
content, | ||
author: { | ||
name: _author.fullName, | ||
reference: authorID, | ||
}, | ||
parent: { | ||
reference: parentID, | ||
model: parentType, | ||
}, | ||
approved: approved || false, | ||
createdBy: UserSession.valid(session, authToken) ? mid : null, | ||
}, | ||
]); | ||
|
||
return _comment; | ||
} catch (error) { | ||
throw APIError(null, error); | ||
} | ||
}; | ||
|
||
const updateContent = async (id, content, session, authToken, mid) => { | ||
try { | ||
const _comment = await CommentModel.findByIdAndUpdate( | ||
id, | ||
{ | ||
content, | ||
updatedBy: UserSession.valid(session, authToken) ? mid : null, | ||
}, | ||
{ new: true } | ||
); | ||
|
||
return _comment; | ||
} catch (error) { | ||
throw APIError(null, error); | ||
} | ||
}; | ||
|
||
const approve = (id) => CommentModel.findByIdAndUpdate(id, { approved: true }, { new: true }); | ||
|
||
const remove = (id) => CommentModel.findByIdAndDelete(id); | ||
|
||
const CommentDataSources = () => ({ | ||
findAll, | ||
findByID: findByID(), | ||
countNumberOfComments, | ||
create, | ||
approve, | ||
updateContent, | ||
remove, | ||
}); | ||
|
||
module.exports = CommentDataSources; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const { | ||
// GraphQLObjectType, | ||
// GraphQLScalarType, | ||
// GraphQLUnionType, | ||
// GraphQLInputObjectType, | ||
GraphQLEnumType, | ||
// GraphQLInterfaceType, | ||
// GraphQLSchema, | ||
// GraphQLNonNull, | ||
// GraphQLError, | ||
// GraphQLList, | ||
// GraphQLString, | ||
// GraphQLID, | ||
// GraphQLBoolean, | ||
// GraphQLInt, | ||
// GraphQLFloat, | ||
// GraphQLDate, | ||
// GraphQLTime, | ||
// GraphQLDateTime, | ||
// GraphQLJSON, | ||
// GraphQLJSONObject, | ||
} = require('../scalars'); | ||
|
||
const CommentParentModelEmum = new GraphQLEnumType({ | ||
name: 'CommentParentModelEnum', | ||
values: { | ||
ARTICLE: { value: 'Article' }, | ||
COMMENT: { value: 'Comment' }, | ||
}, | ||
}); | ||
|
||
module.exports = { | ||
CommentParentModelEmum, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const { | ||
GraphQLObjectType, | ||
// GraphQLScalarType, | ||
// GraphQLUnionType, | ||
// GraphQLInputObjectType, | ||
// GraphQLEnumType, | ||
// GraphQLInterfaceType, | ||
// GraphQLSchema, | ||
GraphQLNonNull, | ||
// GraphQLError, | ||
// GraphQLList, | ||
GraphQLString, | ||
GraphQLID, | ||
// GraphQLBoolean, | ||
// GraphQLInt, | ||
// GraphQLFloat, | ||
// GraphQLDate, | ||
// GraphQLTime, | ||
// GraphQLDateTime, | ||
// GraphQLJSON, | ||
// GraphQLJSONObject, | ||
} = require('../scalars'); | ||
const { CommentParentModelEmum } = require('./comment.enum.types'); | ||
const { createComment, deleteComment, updateCommentContent, approveComment } = require('./comment.resolver'); | ||
|
||
const CommentType = require('./comment.type'); | ||
|
||
module.exports = new GraphQLObjectType({ | ||
name: 'CommentMutation', | ||
fields: { | ||
createComment: { | ||
description: 'Create a comment', | ||
type: CommentType, | ||
args: { | ||
content: { type: new GraphQLNonNull(GraphQLString) }, | ||
authorID: { type: new GraphQLNonNull(GraphQLID) }, | ||
parentID: { type: new GraphQLNonNull(GraphQLID) }, | ||
parentType: { type: new GraphQLNonNull(CommentParentModelEmum) }, | ||
}, | ||
resolve: createComment, | ||
}, | ||
approveComment: { | ||
description: 'Approve a comment', | ||
type: CommentType, | ||
args: { | ||
id: { type: new GraphQLNonNull(GraphQLID) }, | ||
}, | ||
resolve: approveComment, | ||
}, | ||
updateCommentContent: { | ||
description: 'Update Comment by Id', | ||
type: CommentType, | ||
args: { | ||
id: { type: new GraphQLNonNull(GraphQLID) }, | ||
content: { type: new GraphQLNonNull(GraphQLString) }, | ||
}, | ||
resolve: updateCommentContent, | ||
}, | ||
deleteComment: { | ||
description: 'Delete comment by Id', | ||
type: CommentType, | ||
args: { | ||
id: { type: new GraphQLNonNull(GraphQLID) }, | ||
}, | ||
resolve: deleteComment, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const { | ||
GraphQLObjectType, | ||
// GraphQLScalarType, | ||
// GraphQLUnionType, | ||
// GraphQLInputObjectType, | ||
// GraphQLEnumType, | ||
// GraphQLInterfaceType, | ||
// GraphQLSchema, | ||
GraphQLNonNull, | ||
// GraphQLError, | ||
GraphQLList, | ||
// GraphQLString, | ||
GraphQLID, | ||
// GraphQLBoolean, | ||
GraphQLInt, | ||
// GraphQLFloat, | ||
// GraphQLDate, | ||
// GraphQLTime, | ||
// GraphQLDateTime, | ||
// GraphQLJSON, | ||
// GraphQLJSONObject, | ||
} = require('../scalars'); | ||
const { CommentParentModelEmum } = require('./comment.enum.types'); | ||
const { getListOfComments, getCommentById, countOfComments } = require('./comment.resolver'); | ||
|
||
const CommentType = require('./comment.type'); | ||
|
||
module.exports = new GraphQLObjectType({ | ||
name: 'CommentQuery', | ||
fields: { | ||
getListOfComments: { | ||
description: 'Retrieves comments for given list of ids (default all) in descending order of creation time', | ||
type: new GraphQLList(new GraphQLNonNull(CommentType)), | ||
args: { | ||
ids: { | ||
description: 'List of Ids of comments to be retrieved', | ||
type: new GraphQLList(new GraphQLNonNull(GraphQLID)), | ||
}, | ||
limit: { | ||
description: 'No. of Comments to be retrieved', | ||
type: GraphQLInt, | ||
}, | ||
offset: { | ||
description: 'No. of Comments to be skipped | pagination', | ||
type: GraphQLInt, | ||
}, | ||
}, | ||
resolve: getListOfComments, | ||
}, | ||
getCommentById: { | ||
description: 'Retrieves single comment based on id', | ||
type: CommentType, | ||
args: { | ||
id: { | ||
description: 'The id of comment to be retrieved', | ||
type: new GraphQLNonNull(GraphQLID), | ||
}, | ||
}, | ||
resolve: getCommentById, | ||
}, | ||
countOfCommentsByParent: { | ||
description: 'The number of comments on a article/comment', | ||
type: GraphQLInt, | ||
args: { | ||
id: { | ||
description: 'Id of article or comment', | ||
type: new GraphQLNonNull(GraphQLID), | ||
}, | ||
parentType: { | ||
description: 'Type of parent', | ||
type: new GraphQLNonNull(CommentParentModelEmum), | ||
}, | ||
}, | ||
resolve: countOfComments, | ||
}, | ||
}, | ||
}); |
Oops, something went wrong.