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.

Fix: Add system type colum to employee types

Gaurav Bhatt4e743c573218
diff --git a/apps/erp/app/modules/settings/settings.service.ts b/apps/erp/app/modules/settings/settings.service.ts
index 4e7b7bef6b..bb26e0bc78 100644
--- a/apps/erp/app/modules/settings/settings.service.ts
+++ b/apps/erp/app/modules/settings/settings.service.ts
@@ -723,13 +723,18 @@ export async function updateConsoleSetting(
.from("employeeType")
.select("id")
.eq("companyId", companyId)
- .eq("name", "Console Operator")
+ .eq("systemType", "Console Operator")
.maybeSingle();
if (!existing.data) {
const newType = await client
.from("employeeType")
- .insert({ name: "Console Operator", companyId, protected: false })
+ .insert({
+ name: "Console Operator",
+ companyId,
+ protected: true,
+ systemType: "Console Operator"
+ })
.select("id")
.single();
diff --git a/apps/erp/app/modules/users/users.service.ts b/apps/erp/app/modules/users/users.service.ts
index 182e8156e1..2f01eb1cb6 100644
--- a/apps/erp/app/modules/users/users.service.ts
+++ b/apps/erp/app/modules/users/users.service.ts
@@ -11,7 +11,11 @@ export async function deleteEmployeeType(
client: SupabaseClient<Database>,
employeeTypeId: string
) {
- return client.from("employeeType").delete().eq("id", employeeTypeId);
+ return client
+ .from("employeeType")
+ .delete()
+ .eq("id", employeeTypeId)
+ .eq("protected", false);
}
export async function deleteGroup(
diff --git a/apps/erp/app/routes/x+/users+/operators.$operatorId.tsx b/apps/erp/app/routes/x+/users+/operators.$operatorId.tsx
index de5437aa0a..b5188063f0 100644
--- a/apps/erp/app/routes/x+/users+/operators.$operatorId.tsx
+++ b/apps/erp/app/routes/x+/users+/operators.$operatorId.tsx
@@ -101,7 +101,7 @@ export default function ConvertOperatorRoute() {
const employeeTypeOptions =
employeeTypeFetcher.data?.data
- ?.filter((et) => et.name !== "Console Operator")
+ ?.filter((et) => et.systemType !== "Console Operator")
.map((et) => ({
value: et.id,
label: et.name
diff --git a/apps/erp/app/routes/x+/users+/operators.new.tsx b/apps/erp/app/routes/x+/users+/operators.new.tsx
index 558283adf9..ba59e81bf7 100644
--- a/apps/erp/app/routes/x+/users+/operators.new.tsx
+++ b/apps/erp/app/routes/x+/users+/operators.new.tsx
@@ -54,13 +54,13 @@ export async function action({ request }: ActionFunctionArgs) {
const { firstName, lastName, locationId, pin } = validation.data;
- // Auto-assign "Console Operator" employee type
+ // Auto-assign Console Operator employee type
const serviceRole = getCarbonServiceRole();
const operatorType = await serviceRole
.from("employeeType")
.select("id")
.eq("companyId", companyId)
- .eq("name", "Console Operator")
+ .eq("systemType", "Console Operator")
.single();
if (operatorType.error || !operatorType.data) {
diff --git a/apps/mes/app/routes/x+/console.add-operator.tsx b/apps/mes/app/routes/x+/console.add-operator.tsx
index 5dff40f4ba..272321b379 100644
--- a/apps/mes/app/routes/x+/console.add-operator.tsx
+++ b/apps/mes/app/routes/x+/console.add-operator.tsx
@@ -46,12 +46,12 @@ export async function action({ request, context }: ActionFunctionArgs) {
const operatorId = crypto.randomUUID();
const syntheticEmail = `${operatorId}@console.internal`;
- // Get the "Console Operator" employee type (created by migration)
+ // Get the Console Operator employee type
const operatorType = await serviceRole
.from("employeeType")
.select("id")
.eq("companyId", companyId)
- .eq("name", "Console Operator")
+ .eq("systemType", "Console Operator")
.single();
if (operatorType.error || !operatorType.data) {
diff --git a/packages/database/src/seed-dev.ts b/packages/database/src/seed-dev.ts
index c330b2461b..7fa6475fa5 100644
--- a/packages/database/src/seed-dev.ts
+++ b/packages/database/src/seed-dev.ts
@@ -223,7 +223,7 @@ async function seedDev() {
// Create Admin employee type
const employeeTypeResult = await client.query(
- `INSERT INTO "employeeType" (name, "companyId", protected) VALUES ('Admin', $1, true) RETURNING id`,
+ `INSERT INTO "employeeType" (name, "companyId", protected, "systemType") VALUES ('Admin', $1, true, 'Admin') RETURNING id`,
[companyId]
);
const employeeTypeId = employeeTypeResult.rows[0].id;
diff --git a/packages/database/src/swagger-docs-schema.ts b/packages/database/src/swagger-docs-schema.ts
index e9a4e159a7..2f235d1d9f 100644
--- a/packages/database/src/swagger-docs-schema.ts
+++ b/packages/database/src/swagger-docs-schema.ts
@@ -48486,6 +48486,9 @@ export default {
{
$ref: "#/parameters/rowFilter.employeeType.updatedAt",
},
+ {
+ $ref: "#/parameters/rowFilter.employeeType.systemType",
+ },
{
$ref: "#/parameters/select",
},
@@ -48563,6 +48566,9 @@ export default {
{
$ref: "#/parameters/rowFilter.employeeType.updatedAt",
},
+ {
+ $ref: "#/parameters/rowFilter.employeeType.systemType",
+ },
{
$ref: "#/parameters/preferReturn",
},
@@ -48594,6 +48600,9 @@ export default {
{
$ref: "#/parameters/rowFilter.employeeType.updatedAt",
},
+ {
+ $ref: "#/parameters/rowFilter.employeeType.systemType",
+ },
{
$ref: "#/parameters/body.employeeType",
},
@@ -92871,6 +92880,11 @@ export default {
format: "timestamp with time zone",
type: "string",
},
+ systemType: {
+ enum: ["Admin", "Console Operator"],
+ format: 'public."employeeTypeSystemType"',
+ type: "string",
+ },
},
type: "object",
},
@@ -126593,6 +126607,12 @@ export default {
in: "query",
type: "string",
},
+ "rowFilter.employeeType.systemType": {
+ name: "systemType",
+ required: false,
+ in: "query",
+ type: "string",
+ },
"body.shipmentLine": {
name: "shipmentLine",
description: "shipmentLine",
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 8caa5568da..b1fca44ad2 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -7237,6 +7237,9 @@ export type Database = {
id: string
name: string
protected: boolean
+ systemType:
+ | Database["public"]["Enums"]["employeeTypeSystemType"]
+ | null
updatedAt: string | null
}
Insert: {
@@ -7245,6 +7248,9 @@ export type Database = {
id?: string
name: string
protected?: boolean
+ systemType?:
+ | Database["public"]["Enums"]["employeeTypeSystemType"]
+ | null
updatedAt?: string | null
}
Update: {
@@ -7253,6 +7259,9 @@ export type Database = {
id?: string
name?: string
protected?: boolean
+ systemType?:
+ | Database["public"]["Enums"]["employeeTypeSystemType"]
+ | null
updatedAt?: string | null
}
Relationships: [
@@ -58065,6 +58074,7 @@ export type Database = {
| "Audio"
| "Other"
| "Model"
+ employeeTypeSystemType: "Admin" | "Console Operator"
externalLinkDocumentType:
| "Quote"
| "SupplierQuote"
@@ -59193,6 +59203,7 @@ export const Constants = {
"Other",
"Model",
],
+ employeeTypeSystemType: ["Admin", "Console Operator"],
externalLinkDocumentType: [
"Quote",
"SupplierQuote",
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 8caa5568da..b1fca44ad2 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -7237,6 +7237,9 @@ export type Database = {
id: string
name: string
protected: boolean
+ systemType:
+ | Database["public"]["Enums"]["employeeTypeSystemType"]
+ | null
updatedAt: string | null
}
Insert: {
@@ -7245,6 +7248,9 @@ export type Database = {
id?: string
name: string
protected?: boolean
+ systemType?:
+ | Database["public"]["Enums"]["employeeTypeSystemType"]
+ | null
updatedAt?: string | null
}
Update: {
@@ -7253,6 +7259,9 @@ export type Database = {
id?: string
name?: string
protected?: boolean
+ systemType?:
+ | Database["public"]["Enums"]["employeeTypeSystemType"]
+ | null
updatedAt?: string | null
}
Relationships: [
@@ -58065,6 +58074,7 @@ export type Database = {
| "Audio"
| "Other"
| "Model"
+ employeeTypeSystemType: "Admin" | "Console Operator"
externalLinkDocumentType:
| "Quote"
| "SupplierQuote"
@@ -59193,6 +59203,7 @@ export const Constants = {
"Other",
"Model",
],
+ employeeTypeSystemType: ["Admin", "Console Operator"],
externalLinkDocumentType: [
"Quote",
"SupplierQuote",
diff --git a/packages/database/supabase/functions/seed-company/index.ts b/packages/database/supabase/functions/seed-company/index.ts
index 62795c498c..a1af817095 100644
--- a/packages/database/supabase/functions/seed-company/index.ts
+++ b/packages/database/supabase/functions/seed-company/index.ts
@@ -101,6 +101,7 @@ serve(async (req: Request) => {
name: "Admin",
companyId,
protected: true,
+ systemType: "Admin" as const,
},
])
.returning(["id"])
diff --git a/packages/database/supabase/migrations/20260401000000_protect-console-operator-type.sql b/packages/database/supabase/migrations/20260401000000_protect-console-operator-type.sql
new file mode 100644
index 0000000000..c84c4f3c36
--- /dev/null
+++ b/packages/database/supabase/migrations/20260401000000_protect-console-operator-type.sql
@@ -0,0 +1,22 @@
+-- Add a stable systemType column to employeeType so system-managed types
+-- (Admin, Console Operator) are identified by enum value, not display name.
+-- Admins can freely rename the display name without breaking lookups.
+
+-- 1. Create enum
+CREATE TYPE "employeeTypeSystemType" AS ENUM ('Admin', 'Console Operator');
+
+-- 2. Add nullable column
+ALTER TABLE "employeeType"
+ ADD COLUMN "systemType" "employeeTypeSystemType";
+
+-- 3. Backfill existing seeded records
+UPDATE "employeeType" SET "systemType" = 'Admin'
+ WHERE name = 'Admin' AND protected = true;
+
+UPDATE "employeeType" SET "systemType" = 'Console Operator'
+ WHERE name = 'Console Operator' AND protected = true;
+
+-- 4. Partial unique index — each company gets at most one of each system type
+CREATE UNIQUE INDEX "employeeType_companyId_systemType_unique"
+ ON "employeeType" ("companyId", "systemType")
+ WHERE "systemType" IS NOT NULL;