Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

refactor: don't store work center in cookie

Brad Barbin991c0e4a4285
diff --git a/apps/mes/app/routes/x+/_layout.tsx b/apps/mes/app/routes/x+/_layout.tsx
index 6d458f144a..97d48cc107 100644
--- a/apps/mes/app/routes/x+/_layout.tsx
+++ b/apps/mes/app/routes/x+/_layout.tsx
@@ -11,10 +11,7 @@ import { json, redirect } from "@vercel/remix";
import NProgress from "nprogress";
import { useEffect } from "react";
import { AppSidebar } from "~/components";
-import {
- getLocationAndWorkCenter,
- setLocationAndWorkCenter,
-} from "~/services/location.server";
+import { getLocation, setLocation } from "~/services/location.server";
import {
getActiveJobCount,
getLocationsByCompany,
@@ -58,7 +55,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
}
let [storedLocations, locations, activeEvents] = await Promise.all([
- getLocationAndWorkCenter(request, client, {
+ getLocation(request, client, {
companyId,
userId,
}),
@@ -90,11 +87,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
storedLocations.updated
? {
headers: {
- "Set-Cookie": setLocationAndWorkCenter(
- companyId,
- storedLocations.location,
- storedLocations.workCenter
- ),
+ "Set-Cookie": setLocation(companyId, storedLocations.location),
},
}
: undefined
diff --git a/apps/mes/app/routes/x+/company.switch.$companyId.tsx b/apps/mes/app/routes/x+/company.switch.$companyId.tsx
index ad26bd83d8..21015377ae 100644
--- a/apps/mes/app/routes/x+/company.switch.$companyId.tsx
+++ b/apps/mes/app/routes/x+/company.switch.$companyId.tsx
@@ -6,10 +6,7 @@ import {
updateCompanySession,
} from "@carbon/auth/session.server";
import { redirect, type ActionFunctionArgs } from "@vercel/remix";
-import {
- getLocationAndWorkCenter,
- setLocationAndWorkCenter,
-} from "~/services/location.server";
+import { getLocation, setLocation } from "~/services/location.server";
import { path, requestReferrer } from "~/utils/path";
export async function action({ request, params }: ActionFunctionArgs) {
@@ -36,16 +33,15 @@ export async function action({ request, params }: ActionFunctionArgs) {
}
const sessionCookie = await updateCompanySession(request, companyId!);
- const storedLocations = await getLocationAndWorkCenter(request, client, {
+ const storedLocations = await getLocation(request, client, {
userId,
companyId: companyId!,
});
if (storedLocations.updated) {
- const workCenterLocationCookie = await setLocationAndWorkCenter(
+ const workCenterLocationCookie = await setLocation(
companyId!,
- storedLocations.location,
- storedLocations.workCenter
+ storedLocations.location
);
throw redirect(path.to.authenticatedRoot, {
diff --git a/apps/mes/app/routes/x+/location.tsx b/apps/mes/app/routes/x+/location.tsx
index abd291a211..f4c13c09fc 100644
--- a/apps/mes/app/routes/x+/location.tsx
+++ b/apps/mes/app/routes/x+/location.tsx
@@ -2,13 +2,12 @@ import { assertIsPost } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import type { ActionFunctionArgs } from "@vercel/remix";
import { redirect } from "@vercel/remix";
-import { setLocationAndWorkCenter } from "~/services/location.server";
-import { getWorkCentersByLocation } from "~/services/operations.service";
+import { setLocation } from "~/services/location.server";
import { path } from "~/utils/path";
export async function action({ request }: ActionFunctionArgs) {
assertIsPost(request);
- const { client, companyId } = await requirePermissions(request, {});
+ const { companyId } = await requirePermissions(request, {});
const formData = await request.formData();
const currentLocation = formData.get("location");
@@ -16,16 +15,9 @@ export async function action({ request }: ActionFunctionArgs) {
return null;
}
- const workCenters = await getWorkCentersByLocation(client, currentLocation);
- const workCenter = workCenters?.data?.[0]?.id ?? "";
-
throw redirect(path.to.authenticatedRoot, {
headers: {
- "Set-Cookie": setLocationAndWorkCenter(
- companyId,
- currentLocation,
- workCenter
- ),
+ "Set-Cookie": setLocation(companyId, currentLocation),
},
});
}
diff --git a/apps/mes/app/routes/x+/operations.tsx b/apps/mes/app/routes/x+/operations.tsx
index e11c9f7c83..d24be50234 100644
--- a/apps/mes/app/routes/x+/operations.tsx
+++ b/apps/mes/app/routes/x+/operations.tsx
@@ -19,7 +19,7 @@ import type { Column, Item } from "~/components/Kanban";
import { Kanban } from "~/components/Kanban";
import SearchFilter from "~/components/SearchFilter";
import { useUrlParams } from "~/hooks/useUrlParams";
-import { getLocationAndWorkCenter } from "~/services/location.server";
+import { getLocation } from "~/services/location.server";
import { getFilters, setFilters } from "~/services/operation.server";
import {
getActiveJobOperationsByLocation,
@@ -94,7 +94,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
}
}
- const { location } = await getLocationAndWorkCenter(request, serviceRole, {
+ const { location } = await getLocation(request, serviceRole, {
companyId,
userId,
});
diff --git a/apps/mes/app/services/location.server.ts b/apps/mes/app/services/location.server.ts
index 1d0e058153..8e2da126b9 100644
--- a/apps/mes/app/services/location.server.ts
+++ b/apps/mes/app/services/location.server.ts
@@ -3,36 +3,25 @@ import type { Database } from "@carbon/database";
import type { SupabaseClient } from "@supabase/supabase-js";
import * as cookie from "cookie";
-export function getCompanySettings(
- request: Request,
- companyId: string
-):
- | { location: string; workCenter: string }
- | { location: undefined; workCenter: undefined } {
+export function getCompanySettings(request: Request, companyId: string) {
const cookieHeader = request.headers.get("cookie");
const parsed = cookieHeader ? cookie.parse(cookieHeader)[companyId] : null;
if (parsed) {
- const [location, workCenter] = parsed.split(":");
- return { location, workCenter };
+ return { location: parsed };
}
return {
location: undefined,
- workCenter: undefined,
};
}
-export function setLocationAndWorkCenter(
- companyId: string,
- currentLocation: string,
- workCenter: string
-) {
- return cookie.serialize(companyId, `${currentLocation}:${workCenter}`, {
+export function setLocation(companyId: string, locationId: string) {
+ return cookie.serialize(companyId, locationId, {
path: "/",
maxAge: 31536000,
});
}
-export async function getLocationAndWorkCenter(
+export async function getLocation(
request: Request,
client: SupabaseClient<Database>,
args: {
@@ -41,7 +30,7 @@ export async function getLocationAndWorkCenter(
}
) {
const { userId, companyId } = args;
- let { location, workCenter } = getCompanySettings(request, companyId);
+ let { location } = getCompanySettings(request, companyId);
let updated = false;
@@ -73,25 +62,9 @@ export async function getLocationAndWorkCenter(
"Failed to get a valid location. Please add one in the resources module."
);
- if (!workCenter) {
- const workCenters = await client
- .from("workCenter")
- .select("id")
- .eq("locationId", location);
-
- if (workCenters.data && workCenters.data.length > 0) {
- workCenter = workCenters.data[0].id;
- updated = true;
- }
- }
-
- if (!workCenter)
- throw notFound(
- "Failed to get a valid work center. Please add one in the resources module."
- );
if (updated) {
- setLocationAndWorkCenter(companyId, location, workCenter);
+ setLocation(companyId, location);
}
- return { location, workCenter, updated };
+ return { location, updated };
}