-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolvers.js
217 lines (198 loc) · 7.47 KB
/
resolvers.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
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
const { GraphQLError } = require('graphql');
const db = require('./db');
function randomIDGenerator() {
return Math.floor(Math.random() * 100);
}
const Query = {
doctorByName: (root,args,context,info) => {
return db.doctors.find(doctor => doctor.name === args.name);
},
appointmentByDoctorName: (root, args, context, info) => {
return db.appointments.filter(appointment => appointment.doctor_name === args.doctor_name);
}
}
const Mutation = {
createDoctor:(root,args,context,info) => {
if (db.doctors.find(doctor => doctor.name === args.name)) {
throw new GraphQLError("Doctor is already existed", {
extensions: {
code: "BAD_REQUEST",
},
});
}
var newId = "doctor" + (db.doctors.length + 1);
var e = {
id: newId,
name: args.name,
clinic_name: args.clinic_name,
specialty: args.specialty};
db.doctors.push(e);
return e;
},
createPatient:(root,args,context,info) => {
if (db.patients.find(patient => patient.name === args.name)) {
throw new GraphQLError("Patient is already existed", {
extensions: {
code: "BAD_REQUEST",
},
});
}
var newId = "patient" + (db.patients.length + 1);
var e = {
id: newId,
name: args.name,
age: args.age,
email: args.email};
db.patients.push(e);
return e;
},
createTimeslot:(root,args,context,info) => {
if (db.timeslots.find(timeslot => timeslot.start_time === args.start_time &&
timeslot.end_time === args.end_time )) {
var e = {
start_time: args.start_time,
end_time: args.end_time
};
return e;
} else {
throw new GraphQLError("30 minute slots from 9am to 5pm only", {
extensions: {
code: "BAD_REQUEST",
},
});
}
},
createAppointment:(root,args,context,info) => {
if (db.appointments.find(appointment => appointment.doctor_name === args.doctor_name &&
appointment.time.start_time === args.time.start_time &&
appointment.time.end_time === args.time.end_time)) {
throw new GraphQLError("Appointment is already existed", {
extensions: {
code: "BAD_REQUEST",
},
});
}
if (db.doctors.find(doctor => doctor.name === args.doctor_name) === undefined) {
throw new GraphQLError("Such Doctor does not exist", {
extensions: {
code: "BAD_REQUEST",
},
});
}
if (db.timeslots.find(timeslot => timeslot.start_time === args.time.start_time &&
timeslot.end_time === args.time.end_time )) {
var new_id = "appointment" + randomIDGenerator();
while (db.appointments.find(appointment => appointment.id === new_id)) {
new_id = "appointment" + randomIDGenerator();
}
var e = {
id: new_id,
doctor_name: args.doctor_name,
time: args.time
}
db.appointments.push(e);
return e;
}
throw new GraphQLError("30 minute slots from 9am to 5pm only", {
extensions: {
code: "BAD_REQUEST",
},
});
},
deleteAppointment:(root,args,context,info) => {
for (let idx = 0; idx < db.appointments.length; idx++) {
if (db.appointments[idx].doctor_name === args.doctor_name &&
db.appointments[idx].time.start_time === args.time.start_time &&
db.appointments[idx].time.end_time === args.time.end_time) {
let res = db.appointments[idx];
db.appointments.splice(idx, 1);
return res;
}
}
throw new GraphQLError("Appointment does not exist", {
extensions: {
code: "BAD_REQUEST",
},
});
},
createEvent:(root,args,context,info) => {
if (db.events.find(event => event.patient_name === args.patient_name &&
event.time.start_time === args.time.start_time &&
event.time.end_time === args.time.end_time)) {
throw new GraphQLError("Each patient cannot book 2 different events at same timelsot", {
extensions: {
code: "BAD_REQUEST",
},
});
}
if (db.doctors.find(doctor => doctor.name === args.doctor_name) === undefined) {
throw new GraphQLError("Such Doctor does not exist", {
extensions: {
code: "BAD_REQUEST",
},
});
}
if (db.patients.find(patient => patient.name === args.patient_name) === undefined) {
throw new GraphQLError("Such Patient does not exist", {
extensions: {
code: "BAD_REQUEST",
},
});
}
if (db.timeslots.find(timeslot => timeslot.start_time === args.time.start_time &&
timeslot.end_time === args.time.end_time )) {
var new_id = "event" + randomIDGenerator();
while (db.events.find(event => event.id === new_id)) {
new_id = "event" + randomIDGenerator();
}
var e = {
id: new_id,
doctor_name: args.doctor_name,
patient_name: args.patient_name,
time: args.time
}
db.events.push(e);
return e;
}
throw new GraphQLError("30 minute slots from 9am to 5pm only", {
extensions: {
code: "BAD_REQUEST",
},
});
},
deleteEvent:(root,args,context,info) => {
for (let idx = 0; idx < db.events.length; idx++) {
if (db.events[idx].patient_name === args.patient_name &&
db.events[idx].time.start_time === args.time.start_time &&
db.events[idx].time.end_time === args.time.end_time) {
let res = db.events[idx];
db.events.splice(idx, 1);
return res;
}
}
throw new GraphQLError("Event does not exist", {
extensions: {
code: "BAD_REQUEST",
},
});
},
updateEvent:(root,args,context,info) => {
for (let idx = 0; idx < db.events.length; idx++) {
if (db.events[idx].doctor_name === args.doctor_name &&
db.events[idx].time.start_time === args.time.start_time &&
db.events[idx].time.end_time === args.time.end_time) {
db.events[idx].patient_name = args.new_patient_name
return db.events[idx];
}
}
throw new GraphQLError("Event does not exist", {
extensions: {
code: "BAD_REQUEST",
},
});
}
}
module.exports = {
Query,
Mutation
}