fix: soft delete labels
This commit is contained in:
@@ -192,7 +192,11 @@ export const labelRouter = createTRPCRouter({
|
|||||||
|
|
||||||
await cardRepo.hardDeleteAllCardLabelRelationships(ctx.db, label.id);
|
await cardRepo.hardDeleteAllCardLabelRelationships(ctx.db, label.id);
|
||||||
|
|
||||||
await labelRepo.hardDelete(ctx.db, label.id);
|
await labelRepo.softDelete(ctx.db, {
|
||||||
|
labelId: label.id,
|
||||||
|
deletedAt: new Date(),
|
||||||
|
deletedBy: userId,
|
||||||
|
});
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
ALTER TABLE "label" ADD COLUMN "deletedAt" timestamp;--> statement-breakpoint
|
||||||
|
ALTER TABLE "label" ADD COLUMN "deletedBy" uuid;--> statement-breakpoint
|
||||||
|
DO $$ BEGIN
|
||||||
|
ALTER TABLE "label" ADD CONSTRAINT "label_deletedBy_user_id_fk" FOREIGN KEY ("deletedBy") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
|
||||||
|
EXCEPTION
|
||||||
|
WHEN duplicate_object THEN null;
|
||||||
|
END $$;
|
||||||
2087
packages/db/migrations/meta/20250527203813_snapshot.json
Normal file
2087
packages/db/migrations/meta/20250527203813_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -15,6 +15,13 @@
|
|||||||
"when": 1747903068807,
|
"when": 1747903068807,
|
||||||
"tag": "20250522083748_AddEmailToWorkspaceMembers",
|
"tag": "20250522083748_AddEmailToWorkspaceMembers",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 2,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1748378293342,
|
||||||
|
"tag": "20250527203813_AddDeletedAtToLabel",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -113,6 +113,7 @@ export const getByPublicId = async (
|
|||||||
name: true,
|
name: true,
|
||||||
colourCode: true,
|
colourCode: true,
|
||||||
},
|
},
|
||||||
|
where: isNull(labels.deletedAt),
|
||||||
},
|
},
|
||||||
lists: {
|
lists: {
|
||||||
columns: {
|
columns: {
|
||||||
@@ -244,6 +245,7 @@ export const getBySlug = async (
|
|||||||
name: true,
|
name: true,
|
||||||
colourCode: true,
|
colourCode: true,
|
||||||
},
|
},
|
||||||
|
where: isNull(labels.deletedAt),
|
||||||
},
|
},
|
||||||
lists: {
|
lists: {
|
||||||
columns: {
|
columns: {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
cards,
|
cards,
|
||||||
cardsToLabels,
|
cardsToLabels,
|
||||||
cardToWorkspaceMembers,
|
cardToWorkspaceMembers,
|
||||||
|
labels,
|
||||||
lists,
|
lists,
|
||||||
workspaceMembers,
|
workspaceMembers,
|
||||||
} from "@kan/db/schema";
|
} from "@kan/db/schema";
|
||||||
@@ -316,6 +317,7 @@ export const getWithListAndMembersByPublicId = async (
|
|||||||
colourCode: true,
|
colourCode: true,
|
||||||
name: true,
|
name: true,
|
||||||
},
|
},
|
||||||
|
where: isNull(labels.deletedAt),
|
||||||
},
|
},
|
||||||
lists: {
|
lists: {
|
||||||
columns: {
|
columns: {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { eq, inArray } from "drizzle-orm";
|
import { and, eq, inArray, isNull } from "drizzle-orm";
|
||||||
|
|
||||||
import type { dbClient } from "@kan/db/client";
|
import type { dbClient } from "@kan/db/client";
|
||||||
import { cardsToLabels, labels } from "@kan/db/schema";
|
import { cardsToLabels, labels } from "@kan/db/schema";
|
||||||
@@ -98,10 +98,21 @@ export const update = async (
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const hardDelete = async (db: dbClient, labelId: number) => {
|
export const softDelete = async (
|
||||||
|
db: dbClient,
|
||||||
|
args: {
|
||||||
|
labelId: number;
|
||||||
|
deletedAt: Date;
|
||||||
|
deletedBy: string;
|
||||||
|
},
|
||||||
|
) => {
|
||||||
const [result] = await db
|
const [result] = await db
|
||||||
.delete(labels)
|
.update(labels)
|
||||||
.where(eq(labels.id, labelId))
|
.set({
|
||||||
|
deletedAt: args.deletedAt,
|
||||||
|
deletedBy: args.deletedBy,
|
||||||
|
})
|
||||||
|
.where(and(eq(labels.id, args.labelId), isNull(labels.deletedAt)))
|
||||||
.returning({ id: labels.id });
|
.returning({ id: labels.id });
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ export const labels = pgTable("label", {
|
|||||||
.notNull()
|
.notNull()
|
||||||
.references(() => boards.id, { onDelete: "cascade" }),
|
.references(() => boards.id, { onDelete: "cascade" }),
|
||||||
importId: bigint("importId", { mode: "number" }).references(() => imports.id),
|
importId: bigint("importId", { mode: "number" }).references(() => imports.id),
|
||||||
|
deletedAt: timestamp("deletedAt"),
|
||||||
|
deletedBy: uuid("deletedBy").references(() => users.id),
|
||||||
}).enableRLS();
|
}).enableRLS();
|
||||||
|
|
||||||
export const labelsRelations = relations(labels, ({ one, many }) => ({
|
export const labelsRelations = relations(labels, ({ one, many }) => ({
|
||||||
@@ -34,6 +36,10 @@ export const labelsRelations = relations(labels, ({ one, many }) => ({
|
|||||||
fields: [labels.createdBy],
|
fields: [labels.createdBy],
|
||||||
references: [users.id],
|
references: [users.id],
|
||||||
}),
|
}),
|
||||||
|
deletedBy: one(users, {
|
||||||
|
fields: [labels.deletedBy],
|
||||||
|
references: [users.id],
|
||||||
|
}),
|
||||||
board: one(boards, {
|
board: one(boards, {
|
||||||
fields: [labels.boardId],
|
fields: [labels.boardId],
|
||||||
references: [boards.id],
|
references: [boards.id],
|
||||||
|
|||||||
Reference in New Issue
Block a user