11import { json } from "@remix-run/server-runtime" ;
22import type { GetOrgsResponseBody } from "@trigger.dev/core/v3" ;
3- import { z } from "zod " ;
3+ import { CreateOrgRequestBody } from "@trigger.dev/core/v3 " ;
44import { prisma } from "~/db.server" ;
5+ import { env } from "~/env.server" ;
56import { createOrganization } from "~/models/organization.server" ;
67import {
78 createActionPATApiRoute ,
89 createLoaderPATApiRoute ,
910} from "~/services/routeBuilders/apiBuilder.server" ;
11+ import { extractDomain , faviconUrl } from "~/utils/favicon" ;
1012
1113// Identity-only: lists the caller's own orgs, so no authorization gate.
1214export const loader = createLoaderPATApiRoute ( { } , async ( { authentication } ) => {
@@ -35,11 +37,6 @@ export const loader = createLoaderPATApiRoute({}, async ({ authentication }) =>
3537 return json ( result ) ;
3638} ) ;
3739
38- const CreateOrgRequestBody = z . object ( {
39- title : z . string ( ) . min ( 1 ) ,
40- companySize : z . string ( ) . optional ( ) ,
41- } ) ;
42-
4340// No org exists yet, so no authorization gate; any authenticated user can
4441// create an org and becomes its ADMIN.
4542export const action = createActionPATApiRoute (
@@ -48,10 +45,34 @@ export const action = createActionPATApiRoute(
4845 body : CreateOrgRequestBody ,
4946 } ,
5047 async ( { body, authentication } ) => {
48+ if ( env . ORG_CREATION_API_ENABLED !== "1" ) {
49+ return json ( { error : "Not found" } , { status : 404 } ) ;
50+ }
51+
52+ // Mirror the dashboard: stash companyUrl/companySize as onboarding data and
53+ // derive the org avatar from the company domain's favicon.
54+ const onboardingData : Record < string , string > = { } ;
55+ if ( body . companyUrl ) {
56+ onboardingData . companyUrl = body . companyUrl ;
57+ }
58+ if ( body . companySize ) {
59+ onboardingData . companySize = body . companySize ;
60+ }
61+
62+ let avatar : { type : "image" ; url : string } | undefined ;
63+ if ( body . companyUrl ) {
64+ const domain = extractDomain ( body . companyUrl ) ;
65+ if ( domain ) {
66+ avatar = { type : "image" , url : faviconUrl ( domain ) } ;
67+ }
68+ }
69+
5170 const organization = await createOrganization ( {
5271 title : body . title ,
5372 companySize : body . companySize ?? null ,
5473 userId : authentication . userId ,
74+ onboardingData : Object . keys ( onboardingData ) . length > 0 ? onboardingData : undefined ,
75+ avatar,
5576 } ) ;
5677
5778 return json (
0 commit comments