diff --git a/server/routers/resource/createResource.ts b/server/routers/resource/createResource.ts index 94bd3d10..1c4ace3b 100644 --- a/server/routers/resource/createResource.ts +++ b/server/routers/resource/createResource.ts @@ -35,8 +35,8 @@ const createHttpResourceSchema = z name: z.string().min(1).max(255), subdomain: z .string() - .optional() - .transform((val) => val?.toLowerCase()), + .nullable() + .optional(), siteId: z.number(), http: z.boolean(), protocol: z.enum(["tcp", "udp"]), @@ -201,7 +201,8 @@ async function createHttpResource( ); } - const { name, subdomain, domainId } = parsedBody.data; + const { name, domainId } = parsedBody.data; + let subdomain = parsedBody.data.subdomain; const [domainRes] = await db .select() @@ -274,6 +275,10 @@ async function createHttpResource( } } + if (fullDomain === domainRes.domains.baseDomain) { + subdomain = null; + } + fullDomain = fullDomain.toLowerCase(); logger.debug(`Full domain: ${fullDomain}`); diff --git a/server/routers/resource/updateResource.ts b/server/routers/resource/updateResource.ts index 9f623702..fda24f47 100644 --- a/server/routers/resource/updateResource.ts +++ b/server/routers/resource/updateResource.ts @@ -35,8 +35,8 @@ const updateHttpResourceBodySchema = z .object({ name: z.string().min(1).max(255).optional(), subdomain: subdomainSchema - .optional() - .transform((val) => val?.toLowerCase()), + .nullable() + .optional(), ssl: z.boolean().optional(), sso: z.boolean().optional(), blockAccess: z.boolean().optional(), @@ -286,7 +286,7 @@ async function updateHttpResource( } else if (domainRes.domains.type == "cname") { fullDomain = domainRes.domains.baseDomain; } else if (domainRes.domains.type == "wildcard") { - if (updateData.subdomain) { + if (updateData.subdomain !== undefined) { // the subdomain cant have a dot in it const parsedSubdomain = subdomainSchema.safeParse(updateData.subdomain); if (!parsedSubdomain.success) { @@ -341,11 +341,15 @@ async function updateHttpResource( .set({ fullDomain }) .where(eq(resources.resourceId, resource.resourceId)); } + + if (fullDomain === domainRes.domains.baseDomain) { + updateData.subdomain = null; + } } const updatedResource = await db .update(resources) - .set(updateData) + .set({...updateData, }) .where(eq(resources.resourceId, resource.resourceId)) .returning(); diff --git a/src/app/[orgId]/settings/resources/[resourceId]/general/page.tsx b/src/app/[orgId]/settings/resources/[resourceId]/general/page.tsx index c8ed5521..13cb20ba 100644 --- a/src/app/[orgId]/settings/resources/[resourceId]/general/page.tsx +++ b/src/app/[orgId]/settings/resources/[resourceId]/general/page.tsx @@ -118,13 +118,12 @@ export default function GeneralForm() { fullDomain: string; } | null>(null); - const GeneralFormSchema = z - .object({ - enabled: z.boolean(), - subdomain: z.string().optional(), - name: z.string().min(1).max(255), - domainId: z.string().optional() - }); + const GeneralFormSchema = z.object({ + enabled: z.boolean(), + subdomain: z.string().optional(), + name: z.string().min(1).max(255), + domainId: z.string().optional() + }); type GeneralFormValues = z.infer; @@ -197,7 +196,7 @@ export default function GeneralForm() { enabled: data.enabled, name: data.name, subdomain: data.subdomain, - domainId: data.domainId, + domainId: data.domainId } ) .catch((e) => { @@ -299,11 +298,9 @@ export default function GeneralForm() { defaultChecked={ resource.enabled } - label={ - t( - "resourceEnable" - ) - } + label={t( + "resourceEnable" + )} onCheckedChange={( val ) => @@ -341,7 +338,7 @@ export default function GeneralForm() {
- + {resourceFullDomain} - +