From aef29c259a6c9ac745e8e791396d85a0c27b60fe Mon Sep 17 00:00:00 2001 From: DeepaPrasanna Date: Wed, 18 Sep 2024 19:58:08 +0530 Subject: [PATCH 1/4] fix: upsert identity when creating keys in dashboard --- apps/dashboard/lib/db.ts | 3 +- apps/dashboard/lib/trpc/routers/key/create.ts | 50 ++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/apps/dashboard/lib/db.ts b/apps/dashboard/lib/db.ts index 9e0c0dd631..d66407c8f8 100644 --- a/apps/dashboard/lib/db.ts +++ b/apps/dashboard/lib/db.ts @@ -1,6 +1,7 @@ import { dbEnv } from "@/lib/env"; import { Client } from "@planetscale/database"; -import { drizzle, schema } from "@unkey/db"; +import { type PlanetScaleDatabase, drizzle, schema } from "@unkey/db"; +export type Database = PlanetScaleDatabase; export const db = drizzle( new Client({ diff --git a/apps/dashboard/lib/trpc/routers/key/create.ts b/apps/dashboard/lib/trpc/routers/key/create.ts index fd1b4b0bed..591628badb 100644 --- a/apps/dashboard/lib/trpc/routers/key/create.ts +++ b/apps/dashboard/lib/trpc/routers/key/create.ts @@ -1,4 +1,4 @@ -import { db, schema } from "@/lib/db"; +import { type Database, type Identity, db, schema } from "@/lib/db"; import { ingestAuditLogs } from "@/lib/tinybird"; import { TRPCError } from "@trpc/server"; import { newId } from "@unkey/id"; @@ -62,6 +62,10 @@ export const createKey = t.procedure }); } + const identity = input.ownerId + ? await upsertIdentity(db, keyAuth.workspaceId, input.ownerId) + : null; + const keyId = newId("key"); const { key, hash, start } = await newKey({ prefix: input.prefix, @@ -92,6 +96,7 @@ export const createKey = t.procedure deletedAt: null, enabled: input.enabled, environment: input.environment, + identityId: identity?.id, }) .catch((_err) => { throw new TRPCError({ @@ -120,3 +125,46 @@ export const createKey = t.procedure return { keyId, key }; }); + +async function upsertIdentity( + db: Database, + workspaceId: string, + externalId: string, +): Promise { + let identity = await db.query.identities.findFirst({ + where: (table, { and, eq }) => + and(eq(table.workspaceId, workspaceId), eq(table.externalId, externalId)), + }); + if (identity) { + return identity; + } + + await db + .insert(schema.identities) + .values({ + id: newId("identity"), + createdAt: Date.now(), + environment: "default", + meta: {}, + externalId, + updatedAt: null, + workspaceId, + }) + .onDuplicateKeyUpdate({ + set: { + updatedAt: Date.now(), + }, + }); + + identity = await db.query.identities.findFirst({ + where: (table, { and, eq }) => + and(eq(table.workspaceId, workspaceId), eq(table.externalId, externalId)), + }); + if (!identity) { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: "Failed to read identity after upsert", + }); + } + return identity; +} From db49abda45977caf0c8cac8d83dce403312b630f Mon Sep 17 00:00:00 2001 From: DeepaPrasanna Date: Fri, 20 Sep 2024 11:35:13 +0530 Subject: [PATCH 2/4] fix: add error handling --- apps/dashboard/lib/trpc/routers/key/create.ts | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/apps/dashboard/lib/trpc/routers/key/create.ts b/apps/dashboard/lib/trpc/routers/key/create.ts index 591628badb..a3b76a683b 100644 --- a/apps/dashboard/lib/trpc/routers/key/create.ts +++ b/apps/dashboard/lib/trpc/routers/key/create.ts @@ -154,16 +154,30 @@ async function upsertIdentity( set: { updatedAt: Date.now(), }, + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: "Failed to insert identity", + }); + }); + + identity = await db.query.identities + .findFirst({ + where: (table, { and, eq }) => + and(eq(table.workspaceId, workspaceId), eq(table.externalId, externalId)), + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: "Failed to read identity after upsert", + }); }); - identity = await db.query.identities.findFirst({ - where: (table, { and, eq }) => - and(eq(table.workspaceId, workspaceId), eq(table.externalId, externalId)), - }); if (!identity) { throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: "Failed to read identity after upsert", + code: "NOT_FOUND", + message: "No identity present!", }); } return identity; From ed476fdae51b52521482d88a62b12fca17061d1e Mon Sep 17 00:00:00 2001 From: DeepaPrasanna Date: Wed, 2 Oct 2024 22:22:01 +0530 Subject: [PATCH 3/4] refactor: write the upsert identity in a better way --- apps/dashboard/lib/trpc/routers/key/create.ts | 82 +++++++++---------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/apps/dashboard/lib/trpc/routers/key/create.ts b/apps/dashboard/lib/trpc/routers/key/create.ts index a3b76a683b..ab3a7c07d0 100644 --- a/apps/dashboard/lib/trpc/routers/key/create.ts +++ b/apps/dashboard/lib/trpc/routers/key/create.ts @@ -33,7 +33,7 @@ export const createKey = t.procedure .optional(), enabled: z.boolean().default(true), environment: z.string().optional(), - }), + }) ) .mutation(async ({ input, ctx }) => { const workspace = await db.query.workspaces.findFirst({ @@ -129,56 +129,52 @@ export const createKey = t.procedure async function upsertIdentity( db: Database, workspaceId: string, - externalId: string, + externalId: string ): Promise { let identity = await db.query.identities.findFirst({ where: (table, { and, eq }) => and(eq(table.workspaceId, workspaceId), eq(table.externalId, externalId)), }); - if (identity) { - return identity; - } - await db - .insert(schema.identities) - .values({ - id: newId("identity"), - createdAt: Date.now(), - environment: "default", - meta: {}, - externalId, - updatedAt: null, - workspaceId, - }) - .onDuplicateKeyUpdate({ - set: { - updatedAt: Date.now(), - }, - }) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: "Failed to insert identity", + if (!identity) { + await db + .insert(schema.identities) + .values({ + id: newId("identity"), + createdAt: Date.now(), + environment: "default", + meta: {}, + externalId, + updatedAt: null, + workspaceId, + }) + .onDuplicateKeyUpdate({ + set: { + updatedAt: Date.now(), + }, + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: "Failed to insert identity", + }); }); - }); - identity = await db.query.identities - .findFirst({ - where: (table, { and, eq }) => - and(eq(table.workspaceId, workspaceId), eq(table.externalId, externalId)), - }) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: "Failed to read identity after upsert", + identity = await db.query.identities + .findFirst({ + where: (table, { and, eq }) => + and( + eq(table.workspaceId, workspaceId), + eq(table.externalId, externalId) + ), + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: "Failed to read identity after upsert", + }); }); - }); - - if (!identity) { - throw new TRPCError({ - code: "NOT_FOUND", - message: "No identity present!", - }); } - return identity; + + return identity as Identity; } From b3797a42e107ecaabbfa6ed8fa000d8953ded6a0 Mon Sep 17 00:00:00 2001 From: DeepaPrasanna Date: Wed, 2 Oct 2024 22:26:05 +0530 Subject: [PATCH 4/4] refactor: run pnpm fmt --- apps/dashboard/lib/trpc/routers/key/create.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/apps/dashboard/lib/trpc/routers/key/create.ts b/apps/dashboard/lib/trpc/routers/key/create.ts index edb8e4f59a..1ee7b7b26f 100644 --- a/apps/dashboard/lib/trpc/routers/key/create.ts +++ b/apps/dashboard/lib/trpc/routers/key/create.ts @@ -32,7 +32,7 @@ export const createKey = rateLimitedProcedure(ratelimit.create) .optional(), enabled: z.boolean().default(true), environment: z.string().optional(), - }) + }), ) .mutation(async ({ input, ctx }) => { const workspace = await db.query.workspaces @@ -144,7 +144,7 @@ export const createKey = rateLimitedProcedure(ratelimit.create) async function upsertIdentity( db: Database, workspaceId: string, - externalId: string + externalId: string, ): Promise { let identity = await db.query.identities.findFirst({ where: (table, { and, eq }) => @@ -178,10 +178,7 @@ async function upsertIdentity( identity = await db.query.identities .findFirst({ where: (table, { and, eq }) => - and( - eq(table.workspaceId, workspaceId), - eq(table.externalId, externalId) - ), + and(eq(table.workspaceId, workspaceId), eq(table.externalId, externalId)), }) .catch((_err) => { throw new TRPCError({