Skip to content

Commit

Permalink
chore: Convert user.me into a transaction (#47)
Browse files Browse the repository at this point in the history
* Convert me into a transaction

* Add rollback

* fix nit

* Removed radash and try catch in db

* Fix typing on me.ts
  • Loading branch information
averyyip authored Dec 6, 2023
1 parent e5d6460 commit c548041
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 25 deletions.
56 changes: 33 additions & 23 deletions packages/api/src/router/user/me.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TRPCError } from '@trpc/server';
import type { Db } from '@vessel/db';
import { db } from '@vessel/db';

import { IdGenerator } from '@vessel/db/id-generator';
import { retry } from 'radash';
import { useServicesHook } from '../../middlewares/trpc/use-services-hook';
import type { UserManager } from '../../services/user-manager';
import { makeUserManager } from '../../services/user-manager';
Expand Down Expand Up @@ -36,29 +36,39 @@ export const userMe = procedure
if (foundUser)
return { user: await ctx.userManager.profilePic.addToUser(foundUser) };

const org = await db.orgs.create();

const userId = IdGenerator.user();

const uploadProfilePic = async () => {
if (!claims.image_url) return;
const { key } = await ctx.userManager.profilePic.put({
id: userId,
url: claims.image_url,
try {
const user = await db.user.newSignUp({
email: claims.email,
firstName: claims.first_name,
lastName: claims.last_name,
externalId: claims.id,
});
return key;
};
const imageS3Key = await uploadProfilePic();

const newUser = await ctx.db.user.create({
id: userId,
email: claims.email,
orgId: org.id,
firstName: claims.first_name,
lastName: claims.last_name,
externalId: claims.id,
imageS3Key,
});
const uploadProfilePic = async () => {
if (!claims.image_url) return;
const { key } = await ctx.userManager.profilePic.put({
id: user.id,
url: claims.image_url,
});
return key;
};
const imageS3Key = await uploadProfilePic();

return { user: await ctx.userManager.profilePic.addToUser(newUser) };
const updatedUser = await ctx.db.user.update(user.id, {
imageS3Key,
});
return { user: await ctx.userManager.profilePic.addToUser(updatedUser) };
} catch (err) {
const user = await retry({ times: 2, delay: 1000 }, async () => {
const user = await ctx.db.user.findByEmail(claims.email);
if (!user) {
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'User not found',
});
}
return user;
});
return { user: await ctx.userManager.profilePic.addToUser(user) };
}
});
45 changes: 43 additions & 2 deletions packages/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { and, eq } from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import type { z } from 'zod';
import { type z } from 'zod';

import type {
AlertEventId,
Expand Down Expand Up @@ -52,7 +52,11 @@ import {
integration as integrationSchema,
selectIntegrationSchema,
} from './schema/integration';
import { org as orgSchema, selectOrgSchema } from './schema/org';
import {
insertOrgSchema,
org as orgSchema,
selectOrgSchema,
} from './schema/org';
import type { CreateSchedule, Schedule } from './schema/schedule';
import {
insertScheduleSchema,
Expand Down Expand Up @@ -364,6 +368,43 @@ const createDbClient = (db: typeof drizzleDbClient) => ({
.returning();
return selectUserSchema.parse(dbUser[0]);
},
newSignUp: async ({
email,
firstName,
lastName,
externalId,
}: {
email: string;
firstName: string;
lastName: string;
externalId: string;
}) => {
const user = await db.transaction(async (tx) => {
const insertOrg = insertOrgSchema.parse({
id: IdGenerator.org(),
name: 'My Organization',
});
const dbOrg = (
await tx.insert(orgSchema).values(insertOrg).returning()
)[0];
const org = selectOrgSchema.parse(dbOrg);

const insertUser = insertUserSchema.parse({
id: IdGenerator.user(),
email,
firstName,
lastName,
orgId: org.id,
externalId,
});
const user = await (
await tx.insert(userSchema).values(insertUser).returning()
)[0];

return selectUserSchema.parse(user);
});
return user;
},
},
secret: {
find: async (id: SecretId) => {
Expand Down

0 comments on commit c548041

Please sign in to comment.