Skip to content

Commit

Permalink
#103 Add new quiz columns to track latest updated and deletions
Browse files Browse the repository at this point in the history
  • Loading branch information
danielemery committed Aug 14, 2024
1 parent bbfcccc commit d60f193
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
12 changes: 12 additions & 0 deletions prisma/migrations/20240813170920_track_quiz_changes/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 15 additions & 6 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
}
Expand Down
5 changes: 5 additions & 0 deletions src/quiz/quiz.persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 && {
Expand Down
5 changes: 5 additions & 0 deletions src/quiz/quiz.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d60f193

Please sign in to comment.