Unified · Split
| @@ -3,6 +3,7 @@ import { v } from "convex/values"; · 2 unmodified lines | |||
| 3 | import { components, internal } from "./_generated/api"; | 3 | import { components, internal } from "./_generated/api"; |
| 4 | import type { DataModel, Doc, Id } from "./_generated/dataModel"; | 4 | import type { DataModel, Doc, Id } from "./_generated/dataModel"; |
| 5 | import { internalMutation, mutation, query } from "./_generated/server"; | 5 | import { internalMutation, mutation, query } from "./_generated/server"; |
| 6 | import { ensureUniqueSlug } from "./lib/slug"; | ||
| 6 | 7 | ||
| 7 | const workosEnabled = Boolean( | 8 | const workosEnabled = Boolean( |
| 8 | process.env.WORKOS_CLIENT_ID && process.env.WORKOS_API_KEY && process.env.WORKOS_WEBHOOK_SECRET, | 9 | process.env.WORKOS_CLIENT_ID && process.env.WORKOS_API_KEY && process.env.WORKOS_WEBHOOK_SECRET, |
| @@ -63,26 +64,13 @@ async function getOrganizationByWorkosOrgId(ctx: { db: any }, workosOrgId: strin · 54 unmodified lines | |||
| 63 | 64 | ||
| 64 | async function ensureUniqueOrganizationSlug(ctx: { db: any }, baseName: string): Promise<string> { | 65 | async function ensureUniqueOrganizationSlug(ctx: { db: any }, baseName: string): Promise<string> { |
| 65 | const baseSlug = slugify(baseName); | 66 | const baseSlug = slugify(baseName); |
| 66 | const existing = await ctx.db | 67 | return await ensureUniqueSlug(baseSlug, async (candidate) => { |
| 67 | .query("organizations") | ||
| 68 | .withIndex("by_slug", (q: any) => q.eq("slug", baseSlug)) | ||
| 69 | .unique(); | ||
| 70 | if (!existing) { | ||
| 71 | return baseSlug; | ||
| 72 | } | ||
| 73 | |||
| 74 | for (let i = 0; i < 20; i += 1) { | ||
| 75 | const candidate = `${baseSlug}-${crypto.randomUUID().slice(0, 6)}`; | ||
| 76 | const collision = await ctx.db | 68 | const collision = await ctx.db |
| 77 | .query("organizations") | 69 | .query("organizations") |
| 78 | .withIndex("by_slug", (q: any) => q.eq("slug", candidate)) | 70 | .withIndex("by_slug", (q: any) => q.eq("slug", candidate)) |
| 79 | .unique(); | 71 | .unique(); |
| 80 | if (!collision) { | 72 | return collision !== null; |
| 81 | return candidate; | 73 | }); |
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | return `${baseSlug}-${Date.now()}`; | ||
| 86 | } | 74 | } |
| 87 | 75 | ||
| 88 | async function upsertOrganizationMembership( | 76 | async function upsertOrganizationMembership( |
| @@ -2,6 +2,7 @@ import { v } from "convex/values"; · 1 unmodified line | |||
| 2 | import type { Doc } from "./_generated/dataModel"; | 2 | import type { Doc } from "./_generated/dataModel"; |
| 3 | import type { MutationCtx, QueryCtx } from "./_generated/server"; | 3 | import type { MutationCtx, QueryCtx } from "./_generated/server"; |
| 4 | import { mutation, query } from "./_generated/server"; | 4 | import { mutation, query } from "./_generated/server"; |
| 5 | import { ensureUniqueSlug } from "./lib/slug"; | ||
| 5 | 6 | ||
| 6 | const DEFAULT_TIMEOUT_MS = 15_000; | 7 | const DEFAULT_TIMEOUT_MS = 15_000; |
| 7 | 8 | ||
| @@ -30,26 +31,13 @@ function slugify(input: string): string { · 22 unmodified lines | |||
| 30 | 31 | ||
| 31 | async function ensureUniqueOrganizationSlug(ctx: Pick<MutationCtx, "db">, baseName: string): Promise<string> { | 32 | async function ensureUniqueOrganizationSlug(ctx: Pick<MutationCtx, "db">, baseName: string): Promise<string> { |
| 32 | const baseSlug = slugify(baseName); | 33 | const baseSlug = slugify(baseName); |
| 33 | const existing = await ctx.db | 34 | return await ensureUniqueSlug(baseSlug, async (candidate) => { |
| 34 | .query("organizations") | ||
| 35 | .withIndex("by_slug", (q) => q.eq("slug", baseSlug)) | ||
| 36 | .unique(); | ||
| 37 | if (!existing) { | ||
| 38 | return baseSlug; | ||
| 39 | } | ||
| 40 | |||
| 41 | for (let i = 0; i < 20; i += 1) { | ||
| 42 | const candidate = `${baseSlug}-${crypto.randomUUID().slice(0, 6)}`; | ||
| 43 | const collision = await ctx.db | 35 | const collision = await ctx.db |
| 44 | .query("organizations") | 36 | .query("organizations") |
| 45 | .withIndex("by_slug", (q) => q.eq("slug", candidate)) | 37 | .withIndex("by_slug", (q) => q.eq("slug", candidate)) |
| 46 | .unique(); | 38 | .unique(); |
| 47 | if (!collision) { | 39 | return collision !== null; |
| 48 | return candidate; | 40 | }); |
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | return `${baseSlug}-${Date.now()}`; | ||
| 53 | } | 41 | } |
| 54 | 42 | ||
| 55 | async function upsertOrganizationMembership( | 43 | async function upsertOrganizationMembership( |
| @@ -0,0 +1,20 @@ | |||
| 1 | const DEFAULT_MAX_RANDOM_SLUG_ATTEMPTS = 20; | ||
| 2 | |||
| 3 | export async function ensureUniqueSlug( | ||
| 4 | baseSlug: string, | ||
| 5 | hasCollision: (candidate: string) => Promise<boolean>, | ||
| 6 | maxRandomAttempts = DEFAULT_MAX_RANDOM_SLUG_ATTEMPTS, | ||
| 7 | ): Promise<string> { | ||
| 8 | if (!(await hasCollision(baseSlug))) { | ||
| 9 | return baseSlug; | ||
| 10 | } | ||
| 11 | |||
| 12 | for (let attempt = 0; attempt < maxRandomAttempts; attempt += 1) { | ||
| 13 | const candidate = `${baseSlug}-${crypto.randomUUID().slice(0, 6)}`; | ||
| 14 | if (!(await hasCollision(candidate))) { | ||
| 15 | return candidate; | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | return `${baseSlug}-${Date.now()}`; | ||
| 20 | } | ||
| @@ -3,6 +3,7 @@ import type { Doc, Id } from "./_generated/dataModel"; · 2 unmodified lines | |||
| 3 | import type { MutationCtx, QueryCtx } from "./_generated/server"; | 3 | import type { MutationCtx, QueryCtx } from "./_generated/server"; |
| 4 | import { optionalAccountQuery, authedMutation } from "./lib/functionBuilders"; | 4 | import { optionalAccountQuery, authedMutation } from "./lib/functionBuilders"; |
| 5 | import { getOrganizationMembership, slugify } from "./lib/identity"; | 5 | import { getOrganizationMembership, slugify } from "./lib/identity"; |
| 6 | import { ensureUniqueSlug } from "./lib/slug"; | ||
| 6 | 7 | ||
| 7 | type WorkspaceSummary = { | 8 | type WorkspaceSummary = { |
| 8 | id: Id<"workspaces">; | 9 | id: Id<"workspaces">; |
| @@ -16,26 +17,13 @@ type WorkspaceSummary = { · 7 unmodified lines | |||
| 16 | 17 | ||
| 17 | async function ensureUniqueOrganizationSlug(ctx: Pick<MutationCtx, "db">, baseName: string): Promise<string> { | 18 | async function ensureUniqueOrganizationSlug(ctx: Pick<MutationCtx, "db">, baseName: string): Promise<string> { |
| 18 | const baseSlug = slugify(baseName); | 19 | const baseSlug = slugify(baseName); |
| 19 | const existing = await ctx.db | 20 | return await ensureUniqueSlug(baseSlug, async (candidate) => { |
| 20 | .query("organizations") | ||
| 21 | .withIndex("by_slug", (q) => q.eq("slug", baseSlug)) | ||
| 22 | .unique(); | ||
| 23 | if (!existing) { | ||
| 24 | return baseSlug; | ||
| 25 | } | ||
| 26 | |||
| 27 | for (let i = 0; i < 20; i += 1) { | ||
| 28 | const candidate = `${baseSlug}-${crypto.randomUUID().slice(0, 6)}`; | ||
| 29 | const collision = await ctx.db | 21 | const collision = await ctx.db |
| 30 | .query("organizations") | 22 | .query("organizations") |
| 31 | .withIndex("by_slug", (q) => q.eq("slug", candidate)) | 23 | .withIndex("by_slug", (q) => q.eq("slug", candidate)) |
| 32 | .unique(); | 24 | .unique(); |
| 33 | if (!collision) { | 25 | return collision !== null; |
| 34 | return candidate; | 26 | }); |
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | return `${baseSlug}-${Date.now()}`; | ||
| 39 | } | 27 | } |
| 40 | 28 | ||
| 41 | async function mapWorkspaceWithIcon( | 29 | async function mapWorkspaceWithIcon( |
| @@ -3,6 +3,7 @@ import type { Doc, Id } from "./_generated/dataModel"; · 2 unmodified lines | |||
| 3 | import type { MutationCtx, QueryCtx } from "./_generated/server"; | 3 | import type { MutationCtx, QueryCtx } from "./_generated/server"; |
| 4 | import { optionalAccountQuery, authedMutation } from "./lib/functionBuilders"; | 4 | import { optionalAccountQuery, authedMutation } from "./lib/functionBuilders"; |
| 5 | import { getOrganizationMembership, slugify } from "./lib/identity"; | 5 | import { getOrganizationMembership, slugify } from "./lib/identity"; |
| 6 | import { ensureUniqueSlug } from "./lib/slug"; | ||
| 6 | 7 | ||
| 7 | type WorkspaceResult = { | 8 | type WorkspaceResult = { |
| 8 | id: Id<"workspaces">; | 9 | id: Id<"workspaces">; |
| @@ -21,26 +22,13 @@ async function ensureUniqueWorkspaceSlug( · 12 unmodified lines | |||
| 21 | baseName: string, | 22 | baseName: string, |
| 22 | ): Promise<string> { | 23 | ): Promise<string> { |
| 23 | const baseSlug = slugify(baseName); | 24 | const baseSlug = slugify(baseName); |
| 24 | const existing = await ctx.db | 25 | return await ensureUniqueSlug(baseSlug, async (candidate) => { |
| 25 | .query("workspaces") | ||
| 26 | .withIndex("by_organization_slug", (q) => q.eq("organizationId", organizationId).eq("slug", baseSlug)) | ||
| 27 | .unique(); | ||
| 28 | if (!existing) { | ||
| 29 | return baseSlug; | ||
| 30 | } | ||
| 31 | |||
| 32 | for (let i = 0; i < 20; i += 1) { | ||
| 33 | const candidate = `${baseSlug}-${crypto.randomUUID().slice(0, 6)}`; | ||
| 34 | const collision = await ctx.db | 26 | const collision = await ctx.db |
| 35 | .query("workspaces") | 27 | .query("workspaces") |
| 36 | .withIndex("by_organization_slug", (q) => q.eq("organizationId", organizationId).eq("slug", candidate)) | 28 | .withIndex("by_organization_slug", (q) => q.eq("organizationId", organizationId).eq("slug", candidate)) |
| 37 | .unique(); | 29 | .unique(); |
| 38 | if (!collision) { | 30 | return collision !== null; |
| 39 | return candidate; | 31 | }); |
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | return `${baseSlug}-${Date.now()}`; | ||
| 44 | } | 32 | } |
| 45 | 33 | ||
| 46 | async function toWorkspaceResult( | 34 | async function toWorkspaceResult( |