survey.controller.ts 2.95 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import { NextFunction, Request, Response } from "express";
Yoon, Daeki's avatar
Yoon, Daeki committed
2
import { Types } from "mongoose";
Yoon, Daeki's avatar
Yoon, Daeki committed
3
import { questionDb, surveyDb } from "../db";
Jiwon Yoon's avatar
Jiwon Yoon committed
4
import { asyncWrap } from "../helpers/asyncWrap";
Yoon, Daeki's avatar
Yoon, Daeki committed
5
import { ISurvey } from "../models";
Jiwon Yoon's avatar
Jiwon Yoon committed
6

Jiwon Yoon's avatar
Jiwon Yoon committed
7
8
9
10
11
export interface TypedRequestAuth<T> extends Request {
  auth: T;
  user: any;
}

Yoon, Daeki's avatar
Yoon, Daeki committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
 * 설문에 새로운 질문을 추가
 */
export const addQuestion = asyncWrap(async (reqExp: Request, res: Response) => {
  const req = reqExp as TypedRequestAuth<{ userId: string }>;
  // Question controller 이용 질문 생성
  const { userId } = req.auth;
  const { _id, ...questionInput } = req.body;
  questionInput.user = userId;
  const newQuestion = await questionDb.createQuestion(questionInput);

  // 생성된 질문을 survey에 추가
  const { surveyId } = req.params;
  await surveyDb.addQuestion(surveyId, newQuestion);
  res.json(newQuestion);
});

Jiwon Yoon's avatar
Jiwon Yoon committed
29
export const createSurvey = asyncWrap(
30
  async (reqExp: Request, res: Response) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
31
32
    const req = reqExp as TypedRequestAuth<{ userId: string }>;
    const { userId } = req.auth;
Yoon, Daeki's avatar
Yoon, Daeki committed
33
34
    let survey = req.body as ISurvey;
    survey.user = new Types.ObjectId(userId);
Jiwon Yoon's avatar
Jiwon Yoon committed
35
36
37
38
39
40
    console.log("survey body", survey);
    const newSurvey = await surveyDb.createSurvey(survey);
    return res.json(newSurvey);
  }
);

Yoon, Daeki's avatar
Yoon, Daeki committed
41
42
43
44
45
46
47
48
49
50
51
52
53
export const deleteQuestion = asyncWrap(async (req, res) => {
  const { surveyId, questionId } = req.params;
  const deletedQuestion = await questionDb.deleteQuestionById(questionId);
  const survey = await surveyDb.removeQuestion(surveyId, questionId);
  return res.json(deletedQuestion);
});

export const deleteSurvey = asyncWrap(async (req, res) => {
  const { surveyId } = req.params;
  const survey = await surveyDb.deleteSurvey(surveyId);
  return res.json(survey);
});

Jiwon Yoon's avatar
Jiwon Yoon committed
54
55
export const getSurveyById = asyncWrap(async (req, res) => {
  const { surveyId } = req.params;
56
  const survey: any = await surveyDb.getSurveyById(surveyId);
Jiwon Yoon's avatar
Jiwon Yoon committed
57
58
59
60
  console.log("Get완료", survey);
  return res.json(survey);
});

61
62
63
64
export const getSurveys = asyncWrap(async (reqExp: Request, res: Response) => {
  const req = reqExp as TypedRequestAuth<{ userId: string }>;
  const { userId } = req.auth;
  const surveys = await surveyDb.getSurveys(userId);
65
66
67
  return res.json(surveys);
});

Jiwon Yoon's avatar
Jiwon Yoon committed
68
export const updateSurvey = asyncWrap(async (req, res) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
69
  const survey = req.body;
70
71
  const updatedSurvey = await surveyDb.updateSurvey(survey);
  return res.json(updatedSurvey);
Jiwon Yoon's avatar
Jiwon Yoon committed
72
});
Jiwon Yoon's avatar
Jiwon Yoon committed
73
74
75
76
77
78
79
80
81
82
83

export const userBySurveyId = async (
  reqExp: Request,
  res: Response,
  next: NextFunction,
  surveyId: string
) => {
  try {
    const req = reqExp as TypedRequestAuth<{ userId: string }>;
    let user = await surveyDb.findUserBySurveyId(surveyId);
    if (!user) {
84
85
86
87
      return res.status(404).send("올바른 접근이 아닙니다.");
    } else {
      req.user = user;
      next();
Jiwon Yoon's avatar
Jiwon Yoon committed
88
89
90
91
92
93
94
95
    }
  } catch (error: any) {
    return res
      .status(500)
      .send(
        error.message || "설문조사를 작성한 사용자를 찾아내는 중 오류 발생"
      );
  }
Jiwon Yoon's avatar
Jiwon Yoon committed
96
};