Skip to content

Commit

Permalink
fix: update quiz if I have access
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinand11 committed Dec 8, 2023
1 parent bafc0d7 commit d64d3db
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions services/api/src/modules/study/data/repositories/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -40,10 +40,15 @@ export class QuestionRepository implements IQuestionRepository {
}

async update (quizId: string, id: string, userId: string, data: Partial<QuestionToModel>) {
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) {
Expand Down

0 comments on commit d64d3db

Please sign in to comment.