MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / upsertOrganization

Function upsertOrganization

apps/cloud/src/auth/user-store.ts:53–75  ·  view source on GitHub ↗
(org: { id: string; name: string })

Source from the content-addressed store, hash-verified

51 // self-healing. Existing rows keep their slug (stable across renames, so org
52 // URLs survive) and only refresh their name.
53 const upsertOrganization = async (org: { id: string; name: string }) => {
54 const existing = await getOrganization(org.id);
55 if (existing) {
56 const [updated] = await db
57 .update(organizations)
58 .set({ name: org.name })
59 .where(eq(organizations.id, org.id))
60 .returning();
61 return updated ?? existing;
62 }
63 for (let attempt = 0; attempt < 4; attempt++) {
64 const slug = await generateOrgSlug(org.name, slugTaken);
65 const inserted = await tryInsertOrg(org.id, org.name, slug);
66 if (inserted) return inserted;
67 // The insert was swallowed by a conflict. If the id now exists, a
68 // concurrent request mirrored it — return that row. Otherwise the slug
69 // candidate collided; loop and mint a fresh one.
70 const fresh = await getOrganization(org.id);
71 if (fresh) return fresh;
72 }
73 // oxlint-disable-next-line executor/no-try-catch-or-throw, executor/no-error-constructor -- boundary: slug minting exhausted retries; surfacing loudly beats a silently unslugged org
74 throw new Error(`unable to mint a slug for organization ${org.id}`);
75 };
76
77 return {
78 // --- Accounts ---

Callers

nothing calls this directly

Calls 5

generateOrgSlugFunction · 0.90
getOrganizationFunction · 0.85
tryInsertOrgFunction · 0.85
setMethod · 0.80
updateMethod · 0.65

Tested by

no test coverage detected