mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-01 15:39:03 +02:00
♻️ Improvements to house-keeping webhook (#1063)
This commit is contained in:
parent
0d82c158ce
commit
90f0d90fab
9 changed files with 173 additions and 104 deletions
|
@ -237,7 +237,10 @@ export const polls = router({
|
|||
)
|
||||
.mutation(async ({ input: { urlId } }) => {
|
||||
const pollId = await getPollIdFromAdminUrlId(urlId);
|
||||
await prisma.poll.delete({ where: { id: pollId } });
|
||||
await prisma.poll.update({
|
||||
where: { id: pollId },
|
||||
data: { deleted: true, deletedAt: new Date() },
|
||||
});
|
||||
}),
|
||||
touch: publicProcedure
|
||||
.input(
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
};
|
|
@ -0,0 +1,2 @@
|
|||
-- CreateIndex
|
||||
CREATE INDEX "votes_poll_id_idx" ON "votes" USING HASH ("poll_id");
|
|
@ -226,6 +226,7 @@ model Vote {
|
|||
|
||||
@@index([participantId], type: Hash)
|
||||
@@index([optionId], type: Hash)
|
||||
@@index([pollId], type: Hash)
|
||||
@@map("votes")
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ import { cn } from "@rallly/ui";
|
|||
|
||||
const buttonVariants = cva(
|
||||
cn(
|
||||
"inline-flex border font-medium disabled:text-muted-foreground disabled:bg-muted disabled:pointer-events-none select-none items-center justify-center whitespace-nowrap rounded-md border",
|
||||
"inline-flex shadow-[inset_0_0.5px_0_0.5px_rgb(255,255,255,0.4),_inset_0_-0.5px_0_0.5px_rgb(0,0,0,0.1)] border font-medium disabled:text-muted-foreground disabled:bg-muted disabled:pointer-events-none select-none items-center justify-center whitespace-nowrap rounded-md border",
|
||||
"focus-visible:ring-offset-input-background focus-visible:border-primary-400 focus-visible:ring-2 focus-visible:ring-indigo-100",
|
||||
),
|
||||
{
|
||||
|
@ -19,7 +19,7 @@ const buttonVariants = cva(
|
|||
destructive:
|
||||
"bg-destructive text-destructive-foreground focus:ring-offset-1 active:bg-destructive border-destructive hover:bg-destructive/90",
|
||||
default:
|
||||
"rounded-md px-3.5 py-2.5 data-[state=open]:shadow-none data-[state=open]:bg-gray-100 active:bg-gray-200 focus:border-gray-300 hover:bg-gray-100 bg-gray-50",
|
||||
"rounded-md px-3.5 shadow-inner py-2.5 data-[state=open]:shadow-none data-[state=open]:bg-gray-100 active:bg-gray-200 focus:border-gray-300 hover:bg-gray-100 bg-gray-50",
|
||||
secondary:
|
||||
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
|
||||
ghost: "border-transparent hover:bg-gray-200 active:bg-gray-300",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue