From be3dc5b42d9a644bdb607cbbf4832d88c09c8aa6 Mon Sep 17 00:00:00 2001 From: Harsh Shrikant Bhat <90265455+harshsbhat@users.noreply.github.com> Date: Wed, 9 Oct 2024 19:36:04 +0530 Subject: [PATCH 1/3] chore: add catch block after every db transaction --- apps/dashboard/lib/trpc/routers/api/create.ts | 80 +++++----- .../lib/trpc/routers/api/setDefaultBytes.ts | 70 +++++---- .../lib/trpc/routers/api/setDefaultPrefix.ts | 70 +++++---- .../routers/api/updateDeleteProtection.ts | 78 +++++----- .../lib/trpc/routers/api/updateIpWhitelist.ts | 70 +++++---- .../lib/trpc/routers/api/updateName.ts | 70 +++++---- .../lib/trpc/routers/key/updateEnabled.ts | 70 +++++---- .../lib/trpc/routers/key/updateExpiration.ts | 78 +++++----- .../lib/trpc/routers/key/updateMetadata.ts | 70 +++++---- .../lib/trpc/routers/key/updateName.ts | 70 +++++---- .../lib/trpc/routers/key/updateOwnerId.ts | 70 +++++---- .../lib/trpc/routers/key/updateRootKeyName.ts | 70 +++++---- .../lib/trpc/routers/llmGateway/delete.ts | 64 ++++---- .../trpc/routers/ratelimit/deleteOverride.ts | 6 + .../trpc/routers/ratelimit/updateOverride.ts | 84 ++++++----- .../routers/rbac/addPermissionToRootKey.ts | 82 ++++++----- .../routers/rbac/connectPermissionToRole.ts | 72 +++++---- .../lib/trpc/routers/rbac/connectRoleToKey.ts | 72 +++++---- .../lib/trpc/routers/rbac/createPermission.ts | 7 + .../lib/trpc/routers/rbac/createRole.ts | 137 +++++++++--------- .../lib/trpc/routers/rbac/upsertPermission.ts | 6 + .../lib/trpc/routers/workspace/changeName.ts | 74 +++++----- .../lib/trpc/routers/workspace/changePlan.ts | 58 ++++---- 23 files changed, 853 insertions(+), 675 deletions(-) diff --git a/apps/dashboard/lib/trpc/routers/api/create.ts b/apps/dashboard/lib/trpc/routers/api/create.ts index 434b618b85..04e109f0fb 100644 --- a/apps/dashboard/lib/trpc/routers/api/create.ts +++ b/apps/dashboard/lib/trpc/routers/api/create.ts @@ -50,46 +50,54 @@ export const createApi = rateLimitedProcedure(ratelimit.create) const apiId = newId("api"); - await db.transaction(async (tx) => { - await tx - .insert(schema.apis) - .values({ - id: apiId, - name: input.name, - workspaceId: ws.id, - keyAuthId, - authType: "key", - ipWhitelist: null, - createdAt: new Date(), - }) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We are unable to create the API. Please contact support using support@unkey.dev", + await db + .transaction(async (tx) => { + await tx + .insert(schema.apis) + .values({ + id: apiId, + name: input.name, + workspaceId: ws.id, + keyAuthId, + authType: "key", + ipWhitelist: null, + createdAt: new Date(), + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to create the API. Please contact support using support@unkey.dev", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: ws.id, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "api.create", - description: `Created ${apiId}`, - resources: [ - { - type: "api", - id: apiId, + await insertAuditLogs(tx, { + workspaceId: ws.id, + actor: { + type: "user", + id: ctx.user.id, + }, + event: "api.create", + description: `Created ${apiId}`, + resources: [ + { + type: "api", + id: apiId, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to create the API. Please contact support using support@unkey.dev", + }); }); - }); return { id: apiId, diff --git a/apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts b/apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts index 4b5fb13b54..d8f5bdfd25 100644 --- a/apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts +++ b/apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts @@ -36,38 +36,46 @@ export const setDefaultApiBytes = rateLimitedProcedure(ratelimit.update) "We are unable to find the correct keyAuth. Please contact support using support@unkey.dev", }); } - await db.transaction(async (tx) => { - await tx - .update(schema.keyAuth) - .set({ - defaultBytes: input.defaultBytes, - }) - .where(eq(schema.keyAuth.id, input.keyAuthId)) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We were unable to update the API default bytes. Please contact support using support@unkey.dev.", + await db + .transaction(async (tx) => { + await tx + .update(schema.keyAuth) + .set({ + defaultBytes: input.defaultBytes, + }) + .where(eq(schema.keyAuth.id, input.keyAuthId)) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were unable to update the API default bytes. Please contact support using support@unkey.dev.", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: keyAuth.workspaceId, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "api.update", - description: `Changed ${keyAuth.workspaceId} default byte size for keys from ${keyAuth.defaultBytes} to ${input.defaultBytes}`, - resources: [ - { - type: "keyAuth", - id: keyAuth.id, + await insertAuditLogs(tx, { + workspaceId: keyAuth.workspaceId, + actor: { + type: "user", + id: ctx.user.id, + }, + event: "api.update", + description: `Changed ${keyAuth.workspaceId} default byte size for keys from ${keyAuth.defaultBytes} to ${input.defaultBytes}`, + resources: [ + { + type: "keyAuth", + id: keyAuth.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were update the default bytes. Please contact support using support@unkey.dev.", + }); }); - }); }); diff --git a/apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts b/apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts index 035af31609..58f4640ae9 100644 --- a/apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts +++ b/apps/dashboard/lib/trpc/routers/api/setDefaultPrefix.ts @@ -32,38 +32,46 @@ export const setDefaultApiPrefix = rateLimitedProcedure(ratelimit.update) "We are unable to find the correct keyAuth. Please contact support using support@unkey.dev", }); } - await db.transaction(async (tx) => { - await tx - .update(schema.keyAuth) - .set({ - defaultPrefix: input.defaultPrefix, - }) - .where(eq(schema.keyAuth.id, input.keyAuthId)) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We were unable to update the API default prefix. Please contact support using support@unkey.dev.", + await db + .transaction(async (tx) => { + await tx + .update(schema.keyAuth) + .set({ + defaultPrefix: input.defaultPrefix, + }) + .where(eq(schema.keyAuth.id, input.keyAuthId)) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were unable to update the API default prefix. Please contact support using support@unkey.dev.", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: keyAuth.workspaceId, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "api.update", - description: `Changed ${keyAuth.workspaceId} default prefix from ${keyAuth.defaultPrefix} to ${input.defaultPrefix}`, - resources: [ - { - type: "keyAuth", - id: keyAuth.id, + await insertAuditLogs(tx, { + workspaceId: keyAuth.workspaceId, + actor: { + type: "user", + id: ctx.user.id, + }, + event: "api.update", + description: `Changed ${keyAuth.workspaceId} default prefix from ${keyAuth.defaultPrefix} to ${input.defaultPrefix}`, + resources: [ + { + type: "keyAuth", + id: keyAuth.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were unable to update the default prefix. Please contact support using support@unkey.dev.", + }); }); - }); }); diff --git a/apps/dashboard/lib/trpc/routers/api/updateDeleteProtection.ts b/apps/dashboard/lib/trpc/routers/api/updateDeleteProtection.ts index 2306ca17b4..368f23aa60 100644 --- a/apps/dashboard/lib/trpc/routers/api/updateDeleteProtection.ts +++ b/apps/dashboard/lib/trpc/routers/api/updateDeleteProtection.ts @@ -36,43 +36,51 @@ export const updateAPIDeleteProtection = rateLimitedProcedure(ratelimit.update) }); } - await db.transaction(async (tx) => { - await tx - .update(schema.apis) - .set({ - deleteProtection: input.enabled, - }) - .where(eq(schema.apis.id, input.apiId)) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We were unable to update the API. Please contact support using support@unkey.dev.", + await db + .transaction(async (tx) => { + await tx + .update(schema.apis) + .set({ + deleteProtection: input.enabled, + }) + .where(eq(schema.apis.id, input.apiId)) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were unable to update the API. Please contact support using support@unkey.dev.", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: api.workspace.id, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "api.update", - description: `API ${api.name} delete protection is now ${ - input.enabled ? "enabled" : "disabled" - }.}`, - resources: [ - { - type: "api", - id: api.id, - meta: { - deleteProtection: input.enabled, + await insertAuditLogs(tx, { + workspaceId: api.workspace.id, + actor: { + type: "user", + id: ctx.user.id, + }, + event: "api.update", + description: `API ${api.name} delete protection is now ${ + input.enabled ? "enabled" : "disabled" + }.}`, + resources: [ + { + type: "api", + id: api.id, + meta: { + deleteProtection: input.enabled, + }, }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were unable to update the API. Please contact support using support@unkey.dev", + }); }); - }); }); diff --git a/apps/dashboard/lib/trpc/routers/api/updateIpWhitelist.ts b/apps/dashboard/lib/trpc/routers/api/updateIpWhitelist.ts index b7dc45041b..513e6d7840 100644 --- a/apps/dashboard/lib/trpc/routers/api/updateIpWhitelist.ts +++ b/apps/dashboard/lib/trpc/routers/api/updateIpWhitelist.ts @@ -53,38 +53,46 @@ export const updateApiIpWhitelist = rateLimitedProcedure(ratelimit.update) const newIpWhitelist = input.ipWhitelist === null ? null : input.ipWhitelist.join(","); - await db.transaction(async (tx) => { - await tx - .update(schema.apis) - .set({ - ipWhitelist: newIpWhitelist, - }) - .where(eq(schema.apis.id, input.apiId)) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We are unable to update the API whitelist. Please contact support using support@unkey.dev", + await db + .transaction(async (tx) => { + await tx + .update(schema.apis) + .set({ + ipWhitelist: newIpWhitelist, + }) + .where(eq(schema.apis.id, input.apiId)) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to update the API whitelist. Please contact support using support@unkey.dev", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: api.workspace.id, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "api.update", - description: `Changed ${api.id} IP whitelist from ${api.ipWhitelist} to ${newIpWhitelist}`, - resources: [ - { - type: "api", - id: api.id, + await insertAuditLogs(tx, { + workspaceId: api.workspace.id, + actor: { + type: "user", + id: ctx.user.id, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + event: "api.update", + description: `Changed ${api.id} IP whitelist from ${api.ipWhitelist} to ${newIpWhitelist}`, + resources: [ + { + type: "api", + id: api.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, + }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to update the API whitelist. Please contact support using support@unkey.dev", + }); }); - }); }); diff --git a/apps/dashboard/lib/trpc/routers/api/updateName.ts b/apps/dashboard/lib/trpc/routers/api/updateName.ts index a054eefd43..f77153eed7 100644 --- a/apps/dashboard/lib/trpc/routers/api/updateName.ts +++ b/apps/dashboard/lib/trpc/routers/api/updateName.ts @@ -36,38 +36,46 @@ export const updateApiName = rateLimitedProcedure(ratelimit.update) "We are unable to find the correct API. Please contact support using support@unkey.dev.", }); } - await db.transaction(async (tx) => { - await tx - .update(schema.apis) - .set({ - name: input.name, - }) - .where(eq(schema.apis.id, input.apiId)) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We were unable to update the API name. Please contact support using support@unkey.dev.", + await db + .transaction(async (tx) => { + await tx + .update(schema.apis) + .set({ + name: input.name, + }) + .where(eq(schema.apis.id, input.apiId)) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were unable to update the API name. Please contact support using support@unkey.dev.", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: api.workspace.id, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "api.update", - description: `Changed ${api.id} name from ${api.name} to ${input.name}`, - resources: [ - { - type: "api", - id: api.id, + await insertAuditLogs(tx, { + workspaceId: api.workspace.id, + actor: { + type: "user", + id: ctx.user.id, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + event: "api.update", + description: `Changed ${api.id} name from ${api.name} to ${input.name}`, + resources: [ + { + type: "api", + id: api.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, + }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were unable to update the API name. Please contact support using support@unkey.dev.", + }); }); - }); }); diff --git a/apps/dashboard/lib/trpc/routers/key/updateEnabled.ts b/apps/dashboard/lib/trpc/routers/key/updateEnabled.ts index 92a5ced6a9..328814a3b3 100644 --- a/apps/dashboard/lib/trpc/routers/key/updateEnabled.ts +++ b/apps/dashboard/lib/trpc/routers/key/updateEnabled.ts @@ -35,38 +35,46 @@ export const updateKeyEnabled = rateLimitedProcedure(ratelimit.update) }); } - await db.transaction(async (tx) => { - await tx - .update(schema.keys) - .set({ - enabled: input.enabled, - }) - .where(eq(schema.keys.id, key.id)) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We were unable to update enabled on this key. Please contact support using support@unkey.dev", + await db + .transaction(async (tx) => { + await tx + .update(schema.keys) + .set({ + enabled: input.enabled, + }) + .where(eq(schema.keys.id, key.id)) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were unable to update enabled on this key. Please contact support using support@unkey.dev", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: key.workspace.id, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "key.update", - description: `${input.enabled ? "Enabled" : "Disabled"} ${key.id}`, - resources: [ - { - type: "key", - id: key.id, + await insertAuditLogs(tx, { + workspaceId: key.workspace.id, + actor: { + type: "user", + id: ctx.user.id, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + event: "key.update", + description: `${input.enabled ? "Enabled" : "Disabled"} ${key.id}`, + resources: [ + { + type: "key", + id: key.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, + }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were unable to update enabled on this key. Please contact support using support@unkey.dev", + }); }); - }); }); diff --git a/apps/dashboard/lib/trpc/routers/key/updateExpiration.ts b/apps/dashboard/lib/trpc/routers/key/updateExpiration.ts index c49f677fda..d46a138cca 100644 --- a/apps/dashboard/lib/trpc/routers/key/updateExpiration.ts +++ b/apps/dashboard/lib/trpc/routers/key/updateExpiration.ts @@ -55,44 +55,52 @@ export const updateKeyExpiration = rateLimitedProcedure(ratelimit.update) code: "NOT_FOUND", }); } - await db.transaction(async (tx) => { - await tx - .update(schema.keys) - .set({ - expires, - }) - .where(eq(schema.keys.id, key.id)) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We were unable to update expiration on this key. Please contact support using support@unkey.dev", + await db + .transaction(async (tx) => { + await tx + .update(schema.keys) + .set({ + expires, + }) + .where(eq(schema.keys.id, key.id)) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were unable to update expiration on this key. Please contact support using support@unkey.dev", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: key.workspace.id, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "key.update", - description: `${ - input.expiration - ? `Changed expiration of ${key.id} to ${input.expiration.toUTCString()}` - : `Disabled expiration for ${key.id}` - }`, - resources: [ - { - type: "key", - id: key.id, + await insertAuditLogs(tx, { + workspaceId: key.workspace.id, + actor: { + type: "user", + id: ctx.user.id, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + event: "key.update", + description: `${ + input.expiration + ? `Changed expiration of ${key.id} to ${input.expiration.toUTCString()}` + : `Disabled expiration for ${key.id}` + }`, + resources: [ + { + type: "key", + id: key.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, + }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were unable to update expiration on this key. Please contact support using support@unkey.dev", + }); }); - }); return true; }); diff --git a/apps/dashboard/lib/trpc/routers/key/updateMetadata.ts b/apps/dashboard/lib/trpc/routers/key/updateMetadata.ts index 949f02acde..26aaecee00 100644 --- a/apps/dashboard/lib/trpc/routers/key/updateMetadata.ts +++ b/apps/dashboard/lib/trpc/routers/key/updateMetadata.ts @@ -48,40 +48,48 @@ export const updateKeyMetadata = rateLimitedProcedure(ratelimit.update) code: "NOT_FOUND", }); } - await db.transaction(async (tx) => { - await tx - .update(schema.keys) - .set({ - meta: meta ? JSON.stringify(meta) : null, - }) - .where(eq(schema.keys.id, key.id)) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We are unable to update metadata on this key. Please contact support using support@unkey.dev", + await db + .transaction(async (tx) => { + await tx + .update(schema.keys) + .set({ + meta: meta ? JSON.stringify(meta) : null, + }) + .where(eq(schema.keys.id, key.id)) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to update metadata on this key. Please contact support using support@unkey.dev", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: key.workspace.id, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "key.update", - description: `Updated metadata of ${key.id}`, - resources: [ - { - type: "key", - id: key.id, + await insertAuditLogs(tx, { + workspaceId: key.workspace.id, + actor: { + type: "user", + id: ctx.user.id, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + event: "key.update", + description: `Updated metadata of ${key.id}`, + resources: [ + { + type: "key", + id: key.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, + }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to update metadata on this key. Please contact support using support@unkey.dev", + }); }); - }); return true; }); diff --git a/apps/dashboard/lib/trpc/routers/key/updateName.ts b/apps/dashboard/lib/trpc/routers/key/updateName.ts index 23ab0d1a9f..70a110ca1b 100644 --- a/apps/dashboard/lib/trpc/routers/key/updateName.ts +++ b/apps/dashboard/lib/trpc/routers/key/updateName.ts @@ -36,40 +36,48 @@ export const updateKeyName = rateLimitedProcedure(ratelimit.update) code: "NOT_FOUND", }); } - await db.transaction(async (tx) => { - await tx - .update(schema.keys) - .set({ - name: input.name ?? null, - }) - .where(eq(schema.keys.id, key.id)) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We are unable to update name on this key. Please contact support using support@unkey.dev", + await db + .transaction(async (tx) => { + await tx + .update(schema.keys) + .set({ + name: input.name ?? null, + }) + .where(eq(schema.keys.id, key.id)) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to update name on this key. Please contact support using support@unkey.dev", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: key.workspace.id, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "key.update", - description: `Changed name of ${key.id} to ${input.name}`, - resources: [ - { - type: "key", - id: key.id, + await insertAuditLogs(tx, { + workspaceId: key.workspace.id, + actor: { + type: "user", + id: ctx.user.id, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + event: "key.update", + description: `Changed name of ${key.id} to ${input.name}`, + resources: [ + { + type: "key", + id: key.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, + }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to update name on this key. Please contact support using support@unkey.dev", + }); }); - }); return true; }); diff --git a/apps/dashboard/lib/trpc/routers/key/updateOwnerId.ts b/apps/dashboard/lib/trpc/routers/key/updateOwnerId.ts index 7fb8a5b807..72a75036a4 100644 --- a/apps/dashboard/lib/trpc/routers/key/updateOwnerId.ts +++ b/apps/dashboard/lib/trpc/routers/key/updateOwnerId.ts @@ -36,40 +36,48 @@ export const updateKeyOwnerId = rateLimitedProcedure(ratelimit.update) }); } - await db.transaction(async (tx) => { - await tx - .update(schema.keys) - .set({ - ownerId: input.ownerId ?? null, - }) - .where(eq(schema.keys.id, key.id)) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We were unable to update ownerId on this key. Please contact support using support@unkey.dev", + await db + .transaction(async (tx) => { + await tx + .update(schema.keys) + .set({ + ownerId: input.ownerId ?? null, + }) + .where(eq(schema.keys.id, key.id)) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were unable to update ownerId on this key. Please contact support using support@unkey.dev", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: key.workspace.id, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "key.update", - description: `Changed ownerId of ${key.id} to ${input.ownerId}`, - resources: [ - { - type: "key", - id: key.id, + await insertAuditLogs(tx, { + workspaceId: key.workspace.id, + actor: { + type: "user", + id: ctx.user.id, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + event: "key.update", + description: `Changed ownerId of ${key.id} to ${input.ownerId}`, + resources: [ + { + type: "key", + id: key.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, + }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We were unable to update ownerId on this key. Please contact support using support@unkey.dev", + }); }); - }); return true; }); diff --git a/apps/dashboard/lib/trpc/routers/key/updateRootKeyName.ts b/apps/dashboard/lib/trpc/routers/key/updateRootKeyName.ts index 79c4978e01..11a08bc681 100644 --- a/apps/dashboard/lib/trpc/routers/key/updateRootKeyName.ts +++ b/apps/dashboard/lib/trpc/routers/key/updateRootKeyName.ts @@ -48,38 +48,46 @@ export const updateRootKeyName = t.procedure }); } - await db.transaction(async (tx) => { - await tx - .update(schema.keys) - .set({ - name: input.name ?? null, - }) - .where(eq(schema.keys.id, key.id)) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We are unable to update root key name. Please contact support using support@unkey.dev", + await db + .transaction(async (tx) => { + await tx + .update(schema.keys) + .set({ + name: input.name ?? null, + }) + .where(eq(schema.keys.id, key.id)) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to update root key name. Please contact support using support@unkey.dev", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: workspace.id, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "key.update", - description: `Changed name of ${key.id} to ${input.name}`, - resources: [ - { - type: "key", - id: key.id, + await insertAuditLogs(tx, { + workspaceId: workspace.id, + actor: { + type: "user", + id: ctx.user.id, + }, + event: "key.update", + description: `Changed name of ${key.id} to ${input.name}`, + resources: [ + { + type: "key", + id: key.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to update root key name. Please contact support using support@unkey.dev", + }); }); - }); }); diff --git a/apps/dashboard/lib/trpc/routers/llmGateway/delete.ts b/apps/dashboard/lib/trpc/routers/llmGateway/delete.ts index 6b9467eb0e..f9cf66c250 100644 --- a/apps/dashboard/lib/trpc/routers/llmGateway/delete.ts +++ b/apps/dashboard/lib/trpc/routers/llmGateway/delete.ts @@ -35,37 +35,45 @@ export const deleteLlmGateway = rateLimitedProcedure(ratelimit.delete) }); } - await db.transaction(async (tx) => { - await tx - .delete(schema.llmGateways) - .where(eq(schema.llmGateways.id, input.gatewayId)) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We are unable to delete the LLM gateway. Please contact support using support@unkey.dev", + await db + .transaction(async (tx) => { + await tx + .delete(schema.llmGateways) + .where(eq(schema.llmGateways.id, input.gatewayId)) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to delete the LLM gateway. Please contact support using support@unkey.dev", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: llmGateway.workspace.id, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "llmGateway.delete", - description: `Deleted ${llmGateway.id}`, - resources: [ - { - type: "gateway", - id: llmGateway.id, + await insertAuditLogs(tx, { + workspaceId: llmGateway.workspace.id, + actor: { + type: "user", + id: ctx.user.id, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + event: "llmGateway.delete", + description: `Deleted ${llmGateway.id}`, + resources: [ + { + type: "gateway", + id: llmGateway.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, + }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to delete LLM gateway. Please contact support using support@unkey.dev", + }); }); - }); return { id: llmGateway.id, diff --git a/apps/dashboard/lib/trpc/routers/ratelimit/deleteOverride.ts b/apps/dashboard/lib/trpc/routers/ratelimit/deleteOverride.ts index 795dac6a45..70740587d9 100644 --- a/apps/dashboard/lib/trpc/routers/ratelimit/deleteOverride.ts +++ b/apps/dashboard/lib/trpc/routers/ratelimit/deleteOverride.ts @@ -81,6 +81,12 @@ export const deleteOverride = rateLimitedProcedure(ratelimit.create) location: ctx.audit.location, userAgent: ctx.audit.userAgent, }, + }).catch((_err) => { + throw new TRPCError({ + message: + "We are unable to delete the override. Please contact support using support@unkey.dev", + code: "INTERNAL_SERVER_ERROR", + }); }); }); }); diff --git a/apps/dashboard/lib/trpc/routers/ratelimit/updateOverride.ts b/apps/dashboard/lib/trpc/routers/ratelimit/updateOverride.ts index a404348a03..108509e4cf 100644 --- a/apps/dashboard/lib/trpc/routers/ratelimit/updateOverride.ts +++ b/apps/dashboard/lib/trpc/routers/ratelimit/updateOverride.ts @@ -50,51 +50,59 @@ export const updateOverride = rateLimitedProcedure(ratelimit.update) }); } - await db.transaction(async (tx) => { - await tx - .update(schema.ratelimitOverrides) - .set({ - limit: input.limit, - duration: input.duration, - updatedAt: new Date(), - async: input.async, - }) - .where(eq(schema.ratelimitOverrides.id, override.id)) - .catch((_err) => { + await db + .transaction(async (tx) => { + await tx + .update(schema.ratelimitOverrides) + .set({ + limit: input.limit, + duration: input.duration, + updatedAt: new Date(), + async: input.async, + }) + .where(eq(schema.ratelimitOverrides.id, override.id)) + .catch((_err) => { + throw new TRPCError({ + message: + "We are unable to update the override. Please contact support using support@unkey.dev.", + code: "INTERNAL_SERVER_ERROR", + }); + }); + await insertAuditLogs(tx, { + workspaceId: override.namespace.workspace.id, + actor: { + type: "user", + id: ctx.user.id, + }, + event: "ratelimitOverride.update", + description: `Changed ${override.id} limits from ${override.limit}/${override.duration} to ${input.limit}/${input.duration}`, + resources: [ + { + type: "ratelimitNamespace", + id: override.namespace.id, + }, + { + type: "ratelimitOverride", + id: override.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, + }, + }).catch((_err) => { throw new TRPCError({ - message: - "We are unable to update the override. Please contact support using support@unkey.dev.", code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to update the override. Please contact support using support@unkey.dev", }); }); - await insertAuditLogs(tx, { - workspaceId: override.namespace.workspace.id, - actor: { - type: "user", - id: ctx.user.id, - }, - event: "ratelimitOverride.update", - description: `Changed ${override.id} limits from ${override.limit}/${override.duration} to ${input.limit}/${input.duration}`, - resources: [ - { - type: "ratelimitNamespace", - id: override.namespace.id, - }, - { - type: "ratelimitOverride", - id: override.id, - }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, - }).catch((_err) => { + }) + .catch((_err) => { throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: - "We are unable to update the override. Please contact support using support@unkey.dev", + "We are unable to update this override for this namespae. Please contact support using support@unkey.dev", }); }); - }); }); diff --git a/apps/dashboard/lib/trpc/routers/rbac/addPermissionToRootKey.ts b/apps/dashboard/lib/trpc/routers/rbac/addPermissionToRootKey.ts index 3ed8fa5ed3..6e6f5b097b 100644 --- a/apps/dashboard/lib/trpc/routers/rbac/addPermissionToRootKey.ts +++ b/apps/dashboard/lib/trpc/routers/rbac/addPermissionToRootKey.ts @@ -65,44 +65,52 @@ export const addPermissionToRootKey = rateLimitedProcedure(ratelimit.create) permission.data, ]); const p = permissions[0]; - await db.transaction(async (tx) => { - await tx - .insert(schema.keysPermissions) - .values({ - keyId: rootKey.id, - permissionId: p.id, - workspaceId: p.workspaceId, - }) - .onDuplicateKeyUpdate({ set: { permissionId: p.id } }) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We are unable to add permission to the root key. Please contact support using support@unkey.dev.", + await db + .transaction(async (tx) => { + await tx + .insert(schema.keysPermissions) + .values({ + keyId: rootKey.id, + permissionId: p.id, + workspaceId: p.workspaceId, + }) + .onDuplicateKeyUpdate({ set: { permissionId: p.id } }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to add permission to the root key. Please contact support using support@unkey.dev.", + }); }); - }); - await insertAuditLogs(tx, [ - ...auditLogs, - { - workspaceId: workspace.id, - actor: { type: "user", id: ctx.user.id }, - event: "authorization.connect_permission_and_key", - description: `Attached ${p.id} to ${rootKey.id}`, - resources: [ - { - type: "key", - id: rootKey.id, - }, - { - type: "permission", - id: p.id, + await insertAuditLogs(tx, [ + ...auditLogs, + { + workspaceId: workspace.id, + actor: { type: "user", id: ctx.user.id }, + event: "authorization.connect_permission_and_key", + description: `Attached ${p.id} to ${rootKey.id}`, + resources: [ + { + type: "key", + id: rootKey.id, + }, + { + type: "permission", + id: p.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, }, - }, - ]); - }); + ]); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to add permission to the rootkey. Please contact support using support@unkey.dev", + }); + }); }); diff --git a/apps/dashboard/lib/trpc/routers/rbac/connectPermissionToRole.ts b/apps/dashboard/lib/trpc/routers/rbac/connectPermissionToRole.ts index a291cb529f..132fb558f2 100644 --- a/apps/dashboard/lib/trpc/routers/rbac/connectPermissionToRole.ts +++ b/apps/dashboard/lib/trpc/routers/rbac/connectPermissionToRole.ts @@ -61,39 +61,47 @@ export const connectPermissionToRole = rateLimitedProcedure(ratelimit.update) permissionId: permission.id, roleId: role.id, }; - await db.transaction(async (tx) => { - await tx - .insert(schema.rolesPermissions) - .values({ ...tuple, createdAt: new Date() }) - .onDuplicateKeyUpdate({ - set: { ...tuple, updatedAt: new Date() }, - }) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We are unable to connect the permission to the role. Please contact support using support@unkey.dev.", + await db + .transaction(async (tx) => { + await tx + .insert(schema.rolesPermissions) + .values({ ...tuple, createdAt: new Date() }) + .onDuplicateKeyUpdate({ + set: { ...tuple, updatedAt: new Date() }, + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to connect the permission to the role. Please contact support using support@unkey.dev.", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: workspace.id, - actor: { type: "user", id: ctx.user.id }, - event: "authorization.connect_role_and_permission", - description: `Connect role ${role.id} to ${permission.id}`, - resources: [ - { - type: "role", - id: role.id, - }, - { - type: "permission", - id: permission.id, + await insertAuditLogs(tx, { + workspaceId: workspace.id, + actor: { type: "user", id: ctx.user.id }, + event: "authorization.connect_role_and_permission", + description: `Connect role ${role.id} to ${permission.id}`, + resources: [ + { + type: "role", + id: role.id, + }, + { + type: "permission", + id: permission.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to connect this permission to role. Please contact support using support@unkey.dev", + }); }); - }); }); diff --git a/apps/dashboard/lib/trpc/routers/rbac/connectRoleToKey.ts b/apps/dashboard/lib/trpc/routers/rbac/connectRoleToKey.ts index 2809667048..fc08d27e6d 100644 --- a/apps/dashboard/lib/trpc/routers/rbac/connectRoleToKey.ts +++ b/apps/dashboard/lib/trpc/routers/rbac/connectRoleToKey.ts @@ -61,39 +61,47 @@ export const connectRoleToKey = rateLimitedProcedure(ratelimit.update) keyId: key.id, roleId: role.id, }; - await db.transaction(async (tx) => { - await tx - .insert(schema.keysRoles) - .values({ ...tuple, createdAt: new Date() }) - .onDuplicateKeyUpdate({ - set: { ...tuple, updatedAt: new Date() }, - }) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We are unable to connect the role and key. Please contact support using support@unkey.dev.", + await db + .transaction(async (tx) => { + await tx + .insert(schema.keysRoles) + .values({ ...tuple, createdAt: new Date() }) + .onDuplicateKeyUpdate({ + set: { ...tuple, updatedAt: new Date() }, + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to connect the role and key. Please contact support using support@unkey.dev.", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: workspace.id, - actor: { type: "user", id: ctx.user.id }, - event: "authorization.connect_role_and_key", - description: `Connect role ${role.id} to ${key.id}`, - resources: [ - { - type: "role", - id: role.id, - }, - { - type: "key", - id: key.id, + await insertAuditLogs(tx, { + workspaceId: workspace.id, + actor: { type: "user", id: ctx.user.id }, + event: "authorization.connect_role_and_key", + description: `Connect role ${role.id} to ${key.id}`, + resources: [ + { + type: "role", + id: role.id, + }, + { + type: "key", + id: key.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to connect the role to the key. Please contact support using support@unkey.dev", + }); }); - }); }); diff --git a/apps/dashboard/lib/trpc/routers/rbac/createPermission.ts b/apps/dashboard/lib/trpc/routers/rbac/createPermission.ts index af87eb79c3..af9cbfc3c1 100644 --- a/apps/dashboard/lib/trpc/routers/rbac/createPermission.ts +++ b/apps/dashboard/lib/trpc/routers/rbac/createPermission.ts @@ -77,6 +77,13 @@ export const createPermission = rateLimitedProcedure(ratelimit.create) message: "We are unable to create a permission. Please contact support using support@unkey.dev.", }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to create permission. Please contact support using support@unkey.dev", + }); }); return { permissionId }; diff --git a/apps/dashboard/lib/trpc/routers/rbac/createRole.ts b/apps/dashboard/lib/trpc/routers/rbac/createRole.ts index ba6a505438..c69eef9a2d 100644 --- a/apps/dashboard/lib/trpc/routers/rbac/createRole.ts +++ b/apps/dashboard/lib/trpc/routers/rbac/createRole.ts @@ -42,76 +42,83 @@ export const createRole = rateLimitedProcedure(ratelimit.create) }); } const roleId = newId("role"); - await db.transaction(async (tx) => { - await tx - .insert(schema.roles) - .values({ - id: roleId, - name: input.name, - description: input.description, - workspaceId: workspace.id, - }) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We are unable to create a role. Please contact support using support@unkey.dev.", - }); - }); - await insertAuditLogs(tx, { - workspaceId: workspace.id, - event: "role.create", - actor: { - type: "user", - id: ctx.user.id, - }, - description: `Created ${roleId}`, - resources: [ - { - type: "role", + await db + .transaction(async (tx) => { + await tx + .insert(schema.roles) + .values({ id: roleId, + name: input.name, + description: input.description, + workspaceId: workspace.id, + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to create a role. Please contact support using support@unkey.dev.", + }); + }); + await insertAuditLogs(tx, { + workspaceId: workspace.id, + event: "role.create", + actor: { + type: "user", + id: ctx.user.id, }, - ], + description: `Created ${roleId}`, + resources: [ + { + type: "role", + id: roleId, + }, + ], - context: { - userAgent: ctx.audit.userAgent, - location: ctx.audit.location, - }, - }); + context: { + userAgent: ctx.audit.userAgent, + location: ctx.audit.location, + }, + }); - if (input.permissionIds && input.permissionIds.length > 0) { - await tx.insert(schema.rolesPermissions).values( - input.permissionIds.map((permissionId) => ({ - permissionId, - roleId: roleId, - workspaceId: workspace.id, - })), - ); - await insertAuditLogs( - tx, - input.permissionIds.map((permissionId) => ({ - workspaceId: workspace.id, - event: "authorization.connect_role_and_permission", - actor: { - type: "user", - id: ctx.user.id, - }, - description: `Connected ${roleId} and ${permissionId}`, - resources: [ - { type: "role", id: roleId }, - { - type: "permission", - id: permissionId, + if (input.permissionIds && input.permissionIds.length > 0) { + await tx.insert(schema.rolesPermissions).values( + input.permissionIds.map((permissionId) => ({ + permissionId, + roleId: roleId, + workspaceId: workspace.id, + })), + ); + await insertAuditLogs( + tx, + input.permissionIds.map((permissionId) => ({ + workspaceId: workspace.id, + event: "authorization.connect_role_and_permission", + actor: { + type: "user", + id: ctx.user.id, }, - ], + description: `Connected ${roleId} and ${permissionId}`, + resources: [ + { type: "role", id: roleId }, + { + type: "permission", + id: permissionId, + }, + ], - context: { - userAgent: ctx.audit.userAgent, - location: ctx.audit.location, - }, - })), - ); - } - }); + context: { + userAgent: ctx.audit.userAgent, + location: ctx.audit.location, + }, + })), + ); + } + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: "We are unable to create role. Please contact support using support@unkey.dev", + }); + }); return { roleId }; }); diff --git a/apps/dashboard/lib/trpc/routers/rbac/upsertPermission.ts b/apps/dashboard/lib/trpc/routers/rbac/upsertPermission.ts index b903ba1512..aed1f4299f 100644 --- a/apps/dashboard/lib/trpc/routers/rbac/upsertPermission.ts +++ b/apps/dashboard/lib/trpc/routers/rbac/upsertPermission.ts @@ -60,6 +60,12 @@ export async function upsertPermission( location: ctx.audit.location, userAgent: ctx.audit.userAgent, }, + }).catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to upsert the permission. Please contact support using support@unkey.dev", + }); }); return permission; diff --git a/apps/dashboard/lib/trpc/routers/workspace/changeName.ts b/apps/dashboard/lib/trpc/routers/workspace/changeName.ts index d42972fa60..31b94d3dbd 100644 --- a/apps/dashboard/lib/trpc/routers/workspace/changeName.ts +++ b/apps/dashboard/lib/trpc/routers/workspace/changeName.ts @@ -28,41 +28,49 @@ export const changeWorkspaceName = rateLimitedProcedure(ratelimit.update) if (!ws || ws.tenantId !== ctx.tenant.id) { throw new Error("Workspace not found, Please sign back in and try again"); } - await db.transaction(async (tx) => { - await tx - .update(schema.workspaces) - .set({ - name: input.name, - }) - .where(eq(schema.workspaces.id, input.workspaceId)) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We are unable to update the workspace name. Please contact support using support@unkey.dev", + await db + .transaction(async (tx) => { + await tx + .update(schema.workspaces) + .set({ + name: input.name, + }) + .where(eq(schema.workspaces.id, input.workspaceId)) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to update the workspace name. Please contact support using support@unkey.dev", + }); }); - }); - await insertAuditLogs(tx, { - workspaceId: ws.id, - actor: { type: "user", id: ctx.user.id }, - event: "workspace.update", - description: `Changed name from ${ws.name} to ${input.name}`, - resources: [ - { - type: "workspace", - id: ws.id, + await insertAuditLogs(tx, { + workspaceId: ws.id, + actor: { type: "user", id: ctx.user.id }, + event: "workspace.update", + description: `Changed name from ${ws.name} to ${input.name}`, + resources: [ + { + type: "workspace", + id: ws.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, - }); + }); - if (ctx.tenant.id.startsWith("org_")) { - await clerkClient.organizations.updateOrganization(ctx.tenant.id, { - name: input.name, + if (ctx.tenant.id.startsWith("org_")) { + await clerkClient.organizations.updateOrganization(ctx.tenant.id, { + name: input.name, + }); + } + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to update the workspace name. Please contact support using support@unkey.dev", }); - } - }); + }); }); diff --git a/apps/dashboard/lib/trpc/routers/workspace/changePlan.ts b/apps/dashboard/lib/trpc/routers/workspace/changePlan.ts index 56ec6ccfd2..c00e4723be 100644 --- a/apps/dashboard/lib/trpc/routers/workspace/changePlan.ts +++ b/apps/dashboard/lib/trpc/routers/workspace/changePlan.ts @@ -160,33 +160,41 @@ export const changeWorkspacePlan = rateLimitedProcedure(ratelimit.update) message: "You do not have a payment method. Please add one before upgrading.", }); } - await db.transaction(async (tx) => { - await tx - .update(schema.workspaces) - .set({ - plan: "pro", - planChanged: new Date(), - subscriptions: defaultProSubscriptions(), - planDowngradeRequest: null, - }) - .where(eq(schema.workspaces.id, input.workspaceId)); - await insertAuditLogs(tx, { - workspaceId: workspace.id, - actor: { type: "user", id: ctx.user.id }, - event: "workspace.update", - description: "Changed plan to 'pro'", - resources: [ - { - type: "workspace", - id: workspace.id, + await db + .transaction(async (tx) => { + await tx + .update(schema.workspaces) + .set({ + plan: "pro", + planChanged: new Date(), + subscriptions: defaultProSubscriptions(), + planDowngradeRequest: null, + }) + .where(eq(schema.workspaces.id, input.workspaceId)); + await insertAuditLogs(tx, { + workspaceId: workspace.id, + actor: { type: "user", id: ctx.user.id }, + event: "workspace.update", + description: "Changed plan to 'pro'", + resources: [ + { + type: "workspace", + id: workspace.id, + }, + ], + context: { + location: ctx.audit.location, + userAgent: ctx.audit.userAgent, }, - ], - context: { - location: ctx.audit.location, - userAgent: ctx.audit.userAgent, - }, + }); + }) + .catch((_err) => { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: + "We are unable to change plans. Please contact support using support@unkey.dev", + }); }); - }); return { title: "Your workspace has been upgraded" }; } } From a1dd4d910e9f8489f917981c0adc030b9cd78ad8 Mon Sep 17 00:00:00 2001 From: Harsh Shrikant Bhat <90265455+harshsbhat@users.noreply.github.com> Date: Wed, 9 Oct 2024 20:03:41 +0530 Subject: [PATCH 2/3] chore: typo --- apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts | 2 +- .../dashboard/lib/trpc/routers/ratelimit/updateOverride.ts | 6 +++--- apps/dashboard/lib/trpc/routers/rbac/createPermission.ts | 7 ------- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts b/apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts index d8f5bdfd25..600f682a8b 100644 --- a/apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts +++ b/apps/dashboard/lib/trpc/routers/api/setDefaultBytes.ts @@ -75,7 +75,7 @@ export const setDefaultApiBytes = rateLimitedProcedure(ratelimit.update) throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: - "We were update the default bytes. Please contact support using support@unkey.dev.", + "We were unable to update the default bytes. Please contact support using support@unkey.dev.", }); }); }); diff --git a/apps/dashboard/lib/trpc/routers/ratelimit/updateOverride.ts b/apps/dashboard/lib/trpc/routers/ratelimit/updateOverride.ts index 108509e4cf..8d7336a486 100644 --- a/apps/dashboard/lib/trpc/routers/ratelimit/updateOverride.ts +++ b/apps/dashboard/lib/trpc/routers/ratelimit/updateOverride.ts @@ -38,7 +38,7 @@ export const updateOverride = rateLimitedProcedure(ratelimit.update) throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: - "We are unable to update this override for this namespae. Please contact support using support@unkey.dev", + "We are unable to update the override for this namespace. Please contact support using support@unkey.dev", }); }); @@ -64,7 +64,7 @@ export const updateOverride = rateLimitedProcedure(ratelimit.update) .catch((_err) => { throw new TRPCError({ message: - "We are unable to update the override. Please contact support using support@unkey.dev.", + "We are unable to update the override for this namespace. Please contact support using support@unkey.dev.", code: "INTERNAL_SERVER_ERROR", }); }); @@ -102,7 +102,7 @@ export const updateOverride = rateLimitedProcedure(ratelimit.update) throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: - "We are unable to update this override for this namespae. Please contact support using support@unkey.dev", + "We are unable to update the override for this namespace. Please contact support using support@unkey.dev", }); }); }); diff --git a/apps/dashboard/lib/trpc/routers/rbac/createPermission.ts b/apps/dashboard/lib/trpc/routers/rbac/createPermission.ts index af9cbfc3c1..af87eb79c3 100644 --- a/apps/dashboard/lib/trpc/routers/rbac/createPermission.ts +++ b/apps/dashboard/lib/trpc/routers/rbac/createPermission.ts @@ -77,13 +77,6 @@ export const createPermission = rateLimitedProcedure(ratelimit.create) message: "We are unable to create a permission. Please contact support using support@unkey.dev.", }); - }) - .catch((_err) => { - throw new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - message: - "We are unable to create permission. Please contact support using support@unkey.dev", - }); }); return { permissionId }; From 465e5aa425ac76a0e9c8f8041504bf4a0493b605 Mon Sep 17 00:00:00 2001 From: Harsh Shrikant Bhat <90265455+harshsbhat@users.noreply.github.com> Date: Wed, 9 Oct 2024 23:22:02 +0530 Subject: [PATCH 3/3] feat: add roadmap link in the footer --- apps/dashboard/components/dashboard/feedback-component.tsx | 2 +- apps/dashboard/lib/trpc/routers/plain.ts | 4 +--- apps/www/app/roadmap/page.tsx | 5 +++++ apps/www/components/footer/footer.tsx | 1 + 4 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 apps/www/app/roadmap/page.tsx diff --git a/apps/dashboard/components/dashboard/feedback-component.tsx b/apps/dashboard/components/dashboard/feedback-component.tsx index dddf351c78..8d1e8da58b 100644 --- a/apps/dashboard/components/dashboard/feedback-component.tsx +++ b/apps/dashboard/components/dashboard/feedback-component.tsx @@ -48,7 +48,7 @@ export const Feedback: React.FC = ({ variant, FeedbackOpen }) => const schema = z.object({ severity: z.enum(["p0", "p1", "p2", "p3"]), issueType: z.enum(["bug", "feature", "security", "payment", "question"]), - message: z.string().min(20, 'Feedback must contain at least 20 characters'), + message: z.string().min(20, "Feedback must contain at least 20 characters"), }); const form = useForm>({ diff --git a/apps/dashboard/lib/trpc/routers/plain.ts b/apps/dashboard/lib/trpc/routers/plain.ts index b52f4f13d0..8bbd65a4c0 100644 --- a/apps/dashboard/lib/trpc/routers/plain.ts +++ b/apps/dashboard/lib/trpc/routers/plain.ts @@ -12,9 +12,7 @@ export const createPlainIssue = rateLimitedProcedure(ratelimit.create) z.object({ issueType, severity, - message: z - .string() - .min(20, 'Feedback must contain at least 20 characters'), + message: z.string().min(20, "Feedback must contain at least 20 characters"), }), ) .mutation(async ({ input, ctx }) => { diff --git a/apps/www/app/roadmap/page.tsx b/apps/www/app/roadmap/page.tsx new file mode 100644 index 0000000000..f444cb2586 --- /dev/null +++ b/apps/www/app/roadmap/page.tsx @@ -0,0 +1,5 @@ +import { redirect } from "next/navigation"; + +export default function RoadmapRedirect() { + return redirect("https://github.com/unkeyed/unkey/discussions/categories/roadmap"); +} diff --git a/apps/www/components/footer/footer.tsx b/apps/www/components/footer/footer.tsx index 5beb995c26..3860eabd9f 100644 --- a/apps/www/components/footer/footer.tsx +++ b/apps/www/components/footer/footer.tsx @@ -13,6 +13,7 @@ const navigation = [ { title: "Company", links: [ + { title: "Roadmap", href: "/roadmap", external: true }, { title: "About", href: "/about" }, { title: "Blog", href: "/blog" }, { title: "Changelog", href: "/changelog" },