answer.controller.ts 2.29 KB
Newer Older
1
2
3
4
import { asyncWrap } from "../helpers";
import { TypedRequest } from "../types";
import formidable from "formidable";
import { FileInfo } from "../models";
Jiwon Yoon's avatar
Jiwon Yoon committed
5
import { fileDb, userDb, answerDb } from "../db";
6
7
8
9
10
11
12
import fs from "fs/promises";

export const createAnswers = asyncWrap(async (reqExp, res) => {
  const req = reqExp as TypedRequest;
  const answer = req.body;
  const answers = JSON.parse(answer.answers);
  answer.answers = answers;
Jiwon Yoon's avatar
Jiwon Yoon committed
13
  const files = req.files.uploadFiles as formidable.File[];
Jiwon Yoon's avatar
Jiwon Yoon committed
14
  // console.log("controller의 files", files);
Jiwon Yoon's avatar
Jiwon Yoon committed
15
  let uploadFile;
16
  try {
Jiwon Yoon's avatar
Jiwon Yoon committed
17
18
    if (files) {
      // 1) 파일을 DB에 저장 후 다시 retFile가져와서
Jiwon Yoon's avatar
Jiwon Yoon committed
19
20
21
      // *근데 파일이 여러 개일 수 있기 때문에 순회해야 됨-map()을 쓰면 async function이 되어버려서 for문 이용함
      for (let index = 0; index < files.length; index++) {
        const file = files[index];
Jiwon Yoon's avatar
Jiwon Yoon committed
22
23
24
25
26
27
28
        uploadFile = new FileInfo({
          name: file.originalFilename,
          url: file.newFilename,
          isNew: true,
        });
        const retFile = await fileDb.createFile(file);
        // 2) answers의 type이 file인 친구들 찾아서 그 친구의 answer와 filename을 비교 후 같으면
Jiwon Yoon's avatar
Jiwon Yoon committed
29
30
31
        const targetObj = answer.answers.find(
          (ans: any) => ans.answer === file.originalFilename
        );
Jiwon Yoon's avatar
Jiwon Yoon committed
32
33
        // 3) answer에다가 retFile의 _id 넣어주기
        targetObj.answer = retFile._id;
Jiwon Yoon's avatar
Jiwon Yoon committed
34
      }
35
    }
Jiwon Yoon's avatar
Jiwon Yoon committed
36
    // 3) Answer DB 만들기(map을 돌려서 하나씩 추가시켜야 함)
Jiwon Yoon's avatar
Jiwon Yoon committed
37
38
39
40
41
42
43
44
45
46
    for (let index = 0; index < answer.answers.length; index++) {
      const element = answer.answers[index];
      const newAnswer = await answerDb.createAnswer({
        surveyId: answer.surveyId,
        guestId: answer.guestId,
        questionId: element.questionId,
        answer: element.answer,
      });
      console.log("DB에 넣은 answer", newAnswer);
    }
47
48
49
50
51
    // 주의: ref는 반드시 save를 해야 디비에 생성이 됩니다.
    return res.json();
  } catch (error: any) {
    console.log("error in create user:", error);
    // 오류 발생시 저장된 파일 제거
Jiwon Yoon's avatar
Jiwon Yoon committed
52
53
54
    if (files) {
      //   uploadFiles && (await fileDb.deleteFileById(uploadFiles._id.toString()));
      // await fs.unlink(files.filepath);
55
56
57
58
    }
    res.status(422).send(error.message || "사용자 생성 오류");
  }
});