Browse Source

make update raw resource port functional

Milo Schwartz 5 months ago
parent
commit
65a537a670

+ 42 - 9
server/routers/resource/updateResource.ts

@@ -2,7 +2,7 @@ import { Request, Response, NextFunction } from "express";
 import { z } from "zod";
 import { db } from "@server/db";
 import { orgs, resources, sites } from "@server/db/schema";
-import { eq, or } from "drizzle-orm";
+import { eq, or, and } from "drizzle-orm";
 import response from "@server/lib/response";
 import HttpCode from "@server/types/HttpCode";
 import createHttpError from "http-errors";
@@ -63,13 +63,16 @@ export async function updateResource(
         const { resourceId } = parsedParams.data;
         const updateData = parsedBody.data;
 
-        const resource = await db
+        const [result] = await db
             .select()
             .from(resources)
             .where(eq(resources.resourceId, resourceId))
             .leftJoin(orgs, eq(resources.orgId, orgs.orgId));
 
-        if (resource.length === 0) {
+        const resource = result.resources;
+        const org = result.orgs;
+
+        if (!resource || !org) {
             return next(
                 createHttpError(
                     HttpCode.NOT_FOUND,
@@ -78,7 +81,41 @@ export async function updateResource(
             );
         }
 
-        if (!resource[0].orgs?.domain) {
+        if (updateData.proxyPort) {
+            const proxyPort = updateData.proxyPort;
+            const existingResource = await db
+                .select()
+                .from(resources)
+                .where(
+                    and(
+                        eq(resources.protocol, resource.protocol),
+                        eq(resources.proxyPort, proxyPort!)
+                    )
+                );
+
+            if (proxyPort === 443 || proxyPort === 80) {
+                return next(
+                    createHttpError(
+                        HttpCode.BAD_REQUEST,
+                        "Port 80 and 443 are reserved for https resources"
+                    )
+                );
+            }
+
+            if (
+                existingResource.length > 0 &&
+                existingResource[0].resourceId !== resourceId
+            ) {
+                return next(
+                    createHttpError(
+                        HttpCode.CONFLICT,
+                        "Resource with that protocol and port already exists"
+                    )
+                );
+            }
+        }
+
+        if (!org?.domain) {
             return next(
                 createHttpError(
                     HttpCode.BAD_REQUEST,
@@ -88,7 +125,7 @@ export async function updateResource(
         }
 
         const fullDomain = updateData.subdomain
-            ? `${updateData.subdomain}.${resource[0].orgs.domain}`
+            ? `${updateData.subdomain}.${org.domain}`
             : undefined;
 
         const updatePayload = {
@@ -111,10 +148,6 @@ export async function updateResource(
             );
         }
 
-        if (resource[0].resources.ssl !== updatedResource[0].ssl) {
-            // invalidate all sessions?
-        }
-
         return response(res, {
             data: updatedResource[0],
             success: true,

+ 13 - 13
src/app/[orgId]/settings/resources/[resourceId]/general/page.tsx

@@ -145,14 +145,11 @@ export default function GeneralForm() {
         setSaveLoading(true);
 
         const res = await api
-            .post<AxiosResponse<GetResourceAuthInfoResponse>>(
-                `resource/${resource?.resourceId}`,
-                {
-                    name: data.name,
-                    subdomain: data.subdomain
-                    // siteId: data.siteId,
-                }
-            )
+            .post(`resource/${resource?.resourceId}`, {
+                name: data.name,
+                subdomain: data.subdomain,
+                proxyPort: data.proxyPort
+            })
             .catch((e) => {
                 toast({
                     variant: "destructive",
@@ -170,7 +167,11 @@ export default function GeneralForm() {
                 description: "The resource has been updated successfully"
             });
 
-            updateResource({ name: data.name, subdomain: data.subdomain });
+            updateResource({
+                name: data.name,
+                subdomain: data.subdomain,
+                proxyPort: data.proxyPort
+            });
         }
         setSaveLoading(false);
     }
@@ -395,9 +396,7 @@ export default function GeneralForm() {
                                                             {sites.map(
                                                                 (site) => (
                                                                     <CommandItem
-                                                                        value={
-                                                                            `${site.name}:${site.siteId}`
-                                                                        }
+                                                                        value={`${site.name}:${site.siteId}`}
                                                                         key={
                                                                             site.siteId
                                                                         }
@@ -431,7 +430,8 @@ export default function GeneralForm() {
                                                 </PopoverContent>
                                             </Popover>
                                             <FormDescription>
-                                                Select the new site to transfer this resource to.
+                                                Select the new site to transfer
+                                                this resource to.
                                             </FormDescription>
                                             <FormMessage />
                                         </FormItem>