From d64d3db7feb6ba27fb51e4f56a9e950c2b2dc50c Mon Sep 17 00:00:00 2001 From: kevinand11 Date: Fri, 8 Dec 2023 13:17:09 +0100 Subject: [PATCH] fix: update quiz if I have access --- .../modules/study/data/repositories/questions.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/services/api/src/modules/study/data/repositories/questions.ts b/services/api/src/modules/study/data/repositories/questions.ts index 992417dd..8e2aa875 100644 --- a/services/api/src/modules/study/data/repositories/questions.ts +++ b/services/api/src/modules/study/data/repositories/questions.ts @@ -3,7 +3,7 @@ import { appInstance } from '@utils/types' import { BadRequestError, QueryParams } from 'equipped' import { IQuestionRepository } from '../../domain/irepositories/questions' import { QuestionMapper } from '../mappers/questions' -import { QuestionToModel } from '../models/questions' +import { QuestionFromModel, QuestionToModel } from '../models/questions' import { Question } from '../mongooseModels/questions' import { Quiz } from '../mongooseModels/quizzes' @@ -40,10 +40,15 @@ export class QuestionRepository implements IQuestionRepository { } async update (quizId: string, id: string, userId: string, data: Partial) { - const question = await Question.findOneAndUpdate({ - _id: id, userId, quizId - }, { $set: data }, { new: true }) - return this.mapper.mapFrom(question) + let res = null as QuestionFromModel | null + await Question.collection.conn.transaction(async (session) => { + const quiz = await Quiz.findById(quizId, {}, { session }) + if (!quiz|| !quiz.access.members.concat(quiz.user.id).includes(userId)) return + const question = await Question.findOneAndUpdate({ _id: id, quizId }, { $set: data }, { new: true }) + res = question + return res + }) + return this.mapper.mapFrom(res) } async delete (quizId: string, id: string, userId: string) {