feat: swap drizzle for supabase on create new card
This commit is contained in:
@@ -19,65 +19,69 @@ export const cardRouter = createTRPCRouter({
|
|||||||
memberPublicIds: z.array(z.string().min(12))
|
memberPublicIds: z.array(z.string().min(12))
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.mutation(({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
const userId = ctx.session?.user.id;
|
const userId = ctx.user?.id;
|
||||||
|
|
||||||
if (!userId) return;
|
if (!userId) return;
|
||||||
|
|
||||||
return ctx.db.transaction(async (tx) => {
|
const list = await ctx.supabase
|
||||||
const list = await tx.query.lists.findFirst({
|
.from('list')
|
||||||
where: eq(lists.publicId, input.listPublicId),
|
.select(`id, cards:card (index)`)
|
||||||
columns: {
|
.eq('publicId', input.listPublicId)
|
||||||
id: true
|
.order('index', { foreignTable: 'card', ascending: false })
|
||||||
}
|
.limit(1)
|
||||||
});
|
.single();
|
||||||
|
|
||||||
if (!list) return;
|
if (!list.data?.id) return;
|
||||||
|
|
||||||
const latestCard = await tx.query.cards.findFirst({
|
const latestCard = list.data.cards.length && list.data.cards[0]
|
||||||
where: and(eq(cards.listId, list.id), isNull(cards.deletedAt)),
|
|
||||||
columns: {
|
|
||||||
index: true
|
|
||||||
},
|
|
||||||
orderBy: desc(cards.index)
|
|
||||||
});
|
|
||||||
|
|
||||||
const newCard = await tx.insert(cards).values({
|
const newCard = await ctx.supabase
|
||||||
|
.from('card')
|
||||||
|
.insert({
|
||||||
publicId: generateUID(),
|
publicId: generateUID(),
|
||||||
title: input.title,
|
title: input.title,
|
||||||
createdBy: userId,
|
createdBy: userId,
|
||||||
listId: list.id,
|
listId: list.data.id,
|
||||||
index: latestCard ? latestCard.index + 1 : 0
|
index: latestCard ? latestCard.index + 1 : 0
|
||||||
}).returning({ id: cards.id });
|
})
|
||||||
|
.select(`id`)
|
||||||
|
.limit(1)
|
||||||
|
.single();
|
||||||
|
|
||||||
const newCardId = newCard[0]?.id;
|
const newCardId = newCard.data?.id;
|
||||||
|
|
||||||
if (newCardId && input.labelsPublicIds.length) {
|
if (newCardId && input.labelsPublicIds.length) {
|
||||||
const labels = await tx.query.labels.findMany({
|
const labels = await ctx.supabase
|
||||||
where: inArray(cards.publicId, input.labelsPublicIds),
|
.from('label')
|
||||||
});
|
.select(`id`)
|
||||||
|
.eq('publicId', input.labelsPublicIds);
|
||||||
|
|
||||||
if (!labels.length) return;
|
if (!labels.data?.length) return;
|
||||||
|
|
||||||
const labelsInsert = labels.map((label) => ({ cardId: newCardId, labelId: label.id }))
|
const labelsInsert = labels.data.map((label) => ({ cardId: newCardId, labelId: label.id }));
|
||||||
|
|
||||||
await tx.insert(cardsToLabels).values(labelsInsert);
|
await ctx.supabase
|
||||||
}
|
.from('_card_labels')
|
||||||
|
.insert(labelsInsert);
|
||||||
|
}
|
||||||
|
|
||||||
if (newCardId && input.memberPublicIds.length) {
|
if (newCardId && input.memberPublicIds.length) {
|
||||||
const members = await tx.query.workspaceMembers.findMany({
|
const members = await ctx.supabase
|
||||||
where: inArray(workspaceMembers.publicId, input.memberPublicIds),
|
.from('workspace_members')
|
||||||
});
|
.select(`id`)
|
||||||
|
.eq('publicId', input.memberPublicIds);
|
||||||
|
|
||||||
if (!members.length) return;
|
if (!members.data?.length) return;
|
||||||
|
|
||||||
const membersInsert = members.map((member) => ({ cardId: newCardId, workspaceMemberId: member.id}))
|
const membersInsert = members.data.map((member) => ({ cardId: newCardId, workspaceMemberId: member.id }));
|
||||||
|
|
||||||
await tx.insert(cardToWorkspaceMembers).values(membersInsert);
|
await ctx.supabase
|
||||||
}
|
.from('_card_workspace_members')
|
||||||
|
.insert(membersInsert);
|
||||||
|
}
|
||||||
|
|
||||||
return newCard;
|
return newCard;
|
||||||
})
|
|
||||||
}),
|
}),
|
||||||
addOrRemoveLabel: publicProcedure
|
addOrRemoveLabel: publicProcedure
|
||||||
.input(
|
.input(
|
||||||
@@ -87,7 +91,7 @@ export const cardRouter = createTRPCRouter({
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.mutation(({ ctx, input }) => {
|
.mutation(({ ctx, input }) => {
|
||||||
const userId = ctx.session?.user.id;
|
const userId = ctx.user?.id;
|
||||||
|
|
||||||
if (!userId) return;
|
if (!userId) return;
|
||||||
|
|
||||||
@@ -124,7 +128,7 @@ export const cardRouter = createTRPCRouter({
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.mutation(({ ctx, input }) => {
|
.mutation(({ ctx, input }) => {
|
||||||
const userId = ctx.session?.user.id;
|
const userId = ctx.user?.id;
|
||||||
|
|
||||||
if (!userId) return;
|
if (!userId) return;
|
||||||
|
|
||||||
@@ -255,7 +259,7 @@ export const cardRouter = createTRPCRouter({
|
|||||||
description: z.string(),
|
description: z.string(),
|
||||||
}))
|
}))
|
||||||
.mutation(({ ctx, input }) => {
|
.mutation(({ ctx, input }) => {
|
||||||
const userId = ctx.session?.user.id;
|
const userId = ctx.user?.id;
|
||||||
|
|
||||||
if (!userId) return;
|
if (!userId) return;
|
||||||
|
|
||||||
@@ -267,7 +271,7 @@ export const cardRouter = createTRPCRouter({
|
|||||||
cardPublicId: z.string().min(12),
|
cardPublicId: z.string().min(12),
|
||||||
}))
|
}))
|
||||||
.mutation(({ ctx, input }) => {
|
.mutation(({ ctx, input }) => {
|
||||||
const userId = ctx.session?.user.id;
|
const userId = ctx.user?.id;
|
||||||
|
|
||||||
if (!userId) return;
|
if (!userId) return;
|
||||||
|
|
||||||
@@ -291,7 +295,7 @@ export const cardRouter = createTRPCRouter({
|
|||||||
newIndex: z.number().optional(),
|
newIndex: z.number().optional(),
|
||||||
}))
|
}))
|
||||||
.mutation(({ ctx, input }) => {
|
.mutation(({ ctx, input }) => {
|
||||||
const userId = ctx.session?.user.id;
|
const userId = ctx.user?.id;
|
||||||
|
|
||||||
if (!userId) return;
|
if (!userId) return;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user