diff --git a/prisma/migrations/20240813170920_track_quiz_changes/migration.sql b/prisma/migrations/20240813170920_track_quiz_changes/migration.sql new file mode 100644 index 0000000..dd59a07 --- /dev/null +++ b/prisma/migrations/20240813170920_track_quiz_changes/migration.sql @@ -0,0 +1,12 @@ +-- AlterTable +ALTER TABLE "quiz" ADD COLUMN "deleted_at" TIMESTAMP(3), +ADD COLUMN "deleted_by_user_id" TEXT, +ADD COLUMN "deletion_reason" TEXT, +ADD COLUMN "updated_at" TIMESTAMP(3), +ADD COLUMN "updated_by_user_id" TEXT; + +-- AddForeignKey +ALTER TABLE "quiz" ADD CONSTRAINT "quiz_deleted_by_user_id_fkey" FOREIGN KEY ("deleted_by_user_id") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "quiz" ADD CONSTRAINT "quiz_updated_by_user_id_fkey" FOREIGN KEY ("updated_by_user_id") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7a54d1c..c7528eb 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -23,16 +23,23 @@ enum QuizImageState { } model Quiz { - id String @id + id String @id type QuizType - date DateTime @db.Date - uploadedAt DateTime @map("uploaded_at") - uploadedByUserId String @map("uploaded_by_user_id") + date DateTime @db.Date + uploadedAt DateTime @map("uploaded_at") + uploadedByUserId String @map("uploaded_by_user_id") + updatedAt DateTime? @map("updated_at") + updatedByUserId String? @map("updated_by_user_id") + deletedAt DateTime? @map("deleted_at") + deletedByUserId String? @map("deleted_by_user_id") + deletionReason String? @map("deletion_reason") completions QuizCompletion[] images QuizImage[] notes QuizNote[] - uploadedByUser User @relation(fields: [uploadedByUserId], references: [id]) + uploadedByUser User @relation(name: "uploadedByUser", fields: [uploadedByUserId], references: [id], onDelete: Restrict) + deletedByUser User? @relation(name: "deletedByUser", fields: [deletedByUserId], references: [id], onDelete: Restrict) + updatedByUser User? @relation(name: "updatedByUser", fields: [updatedByUserId], references: [id], onDelete: Restrict) @@unique(fields: [date, type]) @@map("quiz") @@ -102,7 +109,9 @@ model User { roles UserRole[] quizCompletions QuizCompletionUser[] quizNotes QuizNote[] - uploadedQuizzes Quiz[] + uploadedQuizzes Quiz[] @relation(name: "uploadedByUser") + deletedQuizzes Quiz[] @relation(name: "deletedByUser") + updatedQuizzes Quiz[] @relation(name: "updatedByUser") @@map("user") } diff --git a/src/quiz/quiz.persistence.ts b/src/quiz/quiz.persistence.ts index 1f6b3b5..86ea3ec 100644 --- a/src/quiz/quiz.persistence.ts +++ b/src/quiz/quiz.persistence.ts @@ -54,6 +54,11 @@ export class QuizPersistence { uploadedAt: true, uploadedByUserId: true, uploadedByUser: true, + updatedAt: true, + updatedByUserId: true, + deletedAt: true, + deletedByUserId: true, + deletionReason: true, }, where: { ...(filters.excludeCompletedBy && { diff --git a/src/quiz/quiz.service.ts b/src/quiz/quiz.service.ts index efbe090..847d39d 100644 --- a/src/quiz/quiz.service.ts +++ b/src/quiz/quiz.service.ts @@ -125,6 +125,11 @@ export class QuizService { type, uploadedAt: new Date(), uploadedByUserId: userId, + updatedAt: null, + updatedByUserId: null, + deletedAt: null, + deletedByUserId: null, + deletionReason: null, }, filesWithKeys.map((file) => ({ imageKey: file.imageKey,