feat: swap out drizzle for supabase in label, list and workspace routers

This commit is contained in:
Henry
2024-04-15 20:29:01 +01:00
parent 1712a1a41b
commit b695f7bdf3
10 changed files with 257 additions and 176 deletions

View File

@@ -1,7 +1,4 @@
import { z } from "zod";
import { and, eq, isNull } from "drizzle-orm";
import { cards, cardsToLabels, labels } from "~/server/db/schema";
import { generateUID } from "~/utils/generateUID";
import {
@@ -18,41 +15,43 @@ export const labelRouter = createTRPCRouter({
colourCode: z.string().length(7),
}),
)
.mutation(({ ctx, input }) => {
const userId = ctx.session?.user.id;
.mutation(async ({ ctx, input }) => {
const userId = ctx.user?.id;
if (!userId) return;
return ctx.db.transaction(async (tx) => {
const card = await tx.query.cards.findFirst({
where: and(eq(cards.publicId, input.cardPublicId), isNull(cards.deletedAt)),
with: {
list: {
with: {
board: true
}
}
}
});
const card = await ctx.supabase
.from('card')
.select(`id, list (boardId)`)
.eq('publicId', input.cardPublicId)
.is('deletedAt', null)
.limit(1)
.single();
if (!card) return;
if (!card.data?.list) return;
const newLabel = await tx.insert(labels).values({
const newLabel = await ctx.supabase
.from('label')
.insert({
publicId: generateUID(),
name: input.name,
colourCode: input.colourCode,
createdBy: userId,
boardId: card.list.boardId,
}).returning({ id: labels.id });
boardId: card.data.list.boardId,
})
.select(`id`)
.limit(1)
.single();
const newLabelId = newLabel[0]?.id
if (!newLabel.data) return;
if (!newLabelId) return;
await ctx.supabase
.from('_card_labels')
.insert({
cardId: card.data.id,
labelId: newLabel.data.id,
})
return tx.insert(cardsToLabels).values({
cardId: card.id,
labelId: newLabelId,
});
})
return newLabel.data;
}),
});