-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
218 lines (187 loc) · 5.92 KB
/
server.js
File metadata and controls
218 lines (187 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const express = require('express');
// getting ApolloServer object from the package
const { ApolloServer, UserInputError } = require('apollo-server-express');
const GraphQLDate=require('./graphqldate')
const { MongoClient, Int32 } = require('mongodb');
// file sync
const fs = require('fs');
// Issues
require('dotenv').config();
const about=require('./aboutmessage')
const auth=require('./signinauth.js')
const cookieParser= require('cookie-parser');
const app = express();
app.use('/auth',auth.routes)
app.use(cookieParser());
let cors;
if (true) {
const origin = process.env.UI_SERVER_ORIGIN || 'http://localhost:8000';
const methods = 'POST';
cors = { origin, methods, credentials: true };
} else {
cors = 'false';
}
const enable = process.env.ENABLE_CORS || true;
let db;
const url = process.env.DB_URL || 'mongodb+srv://theophilus:kritheo2420@cluster0.5ggmpye.mongodb.net/?retryWrites=true&w=majority';
// resolvers
const port = process.env.API_SERVER || 3000;
// function start
// IssuesStore function
async function issueCount(_,{status,effortmin,effortmax}){
const filter={};
if(status){
filter.status=status;
}
if(effortmin!==undefined ||effortmax!==undefined){
filter.effort={};
if(effortmax!==undefined){
filter.effort.$gte=effortmin;
}
if(effortmin!==undefined){
filter.effort.$lte=effortmax;
}
}
const results=await db.collection('issues').aggregate([{$match:filter},{$group:{_id:{owner:'$owner',status:'$status'},count:{$sum:1}}}]).toArray();
const stats = {};
results.forEach((result) => {
// eslint-disable-next-line no-underscore-dangle
const { owner, status: statusKey } = result._id;
if (!stats[owner]) stats[owner] = { owner };
stats[owner][statusKey] = result.count;
});
return Object.values(stats);
}
async function issueList(_,{status,search,effortmin,effortmax,page=1}) {
const PAGE_SIZE=10;
const filter={};
if(status){
filter.status=status;
}
if(effortmin!==undefined ||effortmax!==undefined){
filter.effort={};
if(effortmax!==undefined){
filter.effort.$gte=effortmin;
}
if(effortmin!==undefined){
filter.effort.$lte=effortmax;
}
}
if(search){
filter.$text={$search:search};
}
const cursor = await db.collection('issues').find(filter).sort({id:1}).skip(PAGE_SIZE*(page-1)).limit(PAGE_SIZE);
const totalCount = await db.collection('issues').countDocuments(filter);
const issuesDb=await cursor.toArray()
var pages=Math.ceil(totalCount/PAGE_SIZE);
exports.issuesDb = issuesDb;
return {issuesDb,pages};
}
async function issueDelete(_,{id}){
const issue = await db.collection('issues').findOne({id});
if(issue){
issue.deleted=new Date();
const res= await db.collection('issuesdeleted').insertOne({issue});
if(res){
const del= await db.collection('issues').deleteOne({id});
if(del){
const result = await db.collection('counters').findOneAndUpdate({ _id: 'issues' }, { $inc: { current: -1 } }, { returnOriginal: false });
return true
}else{
return false;
}
}
else{
return false;
}
}else{
return false;
}
}
async function connecttodb() {
const client = new MongoClient(url);
await client.connect();
console.log('Connected to MongodbAtlas');
db = client.db('issuetracker');
}
async function issue(_, { id }) {
const issue = await db.collection('issues').findOne({ id: id });
return issue;
}
function validateIssue(_, { issue }) { const errors = []; if (issue.title.length < 3) { errors.push('Field "title" must be at least 3 characters long.'); } if (issue.status == 'Assigned' && !issue.owner) { errors.push('Field "owner" is required when status is "Assigned"'); } if (errors.length > 0) { throw new UserInputError('Invalid input(s)', { errors }); } }
function getContext({req}){
const {signedIn}=auth.getUser(req)
console.log("this s signed i n status"+signedIn)
return {signedIn};
}
async function getNextSequence(name) {
const result = await db.collection('counters').findOneAndUpdate({ _id: name }, { $inc: { current: 1 } }, { returnOriginal: false });
return result.value.current+1;
}
async function issueAdd(_, { issue }) {
const newissues = {
id: await getNextSequence('issues'),
title: issue.title,
created: new Date(),
owner: issue.owner,
};
const res = await db.collection('issues').insertOne(newissues);
const savedIssue = await db.collection('issues').findOne({ _id: res.insertedId });
return savedIssue;
}
async function issueUpdate(_,{id,Changes}){
if(Changes.title||Changes.status||Changes.owner ||Changes.description){
const issue = await db.collection('issues').findOne({ id: id})
Object.assign(issue,Changes)
}
await db.collection('issues').updateOne({id: id},{$set:Changes})
const savedissue=await db.collection('issues').findOne({id: id});
return savedissue;
}
// function e
const resolvers = {
Query: {
about:about.getaboutMessage ,
issueList,
issueAdd,
examplethrow: (_, { a, b }) => {
const val = a + b;
exports.val = val;
return a + b;
},
issue,
examplecatchfromquery: () => {
const { val } = exports;
return val;
},
issueCount
},
Mutation: {
setAboutMessage:auth.mustbesignedin( about.setAboutMessage),
issueUpdate:auth.mustbesignedin(issueUpdate),
issueDelete:auth.mustbesignedin(issueDelete),
examplecatchfrommutation: auth.mustbesignedin(() => {
const { val } = exports;
return val;
},)
},
GraphQLDate,
};
// INstantiating the ApolloServer
const server = new ApolloServer({
typeDefs: fs.readFileSync('schema.graphql', 'utf-8'),
resolvers,
formatError: (error) => {
console.log(error);
return error;
},
context:getContext
});
async function startServer() {
await server.start();
server.applyMiddleware({ app, path: '/graphql', cors });
}
startServer().then(async () => {
await connecttodb();
app.listen(port, () => console.log('Running on 3000'));
});