refactor subdomain inputs

This commit is contained in:
miloschwartz 2025-02-18 22:56:46 -05:00
parent 82f990eb8b
commit e49fb646b0
No known key found for this signature in database
8 changed files with 404 additions and 144 deletions

View file

@ -33,16 +33,17 @@ const listDomainsSchema = z
.strict();
async function queryDomains(orgId: string, limit: number, offset: number) {
return await db
const res = await db
.select({
domainId: domains.domainId,
baseDomain: domains.baseDomain
})
.from(orgDomains)
.where(eq(orgDomains.orgId, orgId))
.leftJoin(domains, eq(domains.domainId, orgDomains.domainId))
.innerJoin(domains, eq(domains.domainId, orgDomains.domainId))
.limit(limit)
.offset(offset);
return res;
}
export type ListDomainsResponse = {

View file

@ -31,7 +31,7 @@ const createResourceParamsSchema = z
const createHttpResourceSchema = z
.object({
name: z.string().min(1).max(255),
subdomain: subdomainSchema.optional(),
subdomain: z.string().optional(),
isBaseDomain: z.boolean().optional(),
siteId: z.number(),
http: z.boolean(),
@ -39,6 +39,15 @@ const createHttpResourceSchema = z
domainId: z.string()
})
.strict()
.refine(
(data) => {
if (data.subdomain) {
return subdomainSchema.safeParse(data.subdomain).success;
}
return true;
},
{ message: "Invalid subdomain" }
)
.refine(
(data) => {
if (!config.getRawConfig().flags?.allow_base_domain_resources) {
@ -199,6 +208,8 @@ async function createHttpResource(
fullDomain = `${subdomain}.${domain.baseDomain}`;
}
logger.debug(`Full domain: ${fullDomain}`);
// make sure the full domain is unique
const existingResource = await db
.select()
@ -221,7 +232,7 @@ async function createHttpResource(
.insert(resources)
.values({
siteId,
fullDomain: http ? fullDomain : null,
fullDomain,
orgId,
name,
subdomain,

View file

@ -43,6 +43,15 @@ const updateHttpResourceBodySchema = z
.refine((data) => Object.keys(data).length > 0, {
message: "At least one field must be provided for update"
})
.refine(
(data) => {
if (data.subdomain) {
return subdomainSchema.safeParse(data.subdomain).success;
}
return true;
},
{ message: "Invalid subdomain" }
)
.refine(
(data) => {
if (!config.getRawConfig().flags?.allow_base_domain_resources) {
@ -206,7 +215,7 @@ async function updateHttpResource(
if (updateData.isBaseDomain) {
fullDomain = domain.baseDomain;
} else if (subdomain && domain) {
fullDomain = `${subdomain}.${domain}`;
fullDomain = `${subdomain}.${domain.baseDomain}`;
}
if (fullDomain) {

View file

@ -69,7 +69,7 @@ export async function copyInConfig() {
if (resource.isBaseDomain) {
fullDomain = domain.baseDomain;
} else {
fullDomain = `${resource.subdomain}.${domain}`;
fullDomain = `${resource.subdomain}.${domain.baseDomain}`;
}
await trx