Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add poll feature #210

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@
"jsdoc": "^3.6.6",
"jsdoc-to-markdown": "^6.0.1",
"mocha": "^8.2.1",
"prettier": "^2.2.1"
"prettier": "^3.0.3"
}
}
3 changes: 2 additions & 1 deletion server/config/apolloServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const ArticleDataSources = require('../schema/article/article.datasources');
const CommentDataSources = require('../schema/comment/comment.datasources');
const CompanyDataSources = require('../schema/company/company.datasources');
const LiveDataSources = require('../schema/live/live.datasources');

const PollDataSources = require('../schema/poll/poll.datasources');
const APOLLO_ENDPOINT = process.env.APOLLO_ENDPOINT?.includes('herokuapp')
? process.env.APOLLO_ENDPOINT.replace('num', process.env.HEROKU_PR_NUMBER)
: process.env.APOLLO_ENDPOINT;
Expand Down Expand Up @@ -61,6 +61,7 @@ const apolloServer = (httpServer) =>
Company: CompanyDataSources(),
Live: LiveDataSources(),
Comment: CommentDataSources(),
Poll:PollDataSources()
},
}),
debug: !process.env.NODE_ENV || process.env.NODE_ENV === 'development',
Expand Down
5 changes: 2 additions & 3 deletions server/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ const CompanySchema = require('./company/company.schema');
const CompanyType = require('./company/company.type');
const LiveSchema = require('./live/live.schema');
const MediaSchema = require('./media/media.schema');
const CommentSchema = require('./comment/comment.schema');
const CommentType = require('./comment/comment.type');

