survey.controller.ts 2.95 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import { NextFunction, Request, Response } from "express";
Jiwon Yoon's avatar
Jiwon Yoon committed
2
3
import { surveyDb } from "../db";
import { asyncWrap } from "../helpers/asyncWrap";
Lee SeoYeon's avatar
0720    
Lee SeoYeon committed
4
5
// import jwt, { JwtPayload } from "jsonwebtoken";
// import { cookieConfig, envConfig, jwtCofig } from "../config";
Jiwon Yoon's avatar
Jiwon Yoon committed
6

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

export const createSurvey = asyncWrap(
13
  async (reqExp: Request, res: Response) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
14
15
16
17
18
19
20
21
22
23
24
25
    const req = reqExp as TypedRequestAuth<{ userId: string }>;
    const { userId } = req.auth;
    let survey = req.body;
    survey.user = userId;
    console.log("survey body", survey);
    const newSurvey = await surveyDb.createSurvey(survey);
    return res.json(newSurvey);
  }
);

export const getSurveyById = asyncWrap(async (req, res) => {
  const { surveyId } = req.params;
26
  const survey: any = await surveyDb.getSurveyById(surveyId);
Jiwon Yoon's avatar
Jiwon Yoon committed
27
28
29
30
  console.log("Get완료", survey);
  return res.json(survey);
});

31
32
33
34
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);
35
36
37
  return res.json(surveys);
});

Jiwon Yoon's avatar
Jiwon Yoon committed
38
export const updateSurvey = asyncWrap(async (req, res) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
39
  const survey = req.body;
Jiwon Yoon's avatar
Jiwon Yoon committed
40
  const newSurvey = await surveyDb.updateSurvey(survey);
Jiwon Yoon's avatar
Jiwon Yoon committed
41
42
  return res.json(newSurvey);
});
Jiwon Yoon's avatar
Jiwon Yoon committed
43

44
45
46
47
48
49
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
50
51
52
53
54
55
56
57
58
59
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) {
60
61
62
63
      return res.status(404).send("올바른 접근이 아닙니다.");
    } else {
      req.user = user;
      next();
Jiwon Yoon's avatar
Jiwon Yoon committed
64
65
66
67
68
69
70
71
72
    }
  } catch (error: any) {
    return res
      .status(500)
      .send(
        error.message || "설문조사를 작성한 사용자를 찾아내는 중 오류 발생"
      );
  }
};
Lee SeoYeon's avatar
0720    
Lee SeoYeon committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

// export const checksurvey = asyncWrap(async(req, res)=> {
//   const {_id} = req.body
//   const surveyExist = await surveyDb.isSurvey(_id);
//   if (surveyExist) {
//     return res.status(422).send("이미 제출된 설문조사입니다")
//   }
// });

// export const surveynotexist = asyncWrap(async (req, res) => {
//   const { _id } = req.body;
//   console.log(`surveyId: ${_id}`);
//   const checksurveyId = await surveyDb.findUserBySurveyId(_id);
//   const surveytoken = jwt.sign({existsurveyId: checksurveyId?.id}, jwtCofig.secret, {
//     expiresIn:jwtCofig.expires,
//   });
//   res.cookie(cookieConfig.name, surveytoken, {
//     maxAge:cookieConfig.maxAge,
//     path:"/",
//     httpOnly: envConfig.mode === "production",
//     secure: envConfig.mode === "production",
//   })
//   res.json({
//     surveyId: checksurveyId?._id
//   })
// });