-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrecords.js
422 lines (361 loc) · 8.63 KB
/
records.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import firestore from '@react-native-firebase/firestore';
import {formatDate} from '../utils/date';
const recordsCollection = firestore().collection('records');
//Create Record
//1. eat {공유코드, 작성자, 뭘 먹었는지, 얼마나 먹었는지, 언제 먹었는지, 특이사항}
export function createEatRecord({code, order, writer, what, how, date, memo}) {
const when = firestore.Timestamp.fromDate(date);
const colName = formatDate(new Date());
return recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('eat')
.add({
writer,
what,
how,
when,
memo,
});
}
//2. toilet {공유코드, 작성자, 대/소변 중 무엇인지, 대/소변 양, 언제 기저귀 교체, 특이사항}
export function createToiletRecord({
code,
order,
writer,
what,
how,
date,
memo,
}) {
const when = firestore.Timestamp.fromDate(date);
const colName = formatDate(new Date());
return recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('toilet')
.add({
writer,
what,
how,
when,
memo,
});
}
//3. sleep {공유코드, 작성자, 낮잠/밤잠 중 무엇인지, 언제 잠들었는지, 언제 깼는지, 특이사항}
export function createSleepRecord({
code,
order,
writer,
what,
startDate,
endDate,
diff,
memo,
}) {
const when = firestore.Timestamp.fromDate(startDate);
const whenEnd = firestore.Timestamp.fromDate(endDate);
const colName = formatDate(new Date());
return recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('sleep')
.add({
writer,
what,
when,
whenEnd,
diff,
memo,
});
}
//4. health {공유코드, 작성자, 뭘 먹었는지, 얼마나 먹었는지, 언제 먹었는지, 특이사항}
export function createHealthRecord({
code,
order,
writer,
height,
weight,
what,
date,
memo,
}) {
const when = firestore.Timestamp.fromDate(date);
const colName = formatDate(new Date());
return recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('health')
.add({
writer,
what,
height,
weight,
when,
memo,
});
}
//5. etc {공유코드, 작성자, 무슨 활동인지, 언제 시작했는지, 언제 끝났는지, 특이사항}
export function createEtcRecord({
code,
order,
writer,
what,
startDate,
endDate,
diff,
memo,
}) {
const when = firestore.Timestamp.fromDate(startDate);
const whenEnd = firestore.Timestamp.fromDate(endDate);
const colName = formatDate(new Date());
return recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('etc')
.add({
writer,
what,
when,
whenEnd,
diff,
memo,
});
}
//Get Record - 필요시 구현
//1. eat
export async function getEat({code, order}) {
const colName = formatDate(new Date());
const snapshot = await recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('eat')
.orderBy('when', 'desc')
.get();
return snapshot.docs.map(doc => doc.data());
}
//2. toilet
export async function getToilet({code, order}) {
const colName = formatDate(new Date());
const snapshot = await recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('toilet')
.orderBy('when', 'desc')
.get();
return snapshot.docs.map(doc => doc.data());
}
//3. sleep
export async function getSleep({code, order}) {
const colName = formatDate(new Date());
const snapshot = await recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('sleep')
.orderBy('when', 'desc')
.get();
return snapshot.docs.map(doc => doc.data());
}
//4. health
//5. etc
//Get Recent Record
//1. eat
export async function getRecentEat({code, order}) {
const colName = formatDate(new Date());
const snapshot = await recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('eat')
.orderBy('when', 'desc')
.limit(1)
.get();
const records = snapshot.docs.map(doc => doc.data().when);
return records;
}
//2. toilet
export async function getRecentToilet({code, order}) {
const colName = formatDate(new Date());
const snapshot = await recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('toilet')
.orderBy('when', 'desc')
.limit(1)
.get();
const records = snapshot.docs.map(doc => doc.data().when);
return records;
}
//3. sleep
export async function getRecentSleep({code, order}) {
const colName = formatDate(new Date());
const snapshot = await recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('sleep')
.orderBy('when', 'desc')
.limit(1)
.get();
const records = snapshot.docs.map(doc => doc.data().when);
return records;
}
//Get Document Size
//1. eat
export async function getEatRecordSize({code, order}) {
const colName = formatDate(new Date());
const size = await recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('eat')
.get()
.then(snap => snap.size);
return size;
}
//2. toilet
export async function getToiletRecordSize({code, order}) {
const colName = formatDate(new Date());
const size = await recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('toilet')
.get()
.then(snap => snap.size);
return size;
}
//3. sleep
export async function getSleepRecordSize({code, order}) {
const colName = formatDate(new Date());
const size = await recordsCollection
.doc(code)
.collection(colName)
.doc(order + '')
.collection('sleep')
.get()
.then(snap => snap.size);
return size;
}
//For Record Screen
//특정 날짜에 해당하는 records 전부 가져오기
export async function getAllRecordWithDate({code, order, date}) {
const eatSnapshot = await recordsCollection
.doc(code)
.collection(date)
.doc(order + '')
.collection('eat')
.get();
const eat = eatSnapshot.docs.map(doc => ({
type: 'eat',
id: doc.id,
...doc.data(),
}));
const toiletSnapshot = await recordsCollection
.doc(code)
.collection(date)
.doc(order + '')
.collection('toilet')
.get();
const toilet = toiletSnapshot.docs.map(doc => ({
type: 'toilet',
id: doc.id,
...doc.data(),
}));
const sleepSnapshot = await recordsCollection
.doc(code)
.collection(date)
.doc(order + '')
.collection('sleep')
.get();
const sleep = sleepSnapshot.docs.map(doc => ({
type: 'sleep',
id: doc.id,
...doc.data(),
}));
const healthSnapshot = await recordsCollection
.doc(code)
.collection(date)
.doc(order + '')
.collection('health')
.get();
const health = healthSnapshot.docs.map(doc => ({
type: 'health',
id: doc.id,
...doc.data(),
}));
const etcSnapshot = await recordsCollection
.doc(code)
.collection(date)
.doc(order + '')
.collection('etc')
.get();
const etc = etcSnapshot.docs.map(doc => ({
type: 'etc',
id: doc.id,
...doc.data(),
}));
const data = eat
.concat(toilet, sleep, health, etc)
.sort((a, b) => (a.when > b.when ? 1 : -1));
return data;
}
//1. eat
export async function getAllEatWithDate({code, order, date}) {
const snapshot = await recordsCollection
.doc(code)
.collection(date)
.doc(order + '')
.collection('eat')
.get();
return snapshot.docs.map(doc => doc.data());
}
//2. toilet
export async function getAllToiletWithDate({code, order, date}) {
const snapshot = await recordsCollection
.doc(code)
.collection(date)
.doc(order + '')
.collection('toilet')
.get();
return snapshot.docs.map(doc => doc.data());
}
//3. sleep
export async function getAllSleepWithDate({code, order, date}) {
const snapshot = await recordsCollection
.doc(code)
.collection(date)
.doc(order + '')
.collection('sleep')
.get();
return snapshot.docs.map(doc => doc.data());
}
//4. health
export async function getAllHealthWithDate({code, order, date}) {
const snapshot = await recordsCollection
.doc(code)
.collection(date)
.doc(order + '')
.collection('health')
.get();
return snapshot.docs.map(doc => doc.data());
}
//5. etc
export async function getAllEtcWithDate({code, order, date}) {
const snapshot = await recordsCollection
.doc(code)
.collection(date)
.doc(order + '')
.collection('etc')
.get();
return snapshot.docs.map(doc => doc.data());
}