survey.db.ts 2.98 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
import { HydratedDocument } from "mongoose";
import { Survey, ISurvey, Question, IQuestion } from "../models";
Jiwon Yoon's avatar
Jiwon Yoon committed
3

Yoon, Daeki's avatar
Yoon, Daeki committed
4
5
6
7
8
9
10
11
export const addQuestion = async (surveyId: string, question: any) => {
  if (question !== null) {
    const updatedSurvey = await Survey.findOneAndUpdate(
      { _id: surveyId },
      { $push: { questions: question } },
      { new: true }
    ).populate("questions");
    return updatedSurvey;
Jiwon Yoon's avatar
Jiwon Yoon committed
12
13
14
15
  }
  return null;
};

Yoon, Daeki's avatar
Yoon, Daeki committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
export const createSurvey = async (surveyData: ISurvey) => {
  const { _id, questions, ...rest } = surveyData;
  console.log("questions in survey db:", questions, "rest:", rest);
  let newQuestions;
  // questions 있으면 먼저 저장
  if (questions && questions.length > 0) {
    newQuestions = await Promise.all(
      questions.map(async (question) => {
        const { _id, ...questionsWithoutId } = question;
        return await Question.create(questionsWithoutId);
      })
    );
  }
  const survey = new Survey({
    ...rest,
    questions: newQuestions,
  });
  const newSurvey = await (await survey.save()).populate("questions");
Jiwon Yoon's avatar
Jiwon Yoon committed
34
35
  return newSurvey;
};
Jiwon Yoon's avatar
Jiwon Yoon committed
36

Yoon, Daeki's avatar
Yoon, Daeki committed
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
export const removeQuestion = async (surveyId: string, questionId: string) => {
  const updatedSurvey = await Survey.findOneAndUpdate(
    { _id: surveyId },
    { $pull: { questions: questionId } },
    { new: true }
  );

  return updatedSurvey;
};

export const deleteSurvey = async (surveyId: string) => {
  console.log("survey id", surveyId);
  const survey = await Survey.findOneAndDelete({ _id: surveyId });
  return survey;
};

export const findUserBySurveyId = async (surveyId: string) => {
  const survey = await Survey.findById(surveyId).populate("user");
  console.log(survey);
  if (survey !== null) {
    console.log(survey.user);
    return survey.user;
  }
  return null;
};

Jiwon Yoon's avatar
Jiwon Yoon committed
63
64
65
66
67
export const getSurveyById = async (surveyId: string) => {
  console.log("survey id", surveyId);
  const survey = await Survey.findById(surveyId).populate("questions");
  return survey;
};
68
69

export const getSurveys = async (userId: string) => {
Yoon, Daeki's avatar
Yoon, Daeki committed
70
71
72
  const surveys = await Survey.find({ user: userId })
    .sort({ updatedAt: -1 })
    .populate("questions");
73
74
  return surveys;
};
Jiwon Yoon's avatar
Jiwon Yoon committed
75

Yoon, Daeki's avatar
Yoon, Daeki committed
76
export const updateSurvey = async (survey: HydratedDocument<ISurvey>) => {
77
  console.log("update survey", survey);
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  // await Promise.all(
  //   survey.questions.map(
  //     async (question) =>
  //       await Question.findOneAndUpdate({ _id: question._id }, question, {
  //         upsert: true,
  //       })
  //   )
  // );
  const updatedSurvey = await Survey.findOneAndUpdate(
    { _id: survey._id },
    survey,
    {
      new: true,
    }
  ).populate("questions");
  return updatedSurvey;
Jiwon Yoon's avatar
Jiwon Yoon committed
94
};
95

Jiwon Yoon's avatar
Jiwon Yoon committed
96
97
98
99
100
101
102
103
104
105
106
107
export const putNewQuestion = async (newQuestion: any, surveyId: string) => {
  console.log(newQuestion, surveyId);
  if (newQuestion !== null) {
    const updatedSurvey = await Survey.findOneAndUpdate(
      { _id: surveyId },
      { $push: { questions: newQuestion } },
      { new: true }
    ).populate("questions");
    return updatedSurvey;
  }
  return null;
};