answer.db.ts 666 Bytes
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import { Answer, IAnswer } from "../models";
Jiwon Yoon's avatar
Jiwon Yoon committed
2
import { model, Schema, Types } from "mongoose";
Jiwon Yoon's avatar
Jiwon Yoon committed
3
4
5
6
7

export const createAnswer = async (answer: IAnswer) => {
  const newQuestion = await Answer.create(answer);
  return newQuestion;
};
Jiwon Yoon's avatar
Jiwon Yoon committed
8
9
10
11
12

export const getAnswers = async (surveyId: string) => {
  const answers = await Answer.aggregate([
    { $match: { surveyId: new Types.ObjectId(surveyId) } },
    {
Jiwon Yoon's avatar
Jiwon Yoon committed
13
14
15
      $group: {
        _id: "$questionId",
        answers: { $push: "$answer" },
Jiwon Yoon's avatar
Jiwon Yoon committed
16
17
18
      },
    },
    {
Jiwon Yoon's avatar
Jiwon Yoon committed
19
20
21
22
23
      $lookup: {
        from: "fileinfos",
        localField: "answers",
        foreignField: "_id",
        as: "file",
Jiwon Yoon's avatar
Jiwon Yoon committed
24
25
26
27
28
      },
    },
  ]);
  return answers;
};