mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-10 22:14:58 +02:00
Merge branch 'dev' of https://github.com/fosrl/pangolin into dev
This commit is contained in:
commit
889f8e1394
11 changed files with 427 additions and 70 deletions
|
@ -308,6 +308,13 @@ authenticated.get(
|
||||||
resource.getResourceWhitelist
|
resource.getResourceWhitelist
|
||||||
);
|
);
|
||||||
|
|
||||||
|
authenticated.post(
|
||||||
|
`/resource/:resourceId/transfer`,
|
||||||
|
verifyResourceAccess,
|
||||||
|
verifyUserHasAction(ActionsEnum.updateResource),
|
||||||
|
resource.transferResource
|
||||||
|
);
|
||||||
|
|
||||||
authenticated.post(
|
authenticated.post(
|
||||||
`/resource/:resourceId/access-token`,
|
`/resource/:resourceId/access-token`,
|
||||||
verifyResourceAccess,
|
verifyResourceAccess,
|
||||||
|
|
|
@ -11,6 +11,7 @@ import config from "@server/lib/config";
|
||||||
import { getUniqueExitNodeEndpointName } from '@server/db/names';
|
import { getUniqueExitNodeEndpointName } from '@server/db/names';
|
||||||
import { findNextAvailableCidr } from "@server/lib/ip";
|
import { findNextAvailableCidr } from "@server/lib/ip";
|
||||||
import { fromError } from 'zod-validation-error';
|
import { fromError } from 'zod-validation-error';
|
||||||
|
import { getAllowedIps } from '../target/helpers';
|
||||||
// Define Zod schema for request validation
|
// Define Zod schema for request validation
|
||||||
const getConfigSchema = z.object({
|
const getConfigSchema = z.object({
|
||||||
publicKey: z.string(),
|
publicKey: z.string(),
|
||||||
|
@ -83,22 +84,9 @@ export async function getConfig(req: Request, res: Response, next: NextFunction)
|
||||||
});
|
});
|
||||||
|
|
||||||
const peers = await Promise.all(sitesRes.map(async (site) => {
|
const peers = await Promise.all(sitesRes.map(async (site) => {
|
||||||
// Fetch resources for this site
|
|
||||||
const resourcesRes = await db.query.resources.findMany({
|
|
||||||
where: eq(resources.siteId, site.siteId),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Fetch targets for all resources of this site
|
|
||||||
const targetIps = await Promise.all(resourcesRes.map(async (resource) => {
|
|
||||||
const targetsRes = await db.query.targets.findMany({
|
|
||||||
where: eq(targets.resourceId, resource.resourceId),
|
|
||||||
});
|
|
||||||
return targetsRes.map(target => `${target.ip}/32`);
|
|
||||||
}));
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
publicKey: site.pubKey,
|
publicKey: site.pubKey,
|
||||||
allowedIps: targetIps.flat(),
|
allowedIps: await getAllowedIps(site.siteId)
|
||||||
};
|
};
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import { Target } from "@server/db/schema";
|
import { Target } from "@server/db/schema";
|
||||||
import { sendToClient } from "../ws";
|
import { sendToClient } from "../ws";
|
||||||
|
|
||||||
export async function addTargets(
|
export function addTargets(
|
||||||
newtId: string,
|
newtId: string,
|
||||||
targets: Target[],
|
targets: Target[],
|
||||||
protocol: string
|
protocol: string
|
||||||
): Promise<void> {
|
) {
|
||||||
//create a list of udp and tcp targets
|
//create a list of udp and tcp targets
|
||||||
const payloadTargets = targets.map((target) => {
|
const payloadTargets = targets.map((target) => {
|
||||||
return `${target.internalPort ? target.internalPort + ":" : ""}${
|
return `${target.internalPort ? target.internalPort + ":" : ""}${
|
||||||
|
@ -22,11 +22,11 @@ export async function addTargets(
|
||||||
sendToClient(newtId, payload);
|
sendToClient(newtId, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removeTargets(
|
export function removeTargets(
|
||||||
newtId: string,
|
newtId: string,
|
||||||
targets: Target[],
|
targets: Target[],
|
||||||
protocol: string
|
protocol: string
|
||||||
): Promise<void> {
|
) {
|
||||||
//create a list of udp and tcp targets
|
//create a list of udp and tcp targets
|
||||||
const payloadTargets = targets.map((target) => {
|
const payloadTargets = targets.map((target) => {
|
||||||
return `${target.internalPort ? target.internalPort + ":" : ""}${
|
return `${target.internalPort ? target.internalPort + ":" : ""}${
|
||||||
|
|
|
@ -10,6 +10,7 @@ import logger from "@server/logger";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import { addPeer } from "../gerbil/peers";
|
import { addPeer } from "../gerbil/peers";
|
||||||
import { removeTargets } from "../newt/targets";
|
import { removeTargets } from "../newt/targets";
|
||||||
|
import { getAllowedIps } from "../target/helpers";
|
||||||
|
|
||||||
// Define Zod schema for request parameters validation
|
// Define Zod schema for request parameters validation
|
||||||
const deleteResourceSchema = z
|
const deleteResourceSchema = z
|
||||||
|
@ -75,25 +76,9 @@ export async function deleteResource(
|
||||||
|
|
||||||
if (site.pubKey) {
|
if (site.pubKey) {
|
||||||
if (site.type == "wireguard") {
|
if (site.type == "wireguard") {
|
||||||
// TODO: is this all inefficient?
|
|
||||||
// Fetch resources for this site
|
|
||||||
const resourcesRes = await db.query.resources.findMany({
|
|
||||||
where: eq(resources.siteId, site.siteId)
|
|
||||||
});
|
|
||||||
|
|
||||||
// Fetch targets for all resources of this site
|
|
||||||
const targetIps = await Promise.all(
|
|
||||||
resourcesRes.map(async (resource) => {
|
|
||||||
const targetsRes = await db.query.targets.findMany({
|
|
||||||
where: eq(targets.resourceId, resource.resourceId)
|
|
||||||
});
|
|
||||||
return targetsRes.map((target) => `${target.ip}/32`);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
await addPeer(site.exitNodeId!, {
|
await addPeer(site.exitNodeId!, {
|
||||||
publicKey: site.pubKey,
|
publicKey: site.pubKey,
|
||||||
allowedIps: targetIps.flat()
|
allowedIps: await getAllowedIps(site.siteId)
|
||||||
});
|
});
|
||||||
} else if (site.type == "newt") {
|
} else if (site.type == "newt") {
|
||||||
// get the newt on the site by querying the newt table for siteId
|
// get the newt on the site by querying the newt table for siteId
|
||||||
|
|
|
@ -16,4 +16,5 @@ export * from "./setResourceWhitelist";
|
||||||
export * from "./getResourceWhitelist";
|
export * from "./getResourceWhitelist";
|
||||||
export * from "./authWithWhitelist";
|
export * from "./authWithWhitelist";
|
||||||
export * from "./authWithAccessToken";
|
export * from "./authWithAccessToken";
|
||||||
|
export * from "./transferResource";
|
||||||
export * from "./getExchangeToken";
|
export * from "./getExchangeToken";
|
||||||
|
|
192
server/routers/resource/transferResource.ts
Normal file
192
server/routers/resource/transferResource.ts
Normal file
|
@ -0,0 +1,192 @@
|
||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { db } from "@server/db";
|
||||||
|
import { newts, resources, sites, targets } from "@server/db/schema";
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
|
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 { addPeer } from "../gerbil/peers";
|
||||||
|
import { addTargets, removeTargets } from "../newt/targets";
|
||||||
|
import { getAllowedIps } from "../target/helpers";
|
||||||
|
|
||||||
|
const transferResourceParamsSchema = z
|
||||||
|
.object({
|
||||||
|
resourceId: z
|
||||||
|
.string()
|
||||||
|
.transform(Number)
|
||||||
|
.pipe(z.number().int().positive())
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
const transferResourceBodySchema = z
|
||||||
|
.object({
|
||||||
|
siteId: z.number().int().positive()
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
export async function transferResource(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<any> {
|
||||||
|
try {
|
||||||
|
const parsedParams = transferResourceParamsSchema.safeParse(req.params);
|
||||||
|
if (!parsedParams.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromError(parsedParams.error).toString()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsedBody = transferResourceBodySchema.safeParse(req.body);
|
||||||
|
if (!parsedBody.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromError(parsedBody.error).toString()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { resourceId } = parsedParams.data;
|
||||||
|
const { siteId } = parsedBody.data;
|
||||||
|
|
||||||
|
const [oldResource] = await db
|
||||||
|
.select()
|
||||||
|
.from(resources)
|
||||||
|
.where(eq(resources.resourceId, resourceId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!oldResource) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.NOT_FOUND,
|
||||||
|
`Resource with ID ${resourceId} not found`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldResource.siteId === siteId) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
`Resource is already assigned to this site`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [newSite] = await db
|
||||||
|
.select()
|
||||||
|
.from(sites)
|
||||||
|
.where(eq(sites.siteId, siteId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!newSite) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.NOT_FOUND,
|
||||||
|
`Site with ID ${siteId} not found`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [oldSite] = await db
|
||||||
|
.select()
|
||||||
|
.from(sites)
|
||||||
|
.where(eq(sites.siteId, oldResource.siteId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!oldSite) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.NOT_FOUND,
|
||||||
|
`Site with ID ${oldResource.siteId} not found`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [updatedResource] = await db
|
||||||
|
.update(resources)
|
||||||
|
.set({ siteId })
|
||||||
|
.where(eq(resources.resourceId, resourceId))
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
if (!updatedResource) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.NOT_FOUND,
|
||||||
|
`Resource with ID ${resourceId} not found`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const resourceTargets = await db
|
||||||
|
.select()
|
||||||
|
.from(targets)
|
||||||
|
.where(eq(targets.resourceId, resourceId));
|
||||||
|
|
||||||
|
if (resourceTargets.length > 0) {
|
||||||
|
////// REMOVE THE TARGETS FROM THE OLD SITE //////
|
||||||
|
if (oldSite.pubKey) {
|
||||||
|
if (oldSite.type == "wireguard") {
|
||||||
|
await addPeer(oldSite.exitNodeId!, {
|
||||||
|
publicKey: oldSite.pubKey,
|
||||||
|
allowedIps: await getAllowedIps(oldSite.siteId)
|
||||||
|
});
|
||||||
|
} else if (oldSite.type == "newt") {
|
||||||
|
const [newt] = await db
|
||||||
|
.select()
|
||||||
|
.from(newts)
|
||||||
|
.where(eq(newts.siteId, oldSite.siteId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
removeTargets(
|
||||||
|
newt.newtId,
|
||||||
|
resourceTargets,
|
||||||
|
updatedResource.protocol
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////// ADD THE TARGETS TO THE NEW SITE //////
|
||||||
|
if (newSite.pubKey) {
|
||||||
|
if (newSite.type == "wireguard") {
|
||||||
|
await addPeer(newSite.exitNodeId!, {
|
||||||
|
publicKey: newSite.pubKey,
|
||||||
|
allowedIps: await getAllowedIps(newSite.siteId)
|
||||||
|
});
|
||||||
|
} else if (newSite.type == "newt") {
|
||||||
|
const [newt] = await db
|
||||||
|
.select()
|
||||||
|
.from(newts)
|
||||||
|
.where(eq(newts.siteId, newSite.siteId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
addTargets(
|
||||||
|
newt.newtId,
|
||||||
|
resourceTargets,
|
||||||
|
updatedResource.protocol
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response(res, {
|
||||||
|
data: updatedResource,
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "Resource transferred successfully",
|
||||||
|
status: HttpCode.OK
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error);
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,7 +11,7 @@ import { isIpInCidr } from "@server/lib/ip";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import { addTargets } from "../newt/targets";
|
import { addTargets } from "../newt/targets";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
import { pickPort } from "./ports";
|
import { pickPort } from "./helpers";
|
||||||
|
|
||||||
// Regular expressions for validation
|
// Regular expressions for validation
|
||||||
const DOMAIN_REGEX =
|
const DOMAIN_REGEX =
|
||||||
|
|
|
@ -10,6 +10,7 @@ import logger from "@server/logger";
|
||||||
import { addPeer } from "../gerbil/peers";
|
import { addPeer } from "../gerbil/peers";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import { removeTargets } from "../newt/targets";
|
import { removeTargets } from "../newt/targets";
|
||||||
|
import { getAllowedIps } from "./helpers";
|
||||||
|
|
||||||
const deleteTargetSchema = z
|
const deleteTargetSchema = z
|
||||||
.object({
|
.object({
|
||||||
|
@ -80,25 +81,9 @@ export async function deleteTarget(
|
||||||
|
|
||||||
if (site.pubKey) {
|
if (site.pubKey) {
|
||||||
if (site.type == "wireguard") {
|
if (site.type == "wireguard") {
|
||||||
// TODO: is this all inefficient?
|
|
||||||
// Fetch resources for this site
|
|
||||||
const resourcesRes = await db.query.resources.findMany({
|
|
||||||
where: eq(resources.siteId, site.siteId)
|
|
||||||
});
|
|
||||||
|
|
||||||
// Fetch targets for all resources of this site
|
|
||||||
const targetIps = await Promise.all(
|
|
||||||
resourcesRes.map(async (resource) => {
|
|
||||||
const targetsRes = await db.query.targets.findMany({
|
|
||||||
where: eq(targets.resourceId, resource.resourceId)
|
|
||||||
});
|
|
||||||
return targetsRes.map((target) => `${target.ip}/32`);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
await addPeer(site.exitNodeId!, {
|
await addPeer(site.exitNodeId!, {
|
||||||
publicKey: site.pubKey,
|
publicKey: site.pubKey,
|
||||||
allowedIps: targetIps.flat()
|
allowedIps: await getAllowedIps(site.siteId)
|
||||||
});
|
});
|
||||||
} else if (site.type == "newt") {
|
} else if (site.type == "newt") {
|
||||||
// get the newt on the site by querying the newt table for siteId
|
// get the newt on the site by querying the newt table for siteId
|
||||||
|
|
|
@ -46,3 +46,21 @@ export async function pickPort(siteId: number): Promise<{
|
||||||
|
|
||||||
return { internalPort, targetIps };
|
return { internalPort, targetIps };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getAllowedIps(siteId: number) {
|
||||||
|
// TODO: is this all inefficient?
|
||||||
|
const resourcesRes = await db.query.resources.findMany({
|
||||||
|
where: eq(resources.siteId, siteId)
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fetch targets for all resources of this site
|
||||||
|
const targetIps = await Promise.all(
|
||||||
|
resourcesRes.map(async (resource) => {
|
||||||
|
const targetsRes = await db.query.targets.findMany({
|
||||||
|
where: eq(targets.resourceId, resource.resourceId)
|
||||||
|
});
|
||||||
|
return targetsRes.map((target) => `${target.ip}/32`);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
return targetIps.flat();
|
||||||
|
}
|
|
@ -10,7 +10,7 @@ import logger from "@server/logger";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import { addPeer } from "../gerbil/peers";
|
import { addPeer } from "../gerbil/peers";
|
||||||
import { addTargets } from "../newt/targets";
|
import { addTargets } from "../newt/targets";
|
||||||
import { pickPort } from "./ports";
|
import { pickPort } from "./helpers";
|
||||||
|
|
||||||
// Regular expressions for validation
|
// Regular expressions for validation
|
||||||
const DOMAIN_REGEX =
|
const DOMAIN_REGEX =
|
||||||
|
|
|
@ -14,6 +14,19 @@ import {
|
||||||
FormMessage
|
FormMessage
|
||||||
} from "@/components/ui/form";
|
} from "@/components/ui/form";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
|
import {
|
||||||
|
Command,
|
||||||
|
CommandEmpty,
|
||||||
|
CommandGroup,
|
||||||
|
CommandInput,
|
||||||
|
CommandItem
|
||||||
|
} from "@/components/ui/command";
|
||||||
|
import { cn } from "@app/lib/cn";
|
||||||
|
import {
|
||||||
|
Popover,
|
||||||
|
PopoverContent,
|
||||||
|
PopoverTrigger
|
||||||
|
} from "@/components/ui/popover";
|
||||||
import { useResourceContext } from "@app/hooks/useResourceContext";
|
import { useResourceContext } from "@app/hooks/useResourceContext";
|
||||||
import { ListSitesResponse } from "@server/routers/site";
|
import { ListSitesResponse } from "@server/routers/site";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
@ -37,6 +50,7 @@ import CustomDomainInput from "../CustomDomainInput";
|
||||||
import { createApiClient } from "@app/lib/api";
|
import { createApiClient } from "@app/lib/api";
|
||||||
import { useEnvContext } from "@app/hooks/useEnvContext";
|
import { useEnvContext } from "@app/hooks/useEnvContext";
|
||||||
import { subdomainSchema } from "@server/schemas/subdomainSchema";
|
import { subdomainSchema } from "@server/schemas/subdomainSchema";
|
||||||
|
import { CaretSortIcon, CheckIcon } from "@radix-ui/react-icons";
|
||||||
|
|
||||||
const GeneralFormSchema = z
|
const GeneralFormSchema = z
|
||||||
.object({
|
.object({
|
||||||
|
@ -75,7 +89,12 @@ const GeneralFormSchema = z
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const TransferFormSchema = z.object({
|
||||||
|
siteId: z.number()
|
||||||
|
});
|
||||||
|
|
||||||
type GeneralFormValues = z.infer<typeof GeneralFormSchema>;
|
type GeneralFormValues = z.infer<typeof GeneralFormSchema>;
|
||||||
|
type TransferFormValues = z.infer<typeof TransferFormSchema>;
|
||||||
|
|
||||||
export default function GeneralForm() {
|
export default function GeneralForm() {
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
|
@ -91,6 +110,8 @@ export default function GeneralForm() {
|
||||||
const [sites, setSites] = useState<ListSitesResponse["sites"]>([]);
|
const [sites, setSites] = useState<ListSitesResponse["sites"]>([]);
|
||||||
const [saveLoading, setSaveLoading] = useState(false);
|
const [saveLoading, setSaveLoading] = useState(false);
|
||||||
const [domainSuffix, setDomainSuffix] = useState(org.org.domain);
|
const [domainSuffix, setDomainSuffix] = useState(org.org.domain);
|
||||||
|
const [transferLoading, setTransferLoading] = useState(false);
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
|
||||||
const form = useForm<GeneralFormValues>({
|
const form = useForm<GeneralFormValues>({
|
||||||
resolver: zodResolver(GeneralFormSchema),
|
resolver: zodResolver(GeneralFormSchema),
|
||||||
|
@ -103,6 +124,13 @@ export default function GeneralForm() {
|
||||||
mode: "onChange"
|
mode: "onChange"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const transferForm = useForm<TransferFormValues>({
|
||||||
|
resolver: zodResolver(TransferFormSchema),
|
||||||
|
defaultValues: {
|
||||||
|
siteId: resource.siteId ? Number(resource.siteId) : undefined
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchSites = async () => {
|
const fetchSites = async () => {
|
||||||
const res = await api.get<AxiosResponse<ListSitesResponse>>(
|
const res = await api.get<AxiosResponse<ListSitesResponse>>(
|
||||||
|
@ -116,14 +144,15 @@ export default function GeneralForm() {
|
||||||
async function onSubmit(data: GeneralFormValues) {
|
async function onSubmit(data: GeneralFormValues) {
|
||||||
setSaveLoading(true);
|
setSaveLoading(true);
|
||||||
|
|
||||||
api.post<AxiosResponse<GetResourceAuthInfoResponse>>(
|
const res = await api
|
||||||
`resource/${resource?.resourceId}`,
|
.post<AxiosResponse<GetResourceAuthInfoResponse>>(
|
||||||
{
|
`resource/${resource?.resourceId}`,
|
||||||
name: data.name,
|
{
|
||||||
subdomain: data.subdomain
|
name: data.name,
|
||||||
// siteId: data.siteId,
|
subdomain: data.subdomain
|
||||||
}
|
// siteId: data.siteId,
|
||||||
)
|
}
|
||||||
|
)
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
toast({
|
toast({
|
||||||
variant: "destructive",
|
variant: "destructive",
|
||||||
|
@ -133,18 +162,45 @@ export default function GeneralForm() {
|
||||||
"An error occurred while updating the resource"
|
"An error occurred while updating the resource"
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res && res.status === 200) {
|
||||||
|
toast({
|
||||||
|
title: "Resource updated",
|
||||||
|
description: "The resource has been updated successfully"
|
||||||
|
});
|
||||||
|
|
||||||
|
updateResource({ name: data.name, subdomain: data.subdomain });
|
||||||
|
}
|
||||||
|
setSaveLoading(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function onTransfer(data: TransferFormValues) {
|
||||||
|
setTransferLoading(true);
|
||||||
|
|
||||||
|
const res = await api
|
||||||
|
.post(`resource/${resource?.resourceId}/transfer`, {
|
||||||
|
siteId: data.siteId
|
||||||
})
|
})
|
||||||
.then(() => {
|
.catch((e) => {
|
||||||
toast({
|
toast({
|
||||||
title: "Resource updated",
|
variant: "destructive",
|
||||||
description: "The resource has been updated successfully"
|
title: "Failed to transfer resource",
|
||||||
|
description: formatAxiosError(
|
||||||
|
e,
|
||||||
|
"An error occurred while transferring the resource"
|
||||||
|
)
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
updateResource({ name: data.name, subdomain: data.subdomain });
|
if (res && res.status === 200) {
|
||||||
|
toast({
|
||||||
router.refresh();
|
title: "Resource transferred",
|
||||||
})
|
description: "The resource has been transferred successfully"
|
||||||
.finally(() => setSaveLoading(false));
|
});
|
||||||
|
router.refresh();
|
||||||
|
}
|
||||||
|
setTransferLoading(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -273,6 +329,131 @@ export default function GeneralForm() {
|
||||||
</Button>
|
</Button>
|
||||||
</SettingsSectionFooter>
|
</SettingsSectionFooter>
|
||||||
</SettingsSection>
|
</SettingsSection>
|
||||||
|
|
||||||
|
<SettingsSection>
|
||||||
|
<SettingsSectionHeader>
|
||||||
|
<SettingsSectionTitle>
|
||||||
|
Transfer Resource
|
||||||
|
</SettingsSectionTitle>
|
||||||
|
<SettingsSectionDescription>
|
||||||
|
Transfer this resource to a different site
|
||||||
|
</SettingsSectionDescription>
|
||||||
|
</SettingsSectionHeader>
|
||||||
|
|
||||||
|
<SettingsSectionBody>
|
||||||
|
<SettingsSectionForm>
|
||||||
|
<Form {...transferForm}>
|
||||||
|
<form
|
||||||
|
onSubmit={transferForm.handleSubmit(onTransfer)}
|
||||||
|
className="space-y-4"
|
||||||
|
id="transfer-form"
|
||||||
|
>
|
||||||
|
<FormField
|
||||||
|
control={transferForm.control}
|
||||||
|
name="siteId"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem className="flex flex-col">
|
||||||
|
<FormLabel>
|
||||||
|
Destination Site
|
||||||
|
</FormLabel>
|
||||||
|
<Popover
|
||||||
|
open={open}
|
||||||
|
onOpenChange={setOpen}
|
||||||
|
>
|
||||||
|
<PopoverTrigger asChild>
|
||||||
|
<FormControl>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
role="combobox"
|
||||||
|
className={cn(
|
||||||
|
"w-full justify-between",
|
||||||
|
!field.value &&
|
||||||
|
"text-muted-foreground"
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{field.value
|
||||||
|
? sites.find(
|
||||||
|
(site) =>
|
||||||
|
site.siteId ===
|
||||||
|
field.value
|
||||||
|
)?.name
|
||||||
|
: "Select site"}
|
||||||
|
<CaretSortIcon className="ml-2 h-4 w-4 shrink-0 opacity-50" />
|
||||||
|
</Button>
|
||||||
|
</FormControl>
|
||||||
|
</PopoverTrigger>
|
||||||
|
<PopoverContent className="w-full p-0">
|
||||||
|
<Command>
|
||||||
|
<CommandInput
|
||||||
|
placeholder="Search sites..."
|
||||||
|
className="h-9"
|
||||||
|
/>
|
||||||
|
<CommandEmpty>
|
||||||
|
No sites found.
|
||||||
|
</CommandEmpty>
|
||||||
|
<CommandGroup>
|
||||||
|
{sites.map(
|
||||||
|
(site) => (
|
||||||
|
<CommandItem
|
||||||
|
value={
|
||||||
|
`${site.name}:${site.siteId}`
|
||||||
|
}
|
||||||
|
key={
|
||||||
|
site.siteId
|
||||||
|
}
|
||||||
|
onSelect={() => {
|
||||||
|
transferForm.setValue(
|
||||||
|
"siteId",
|
||||||
|
site.siteId
|
||||||
|
);
|
||||||
|
setOpen(
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{
|
||||||
|
site.name
|
||||||
|
}
|
||||||
|
<CheckIcon
|
||||||
|
className={cn(
|
||||||
|
"ml-auto h-4 w-4",
|
||||||
|
site.siteId ===
|
||||||
|
field.value
|
||||||
|
? "opacity-100"
|
||||||
|
: "opacity-0"
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</CommandItem>
|
||||||
|
)
|
||||||
|
)}
|
||||||
|
</CommandGroup>
|
||||||
|
</Command>
|
||||||
|
</PopoverContent>
|
||||||
|
</Popover>
|
||||||
|
<FormDescription>
|
||||||
|
Select the new site to transfer this resource to.
|
||||||
|
</FormDescription>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
|
</SettingsSectionForm>
|
||||||
|
</SettingsSectionBody>
|
||||||
|
|
||||||
|
<SettingsSectionFooter>
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
loading={transferLoading}
|
||||||
|
disabled={transferLoading}
|
||||||
|
form="transfer-form"
|
||||||
|
variant="destructive"
|
||||||
|
>
|
||||||
|
Transfer Resource
|
||||||
|
</Button>
|
||||||
|
</SettingsSectionFooter>
|
||||||
|
</SettingsSection>
|
||||||
</SettingsContainer>
|
</SettingsContainer>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue