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.

feat: database tables from custom fields

Brad Barbin2feae4d4e70c
diff --git a/apps/carbon/app/routes/x+/quote+/$id.details.tsx b/apps/carbon/app/routes/x+/quote+/$id.details.tsx
index a71c8e585a..284deee10f 100644
--- a/apps/carbon/app/routes/x+/quote+/$id.details.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$id.details.tsx
@@ -76,7 +76,7 @@ export default function QuotationBasicRoute() {
customerContactId: quoteData?.quotation?.customerContactId ?? "",
customerLocationId: quoteData?.quotation?.customerLocationId ?? "",
customerReference: quoteData?.quotation?.customerReference ?? "",
- locationId: quoteData?.quotation?.locationId,
+ locationId: quoteData?.quotation?.locationId ?? "",
quoteDate: quoteData?.quotation?.quoteDate ?? "",
expirationDate: quoteData?.quotation.expirationDate ?? undefined,
status: quoteData?.quotation?.status ?? ("Draft" as "Draft"),
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 82068bc6a6..c1a4c6e4fb 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -2452,6 +2452,242 @@ export type Database = {
},
]
}
+ customField: {
+ Row: {
+ active: boolean | null
+ attributeDataTypeId: number
+ createdAt: string
+ createdBy: string
+ customFieldTableId: string
+ id: string
+ listOptions: string[] | null
+ name: string
+ sortOrder: number
+ updatedAt: string | null
+ updatedBy: string | null
+ }
+ Insert: {
+ active?: boolean | null
+ attributeDataTypeId: number
+ createdAt?: string
+ createdBy: string
+ customFieldTableId: string
+ id?: string
+ listOptions?: string[] | null
+ name: string
+ sortOrder?: number
+ updatedAt?: string | null
+ updatedBy?: string | null
+ }
+ Update: {
+ active?: boolean | null
+ attributeDataTypeId?: number
+ createdAt?: string
+ createdBy?: string
+ customFieldTableId?: string
+ id?: string
+ listOptions?: string[] | null
+ name?: string
+ sortOrder?: number
+ updatedAt?: string | null
+ updatedBy?: string | null
+ }
+ Relationships: [
+ {
+ foreignKeyName: "customField_attributeDataTypeId_fkey"
+ columns: ["attributeDataTypeId"]
+ isOneToOne: false
+ referencedRelation: "attributeDataType"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customField_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customField_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customField_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ {
+ foreignKeyName: "customField_customFieldTableId_fkey"
+ columns: ["customFieldTableId"]
+ isOneToOne: false
+ referencedRelation: "customFieldTable"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customField_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customField_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customField_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ ]
+ }
+ customFieldTable: {
+ Row: {
+ id: string
+ module: Database["public"]["Enums"]["module"]
+ name: string
+ table: string
+ }
+ Insert: {
+ id?: string
+ module: Database["public"]["Enums"]["module"]
+ name: string
+ table: string
+ }
+ Update: {
+ id?: string
+ module?: Database["public"]["Enums"]["module"]
+ name?: string
+ table?: string
+ }
+ Relationships: []
+ }
+ customFieldValue: {
+ Row: {
+ createdAt: string
+ createdBy: string
+ customFieldId: string
+ id: string
+ recordId: string
+ updatedAt: string | null
+ updatedBy: string | null
+ valueBoolean: boolean | null
+ valueDate: string | null
+ valueNumeric: number | null
+ valueText: string | null
+ valueUser: string | null
+ }
+ Insert: {
+ createdAt?: string
+ createdBy: string
+ customFieldId: string
+ id?: string
+ recordId: string
+ updatedAt?: string | null
+ updatedBy?: string | null
+ valueBoolean?: boolean | null
+ valueDate?: string | null
+ valueNumeric?: number | null
+ valueText?: string | null
+ valueUser?: string | null
+ }
+ Update: {
+ createdAt?: string
+ createdBy?: string
+ customFieldId?: string
+ id?: string
+ recordId?: string
+ updatedAt?: string | null
+ updatedBy?: string | null
+ valueBoolean?: boolean | null
+ valueDate?: string | null
+ valueNumeric?: number | null
+ valueText?: string | null
+ valueUser?: string | null
+ }
+ Relationships: [
+ {
+ foreignKeyName: "customFieldValue_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ {
+ foreignKeyName: "customFieldValue_customFieldId_fkey"
+ columns: ["customFieldId"]
+ isOneToOne: false
+ referencedRelation: "customField"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ {
+ foreignKeyName: "customFieldValue_valueUser_fkey"
+ columns: ["valueUser"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_valueUser_fkey"
+ columns: ["valueUser"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_valueUser_fkey"
+ columns: ["valueUser"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ ]
+ }
department: {
Row: {
color: string
@@ -13095,6 +13331,20 @@ export type Database = {
| "Credit Memo"
| "Blanket Order"
| "Return Order"
+ module:
+ | "Accounting"
+ | "Documents"
+ | "Invoicing"
+ | "Inventory"
+ | "Jobs"
+ | "Messaging"
+ | "Parts"
+ | "Purchasing"
+ | "Resources"
+ | "Sales"
+ | "Scheduling"
+ | "Timecards"
+ | "Users"
month:
| "January"
| "February"
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 82068bc6a6..c1a4c6e4fb 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -2452,6 +2452,242 @@ export type Database = {
},
]
}
+ customField: {
+ Row: {
+ active: boolean | null
+ attributeDataTypeId: number
+ createdAt: string
+ createdBy: string
+ customFieldTableId: string
+ id: string
+ listOptions: string[] | null
+ name: string
+ sortOrder: number
+ updatedAt: string | null
+ updatedBy: string | null
+ }
+ Insert: {
+ active?: boolean | null
+ attributeDataTypeId: number
+ createdAt?: string
+ createdBy: string
+ customFieldTableId: string
+ id?: string
+ listOptions?: string[] | null
+ name: string
+ sortOrder?: number
+ updatedAt?: string | null
+ updatedBy?: string | null
+ }
+ Update: {
+ active?: boolean | null
+ attributeDataTypeId?: number
+ createdAt?: string
+ createdBy?: string
+ customFieldTableId?: string
+ id?: string
+ listOptions?: string[] | null
+ name?: string
+ sortOrder?: number
+ updatedAt?: string | null
+ updatedBy?: string | null
+ }
+ Relationships: [
+ {
+ foreignKeyName: "customField_attributeDataTypeId_fkey"
+ columns: ["attributeDataTypeId"]
+ isOneToOne: false
+ referencedRelation: "attributeDataType"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customField_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customField_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customField_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ {
+ foreignKeyName: "customField_customFieldTableId_fkey"
+ columns: ["customFieldTableId"]
+ isOneToOne: false
+ referencedRelation: "customFieldTable"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customField_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customField_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customField_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ ]
+ }
+ customFieldTable: {
+ Row: {
+ id: string
+ module: Database["public"]["Enums"]["module"]
+ name: string
+ table: string
+ }
+ Insert: {
+ id?: string
+ module: Database["public"]["Enums"]["module"]
+ name: string
+ table: string
+ }
+ Update: {
+ id?: string
+ module?: Database["public"]["Enums"]["module"]
+ name?: string
+ table?: string
+ }
+ Relationships: []
+ }
+ customFieldValue: {
+ Row: {
+ createdAt: string
+ createdBy: string
+ customFieldId: string
+ id: string
+ recordId: string
+ updatedAt: string | null
+ updatedBy: string | null
+ valueBoolean: boolean | null
+ valueDate: string | null
+ valueNumeric: number | null
+ valueText: string | null
+ valueUser: string | null
+ }
+ Insert: {
+ createdAt?: string
+ createdBy: string
+ customFieldId: string
+ id?: string
+ recordId: string
+ updatedAt?: string | null
+ updatedBy?: string | null
+ valueBoolean?: boolean | null
+ valueDate?: string | null
+ valueNumeric?: number | null
+ valueText?: string | null
+ valueUser?: string | null
+ }
+ Update: {
+ createdAt?: string
+ createdBy?: string
+ customFieldId?: string
+ id?: string
+ recordId?: string
+ updatedAt?: string | null
+ updatedBy?: string | null
+ valueBoolean?: boolean | null
+ valueDate?: string | null
+ valueNumeric?: number | null
+ valueText?: string | null
+ valueUser?: string | null
+ }
+ Relationships: [
+ {
+ foreignKeyName: "customFieldValue_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ {
+ foreignKeyName: "customFieldValue_customFieldId_fkey"
+ columns: ["customFieldId"]
+ isOneToOne: false
+ referencedRelation: "customField"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ {
+ foreignKeyName: "customFieldValue_valueUser_fkey"
+ columns: ["valueUser"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_valueUser_fkey"
+ columns: ["valueUser"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "customFieldValue_valueUser_fkey"
+ columns: ["valueUser"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ ]
+ }
department: {
Row: {
color: string
@@ -13095,6 +13331,20 @@ export type Database = {
| "Credit Memo"
| "Blanket Order"
| "Return Order"
+ module:
+ | "Accounting"
+ | "Documents"
+ | "Invoicing"
+ | "Inventory"
+ | "Jobs"
+ | "Messaging"
+ | "Parts"
+ | "Purchasing"
+ | "Resources"
+ | "Sales"
+ | "Scheduling"
+ | "Timecards"
+ | "Users"
month:
| "January"
| "February"
diff --git a/packages/database/supabase/migrations/20230123004536_attributes.sql b/packages/database/supabase/migrations/20230123004536_attributes.sql
index 2c093d8fb0..b1b17d9149 100644
--- a/packages/database/supabase/migrations/20230123004536_attributes.sql
+++ b/packages/database/supabase/migrations/20230123004536_attributes.sql
@@ -168,6 +168,9 @@ CREATE TABLE "userAttributeValue" (
UNIQUE ( "userAttributeId", "userId")
);
+CREATE INDEX "userAttributeValue_userAttributeId_index" ON "userAttributeValue" ("userAttributeId");
+CREATE INDEX "userAttributeValue_userId_index" ON "userAttributeValue" ("userId");
+
ALTER TABLE "userAttributeValue" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Claims admin can view/modify user attribute values" ON "userAttributeValue" FOR ALL USING (is_claims_admin());
CREATE POLICY "Users can insert attributes for themselves" ON "userAttributeValue" FOR UPDATE WITH CHECK (auth.uid() = "userId"::uuid);
@@ -182,34 +185,3 @@ CREATE POLICY "Users can view other users attributes if the category is public"
)
)
);
-
--- CREATE TABLE employee_personal_data (
--- "id" TEXT NOT NULL DEFAULT xid(),
--- bank_account_number TEXT,
--- bank_routing_number TEXT,
--- drivers_license_number TEXT,
--- social_security TEXT,
--- user_id TEXT NOT NULL,
--- key_id uuid NOT NULL DEFAULT '7da3ce7e-98f0-4c4f-9b63-b3f783eab919'::uuid,
--- nonce BYTEA DEFAULT pgsodium.crypto_aead_det_noncegen(),
-
--- CONSTRAINT "employee_personal_data_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "user"("id") ON DELETE CASCADE
--- );
-
--- SECURITY LABEL FOR pgsodium
--- ON COLUMN employee_personal_data.bank_account
--- IS 'ENCRYPT WITH KEY COLUMN key_id ASSOCIATED (user_id) NONCE nonce';
-
--- SECURITY LABEL FOR pgsodium
--- ON COLUMN employee_personal_data.bank_routing
--- IS 'ENCRYPT WITH KEY COLUMN key_id ASSOCIATED (user_id) NONCE nonce';
-
--- SECURITY LABEL FOR pgsodium
--- ON COLUMN employee_personal_data.drivers_license
--- IS 'ENCRYPT WITH KEY COLUMN key_id ASSOCIATED (user_id) NONCE nonce';
-
--- SECURITY LABEL FOR pgsodium
--- ON COLUMN employee_personal_data.social_security
--- IS 'ENCRYPT WITH KEY COLUMN key_id ASSOCIATED (user_id) NONCE nonce';
-
--- ALTER TABLE "employee_personal_data" ENABLE ROW LEVEL SECURITY;
\ No newline at end of file
diff --git a/packages/database/supabase/migrations/20240311021818_custom-fields.sql b/packages/database/supabase/migrations/20240311021818_custom-fields.sql
new file mode 100644
index 0000000000..ad2443c23f
--- /dev/null
+++ b/packages/database/supabase/migrations/20240311021818_custom-fields.sql
@@ -0,0 +1,185 @@
+
+CREATE TYPE module AS ENUM (
+ 'Accounting',
+ 'Documents',
+ 'Invoicing',
+ 'Inventory',
+ 'Jobs',
+ 'Messaging',
+ 'Parts',
+ 'Purchasing',
+ 'Resources',
+ 'Sales',
+ 'Scheduling',
+ 'Timecards',
+ 'Users'
+);
+
+CREATE TABLE "customFieldTable" (
+ "id" TEXT NOT NULL DEFAULT xid(),
+ "module" module NOT NULL,
+ "table" TEXT NOT NULL,
+ "name" TEXT NOT NULL,
+
+ CONSTRAINT "customFieldTable_pkey" PRIMARY KEY ("id"),
+ CONSTRAINT "customFieldTable_module_table_name_key" UNIQUE ("module", "table", "name")
+);
+
+CREATE INDEX "customFieldTable_module_index" ON "customFieldTable" ("module");
+CREATE INDEX "customFieldTable_table_index" ON "customFieldTable" ("table");
+
+ALTER TABLE "customFieldTable" ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY "Employees can view custom field tables" ON "customFieldTable"
+ FOR SELECT
+ USING (
+ auth.role() = 'authenticated'
+ AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ );
+
+CREATE TABLE "customField" (
+ "id" TEXT NOT NULL DEFAULT xid(),
+ "name" TEXT NOT NULL,
+ "sortOrder" INTEGER NOT NULL DEFAULT 1,
+ "customFieldTableId" TEXT NOT NULL,
+ "attributeDataTypeId" INTEGER NOT NULL,
+ "listOptions" TEXT ARRAY,
+ "active" BOOLEAN DEFAULT TRUE,
+ "createdAt" TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
+ "createdBy" TEXT NOT NULL,
+ "updatedAt" TIMESTAMP WITH TIME ZONE,
+ "updatedBy" TEXT,
+
+ CONSTRAINT "customField_pkey" PRIMARY KEY ("id"),
+ CONSTRAINT "customField_customFieldTableId_fkey" FOREIGN KEY ("customFieldTableId") REFERENCES "customFieldTable"("id") ON DELETE CASCADE ON UPDATE CASCADE,
+ CONSTRAINT "customField_attributeDataTypeId_fkey" FOREIGN KEY ("attributeDataTypeId") REFERENCES "attributeDataType"("id") ON DELETE CASCADE ON UPDATE CASCADE,
+ CONSTRAINT "customField_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id") ON UPDATE CASCADE,
+ CONSTRAINT "customField_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id") ON UPDATE CASCADE
+);
+
+ALTER TABLE "customField" ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY "Employees can view custom fields" ON "customField"
+ FOR SELECT
+ USING (
+ auth.role() = 'authenticated'
+ AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ );
+
+CREATE POLICY "Employees with settings_create can insert custom fields" ON "customField"
+ FOR INSERT
+ WITH CHECK (
+ coalesce(get_my_claim('settings_create')::boolean,false)
+ AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ );
+
+CREATE POLICY "Employees with settings_update can update custom fields" ON "customField"
+ FOR UPDATE
+ USING (
+ coalesce(get_my_claim('settings_update')::boolean, false) = true
+ AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ );
+
+CREATE POLICY "Employees with settings_delete can delete custom fields" ON "customField"
+ FOR DELETE
+ USING (
+ coalesce(get_my_claim('settings_delete')::boolean, false) = true
+ AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ );
+
+CREATE TABLE "customFieldValue" (
+ "id" TEXT NOT NULL DEFAULT xid(),
+ "customFieldId" TEXT NOT NULL,
+ "recordId" TEXT NOT NULL,
+ "valueBoolean" BOOLEAN,
+ "valueDate" DATE,
+ "valueNumeric" NUMERIC,
+ "valueText" TEXT,
+ "valueUser" TEXT,
+ "createdAt" TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL,
+ "createdBy" TEXT NOT NULL,
+ "updatedAt" TIMESTAMP WITH TIME ZONE,
+ "updatedBy" TEXT,
+
+ CONSTRAINT "customFieldValue_singleValue"
+ CHECK (
+ (
+ "valueBoolean" IS NOT NULL AND
+ "valueDate" IS NULL AND
+ "valueNumeric" IS NULL AND
+ "valueText" IS NULL AND
+ "valueUser" IS NULL
+ )
+ OR (
+ "valueBoolean" IS NULL AND
+ "valueDate" IS NULL AND
+ "valueNumeric" IS NULL AND
+ "valueText" IS NOT NULL AND
+ "valueUser" IS NULL
+ )
+ OR (
+ "valueBoolean" IS NULL AND
+ "valueDate" IS NOT NULL AND
+ "valueNumeric" IS NULL AND
+ "valueText" IS NULL AND
+ "valueUser" IS NULL
+ )
+ OR (
+ "valueBoolean" IS NULL AND
+ "valueDate" IS NULL AND
+ "valueNumeric" IS NOT NULL AND
+ "valueText" IS NULL AND
+ "valueUser" IS NULL
+ )
+ OR (
+ "valueBoolean" IS NULL AND
+ "valueDate" IS NULL AND
+ "valueNumeric" IS NULL AND
+ "valueText" IS NULL AND
+ "valueUser" IS NOT NULL
+ )
+ ),
+
+ CONSTRAINT "customFieldValue_customFieldId_fkey" FOREIGN KEY ("customFieldId") REFERENCES "customField"("id") ON DELETE CASCADE,
+ CONSTRAINT "customFieldValue_valueUser_fkey" FOREIGN KEY ("valueUser") REFERENCES "user"("id") ON DELETE CASCADE,
+ CONSTRAINT "customFieldValue_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user"("id"),
+ CONSTRAINT "customFieldValue_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user"("id"),
+ CONSTRAINT uq_customFieldId_recordId
+ UNIQUE ( "customFieldId", "recordId")
+);
+
+CREATE INDEX "customFieldValue_customFieldId_index" ON "customFieldValue" ("customFieldId");
+CREATE INDEX "customFieldValue_recordId_index" ON "customFieldValue" ("recordId");
+
+-- TODO: fine-grained control over read/write access to custom field values based on customFieldTable module
+
+ALTER TABLE "customFieldValue" ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY "Employees can view custom field values" ON "customFieldValue"
+ FOR SELECT
+ USING (
+ auth.role() = 'authenticated'
+ AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ );
+
+CREATE POLICY "Employees can insert custom field values" ON "customFieldValue"
+ FOR INSERT
+ WITH CHECK (
+ auth.role() = 'authenticated'
+ AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ );
+
+CREATE POLICY "Employees can update custom field values" ON "customFieldValue"
+ FOR UPDATE
+ USING (
+ auth.role() = 'authenticated'
+ AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ );
+
+CREATE POLICY "Employees can delete custom field values" ON "customFieldValue"
+ FOR DELETE
+ USING (
+ auth.role() = 'authenticated'
+ AND (get_my_claim('role'::text)) = '"employee"'::jsonb
+ );
+
diff --git a/packages/database/supabase/seed.sql b/packages/database/supabase/seed.sql
index 03d08dbae4..54c0408bb5 100644
--- a/packages/database/supabase/seed.sql
+++ b/packages/database/supabase/seed.sql
@@ -453,4 +453,73 @@ INSERT INTO "fiscalYearSettings" (
'January',
'January',
'system'
-);
\ No newline at end of file
+);
+
+-- custom fields
+
+INSERT INTO "customFieldTable" ("table", "name", "module")
+VALUES
+('ability', 'Ability', 'Resources'),
+('account', 'Account', 'Accounting'),
+('accountCategory', 'Account Category', 'Accounting'),
+('accountSubcategory', 'Account Subcategory', 'Accounting'),
+('contractor', 'Contractor', 'Resources'),
+('currency', 'Currency', 'Accounting'),
+('customer', 'Customer', 'Sales'),
+('customerContact', 'Customer Contact', 'Sales'),
+('customerLocation', 'Customer Location', 'Sales'),
+('customerStatus', 'Customer Status', 'Sales'),
+('customerType', 'Customer Type', 'Sales'),
+('department', 'Department', 'Resources'),
+('employee', 'Employee', 'Resources'),
+('employeeAbility', 'Employee Ability', 'Resources'),
+('employeeJob', 'Employee Job', 'Resources'),
+('employeeShift', 'Employee Shift', 'Resources'),
+('equipment', 'Equipment', 'Resources'),
+('equipmentType', 'Equipment Type', 'Resources'),
+('holiday', 'Holiday', 'Resources'),
+('journal', 'Journal', 'Accounting'),
+('journalLine', 'Journal Line', 'Accounting'),
+('location', 'Location', 'Resources'),
+('part', 'Part', 'Parts'),
+('partCost', 'Part Cost', 'Parts'),
+('partGroup', 'Part Group', 'Parts'),
+('partInventory', 'Part Inventory', 'Parts'),
+('partPlanning', 'Part Planning', 'Parts'),
+('partReplenishment', 'Part Replenishment', 'Parts'),
+('partSupplier', 'Part Supplier', 'Parts'),
+('partUnitSalePrice', 'Part Unit Sale Price', 'Parts'),
+('partner', 'Partner', 'Resources'),
+('paymentTerm', 'Payment Term', 'Accounting'),
+('purchaseInvoice', 'Purchase Invoice', 'Invoicing'),
+('purchaseInvoiceLine', 'Purchase Invoice Line', 'Invoicing'),
+('purchaseInvoicePayment', 'Purchase Invoice Payment', 'Invoicing'),
+('purchaseOrder', 'Purchase Order', 'Purchasing'),
+('purchaseOrderDelivery', 'Purchase Order Delivery', 'Purchasing'),
+('purchaseOrderLine', 'Purchase Order Line', 'Purchasing'),
+('purchaseOrderPayment', 'Purchase Order Payment', 'Purchasing'),
+('quote', 'Quote', 'Sales'),
+('quoteAssembly', 'Quote Assembly', 'Sales'),
+('quoteLine', 'Quote Line', 'Sales'),
+('quoteLineMaterial', 'Quote Material', 'Sales'),
+('quoteOperation', 'Quote Operation', 'Sales'),
+('receipt', 'Receipt', 'Inventory'),
+('requestForQuote', 'RFQ', 'Purchasing'),
+('requestForQuoteLine', 'RFQ Line', 'Purchasing'),
+('requestForQuoteSupplier', 'RFQ Supplier', 'Purchasing'),
+('service', 'Service', 'Parts'),
+('serviceSupplier', 'Service Supplier', 'Parts'),
+('shelf', 'Shelf', 'Inventory'),
+('shift', 'Shift', 'Resources'),
+('shippingMethod', 'Shipping Method', 'Inventory'),
+-- ('shippingTerm', 'Shipping Term', 'Accounting'),
+('supplier', 'Supplier', 'Purchasing'),
+('supplierContact', 'Supplier Contact', 'Purchasing'),
+('supplierLocation', 'Supplier Location', 'Purchasing'),
+('supplierPayment', 'Supplier Payment', 'Purchasing'),
+('supplierShipping', 'Supplier Shipping', 'Purchasing'),
+('supplierStatus', 'Supplier Status', 'Purchasing'),
+('supplierType', 'Supplier Type', 'Purchasing'),
+('unitOfMeasure', 'Unit of Measure', 'Inventory'),
+('workCell', 'Work Cell', 'Resources'),
+('workCellType', 'Work Cell Type', 'Resources');