survey.db.ts 882 Bytes
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
2
import { Survey, ISurvey } from "../models";

Jiwon Yoon's avatar
Jiwon Yoon committed
3
4
5
6
7
8
9
10
11
12
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
13
14
15
16
export const createSurvey = async (survey: ISurvey) => {
  const newSurvey = await Survey.create(survey);
  return newSurvey;
};
Jiwon Yoon's avatar
Jiwon Yoon committed
17
18
19
20
21
22

export const getSurveyById = async (surveyId: string) => {
  console.log("survey id", surveyId);
  const survey = await Survey.findById(surveyId).populate("questions");
  return survey;
};
23
24
25
26
export const getSurveys = async () => {
  const surveys = await Survey.find();
  return surveys;
};
Jiwon Yoon's avatar
Jiwon Yoon committed
27
28
29
30
31

export const updateSurvey = async (survey: ISurvey) => {
  const newSurvey = await Survey.findOneAndUpdate({ _id: survey._id }, survey);
  return newSurvey;
};