feat: switch next auth and drizzle to supabase (#1)
* feat: add supabase * feat: setup auth * feat: test refactoring board queries * feat: convert to pages router * feat: convert board router to supabase * feat: swap out drizzle for supabase in label, list and workspace routers * feat: swap out drizzle for supabase on the import router * feat: swap drizzle for supabase on create new card * feat: switch card router to use supabase * feat: finish swapping drizzle for supabase * chore: lint all router files * fix: signout * chore: fix types
This commit is contained in:
@@ -1,15 +1,10 @@
|
||||
import { z } from "zod";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
import {
|
||||
createTRPCRouter,
|
||||
publicProcedure,
|
||||
} from "~/server/api/trpc";
|
||||
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
|
||||
|
||||
import { boards, cards, imports, lists, workspaces } from "~/server/db/schema";
|
||||
import { generateUID } from "~/utils/generateUID";
|
||||
|
||||
const TRELLO_API_URL = 'https://api.trello.com/1';
|
||||
const TRELLO_API_URL = "https://api.trello.com/1";
|
||||
|
||||
interface TrelloBoard {
|
||||
id: string;
|
||||
@@ -36,29 +31,29 @@ interface MemberData {
|
||||
|
||||
export const importRouter = createTRPCRouter({
|
||||
trello: createTRPCRouter({
|
||||
getBoards: publicProcedure
|
||||
getBoards: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
apiKey: z.string().length(32),
|
||||
token: z.string().length(76),
|
||||
}),
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const userId = ctx.session?.user.id;
|
||||
|
||||
if (!userId) return;
|
||||
.query(async ({ input }) => {
|
||||
const fetchMemberRes = await fetch(
|
||||
`${TRELLO_API_URL}/tokens/${input.token}/member?key=${input.apiKey}`,
|
||||
);
|
||||
|
||||
const fetchMemberRes = await fetch(`${TRELLO_API_URL}/tokens/${input.token}/member?key=${input.apiKey}`)
|
||||
|
||||
const member = await fetchMemberRes.json() as MemberData;
|
||||
const member = (await fetchMemberRes.json()) as MemberData;
|
||||
|
||||
const boardIds = member.idBoards;
|
||||
|
||||
const fetchBoard = async (boardId: string) => {
|
||||
try {
|
||||
const response = await fetch(`${TRELLO_API_URL}/boards/${boardId}?key=${input.apiKey}&token=${input.token}`);
|
||||
const data = await response.json() as TrelloBoard;
|
||||
|
||||
const response = await fetch(
|
||||
`${TRELLO_API_URL}/boards/${boardId}?key=${input.apiKey}&token=${input.token}`,
|
||||
);
|
||||
const data = (await response.json()) as TrelloBoard;
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
@@ -72,37 +67,57 @@ export const importRouter = createTRPCRouter({
|
||||
}
|
||||
|
||||
const boardDataArray = await Promise.all(boards);
|
||||
|
||||
return boardDataArray.map((board) => ({ id: board.id, name: board.name }));
|
||||
|
||||
return boardDataArray.map((board) => ({
|
||||
id: board.id,
|
||||
name: board.name,
|
||||
}));
|
||||
}),
|
||||
importBoards: publicProcedure
|
||||
importBoards: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
boardIds: z.array(z.string()),
|
||||
apiKey: z.string().length(32),
|
||||
token: z.string().length(76),
|
||||
workspacePublicId: z.string().min(12)
|
||||
workspacePublicId: z.string().min(12),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const userId = ctx.session?.user.id;
|
||||
|
||||
const userId = ctx.user?.id;
|
||||
|
||||
if (!userId) return;
|
||||
|
||||
const newImport = await ctx.db.insert(imports).values({
|
||||
publicId: generateUID(),
|
||||
source: 'trello',
|
||||
createdBy: userId,
|
||||
status: 'started'
|
||||
}).returning({ id: imports.id });
|
||||
const newImport = await ctx.db
|
||||
.from("import")
|
||||
.insert({
|
||||
publicId: generateUID(),
|
||||
source: "trello",
|
||||
createdBy: userId,
|
||||
status: "started",
|
||||
})
|
||||
.select(`id`)
|
||||
.limit(1)
|
||||
.single();
|
||||
|
||||
const newImportId = newImport[0]?.id;
|
||||
const newImportId = newImport.data?.id;
|
||||
|
||||
let boardsCreated = 0;
|
||||
|
||||
const workspace = await ctx.db
|
||||
.from("workspace")
|
||||
.select(`id`)
|
||||
.eq("publicId", input.workspacePublicId)
|
||||
.is("deletedAt", null)
|
||||
.limit(1)
|
||||
.single();
|
||||
|
||||
if (!workspace.data) return;
|
||||
|
||||
for (const boardId of input.boardIds) {
|
||||
const response = await fetch(`${TRELLO_API_URL}/boards/${boardId}?key=${input.apiKey}&token=${input.token}&lists=open&cards=open`);
|
||||
const data = await response.json() as TrelloBoard;
|
||||
const response = await fetch(
|
||||
`${TRELLO_API_URL}/boards/${boardId}?key=${input.apiKey}&token=${input.token}&lists=open&cards=open`,
|
||||
);
|
||||
const data = (await response.json()) as TrelloBoard;
|
||||
|
||||
const formattedData = {
|
||||
name: data.name,
|
||||
@@ -112,72 +127,74 @@ export const importRouter = createTRPCRouter({
|
||||
.filter((card) => card.idList === list.id)
|
||||
.map((_card) => ({
|
||||
name: _card.name,
|
||||
description: _card.desc
|
||||
}))
|
||||
}))
|
||||
}
|
||||
description: _card.desc,
|
||||
})),
|
||||
})),
|
||||
};
|
||||
|
||||
const workspace = await ctx.db.query.workspaces.findFirst({
|
||||
where: eq(workspaces.publicId, input.workspacePublicId),
|
||||
})
|
||||
|
||||
if (!workspace) return;
|
||||
|
||||
await ctx.db.transaction(async (tx) => {
|
||||
const newBoard = await tx.insert(boards).values({
|
||||
const newBoard = await ctx.db
|
||||
.from("board")
|
||||
.insert({
|
||||
publicId: generateUID(),
|
||||
name: data.name,
|
||||
createdBy: userId,
|
||||
importId: newImportId,
|
||||
workspaceId: workspace.id
|
||||
}).returning({ id: boards.id });
|
||||
workspaceId: workspace.data.id,
|
||||
})
|
||||
.select(`id`)
|
||||
.limit(1)
|
||||
.single();
|
||||
|
||||
const newBoardId = newBoard[0]?.id
|
||||
const newBoardId = newBoard.data?.id;
|
||||
|
||||
if (!newBoardId) return;
|
||||
if (!newBoardId) return;
|
||||
|
||||
let listIndex = 0;
|
||||
let listIndex = 0;
|
||||
|
||||
for (const list of formattedData.lists) {
|
||||
const newList = await tx.insert(lists).values({
|
||||
for (const list of formattedData.lists) {
|
||||
const newList = await ctx.db
|
||||
.from("list")
|
||||
.insert({
|
||||
publicId: generateUID(),
|
||||
name: list.name,
|
||||
createdBy: userId,
|
||||
boardId: newBoardId,
|
||||
index: listIndex,
|
||||
importId: newImportId
|
||||
}).returning({ id: lists.id });
|
||||
importId: newImportId,
|
||||
})
|
||||
.select(`id`)
|
||||
.limit(1)
|
||||
.single();
|
||||
|
||||
const newListId = newList[0]?.id
|
||||
const newListId = newList.data?.id;
|
||||
|
||||
if (list.cards.length && newListId) {
|
||||
const cardsInsert = list.cards.map((card, index) => ({
|
||||
publicId: generateUID(),
|
||||
title: card.name,
|
||||
description: card.description,
|
||||
createdBy: userId,
|
||||
listId: newListId,
|
||||
index,
|
||||
importId: newImportId
|
||||
}))
|
||||
if (list.cards.length && newListId) {
|
||||
const cardsInsert = list.cards.map((card, index) => ({
|
||||
publicId: generateUID(),
|
||||
title: card.name,
|
||||
description: card.description,
|
||||
createdBy: userId,
|
||||
listId: newListId,
|
||||
index,
|
||||
importId: newImportId,
|
||||
}));
|
||||
|
||||
await tx.insert(cards).values(cardsInsert);
|
||||
}
|
||||
|
||||
listIndex ++
|
||||
await ctx.db.from("card").insert(cardsInsert);
|
||||
}
|
||||
})
|
||||
|
||||
boardsCreated ++
|
||||
listIndex++;
|
||||
}
|
||||
boardsCreated++;
|
||||
}
|
||||
|
||||
if (boardsCreated > 0 && newImportId) {
|
||||
await ctx.db.update(imports)
|
||||
.set({ status: 'success' })
|
||||
.where(eq(imports.id, newImportId))
|
||||
await ctx.db
|
||||
.from("import")
|
||||
.update({ status: "success" })
|
||||
.eq("importId", newImportId);
|
||||
}
|
||||
|
||||
return { boardsCreated }
|
||||
return { boardsCreated };
|
||||
}),
|
||||
})
|
||||
});
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user