mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-04 09:46:40 +00:00
add update domain Settings for wildcard
This commit is contained in:
@@ -1906,5 +1906,8 @@
|
|||||||
"TTL": "TTL",
|
"TTL": "TTL",
|
||||||
"howToAddRecords": "How to Add Records",
|
"howToAddRecords": "How to Add Records",
|
||||||
"dnsRecord": "DNS Records",
|
"dnsRecord": "DNS Records",
|
||||||
"required": "Required"
|
"required": "Required",
|
||||||
|
"domainSettingsUpdated": "Domain settings updated successfully",
|
||||||
|
"orgOrDomainIdMissing": "Organization or Domain ID is missing",
|
||||||
|
"loadingDNSRecords": "Loading DNS records..."
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,6 +82,7 @@ export enum ActionsEnum {
|
|||||||
getClient = "getClient",
|
getClient = "getClient",
|
||||||
listOrgDomains = "listOrgDomains",
|
listOrgDomains = "listOrgDomains",
|
||||||
getDomain = "getDomain",
|
getDomain = "getDomain",
|
||||||
|
updateOrgDomain = "updateOrgDomain",
|
||||||
getDNSRecords = "getDNSRecords",
|
getDNSRecords = "getDNSRecords",
|
||||||
createNewt = "createNewt",
|
createNewt = "createNewt",
|
||||||
createIdp = "createIdp",
|
createIdp = "createIdp",
|
||||||
|
|||||||
@@ -4,3 +4,4 @@ export * from "./deleteOrgDomain";
|
|||||||
export * from "./restartOrgDomain";
|
export * from "./restartOrgDomain";
|
||||||
export * from "./getDomain";
|
export * from "./getDomain";
|
||||||
export * from "./getDNSRecords";
|
export * from "./getDNSRecords";
|
||||||
|
export * from "./updateDomain";
|
||||||
161
server/routers/domain/updateDomain.ts
Normal file
161
server/routers/domain/updateDomain.ts
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { db, domains, orgDomains } from "@server/db";
|
||||||
|
import response from "@server/lib/response";
|
||||||
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
import createHttpError from "http-errors";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
import { fromError } from "zod-validation-error";
|
||||||
|
import { eq, and } from "drizzle-orm";
|
||||||
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
|
|
||||||
|
const paramsSchema = z
|
||||||
|
.object({
|
||||||
|
orgId: z.string(),
|
||||||
|
domainId: z.string()
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
const bodySchema = z
|
||||||
|
.object({
|
||||||
|
certResolver: z.string().optional().nullable(),
|
||||||
|
preferWildcardCert: z.boolean().optional().nullable()
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
export type UpdateDomainResponse = {
|
||||||
|
domainId: string;
|
||||||
|
certResolver: string | null;
|
||||||
|
preferWildcardCert: boolean | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
registry.registerPath({
|
||||||
|
method: "patch",
|
||||||
|
path: "/org/{orgId}/domain/{domainId}",
|
||||||
|
description: "Update a domain by domainId.",
|
||||||
|
tags: [OpenAPITags.Domain],
|
||||||
|
request: {
|
||||||
|
params: z.object({
|
||||||
|
domainId: z.string(),
|
||||||
|
orgId: z.string()
|
||||||
|
})
|
||||||
|
},
|
||||||
|
responses: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function updateOrgDomain(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<any> {
|
||||||
|
try {
|
||||||
|
const parsedParams = paramsSchema.safeParse(req.params);
|
||||||
|
if (!parsedParams.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromError(parsedParams.error).toString()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsedBody = bodySchema.safeParse(req.body);
|
||||||
|
if (!parsedBody.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromError(parsedBody.error).toString()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { orgId, domainId } = parsedParams.data;
|
||||||
|
const { certResolver, preferWildcardCert } = parsedBody.data;
|
||||||
|
|
||||||
|
const [orgDomain] = await db
|
||||||
|
.select()
|
||||||
|
.from(orgDomains)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(orgDomains.orgId, orgId),
|
||||||
|
eq(orgDomains.domainId, domainId)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!orgDomain) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.NOT_FOUND,
|
||||||
|
"Domain not found or does not belong to this organization"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const [existingDomain] = await db
|
||||||
|
.select()
|
||||||
|
.from(domains)
|
||||||
|
.where(eq(domains.domainId, domainId));
|
||||||
|
|
||||||
|
if (!existingDomain) {
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.NOT_FOUND, "Domain not found")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existingDomain.type !== "wildcard") {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
"Domain settings can only be updated for wildcard domains"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateData: Partial<{
|
||||||
|
certResolver: string | null;
|
||||||
|
preferWildcardCert: boolean;
|
||||||
|
}> = {};
|
||||||
|
|
||||||
|
if (certResolver !== undefined) {
|
||||||
|
updateData.certResolver = certResolver;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preferWildcardCert !== undefined && preferWildcardCert !== null) {
|
||||||
|
updateData.preferWildcardCert = preferWildcardCert;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [updatedDomain] = await db
|
||||||
|
.update(domains)
|
||||||
|
.set(updateData)
|
||||||
|
.where(eq(domains.domainId, domainId))
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
if (!updatedDomain) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||||||
|
"Failed to update domain"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response<UpdateDomainResponse>(res, {
|
||||||
|
data: {
|
||||||
|
domainId: updatedDomain.domainId,
|
||||||
|
certResolver: updatedDomain.certResolver,
|
||||||
|
preferWildcardCert: updatedDomain.preferWildcardCert
|
||||||
|
},
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "Domain updated successfully",
|
||||||
|
status: HttpCode.OK
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error);
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -309,6 +309,13 @@ authenticated.get(
|
|||||||
domain.getDomain
|
domain.getDomain
|
||||||
);
|
);
|
||||||
|
|
||||||
|
authenticated.patch(
|
||||||
|
"/org/:orgId/domain/:domainId",
|
||||||
|
verifyOrgAccess,
|
||||||
|
verifyUserHasAction(ActionsEnum.updateOrgDomain),
|
||||||
|
domain.updateOrgDomain
|
||||||
|
)
|
||||||
|
|
||||||
authenticated.get(
|
authenticated.get(
|
||||||
"/org/:orgId/domain/:domainId/dns-records",
|
"/org/:orgId/domain/:domainId/dns-records",
|
||||||
verifyOrgAccess,
|
verifyOrgAccess,
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ export function DNSRecordsDataTable<TData, TValue>({
|
|||||||
<Table>
|
<Table>
|
||||||
<TableHeader>
|
<TableHeader>
|
||||||
{table.getHeaderGroups().map((headerGroup) => (
|
{table.getHeaderGroups().map((headerGroup) => (
|
||||||
<TableRow key={headerGroup.id} className="bg-secondary">
|
<TableRow key={headerGroup.id} className="bg-secondary dark:bg-transparent">
|
||||||
{headerGroup.headers.map((header) => (
|
{headerGroup.headers.map((header) => (
|
||||||
<TableHead key={header.id}>
|
<TableHead key={header.id}>
|
||||||
{header.isPlaceholder
|
{header.isPlaceholder
|
||||||
@@ -165,7 +165,7 @@ export function DNSRecordsDataTable<TData, TValue>({
|
|||||||
colSpan={columns.length}
|
colSpan={columns.length}
|
||||||
className="h-24 text-center"
|
className="h-24 text-center"
|
||||||
>
|
>
|
||||||
No results found.
|
{t("noResults")}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ export default function DomainInfoCard({ orgId, domainId }: DomainInfoCardProps)
|
|||||||
const [dnsRecords, setDnsRecords] = useState<DNSRecordRow[]>([]);
|
const [dnsRecords, setDnsRecords] = useState<DNSRecordRow[]>([]);
|
||||||
const [loadingRecords, setLoadingRecords] = useState(true);
|
const [loadingRecords, setLoadingRecords] = useState(true);
|
||||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
const [isRefreshing, setIsRefreshing] = useState(false);
|
||||||
|
const [saveLoading, setSaveLoading] = useState(false);
|
||||||
|
|
||||||
const form = useForm<FormValues>({
|
const form = useForm<FormValues>({
|
||||||
resolver: zodResolver(formSchema),
|
resolver: zodResolver(formSchema),
|
||||||
@@ -116,6 +117,21 @@ export default function DomainInfoCard({ orgId, domainId }: DomainInfoCardProps)
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (domain.domainId) {
|
||||||
|
const certResolverValue = domain.certResolver && domain.certResolver.trim() !== ""
|
||||||
|
? domain.certResolver
|
||||||
|
: null;
|
||||||
|
|
||||||
|
form.reset({
|
||||||
|
baseDomain: domain.baseDomain || "",
|
||||||
|
type: (domain.type as "ns" | "cname" | "wildcard") || "wildcard",
|
||||||
|
certResolver: certResolverValue,
|
||||||
|
preferWildcardCert: domain.preferWildcardCert || false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [domain]);
|
||||||
|
|
||||||
const fetchDNSRecords = async (showRefreshing = false) => {
|
const fetchDNSRecords = async (showRefreshing = false) => {
|
||||||
if (showRefreshing) {
|
if (showRefreshing) {
|
||||||
setIsRefreshing(true);
|
setIsRefreshing(true);
|
||||||
@@ -150,6 +166,49 @@ export default function DomainInfoCard({ orgId, domainId }: DomainInfoCardProps)
|
|||||||
}
|
}
|
||||||
}, [domain.domainId]);
|
}, [domain.domainId]);
|
||||||
|
|
||||||
|
const onSubmit = async (values: FormValues) => {
|
||||||
|
if (!orgId || !domainId) {
|
||||||
|
toast({
|
||||||
|
title: t("error"),
|
||||||
|
description: t("orgOrDomainIdMissing", { fallback: "Organization or Domain ID is missing" }),
|
||||||
|
variant: "destructive"
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setSaveLoading(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await api.patch(
|
||||||
|
`/org/${orgId}/domain/${domainId}`,
|
||||||
|
{
|
||||||
|
certResolver: values.certResolver,
|
||||||
|
preferWildcardCert: values.preferWildcardCert
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
updateDomain({
|
||||||
|
...domain,
|
||||||
|
certResolver: values.certResolver || null,
|
||||||
|
preferWildcardCert: values.preferWildcardCert || false
|
||||||
|
});
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: t("success"),
|
||||||
|
description: t("domainSettingsUpdated", { fallback: "Domain settings updated successfully" }),
|
||||||
|
variant: "default"
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
toast({
|
||||||
|
title: t("error"),
|
||||||
|
description: formatAxiosError(error),
|
||||||
|
variant: "destructive"
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
setSaveLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const getTypeDisplay = (type: string) => {
|
const getTypeDisplay = (type: string) => {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "ns":
|
case "ns":
|
||||||
@@ -198,128 +257,128 @@ export default function DomainInfoCard({ orgId, domainId }: DomainInfoCardProps)
|
|||||||
</AlertDescription>
|
</AlertDescription>
|
||||||
</Alert>
|
</Alert>
|
||||||
|
|
||||||
{loadingRecords ? (
|
{domain.type !== "wildcard" && (
|
||||||
<div className="space-y-4">
|
loadingRecords ? (
|
||||||
loading...
|
<div className="space-y-4">
|
||||||
</div>
|
{t("loadingDNSRecords", { fallback: "Loading DNS Records..." })}
|
||||||
) : (
|
</div>
|
||||||
<DNSRecordsTable
|
) : (
|
||||||
domainId={domain.domainId}
|
<DNSRecordsTable
|
||||||
records={dnsRecords}
|
domainId={domain.domainId}
|
||||||
isRefreshing={isRefreshing}
|
records={dnsRecords}
|
||||||
/>
|
isRefreshing={isRefreshing}
|
||||||
|
/>
|
||||||
|
)
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Domain Settings */}
|
{/* Domain Settings - Only show for wildcard domains */}
|
||||||
{/* Add condition later to only show when domain is wildcard */}
|
{domain.type === "wildcard" && (
|
||||||
<SettingsContainer>
|
<SettingsContainer>
|
||||||
<SettingsSection>
|
<SettingsSection>
|
||||||
<SettingsSectionHeader>
|
<SettingsSectionHeader>
|
||||||
<SettingsSectionTitle>
|
<SettingsSectionTitle>
|
||||||
{t("domainSetting")}
|
{t("domainSetting")}
|
||||||
</SettingsSectionTitle>
|
</SettingsSectionTitle>
|
||||||
</SettingsSectionHeader>
|
</SettingsSectionHeader>
|
||||||
|
|
||||||
<SettingsSectionBody>
|
<SettingsSectionBody>
|
||||||
<SettingsSectionForm>
|
<SettingsSectionForm>
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form
|
<form
|
||||||
//onSubmit={form.handleSubmit(onSubmit)}
|
onSubmit={form.handleSubmit(onSubmit)}
|
||||||
className="space-y-4"
|
className="space-y-4"
|
||||||
id="create-domain-form"
|
id="domain-settings-form"
|
||||||
>
|
>
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="certResolver"
|
name="certResolver"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>{t("certResolver")}</FormLabel>
|
<FormLabel>{t("certResolver")}</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Select
|
<Select
|
||||||
value={
|
value={
|
||||||
field.value === null ? "default" :
|
field.value === null ? "default" :
|
||||||
(field.value === "" || (field.value && field.value !== "default")) ? "custom" :
|
(field.value === "" || (field.value && field.value !== "default")) ? "custom" :
|
||||||
"default"
|
"default"
|
||||||
}
|
|
||||||
onValueChange={(val) => {
|
|
||||||
if (val === "default") {
|
|
||||||
field.onChange(null);
|
|
||||||
} else if (val === "custom") {
|
|
||||||
field.onChange("");
|
|
||||||
} else {
|
|
||||||
field.onChange(val);
|
|
||||||
}
|
}
|
||||||
}}
|
onValueChange={(val) => {
|
||||||
>
|
if (val === "default") {
|
||||||
<SelectTrigger>
|
field.onChange(null);
|
||||||
<SelectValue placeholder={t("selectCertResolver")} />
|
} else if (val === "custom") {
|
||||||
</SelectTrigger>
|
field.onChange("");
|
||||||
<SelectContent>
|
} else {
|
||||||
{certResolverOptions.map((opt) => (
|
field.onChange(val);
|
||||||
<SelectItem key={opt.id} value={opt.id}>
|
}
|
||||||
{opt.title}
|
}}
|
||||||
</SelectItem>
|
>
|
||||||
))}
|
<SelectTrigger>
|
||||||
</SelectContent>
|
<SelectValue placeholder={t("selectCertResolver")} />
|
||||||
</Select>
|
</SelectTrigger>
|
||||||
</FormControl>
|
<SelectContent>
|
||||||
<FormMessage />
|
{certResolverOptions.map((opt) => (
|
||||||
{field.value !== null && field.value !== "default" && (
|
<SelectItem key={opt.id} value={opt.id}>
|
||||||
<div className="space-y-2 mt-2">
|
{opt.title}
|
||||||
<FormControl>
|
</SelectItem>
|
||||||
<Input
|
))}
|
||||||
placeholder={t("enterCustomResolver")}
|
</SelectContent>
|
||||||
value={field.value || ""}
|
</Select>
|
||||||
onChange={(e) => field.onChange(e.target.value)}
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
{field.value !== null && field.value !== "default" && (
|
||||||
|
<div className="space-y-2 mt-2">
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
placeholder={t("enterCustomResolver")}
|
||||||
|
value={field.value || domain.certResolver || ""}
|
||||||
|
onChange={(e) => field.onChange(e.target.value)}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="preferWildcardCert"
|
||||||
|
render={({ field: switchField }) => (
|
||||||
|
<FormItem className="items-center space-y-2 mt-4">
|
||||||
|
<FormControl>
|
||||||
|
<div className="flex items-center space-x-2">
|
||||||
|
<Switch
|
||||||
|
checked={switchField.value}
|
||||||
|
onCheckedChange={switchField.onChange}
|
||||||
|
/>
|
||||||
|
<FormLabel>{t("preferWildcardCert")}</FormLabel>
|
||||||
|
</div>
|
||||||
|
</FormControl>
|
||||||
|
|
||||||
|
<FormDescription>
|
||||||
|
{t("preferWildcardCertDescription")}
|
||||||
|
</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</div>
|
||||||
<FormField
|
)}
|
||||||
control={form.control}
|
</FormItem>
|
||||||
name="preferWildcardCert"
|
)}
|
||||||
render={({ field: switchField }) => (
|
/>
|
||||||
<FormItem className="items-center space-y-2 mt-4">
|
</form>
|
||||||
<FormControl>
|
</Form>
|
||||||
<div className="flex items-center space-x-2">
|
</SettingsSectionForm>
|
||||||
<Switch
|
</SettingsSectionBody>
|
||||||
defaultChecked={switchField.value}
|
|
||||||
onCheckedChange={switchField.onChange}
|
|
||||||
/>
|
|
||||||
<FormLabel>{t("preferWildcardCert")}</FormLabel>
|
|
||||||
</div>
|
|
||||||
</FormControl>
|
|
||||||
|
|
||||||
<FormDescription>
|
<SettingsSectionFooter>
|
||||||
{t("preferWildcardCertDescription")}
|
<Button
|
||||||
</FormDescription>
|
type="submit"
|
||||||
<FormMessage />
|
loading={saveLoading}
|
||||||
</FormItem>
|
disabled={saveLoading}
|
||||||
)}
|
form="domain-settings-form"
|
||||||
/>
|
>
|
||||||
</div>
|
{t("saveSettings")}
|
||||||
)}
|
</Button>
|
||||||
</FormItem>
|
</SettingsSectionFooter>
|
||||||
)}
|
</SettingsSection>
|
||||||
/>
|
</SettingsContainer>
|
||||||
</form>
|
)}
|
||||||
</Form>
|
|
||||||
</SettingsSectionForm>
|
|
||||||
</SettingsSectionBody>
|
|
||||||
|
|
||||||
<SettingsSectionFooter>
|
|
||||||
<Button
|
|
||||||
type="submit"
|
|
||||||
// onClick={() => {
|
|
||||||
// console.log(form.getValues());
|
|
||||||
// }}
|
|
||||||
// loading={saveLoading}
|
|
||||||
// disabled={saveLoading}
|
|
||||||
form="general-settings-form"
|
|
||||||
>
|
|
||||||
{t("saveSettings")}
|
|
||||||
</Button>
|
|
||||||
</SettingsSectionFooter>
|
|
||||||
</SettingsSection>
|
|
||||||
</SettingsContainer >
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user