-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissue.js
139 lines (122 loc) · 4.19 KB
/
issue.js
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
const { UserInputError } = require('apollo-server-express');
const { getDb, getNextSequence } = require('./db.js');
const { mustBeSignedIn } = require('./auth.js');
const PAGE_SIZE = 10;
async function get(_, { id }){
const db = getDb();
const issue = await db.collection('issues').findOne({ id });
return issue;
}
async function list(_, { status, effortMin, effortMax, search, page }) {
const db = getDb();
const filter = {};
if (status) filter.status = status;
if (effortMin !== undefined || effortMax !== undefined){
filter.effort = {};
if (effortMin !== undefined) filter.effort.$gte = effortMin;
if (effortMax !== undefined) filter.effort.$lte = effortMax;
}
if (search) filter.$text = { $search: search };
const cursor = db.collection('issues').find(filter)
.sort({ id: 1})
.skip(PAGE_SIZE * (page - 1))
.limit(PAGE_SIZE);
const totalCount = await cursor.count(false);
const issues = cursor.toArray();
const pages = Math.ceil(totalCount / PAGE_SIZE);
return { issues, pages };
}
function validate(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 });
}
}
async function add(_, { issue }) {
const db=getDb();
validate(issue);
const newIssue = Object.assign({}, issue);
newIssue.created = new Date();
newIssue.id = await getNextSequence('issues');
console.log("before adding the issue to database")
const result = await db.collection('issues').insertOne(newIssue);
console.log("after adding the issue to database")
const savedIssue = await db.collection('issues')
.findOne({ _id: result.insertedId });
return savedIssue;
}
async function update(_, { id, changes }) {
const db = getDb();
if (changes.title || changes.status || changes.owner) {
const issue = await db.collection('issues').findOne({ id });
Object.assign(issue, changes);
validate(issue);
}
await db.collection('issues').updateOne({ id }, { $set: changes });
const savedIssue = await db.collection('issues').findOne({ id });
return savedIssue;
}
async function remove(_, { id }){
const db = getDb();
const issue = await db.collection('issues').findOne({ id });
if (!issue) return false;
issue.deleted = new Date();
let result = await db.collection('deleted_issues').insertOne(issue);
if (result.insertedId) {
result = await db.collection('issues').removeOne({ id });
return result.deletedCount === 1;
}
return false;
}
async function restore(_, { id }){
const db = getDb();
const issue = await db.collection('deleted_issues').findOne({ id });
if (!issue) return false;
issue.deleted = new Date();
let result = await db.collection('issues').insertOne(issue);
if (result.insertedId) {
result = await db.collection('deleted_issues').removeOne({ id });
return result.deletedCount === 1;
}
return false;
}
async function counts(_, { status, effortMin, effortMax }){
const db = getDb();
const filter = {};
if (status) filter.status = status;
if (effortMin !== undefined || effortMax !== undefined) {
filter.effort = {};
if (effortMin !== undefined) filter.effort.$gte = effortMin;
if (effortMax !== 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);
}
module.exports = { list,
add: mustBeSignedIn(add),
get,
update: mustBeSignedIn(update),
delete: mustBeSignedIn(remove),
restore: mustBeSignedIn(restore),
counts, };