const PollSchema = require('./poll/poll.schema')
module.exports = stitchSchemas({
subschemas: [
UserSchema,
Expand All @@ -39,6 +37,7 @@ module.exports = stitchSchemas({
LiveSchema,
MediaSchema,
CommentSchema,
PollSchema
],
types: [MediaType, ContentType, UserDetailType, CategoryMapType, CompanyType, CommentType],
mergeTypes: true,
Expand Down
89 changes: 89 additions & 0 deletions server/schema/poll/poll.datasources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const DataLoader = require('dataloader');
const { APIError } = require('../../utils/exception');
const PollModel = require('./poll.model');
const ArticleModel = require('../article/article.model');
const UserSession = require('../../utils/userAuth/session');
const createUpdateObject = require('../../utils/createUpdateObject');

const findByID = new DataLoader(
async (ids) => {
try {
const _polls = await PollModel.find({ _id: ids });
return ids.map((id) => _polls.find((_u) => _u.id.toString() === id.toString()) || null);
} catch (error) {
throw APIError(null, error);
}
},
{
batchScheduleFn: (cb) => setTimeout(cb, 100),
}
);

const find = (limit, offset) => PollModel.find().sort({ _id: 'desc' }).skip(offset).limit(limit);
const Create = async (
question,
options,
optionsCount,
totalVotes,
expiry,
article,
createdBy,
session,
authToken,
mid
) => {
try {
const _poll = await PollModel.create({
question,
options,
optionsCount,
totalVotes,
expiry,
article,
//createdBy: UserSession.valid(session, authToken) ? mid : null,
});
return _poll;
} catch (error) {
throw APIError(null, error);
}
};
const updateProps = async (id, updateFields, session, authToken, mid) => {
await PollModel.findByIdAndUpdate(
id,
{
...createUpdateObject(updateFields),
//updatedBy: UserSession.valid(session, authToken) ? mid : null,
},
{ new: true }
);
};
const updateArticles = async (id, articles, session, authToken, mid) => {
try {
const _articleData = await ArticleModel.find({ _id: articles });

if (!_articleData || _articleData.length !== articles.length) {
throw APIError('NOT_FOUND', null, { reason: 'One or more of the articles provided were not found.' });
}
return PollModel.findByIdAndUpdate(
id,
{
...createUpdateObject({ articles }),
updatedBy: UserSession.valid(session, authToken) ? mid : null,
},
{ new: true }
);
} catch (error) {
throw APIError(null, error);
}
};
const remove = (id) => PollModel.findByIdAndDelete(id);
const PollDataSources = () => ({
findByID,
find,
Create,
updateProps,
updateArticles,
remove,
});

module.exports = PollDataSources;
24 changes: 10 additions & 14 deletions server/schema/poll/poll.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,22 @@ const { Schema, model } = require('mongoose');
*/
const PollSchema = new Schema(
{
question: [
{
type: Object,
required: true,
},
],
question: {
type: String,
required: true,
},
options: [
{
type: String,
required: true,
trim: true,
},
],
optionsCount: [
{
type: Number,
required: true,
min: 0,
},
],
optionsCount: {
type: Number,
required: true,
min: 0,
},
totalVotes: {
type: Number,
required: false,
Expand All @@ -47,7 +43,7 @@ const PollSchema = new Schema(
},
expiry: {
type: Date,
required: true,
required: false,
min: new Date(Date.now()),
},
article: [
Expand Down
78 changes: 78 additions & 0 deletions server/schema/poll/poll.mutation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @module app.schema.PollMutation
* @description Poll Mutation
*
* @requires module:app.schema.scalars
*
* @version v1
* @since 0.1.0
*/

const {
GraphQLObjectType,
//GraphQLString,
// GraphQLSchema,
GraphQLID,
GraphQLList,
//GraphQLBoolean,
GraphQLInt,
GraphQLNonNull,
GraphQLString,
//GraphQLDateTime,
GraphQLTime,
// GraphQLDate,
// GraphQLTime,
// GraphQLDateTime,
// GraphQLJSON,
// GraphQLJSONObject,
} = require('../scalars');
const PollType = require('./poll.type');
const { createPoll, updatePollProp, updatePollArticles, removePoll } = require('./poll.resolver');
module.exports = new GraphQLObjectType({
name: 'PollMutation',
fields: {
createPoll: {
description: 'adds a new poll',
type: PollType,
args: {
question: { type: new GraphQLNonNull(GraphQLString) },
options: { type: new GraphQLNonNull(new GraphQLList(GraphQLString)) },
optionsCount: { type: new GraphQLNonNull(GraphQLInt) },
totalVotes: { type: GraphQLInt },
expiry: { type: GraphQLTime },
article: { type: new GraphQLList(GraphQLID) },
createdBy: { type: GraphQLID },
},
resolve: createPoll,
},
updatePollProp: {
description: "updates a poll's properties",
type: PollType,
args: {
id: { type: new GraphQLNonNull(GraphQLID) },
question: { type: GraphQLString },
options: { type: GraphQLList(GraphQLString) },
totalVotes: { type: GraphQLInt },
optionsCount: { type: GraphQLInt },
},
resolve: updatePollProp,
},
updatePollArticles: {
description: "updates a poll's articles",
type: PollType,
args: {
id: { type: new GraphQLNonNull(GraphQLID) },
article: { type: new GraphQLList(GraphQLID) },
},
resolve: updatePollArticles,
},
removePoll: {
description: 'deletes a poll by ID',
type: PollType,
args: {
id: { type: new GraphQLNonNull(GraphQLID) },
},
resolve: removePoll,
},
},
});
44 changes: 44 additions & 0 deletions server/schema/poll/poll.query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const {
GraphQLObjectType,
//GraphQLString,
// GraphQLSchema,
GraphQLID,
GraphQLList,
GraphQLInt,
GraphQLNonNull,
// GraphQLDate,
// GraphQLTime,
// GraphQLDateTime,
// GraphQLJSON,
// GraphQLJSONObject,
} = require('../scalars');
const { getPollByID, getListOfPolls } = require('./poll.resolver');
const PollType = require('./poll.type');
module.exports = new GraphQLObjectType({
name: 'PollQuery',
fields: {
getPollByID: {
type: PollType,
args: {
id: {
type: new GraphQLNonNull(GraphQLID),
description: "The issue's mongo ID",
},
},
resolve: getPollByID,
},
getListOfPolls: {
description: 'Retrieves a list of all polls.',
type: new GraphQLNonNull(GraphQLList(PollType)),
args: {
limit: {
type: GraphQLInt,
},
offset: {
type: GraphQLInt,
},
},
resolve: getListOfPolls,
},
},
});
Loading