forked from ogjr80/firebase-function
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolvers.js
More file actions
318 lines (282 loc) · 9.57 KB
/
resolvers.js
File metadata and controls
318 lines (282 loc) · 9.57 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
const admin = require("firebase-admin");
const serviceAccount = require("./serviceaccounts.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
const db = admin.firestore();
module.exports = db;
const resolvers = {
Query: {
//User Query Resolvers
getUser: async (_, { userId }) => {
const userDoc = await db.collection('users').doc(userId).get();
return userDoc.exists ? userDoc.data() : null;
},
getUsers: async () => {
const usersSnapshot = await db.collection('users').get();
return usersSnapshot.docs.map(doc =>({ userId: doc.id, ...doc.data() }));
},
//Farm Query Resolvers
getFarm: async (_, { farmId }) => {
const farmDoc = await db.collection('farms').doc(farmId).get();
return farmDoc.exists ? farmDoc.data() : null;
},
getFarms: async () => {
const farmsSnapshot = await db.collection('farms').get();
return farmsSnapshot.docs.map(doc => ({farmid: doc.id, ...doc.data()}));
},
getFarmsByUserId: async (_, { userId }) => {
const farmsSnapshot = await db.collection('farms').where('userId', '==', userId).get();
return farmsSnapshot.docs.map(doc => doc.data());
},
//CROP Query Resolvers
getCrop: async (_, { cropId }) => {
const cropDoc = await db.collection('crops').doc(cropId).get();
return cropDoc.exists ? cropDoc.data() : null;
},
getCrops: async () => {
const cropsSnapshot = await db.collection('crops').get();
return cropsSnapshot.docs.map(doc => ({cropId: doc.id, ...doc.data()}));
},
//Livestock Query Resolvers
getLivestock: async (_, { livestockId }) => {
const livestockSnapshot = await db.collection("livestocks").doc(livestockId).get();
return {
...livestockSnapshot.data(),
livestockId: livestockSnapshot.id,
};
},
getAllLivestock: async () => {
const livestockSnapshot = await db.collection("livestocks").get();
return livestockSnapshot.docs.map(doc => ({
...doc.data(),
livestockId: doc.id,
}));
},
//WeatherData Query Resolvers
getAllWeatherData: async () => {
const weatherDataSnapshot = await db.collection("weatherData").get();
return weatherDataSnapshot.docs.map(doc => ({
...doc.data(),
weatherDataId: doc.id,
}));
},
getWeatherData: async (_, { weatherDataId }) => {
const weatherDataSnapshot = await db.collection("weatherData").doc(weatherDataId).get();
return {
...weatherDataSnapshot.data(),
weatherDataId: weatherDataSnapshot.id,
};
},
getWeatherDataByFarmId: async (_, { farmId }) => {
const querySnapshot = await db.collection("weatherData").where("farmId", "==", farmId).get();
return querySnapshot.docs.map(doc => ({
...doc.data(),
weatherDataId: doc.id,
}));
},
},
Mutation: {
//User Mutation
createUser: async (_, { input }) => {
const newUser = {
...input,
userId: db.collection('users').doc().id,
dateCreated: new Date().toISOString(),
};
await db.collection('users').doc(newUser.userId).set(newUser);
return newUser;
},
updateUser: async (_, { userId, input }) => {
const userRef = db.collection('users').doc(userId);
const userDoc = await userRef.get();
if (!userDoc.exists) {
throw new Error(`User with ID ${userId} not found.`);
}
const updatedUser = {
...userDoc.data(),
...input,
};
await userRef.update(updatedUser);
return updatedUser;
},
//Farm Mutation
createFarm: async (_, { input }) => {
const newFarm = {
...input,
farmId: db.collection('farms').doc().id,
dateCreated: new Date().toISOString(),
};
await db.collection('farms').doc(newFarm.farmId).set(newFarm);
return newFarm;
},
updateFarm: async (_, { farmId, input }) => {
const farmRef = db.collection('farms').doc(farmId);
const farmDoc = await farmRef.get();
if (!farmDoc.exists) {
throw new Error(`Farm with ID ${farmId} not found.`);
}
const updatedFarm = {
...farmDoc.data(),
...input,
};
await farmRef.update(updatedFarm);
return updatedFarm;
},
//CROP Mutation
async createCrop(_, { input }) {
const newCropData = {
...input,
dateCreated: new Date().toISOString(),
};
const cropRef = await db.collection("crops").add(newCropData);
const crop = await cropRef.get();
// Add the cropId using the document ID from Firebase
return {
...crop.data(),
cropId: crop.id,
farmId: farm.id,
userId: user.id
};
},
updateCrop: async (_, { cropId, input }) => {
const cropDoc = db.collection('crops').doc(cropId);
await cropDoc.update(input);
const updatedCrop = await cropDoc.get();
return updatedCrop.exists ? updatedCrop.data() : null;
},
deleteCrop: async (_, { cropId }) => {
await db.collection('crops').doc(cropId).delete();
return `Crop with ID ${cropId} has been deleted`;
},
//Livestock Mutation
createLivestock: async (_, { input }) => {
const newLivestockData = {
...input,
dateCreated: new Date().toISOString(),
};
const livestockRef = await db.collection("livestocks").add(newLivestockData);
const livestock = await livestockRef.get();
return {
...livestock.data(),
livestockId: livestock.id,
};
},
updateLivestock: async (_, { livestockId, input }) => {
const livestockRef = db.collection("livestocks").doc(livestockId);
await livestockRef.update(input);
const updatedLivestock = await livestockRef.get();
return {
...updatedLivestock.data(),
livestockId: updatedLivestock.id,
};
},
deleteLivestock: async (_, { livestockId }) => {
const livestockRef = db.collection("livestocks").doc(livestockId);
const deletedLivestock = await livestockRef.get();
await livestockRef.delete();
return {
...deletedLivestock.data(),
livestockId: deletedLivestock.id,
};
},
//WeatherData Mutation
createWeatherData: async (_, { input }) => {
const newWeatherDataRef = await db.collection("weatherData").add(input);
const newWeatherDataSnapshot = await newWeatherDataRef.get();
return {
...newWeatherDataSnapshot.data(),
weatherDataId: newWeatherDataSnapshot.id,
};
},
updateWeatherData: async (_, { weatherDataId, input }) => {
const weatherDataRef = db.collection("weatherData").doc(weatherDataId);
await weatherDataRef.update(input);
const updatedWeatherDataSnapshot = await weatherDataRef.get();
return {
...updatedWeatherDataSnapshot.data(),
weatherDataId: updatedWeatherDataSnapshot.id,
};
},
},
//Chaining in Mutations
//User Chaining
User: {
farms: async (parent) => {
const userId = parent.userId;
const farmsSnapshot = await db.collection('farms').where('userId', '==', userId).get();
return farmsSnapshot.docs.map(doc => ({farmId:doc.id, ...doc.data()}));
},
},
//Farm Chaining
Farm: {
user: async (parent) => {
const userId = parent.userId;
const userDoc = await db.collection('users').doc(userId).get();
return userDoc.exists ? userDoc.data() : null;
},
crops: async (parent) => {
const farmId = parent.farmId;
const cropsSnapshot = await db.collection('crops').where('farmId', '==', farmId).get();
return cropsSnapshot.docs.map(doc => ({cropId: doc.id, ...doc.data()}));
},
livestocks: async (parent) => {
const querySnapshot = await db.collection("livestocks").where("farmId", "==", parent.farmId).get();
if (querySnapshot.empty) {
return [];
}
return querySnapshot.docs.map((doc) => ({
...doc.data(),
livestockId: doc.id,
}));
},
weatherData: async (parent) => {
const querySnapshot = await db.collection("weatherData").where("farmId", "==", parent.farmId).get();
if (querySnapshot.empty) {
return [];
}
return querySnapshot.docs.map((doc) => ({
...doc.data(),
weatherDataId: doc.id,
}));
},
// Add other resolvers for crops, livestocks, etc.
},
//Crop Chaining
Crop: {
farm: async (parent) => {
const farmId = parent.farmId;
const farmDoc = await db.collection('farms').doc(farmId).get();
return farmDoc.exists ? farmDoc.data() : null;
},
user: async (parent) => {
const userId = parent.userId;
const userDoc = await db.collection('users').doc(userId).get();
return userDoc.exists ? userDoc.data() : null;
},
},
//Livestock Chaining
Livestock: {
farm: async (parent) => {
const farmId = parent.farmId;
const farmSnapshot = await db.collection("farms").doc(farmId).get();
return { farmId: farmSnapshot.id, ...farmSnapshot.data() };
},
user: async (parent) => {
const userId = parent.userId;
const userSnapshot = await db.collection("users").doc(userId).get();
return { userId: userSnapshot.id, ...userSnapshot.data() };
},
},
//WeaterData chaining
WeatherData: {
farm: async (parent) => {
const farmSnapshot = await db.collection("farms").doc(parent.farmId).get();
return {
...farmSnapshot.data(),
farmId: farmSnapshot.id,
};
},
},
};
module.exports = resolvers;