Skip to content

Commit

Permalink
refactor: no need to create user on router anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
matyson committed Jan 30, 2024
1 parent af0abe9 commit 53ee507
Showing 1 changed file with 5 additions and 31 deletions.
36 changes: 5 additions & 31 deletions src/server/api/routers/workspace-db-state-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,8 @@ export const workspaceDbStateRouter = createTRPCRouter({
}),
)
.mutation(async ({ ctx, input }) => {
const uid = ctx.session.user.id ?? '';
if (uid === '') {
throw new Error('User not found');
}
// checking if the user is registered in the user table
const user = await ctx.prisma.user.findUnique({
where: {
id: uid,
},
});
// if the user is not registered, register the user
if (user === null) {
await ctx.prisma.user
.create({
data: {
id: uid,
name: uid,
},
})
.catch((err: ErrorOptions | undefined) => {
throw new Error("Coudn't register the user in the database", err);
});
}
const uid = ctx.session.user.id;

// creating the workspace
const newWorkspace = await ctx.prisma.workspaceState.create({
data: {
Expand All @@ -54,10 +33,8 @@ export const workspaceDbStateRouter = createTRPCRouter({
}),
)
.mutation(async ({ ctx, input }) => {
const uid = ctx.session.user.id ?? '';
if (uid === '') {
throw new Error('User not found');
}
const uid = ctx.session.user.id;

// checking if the user is registered in the user table
const user = await ctx.prisma.user.findUnique({
where: {
Expand All @@ -80,10 +57,7 @@ export const workspaceDbStateRouter = createTRPCRouter({
return updatedWorkspace;
}),
getUserWorkspaces: protectedProcedure.query(async ({ ctx }) => {
const uid = ctx.session.user.id ?? '';
if (uid === '') {
throw new Error('User not found');
}
const uid = ctx.session.user.id;
const userWorkspaces = await ctx.prisma.workspaceState.findMany({
where: {
userId: uid,
Expand Down

0 comments on commit 53ee507

Please sign in to comment.