generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 203
/
answers.ts
75 lines (63 loc) · 2.44 KB
/
answers.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const admin = require("firebase-admin");
const firestore = require("../utils/firestore");
const answerModel = firestore.collection("answers");
import { Answer, AnswerBody, AnswerFieldsToUpdate } from "../typeDefinitions/answers";
const { ANSWER_STATUS } = require("../constants/answers");
const createAnswer = async (answerData: AnswerBody) => {
try {
const { eventId: event_id, answeredBy: answered_by, answer, questionId: question_id } = answerData;
const answerRef = answerModel.doc(answerData.id);
const createdAndUpdatedAt = admin.firestore.Timestamp.now();
await answerRef.set({
answer,
event_id,
answered_by,
question_id,
status: ANSWER_STATUS.PENDING,
reviewed_by: null,
created_at: createdAndUpdatedAt,
updated_at: createdAndUpdatedAt,
});
const answerSnapshot = await answerRef.get();
const id = answerSnapshot.id;
const answerFromDB = answerSnapshot.data();
return { id, ...answerFromDB };
} catch (error) {
logger.error(`Some error occured while creating answer ${error}`);
throw error;
}
};
const updateAnswer = async (id: string, fieldsToUpdate: AnswerFieldsToUpdate) => {
try {
const answerRef = answerModel.doc(id);
const updatedAt = admin.firestore.Timestamp.now();
await answerRef.update({ ...fieldsToUpdate, updated_at: updatedAt });
const answerSnapshot = await answerRef.get();
const answer = answerSnapshot.data();
return { id: answerSnapshot.id, ...answer };
} catch (error) {
logger.error(`Some error occured while updating answer ${error}`);
throw error;
}
};
const getAnswers = async (queryFields) => {
const questionId = queryFields.questionId || "";
const eventId = queryFields.eventId || "";
const status = queryFields.status || "";
let answersRef = answerModel;
let answers: Answer[] = [];
try {
answersRef = questionId ? answersRef.where("question_id", "==", questionId) : answersRef;
answersRef = eventId ? answersRef.where("event_id", "==", eventId) : answersRef;
answersRef = status ? answersRef.where("status", "==", status) : answersRef;
const answerSnapshot = await answersRef.get();
answerSnapshot.forEach((answer) => {
answers.push({ id: answer.id, ...answer.data() });
});
return answers;
} catch (error) {
logger.error(`Some error occured while getting answers ${error}`);
throw error;
}
};
module.exports = { createAnswer, updateAnswer, getAnswers };