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: Update opportunities to handle deleted customers

Bakuto398d0809df90
diff --git a/packages/database/src/swagger-docs-schema.ts b/packages/database/src/swagger-docs-schema.ts
index 7caaabad51..5b600a7db7 100644
--- a/packages/database/src/swagger-docs-schema.ts
+++ b/packages/database/src/swagger-docs-schema.ts
@@ -56679,6 +56679,102 @@ export default {
tags: ["gauge"],
},
},
+ "/riskRegisters": {
+ get: {
+ parameters: [
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.id",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.companyId",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.title",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.description",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.source",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.sourceId",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.severity",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.likelihood",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.itemId",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.status",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.assignee",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.createdBy",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.createdAt",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.updatedAt",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.notes",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.type",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.workCenterName",
+ },
+ {
+ $ref: "#/parameters/rowFilter.riskRegisters.workCenterId",
+ },
+ {
+ $ref: "#/parameters/select",
+ },
+ {
+ $ref: "#/parameters/order",
+ },
+ {
+ $ref: "#/parameters/range",
+ },
+ {
+ $ref: "#/parameters/rangeUnit",
+ },
+ {
+ $ref: "#/parameters/offset",
+ },
+ {
+ $ref: "#/parameters/limit",
+ },
+ {
+ $ref: "#/parameters/preferCount",
+ },
+ ],
+ responses: {
+ "200": {
+ description: "OK",
+ schema: {
+ items: {
+ $ref: "#/definitions/riskRegisters",
+ },
+ type: "array",
+ },
+ },
+ "206": {
+ description: "Partial Content",
+ },
+ },
+ tags: ["riskRegisters"],
+ },
+ },
"/trainingQuestion": {
get: {
parameters: [
@@ -72433,7 +72529,6 @@ export default {
"revisionId",
"status",
"currencyCode",
- "customerId",
"companyId",
"createdAt",
"createdBy",
@@ -91755,6 +91850,103 @@ export default {
},
type: "object",
},
+ riskRegisters: {
+ properties: {
+ id: {
+ description: "Note:\nThis is a Primary Key.<pk/>",
+ format: "uuid",
+ type: "string",
+ },
+ companyId: {
+ description:
+ "Note:\nThis is a Foreign Key to `company.id`.<fk table='company' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ title: {
+ format: "text",
+ type: "string",
+ },
+ description: {
+ format: "text",
+ type: "string",
+ },
+ source: {
+ enum: [
+ "Customer",
+ "General",
+ "Item",
+ "Job",
+ "Quote Line",
+ "Supplier",
+ "Work Center",
+ ],
+ format: 'public."riskSource"',
+ type: "string",
+ },
+ sourceId: {
+ format: "text",
+ type: "string",
+ },
+ severity: {
+ format: "integer",
+ type: "integer",
+ },
+ likelihood: {
+ format: "integer",
+ type: "integer",
+ },
+ itemId: {
+ description:
+ "Note:\nThis is a Foreign Key to `item.id`.<fk table='item' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ status: {
+ enum: ["Open", "In Review", "Mitigating", "Closed", "Accepted"],
+ format: 'public."riskStatus"',
+ type: "string",
+ },
+ assignee: {
+ description:
+ "Note:\nThis is a Foreign Key to `user.id`.<fk table='user' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ createdBy: {
+ description:
+ "Note:\nThis is a Foreign Key to `user.id`.<fk table='user' column='id'/>",
+ format: "text",
+ type: "string",
+ },
+ createdAt: {
+ format: "timestamp with time zone",
+ type: "string",
+ },
+ updatedAt: {
+ format: "timestamp with time zone",
+ type: "string",
+ },
+ notes: {
+ format: "json",
+ },
+ type: {
+ enum: ["Risk", "Opportunity"],
+ format: 'public."riskRegisterType"',
+ type: "string",
+ },
+ workCenterName: {
+ format: "text",
+ type: "string",
+ },
+ workCenterId: {
+ description: "Note:\nThis is a Primary Key.<pk/>",
+ format: "text",
+ type: "string",
+ },
+ },
+ type: "object",
+ },
trainingQuestion: {
required: [
"id",
@@ -92237,7 +92429,7 @@ export default {
type: "object",
},
opportunity: {
- required: ["id", "companyId", "customerId"],
+ required: ["id", "companyId"],
properties: {
id: {
default: "public.id('opp'::text)",
@@ -92472,7 +92664,6 @@ export default {
"quoteId",
"revisionId",
"status",
- "customerId",
"companyId",
"createdAt",
"createdBy",
@@ -124029,6 +124220,123 @@ export default {
in: "query",
type: "string",
},
+ "body.riskRegisters": {
+ name: "riskRegisters",
+ description: "riskRegisters",
+ required: false,
+ in: "body",
+ schema: {
+ $ref: "#/definitions/riskRegisters",
+ },
+ },
+ "rowFilter.riskRegisters.id": {
+ name: "id",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.companyId": {
+ name: "companyId",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.title": {
+ name: "title",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.description": {
+ name: "description",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.source": {
+ name: "source",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.sourceId": {
+ name: "sourceId",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.severity": {
+ name: "severity",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.likelihood": {
+ name: "likelihood",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.itemId": {
+ name: "itemId",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.status": {
+ name: "status",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.assignee": {
+ name: "assignee",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.createdBy": {
+ name: "createdBy",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.createdAt": {
+ name: "createdAt",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.updatedAt": {
+ name: "updatedAt",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.notes": {
+ name: "notes",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.type": {
+ name: "type",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.workCenterName": {
+ name: "workCenterName",
+ required: false,
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.riskRegisters.workCenterId": {
+ name: "workCenterId",
+ required: false,
+ in: "query",
+ type: "string",
+ },
"body.trainingQuestion": {
name: "trainingQuestion",
description: "trainingQuestion",
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index f3f3f762a8..969c8f332f 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -19891,21 +19891,21 @@ export type Database = {
opportunity: {
Row: {
companyId: string
- customerId: string
+ customerId: string | null
id: string
purchaseOrderDocumentPath: string | null
requestForQuoteDocumentPath: string | null
}
Insert: {
companyId: string
- customerId: string
+ customerId?: string | null
id?: string
purchaseOrderDocumentPath?: string | null
requestForQuoteDocumentPath?: string | null
}
Update: {
companyId?: string
- customerId?: string
+ customerId?: string | null
id?: string
purchaseOrderDocumentPath?: string | null
requestForQuoteDocumentPath?: string | null
@@ -25292,7 +25292,7 @@ export type Database = {
currencyCode: string | null
customerContactId: string | null
customerEngineeringContactId: string | null
- customerId: string
+ customerId: string | null
customerLocationId: string | null
customerReference: string | null
customFields: Json | null
@@ -25328,7 +25328,7 @@ export type Database = {
currencyCode?: string | null
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId: string
+ customerId?: string | null
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -25364,7 +25364,7 @@ export type Database = {
currencyCode?: string | null
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId?: string
+ customerId?: string | null
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -29345,7 +29345,7 @@ export type Database = {
currencyCode: string
customerContactId: string | null
customerEngineeringContactId: string | null
- customerId: string
+ customerId: string | null
customerLocationId: string | null
customerReference: string | null
customFields: Json | null
@@ -29377,7 +29377,7 @@ export type Database = {
currencyCode: string
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId: string
+ customerId?: string | null
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -29409,7 +29409,7 @@ export type Database = {
currencyCode?: string
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId?: string
+ customerId?: string | null
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -45373,14 +45373,14 @@ export type Database = {
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["id"]
+ columns: ["supplierLocationId"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["supplierLocationId"]
+ columns: ["id"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
@@ -46753,14 +46753,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["supplierCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["supplierCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index f3f3f762a8..969c8f332f 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -19891,21 +19891,21 @@ export type Database = {
opportunity: {
Row: {
companyId: string
- customerId: string
+ customerId: string | null
id: string
purchaseOrderDocumentPath: string | null
requestForQuoteDocumentPath: string | null
}
Insert: {
companyId: string
- customerId: string
+ customerId?: string | null
id?: string
purchaseOrderDocumentPath?: string | null
requestForQuoteDocumentPath?: string | null
}
Update: {
companyId?: string
- customerId?: string
+ customerId?: string | null
id?: string
purchaseOrderDocumentPath?: string | null
requestForQuoteDocumentPath?: string | null
@@ -25292,7 +25292,7 @@ export type Database = {
currencyCode: string | null
customerContactId: string | null
customerEngineeringContactId: string | null
- customerId: string
+ customerId: string | null
customerLocationId: string | null
customerReference: string | null
customFields: Json | null
@@ -25328,7 +25328,7 @@ export type Database = {
currencyCode?: string | null
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId: string
+ customerId?: string | null
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -25364,7 +25364,7 @@ export type Database = {
currencyCode?: string | null
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId?: string
+ customerId?: string | null
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -29345,7 +29345,7 @@ export type Database = {
currencyCode: string
customerContactId: string | null
customerEngineeringContactId: string | null
- customerId: string
+ customerId: string | null
customerLocationId: string | null
customerReference: string | null
customFields: Json | null
@@ -29377,7 +29377,7 @@ export type Database = {
currencyCode: string
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId: string
+ customerId?: string | null
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -29409,7 +29409,7 @@ export type Database = {
currencyCode?: string
customerContactId?: string | null
customerEngineeringContactId?: string | null
- customerId?: string
+ customerId?: string | null
customerLocationId?: string | null
customerReference?: string | null
customFields?: Json | null
@@ -45373,14 +45373,14 @@ export type Database = {
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["id"]
+ columns: ["supplierLocationId"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["supplierLocationId"]
+ columns: ["id"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
@@ -46753,14 +46753,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["supplierCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["supplierCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
diff --git a/packages/database/supabase/migrations/20260106120000_opportunity-customer-set-null.sql b/packages/database/supabase/migrations/20260106120000_opportunity-customer-set-null.sql
new file mode 100644
index 0000000000..f7189eef9c
--- /dev/null
+++ b/packages/database/supabase/migrations/20260106120000_opportunity-customer-set-null.sql
@@ -0,0 +1,12 @@
+-- Make the customerId column nullable
+ALTER TABLE "opportunity" ALTER COLUMN "customerId" DROP NOT NULL;
+ALTER TABLE "quote" ALTER COLUMN "customerId" DROP NOT NULL;
+ALTER TABLE "salesOrder" ALTER COLUMN "customerId" DROP NOT NULL;
+
+-- Drop the existing foreign key constraint (currently ON DELETE CASCADE)
+ALTER TABLE "opportunity" DROP CONSTRAINT IF EXISTS "opportunity_customerId_fkey";
+
+
+-- Add the foreign key constraint with ON DELETE SET NULL
+ALTER TABLE "opportunity" ADD CONSTRAINT "opportunity_customerId_fkey"
+ FOREIGN KEY ("customerId") REFERENCES "customer" ("id") ON DELETE SET NULL ON UPDATE CASCADE;