Commit bfa63a98 authored by Yoon, Daeki's avatar Yoon, Daeki 😅
Browse files

Merge branch 'jiwon0820'

parents eb70b0c6 c56d2394
...@@ -137,7 +137,7 @@ export const getAnswers = asyncWrap(async (reqExp, res) => { ...@@ -137,7 +137,7 @@ export const getAnswers = asyncWrap(async (reqExp, res) => {
const { surveyId } = req.params; const { surveyId } = req.params;
try { try {
const result = await answerDb.getAnswers(surveyId); const result = await answerDb.getAnswers(surveyId);
console.log("result===", result); // console.log("result===", result);
return res.json(result); return res.json(result);
} catch (error: any) { } catch (error: any) {
res.status(422).send(error.message || "설문조사 결과 불러오기 오류"); res.status(422).send(error.message || "설문조사 결과 불러오기 오류");
......
import { Answer, IAnswer } from "../models"; import { Answer, IAnswer, Survey } from "../models";
import { model, Schema, Types } from "mongoose"; import { model, Schema, Types } from "mongoose";
export const createAnswer = async (answer: IAnswer) => { export const createAnswer = async (answer: IAnswer) => {
...@@ -7,6 +7,7 @@ export const createAnswer = async (answer: IAnswer) => { ...@@ -7,6 +7,7 @@ export const createAnswer = async (answer: IAnswer) => {
}; };
export const getAnswers = async (surveyId: string) => { export const getAnswers = async (surveyId: string) => {
const survey = await Survey.findById(surveyId).populate("questions");
const result = await Answer.aggregate([ const result = await Answer.aggregate([
// surveyId에 해당하는 답변들 find // surveyId에 해당하는 답변들 find
{ $match: { surveyId: new Types.ObjectId(surveyId) } }, { $match: { surveyId: new Types.ObjectId(surveyId) } },
...@@ -27,45 +28,27 @@ export const getAnswers = async (surveyId: string) => { ...@@ -27,45 +28,27 @@ export const getAnswers = async (surveyId: string) => {
from: "questions", from: "questions",
localField: "_id", localField: "_id",
foreignField: "_id", foreignField: "_id",
as: "questionInfo", as: "question",
}, },
}, },
{
$unwind: "$questionInfo",
},
{ $set: { "questionInfo.answers": "$answers" } },
{ $unset: "answers" },
// 질문 순서대로 정렬
{ $sort: { "questionInfo.order": 1 } },
// surveyId로 묶고 questions 내에 { questionInfo, answers }[]
{ {
$group: { $replaceRoot: {
_id: "$surveyId", newRoot: {
questions: { $mergeObjects: [{ $arrayElemAt: ["$question", 0] }, "$$ROOT"],
$push: "$questionInfo",
}, },
}, },
}, },
{ $unset: "question" },
{ $sort: { order: 1 } },
]);
// survey DB populate console.log("result:", result);
{
$lookup: {
from: "surveys",
localField: "_id",
foreignField: "_id",
as: "survey",
},
},
{
$unwind: "$survey",
},
//밖에 있던 questions를 survey 내부로 이동시키고 survey를 가장 root로 변경 if (survey && result.length > 0) {
{ $set: { "survey.questions": "$questions" } }, const Jsurvey = survey.toJSON();
{ $replaceRoot: { newRoot: "$survey" } }, Jsurvey.questions = result;
]); return Jsurvey;
return result[0]; }
return survey;
}; };
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment