♻️ Improvements to house-keeping webhook (#1063)

This commit is contained in:
Luke Vella 2024-03-16 11:56:07 +07:00 committed by GitHub
parent 0d82c158ce
commit 90f0d90fab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 173 additions and 104 deletions

View file

@ -1,7 +1,5 @@
import { PrismaClient } from "@rallly/database";
import { softDeleteMiddleware } from "./middleware/soft-delete-middleware";
export * from "@prisma/client";
declare global {
@ -12,6 +10,4 @@ declare global {
export const prisma = global.prisma || new PrismaClient();
softDeleteMiddleware(prisma, "Poll");
if (process.env.NODE_ENV !== "production") global.prisma = prisma;

View file

@ -1,34 +0,0 @@
import { Prisma, PrismaClient } from "@rallly/database";
export const softDeleteMiddleware = (
prisma: PrismaClient,
model: Prisma.ModelName,
) => {
prisma.$use(async (params, next) => {
// We use middleware to handle soft deletes
// See: https://www.prisma.io/docs/concepts/components/prisma-client/middleware/soft-delete-middleware
if (params.model === model) {
if (params.action === "delete") {
// Delete queries
// Change action to an update
params.action = "update";
params.args["data"] = { deleted: true, deletedAt: new Date() };
}
if (params.action == "deleteMany") {
// Delete many queries
params.action = "updateMany";
if (params.args.data != undefined) {
params.args.data["deleted"] = true;
} else {
params.args["data"] = { deleted: true, deletedAt: new Date() };
}
}
if (params.action === "findFirst") {
// Add 'deleted' filter
// ID filter maintained
params.args.where["deleted"] = params.args.where["deleted"] || false;
}
}
return next(params);
});
};

View file

@ -0,0 +1,2 @@
-- CreateIndex
CREATE INDEX "votes_poll_id_idx" ON "votes" USING HASH ("poll_id");

View file

@ -226,6 +226,7 @@ model Vote {
@@index([participantId], type: Hash)
@@index([optionId], type: Hash)
@@index([pollId], type: Hash)
@@map("votes")
}