generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 203
/
userFutureStatus.ts
56 lines (53 loc) · 1.55 KB
/
userFutureStatus.ts
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
import firestore from "../utils/firestore";
const userFutureStatusModel = firestore.collection("userFutureStatus");
import { UserFutureStatusType } from "../types/userFutureStatus";
import * as admin from "firebase-admin";
/**
* Function to create user future status
* @param body: UserFutureStatusType
* @returns UserFutureStatusType
*/
export const createUserFutureStatus = async (body: UserFutureStatusType) => {
try {
const statusBody: UserFutureStatusType = {
createdAt: Date.now(),
...body,
};
const resultDoc = await userFutureStatusModel.add(statusBody);
return {
id: resultDoc.id,
...body,
};
} catch (error) {
logger.error("Error while creating user future status", error);
throw error;
}
};
/**
* Function to get user future status
* @param: id: string, status: string, state: string
* @returns Array of user future status
**/
export const getUserFutureStatus = async (userId: string, status: string, state: string) => {
try {
let resultArray = [];
let query: admin.firestore.Query = userFutureStatusModel;
if (userId) {
query = query.where("userId", "==", userId);
}
if (status) {
query = query.where("status", "==", status);
}
if (state) {
query = query.where("state", "==", state);
}
const resultDoc = await query.get();
resultDoc.forEach((doc) => {
resultArray.push({ id: doc.id, ...doc.data() });
});
return resultArray;
} catch (error) {
logger.error("Error while fetching user future status", error);
throw error;
}
};