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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix: same readableId across shared companies
Brad Barbin44d1eefc78d7
diff --git a/packages/database/src/swagger-docs-schema.ts b/packages/database/src/swagger-docs-schema.tsindex 9c2bf470e8..22672cd3c9 100644--- a/packages/database/src/swagger-docs-schema.ts+++ b/packages/database/src/swagger-docs-schema.ts@@ -51177,6 +51177,12 @@ export default {{$ref: "#/parameters/rowFilter.companySettings.shelfLabelSize",},+ {+ $ref: "#/parameters/rowFilter.companySettings.inventoryJobCompletedNotificationGroup",+ },+ {+ $ref: "#/parameters/rowFilter.companySettings.salesJobCompletedNotificationGroup",+ },{$ref: "#/parameters/select",},@@ -51257,6 +51263,12 @@ export default {{$ref: "#/parameters/rowFilter.companySettings.shelfLabelSize",},+ {+ $ref: "#/parameters/rowFilter.companySettings.inventoryJobCompletedNotificationGroup",+ },+ {+ $ref: "#/parameters/rowFilter.companySettings.salesJobCompletedNotificationGroup",+ },{$ref: "#/parameters/preferReturn",},@@ -51291,6 +51303,12 @@ export default {{$ref: "#/parameters/rowFilter.companySettings.shelfLabelSize",},+ {+ $ref: "#/parameters/rowFilter.companySettings.inventoryJobCompletedNotificationGroup",+ },+ {+ $ref: "#/parameters/rowFilter.companySettings.salesJobCompletedNotificationGroup",+ },{$ref: "#/parameters/body.companySettings",},@@ -55289,6 +55307,63 @@ export default {tags: ["(rpc) increment_webhook_success"],},},+ "/rpc/is_last_job_operation": {+ get: {+ parameters: [+ {+ format: "text",+ in: "query",+ name: "operation_id",+ required: true,+ type: "string",+ },+ ],+ produces: [+ "application/json",+ "application/vnd.pgrst.object+json;nulls=stripped",+ "application/vnd.pgrst.object+json",+ ],+ responses: {+ "200": {+ description: "OK",+ },+ },+ tags: ["(rpc) is_last_job_operation"],+ },+ post: {+ parameters: [+ {+ in: "body",+ name: "args",+ required: true,+ schema: {+ properties: {+ operation_id: {+ format: "text",+ type: "string",+ },+ },+ required: ["operation_id"],+ type: "object",+ },+ },+ {+ $ref: "#/parameters/preferParams",+ },+ ],+ produces: [+ "application/json",+ "application/vnd.pgrst.object+json;nulls=stripped",+ "application/vnd.pgrst.object+json",+ ],+ responses: {+ "200": {+ description: "OK",+ },+ },+ tags: ["(rpc) is_last_job_operation"],+ },+ },"/rpc/get_next_sequence": {get: {parameters: [@@ -80565,6 +80640,8 @@ export default {"digitalQuoteNotificationGroup","digitalQuoteIncludesPurchaseOrders","rfqReadyNotificationGroup",+ "inventoryJobCompletedNotificationGroup",+ "salesJobCompletedNotificationGroup",],properties: {id: {@@ -80607,6 +80684,20 @@ export default {format: "text",type: "string",},+ inventoryJobCompletedNotificationGroup: {+ format: "text[]",+ items: {+ type: "string",+ },+ type: "array",+ },+ salesJobCompletedNotificationGroup: {+ format: "text[]",+ items: {+ type: "string",+ },+ type: "array",+ },},type: "object",},@@ -108123,6 +108214,18 @@ export default {in: "query",type: "string",},+ "rowFilter.companySettings.inventoryJobCompletedNotificationGroup": {+ name: "inventoryJobCompletedNotificationGroup",+ required: false,+ in: "query",+ type: "string",+ },+ "rowFilter.companySettings.salesJobCompletedNotificationGroup": {+ name: "salesJobCompletedNotificationGroup",+ required: false,+ in: "query",+ type: "string",+ },"body.part": {name: "part",description: "part",diff --git a/packages/database/src/types.ts b/packages/database/src/types.tsindex b1b9f4bf91..d65f2c85ab 100644--- a/packages/database/src/types.ts+++ b/packages/database/src/types.ts@@ -2093,7 +2093,6 @@ export type Database = {digitalQuoteNotificationGroup: string[]id: stringinventoryJobCompletedNotificationGroup: string[]- jobCompletedNotificationGroup: string[]productLabelSize: string | nullrfqReadyNotificationGroup: string[]salesJobCompletedNotificationGroup: string[]@@ -2105,7 +2104,6 @@ export type Database = {digitalQuoteNotificationGroup?: string[]id: stringinventoryJobCompletedNotificationGroup?: string[]- jobCompletedNotificationGroup?: string[]productLabelSize?: string | nullrfqReadyNotificationGroup?: string[]salesJobCompletedNotificationGroup?: string[]@@ -2117,7 +2115,6 @@ export type Database = {digitalQuoteNotificationGroup?: string[]id?: stringinventoryJobCompletedNotificationGroup?: string[]- jobCompletedNotificationGroup?: string[]productLabelSize?: string | nullrfqReadyNotificationGroup?: string[]salesJobCompletedNotificationGroup?: string[]@@ -8215,17 +8212,20 @@ export type Database = {createdBy: stringcritical: booleancustomFields: Json | null+ demandAccumulationIncludesInventory: booleandemandAccumulationPeriod: number- demandAccumulationSafetyStock: number+ demandReschedulingPeriod: numberitemId: stringlocationId: string- maximumInventoryQuantity: numbermaximumOrderQuantity: numberminimumOrderQuantity: numberorderMultiple: numberreorderingPolicy: Database["public"]["Enums"]["itemReorderingPolicy"]+ reorderMaximumInventory: numberreorderPoint: numberreorderQuantity: number+ safetyStockLeadTime: number+ safetyStockQuantity: numbertags: string[] | nullupdatedAt: string | nullupdatedBy: string | null@@ -8236,17 +8236,20 @@ export type Database = {createdBy: stringcritical?: booleancustomFields?: Json | null+ demandAccumulationIncludesInventory?: booleandemandAccumulationPeriod?: number- demandAccumulationSafetyStock?: number+ demandReschedulingPeriod?: numberitemId: stringlocationId: string- maximumInventoryQuantity?: numbermaximumOrderQuantity?: numberminimumOrderQuantity?: numberorderMultiple?: numberreorderingPolicy?: Database["public"]["Enums"]["itemReorderingPolicy"]+ reorderMaximumInventory?: numberreorderPoint?: numberreorderQuantity?: number+ safetyStockLeadTime?: number+ safetyStockQuantity?: numbertags?: string[] | nullupdatedAt?: string | nullupdatedBy?: string | null@@ -8257,17 +8260,20 @@ export type Database = {createdBy?: stringcritical?: booleancustomFields?: Json | null+ demandAccumulationIncludesInventory?: booleandemandAccumulationPeriod?: number- demandAccumulationSafetyStock?: number+ demandReschedulingPeriod?: numberitemId?: stringlocationId?: string- maximumInventoryQuantity?: numbermaximumOrderQuantity?: numberminimumOrderQuantity?: numberorderMultiple?: numberreorderingPolicy?: Database["public"]["Enums"]["itemReorderingPolicy"]+ reorderMaximumInventory?: numberreorderPoint?: numberreorderQuantity?: number+ safetyStockLeadTime?: number+ safetyStockQuantity?: numbertags?: string[] | nullupdatedAt?: string | nullupdatedBy?: string | null@@ -37586,11 +37592,8 @@ export type Database = {| Database["public"]["Enums"]["itemTrackingType"]| nulljobId: string | null- jobMakeMethodId: string | null- jobReadableId: string | nullleadTime: number | nulllocationId: string | null- parentMaterialId: string | nullquantityToIssue: number | nullreplenishmentSystem:| Database["public"]["Enums"]["itemReplenishmentSystem"]@@ -37605,27 +37608,6 @@ export type Database = {referencedRelation: "location"referencedColumns: ["id"]},- {- foreignKeyName: "jobMakeMethod_parentMaterialId_fkey"- columns: ["parentMaterialId"]- isOneToOne: false- referencedRelation: "jobMaterial"- referencedColumns: ["id"]- },- {- foreignKeyName: "jobMakeMethod_parentMaterialId_fkey"- columns: ["parentMaterialId"]- isOneToOne: false- referencedRelation: "jobMaterialWithMakeMethodId"- referencedColumns: ["id"]- },- {- foreignKeyName: "jobMakeMethod_parentMaterialId_fkey"- columns: ["parentMaterialId"]- isOneToOne: false- referencedRelation: "openJobMaterialLines"- referencedColumns: ["id"]- },{foreignKeyName: "jobMaterial_companyId_fkey"columns: ["companyId"]@@ -37710,20 +37692,6 @@ export type Database = {referencedRelation: "openProductionOrders"referencedColumns: ["id"]},- {- foreignKeyName: "jobMaterial_jobMakeMethodId_fkey"- columns: ["jobMakeMethodId"]- isOneToOne: false- referencedRelation: "jobMakeMethod"- referencedColumns: ["id"]- },- {- foreignKeyName: "jobMaterial_jobMakeMethodId_fkey"- columns: ["jobMakeMethodId"]- isOneToOne: false- referencedRelation: "jobMaterialWithMakeMethodId"- referencedColumns: ["jobMaterialMakeMethodId"]- },{foreignKeyName: "jobMaterial_unitOfMeasureCode_fkey"columns: ["unitOfMeasureCode", "companyId"]@@ -37736,14 +37704,12 @@ export type Database = {openProductionOrders: {Row: {companyId: string | null- deadlineType: Database["public"]["Enums"]["deadlineType"] | nulldueDate: string | nullid: string | nullitemId: string | nullitemTrackingType:| Database["public"]["Enums"]["itemTrackingType"]| null- jobId: string | nullleadTime: number | nulllocationId: string | nullquantityToReceive: number | null@@ -41680,14 +41646,14 @@ export type Database = {Relationships: [{foreignKeyName: "address_countryCode_fkey"- columns: ["customerCountryCode"]+ columns: ["invoiceCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]},{foreignKeyName: "address_countryCode_fkey"- columns: ["invoiceCountryCode"]+ columns: ["customerCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]@@ -45113,89 +45079,6 @@ export type Database = {}Returns: string[]}- get_production_planning: {- Args: {- company_id: string- location_id: string- periods: string[]- }- Returns: {- id: string- readableIdWithRevision: string- name: string- active: boolean- type: Database["public"]["Enums"]["itemType"]- itemTrackingType: Database["public"]["Enums"]["itemTrackingType"]- replenishmentSystem: Database["public"]["Enums"]["itemReplenishmentSystem"]- thumbnailPath: string- unitOfMeasureCode: string- leadTime: number- manufacturingBlocked: boolean- lotSize: number- reorderingPolicy: Database["public"]["Enums"]["itemReorderingPolicy"]- demandAccumulationPeriod: number- demandAccumulationSafetyStock: number- reorderPoint: number- reorderQuantity: number- minimumOrderQuantity: number- maximumOrderQuantity: number- orderMultiple: number- quantityOnHand: number- maximumInventoryQuantity: number- week1: number- week2: number- week3: number- week4: number- week5: number- week6: number- week7: number- week8: number- week9: number- week10: number- week11: number- week12: number- week13: number- week14: number- week15: number- week16: number- week17: number- week18: number- week19: number- week20: number- week21: number- week22: number- week23: number- week24: number- week25: number- week26: number- week27: number- week28: number- week29: number- week30: number- week31: number- week32: number- week33: number- week34: number- week35: number- week36: number- week37: number- week38: number- week39: number- week40: number- week41: number- week42: number- week43: number- week44: number- week45: number- week46: number- week47: number- week48: number- week49: number- week50: number- week51: number- week52: number- }[]- }get_quote_methods: {Args: {qid: stringdiff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.tsindex b1b9f4bf91..d65f2c85ab 100644--- a/packages/database/supabase/functions/lib/types.ts+++ b/packages/database/supabase/functions/lib/types.ts@@ -2093,7 +2093,6 @@ export type Database = {digitalQuoteNotificationGroup: string[]id: stringinventoryJobCompletedNotificationGroup: string[]- jobCompletedNotificationGroup: string[]productLabelSize: string | nullrfqReadyNotificationGroup: string[]salesJobCompletedNotificationGroup: string[]@@ -2105,7 +2104,6 @@ export type Database = {digitalQuoteNotificationGroup?: string[]id: stringinventoryJobCompletedNotificationGroup?: string[]- jobCompletedNotificationGroup?: string[]productLabelSize?: string | nullrfqReadyNotificationGroup?: string[]salesJobCompletedNotificationGroup?: string[]@@ -2117,7 +2115,6 @@ export type Database = {digitalQuoteNotificationGroup?: string[]id?: stringinventoryJobCompletedNotificationGroup?: string[]- jobCompletedNotificationGroup?: string[]productLabelSize?: string | nullrfqReadyNotificationGroup?: string[]salesJobCompletedNotificationGroup?: string[]@@ -8215,17 +8212,20 @@ export type Database = {createdBy: stringcritical: booleancustomFields: Json | null+ demandAccumulationIncludesInventory: booleandemandAccumulationPeriod: number- demandAccumulationSafetyStock: number+ demandReschedulingPeriod: numberitemId: stringlocationId: string- maximumInventoryQuantity: numbermaximumOrderQuantity: numberminimumOrderQuantity: numberorderMultiple: numberreorderingPolicy: Database["public"]["Enums"]["itemReorderingPolicy"]+ reorderMaximumInventory: numberreorderPoint: numberreorderQuantity: number+ safetyStockLeadTime: number+ safetyStockQuantity: numbertags: string[] | nullupdatedAt: string | nullupdatedBy: string | null@@ -8236,17 +8236,20 @@ export type Database = {createdBy: stringcritical?: booleancustomFields?: Json | null+ demandAccumulationIncludesInventory?: booleandemandAccumulationPeriod?: number- demandAccumulationSafetyStock?: number+ demandReschedulingPeriod?: numberitemId: stringlocationId: string- maximumInventoryQuantity?: numbermaximumOrderQuantity?: numberminimumOrderQuantity?: numberorderMultiple?: numberreorderingPolicy?: Database["public"]["Enums"]["itemReorderingPolicy"]+ reorderMaximumInventory?: numberreorderPoint?: numberreorderQuantity?: number+ safetyStockLeadTime?: number+ safetyStockQuantity?: numbertags?: string[] | nullupdatedAt?: string | nullupdatedBy?: string | null@@ -8257,17 +8260,20 @@ export type Database = {createdBy?: stringcritical?: booleancustomFields?: Json | null+ demandAccumulationIncludesInventory?: booleandemandAccumulationPeriod?: number- demandAccumulationSafetyStock?: number+ demandReschedulingPeriod?: numberitemId?: stringlocationId?: string- maximumInventoryQuantity?: numbermaximumOrderQuantity?: numberminimumOrderQuantity?: numberorderMultiple?: numberreorderingPolicy?: Database["public"]["Enums"]["itemReorderingPolicy"]+ reorderMaximumInventory?: numberreorderPoint?: numberreorderQuantity?: number+ safetyStockLeadTime?: number+ safetyStockQuantity?: numbertags?: string[] | nullupdatedAt?: string | nullupdatedBy?: string | null@@ -37586,11 +37592,8 @@ export type Database = {| Database["public"]["Enums"]["itemTrackingType"]| nulljobId: string | null- jobMakeMethodId: string | null- jobReadableId: string | nullleadTime: number | nulllocationId: string | null- parentMaterialId: string | nullquantityToIssue: number | nullreplenishmentSystem:| Database["public"]["Enums"]["itemReplenishmentSystem"]@@ -37605,27 +37608,6 @@ export type Database = {referencedRelation: "location"referencedColumns: ["id"]},- {- foreignKeyName: "jobMakeMethod_parentMaterialId_fkey"- columns: ["parentMaterialId"]- isOneToOne: false- referencedRelation: "jobMaterial"- referencedColumns: ["id"]- },- {- foreignKeyName: "jobMakeMethod_parentMaterialId_fkey"- columns: ["parentMaterialId"]- isOneToOne: false- referencedRelation: "jobMaterialWithMakeMethodId"- referencedColumns: ["id"]- },- {- foreignKeyName: "jobMakeMethod_parentMaterialId_fkey"- columns: ["parentMaterialId"]- isOneToOne: false- referencedRelation: "openJobMaterialLines"- referencedColumns: ["id"]- },{foreignKeyName: "jobMaterial_companyId_fkey"columns: ["companyId"]@@ -37710,20 +37692,6 @@ export type Database = {referencedRelation: "openProductionOrders"referencedColumns: ["id"]},- {- foreignKeyName: "jobMaterial_jobMakeMethodId_fkey"- columns: ["jobMakeMethodId"]- isOneToOne: false- referencedRelation: "jobMakeMethod"- referencedColumns: ["id"]- },- {- foreignKeyName: "jobMaterial_jobMakeMethodId_fkey"- columns: ["jobMakeMethodId"]- isOneToOne: false- referencedRelation: "jobMaterialWithMakeMethodId"- referencedColumns: ["jobMaterialMakeMethodId"]- },{foreignKeyName: "jobMaterial_unitOfMeasureCode_fkey"columns: ["unitOfMeasureCode", "companyId"]@@ -37736,14 +37704,12 @@ export type Database = {openProductionOrders: {Row: {companyId: string | null- deadlineType: Database["public"]["Enums"]["deadlineType"] | nulldueDate: string | nullid: string | nullitemId: string | nullitemTrackingType:| Database["public"]["Enums"]["itemTrackingType"]| null- jobId: string | nullleadTime: number | nulllocationId: string | nullquantityToReceive: number | null@@ -41680,14 +41646,14 @@ export type Database = {Relationships: [{foreignKeyName: "address_countryCode_fkey"- columns: ["customerCountryCode"]+ columns: ["invoiceCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]},{foreignKeyName: "address_countryCode_fkey"- columns: ["invoiceCountryCode"]+ columns: ["customerCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]@@ -45113,89 +45079,6 @@ export type Database = {}Returns: string[]}- get_production_planning: {- Args: {- company_id: string- location_id: string- periods: string[]- }- Returns: {- id: string- readableIdWithRevision: string- name: string- active: boolean- type: Database["public"]["Enums"]["itemType"]- itemTrackingType: Database["public"]["Enums"]["itemTrackingType"]- replenishmentSystem: Database["public"]["Enums"]["itemReplenishmentSystem"]- thumbnailPath: string- unitOfMeasureCode: string- leadTime: number- manufacturingBlocked: boolean- lotSize: number- reorderingPolicy: Database["public"]["Enums"]["itemReorderingPolicy"]- demandAccumulationPeriod: number- demandAccumulationSafetyStock: number- reorderPoint: number- reorderQuantity: number- minimumOrderQuantity: number- maximumOrderQuantity: number- orderMultiple: number- quantityOnHand: number- maximumInventoryQuantity: number- week1: number- week2: number- week3: number- week4: number- week5: number- week6: number- week7: number- week8: number- week9: number- week10: number- week11: number- week12: number- week13: number- week14: number- week15: number- week16: number- week17: number- week18: number- week19: number- week20: number- week21: number- week22: number- week23: number- week24: number- week25: number- week26: number- week27: number- week28: number- week29: number- week30: number- week31: number- week32: number- week33: number- week34: number- week35: number- week36: number- week37: number- week38: number- week39: number- week40: number- week41: number- week42: number- week43: number- week44: number- week45: number- week46: number- week47: number- week48: number- week49: number- week50: number- week51: number- week52: number- }[]- }get_quote_methods: {Args: {qid: stringdiff --git a/packages/database/supabase/migrations/20250620124424_revisions-fix.sql b/packages/database/supabase/migrations/20250620124424_revisions-fix.sqlnew file mode 100644index 0000000000..e0d9220720--- /dev/null+++ b/packages/database/supabase/migrations/20250620124424_revisions-fix.sql@@ -0,0 +1,865 @@+DROP FUNCTION IF EXISTS get_part_details;+CREATE OR REPLACE FUNCTION get_part_details(item_id TEXT)+RETURNS TABLE (+ "active" BOOLEAN,+ "assignee" TEXT,+ "defaultMethodType" "methodType",+ "description" TEXT,+ "itemTrackingType" "itemTrackingType",+ "name" TEXT,+ "replenishmentSystem" "itemReplenishmentSystem",+ "unitOfMeasureCode" TEXT,+ "notes" JSONB,+ "thumbnailPath" TEXT,+ "modelId" TEXT,+ "modelPath" TEXT,+ "modelName" TEXT,+ "modelSize" BIGINT,+ "id" TEXT,+ "companyId" TEXT,+ "unitOfMeasure" TEXT,+ "readableId" TEXT,+ "revision" TEXT,+ "readableIdWithRevision" TEXT,+ "revisions" JSON,+ "customFields" JSONB,+ "tags" TEXT[],+ "createdBy" TEXT,+ "createdAt" TIMESTAMP WITH TIME ZONE,+ "updatedBy" TEXT,+ "updatedAt" TIMESTAMP WITH TIME ZONE+) AS $$+DECLARE+ v_readable_id TEXT;+ v_company_id TEXT;+BEGIN+ -- First get the readableId and companyId for the item+ SELECT i."readableId", i."companyId" INTO v_readable_id, v_company_id+ FROM "item" i+ WHERE i.id = item_id;++ RETURN QUERY+ WITH item_revisions AS (+ SELECT+ json_agg(+ json_build_object(+ 'id', i.id,+ 'revision', i."revision",+ 'methodType', i."defaultMethodType",+ 'type', i."type"+ ) ORDER BY+ i."createdAt"+ ) as "revisions"+ FROM "item" i+ WHERE i."readableId" = v_readable_id+ AND i."companyId" = v_company_id+ )+ SELECT+ i."active",+ i."assignee",+ i."defaultMethodType",+ i."description",+ i."itemTrackingType",+ i."name",+ i."replenishmentSystem",+ i."unitOfMeasureCode",+ i."notes",+ CASE+ WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"+ ELSE i."thumbnailPath"+ END as "thumbnailPath",+ mu.id as "modelId",+ mu."modelPath",+ mu."name" as "modelName",+ mu."size" as "modelSize",+ i."id",+ i."companyId",+ uom.name as "unitOfMeasure",+ i."readableId",+ i."revision",+ i."readableIdWithRevision",+ ir."revisions",+ p."customFields",+ p."tags",+ i."createdBy",+ i."createdAt",+ i."updatedBy",+ i."updatedAt"+ FROM "part" p+ LEFT JOIN "item" i ON i."readableId" = p."id" AND i."companyId" = p."companyId"+ LEFT JOIN item_revisions ir ON true+ LEFT JOIN (+ SELECT+ ps."itemId",+ string_agg(ps."supplierPartId", ',') AS "supplierIds"+ FROM "supplierPart" ps+ GROUP BY ps."itemId"+ ) ps ON ps."itemId" = i.id+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"+ LEFT JOIN "unitOfMeasure" uom ON uom.code = i."unitOfMeasureCode" AND uom."companyId" = i."companyId"+ WHERE i."id" = item_id;+END;+$$ LANGUAGE plpgsql;++DROP VIEW IF EXISTS "parts";+CREATE OR REPLACE VIEW "parts" WITH (SECURITY_INVOKER=true) AS+WITH latest_items AS (+ SELECT DISTINCT ON (i."readableId", i."companyId")+ i.*,+ mu.id as "modelUploadId",+ mu."modelPath",+ mu."thumbnailPath" as "modelThumbnailPath",+ mu."name" as "modelName",+ mu."size" as "modelSize"+ FROM "item" i+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"+ ORDER BY i."readableId", i."companyId", i."createdAt" DESC NULLS LAST+),+item_revisions AS (+ SELECT+ i."readableId", i."companyId",+ json_agg(+ json_build_object(+ 'id', i.id,+ 'revision', i."revision",+ 'name', i."name",+ 'description', i."description",+ 'active', i."active",+ 'createdAt', i."createdAt"+ ) ORDER BY i."createdAt"+ ) as "revisions"+ FROM "item" i+ GROUP BY i."readableId", i."companyId"+)+SELECT+ li."active",+ li."assignee",+ li."defaultMethodType",+ li."description",+ li."itemTrackingType",+ li."name",+ li."replenishmentSystem",+ li."unitOfMeasureCode",+ li."notes",+ li."revision",+ li."readableId",+ li."readableIdWithRevision",+ li."id",+ li."companyId",+ CASE+ WHEN li."thumbnailPath" IS NULL AND li."modelThumbnailPath" IS NOT NULL THEN li."modelThumbnailPath"+ ELSE li."thumbnailPath"+ END as "thumbnailPath",++ li."modelPath",+ li."modelName",+ li."modelSize",+ ps."supplierIds",+ uom.name as "unitOfMeasure",+ ir."revisions",+ p."customFields",+ p."tags",+ li."createdBy",+ li."createdAt",+ li."updatedBy",+ li."updatedAt"+FROM "part" p+INNER JOIN latest_items li ON li."readableId" = p."id" AND li."companyId" = p."companyId"+LEFT JOIN item_revisions ir ON ir."readableId" = p."id" AND ir."companyId" = p."companyId"+LEFT JOIN (+ SELECT+ ps."itemId",+ ps."companyId",+ string_agg(ps."supplierPartId", ',') AS "supplierIds"+ FROM "supplierPart" ps+ GROUP BY ps."itemId", ps."companyId"+) ps ON ps."itemId" = li."id" AND ps."companyId" = li."companyId"+LEFT JOIN "unitOfMeasure" uom ON uom.code = li."unitOfMeasureCode" AND uom."companyId" = li."companyId";++++DROP VIEW IF EXISTS "parts";+CREATE OR REPLACE VIEW "parts" WITH (SECURITY_INVOKER=true) AS+WITH latest_items AS (+ SELECT DISTINCT ON (i."readableId", i."companyId")+ i.*,+ mu.id as "modelUploadId",++ mu."modelPath",+ mu."thumbnailPath" as "modelThumbnailPath",+ mu."name" as "modelName",+ mu."size" as "modelSize"+ FROM "item" i+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"+ ORDER BY i."readableId", i."companyId", i."createdAt" DESC NULLS LAST+),+item_revisions AS (+ SELECT+ i."readableId",+ i."companyId",+ json_agg(+ json_build_object(+ 'id', i.id,+ 'revision', i."revision",+ 'name', i."name",+ 'description', i."description",+ 'active', i."active",+ 'createdAt', i."createdAt"+ ) ORDER BY i."createdAt"+ ) as "revisions"+ FROM "item" i+ GROUP BY i."readableId", i."companyId"+)+SELECT+ li."active",+ li."assignee",+ li."defaultMethodType",+ li."description",+ li."itemTrackingType",+ li."name",+ li."replenishmentSystem",+ li."unitOfMeasureCode",+ li."notes",+ li."revision",+ li."readableId",+ li."readableIdWithRevision",+ li."id",+ li."companyId",+ CASE+ WHEN li."thumbnailPath" IS NULL AND li."modelThumbnailPath" IS NOT NULL THEN li."modelThumbnailPath"+ ELSE li."thumbnailPath"+ END as "thumbnailPath",++ li."modelPath",+ li."modelName",+ li."modelSize",+ ps."supplierIds",+ uom.name as "unitOfMeasure",+ ir."revisions",+ p."customFields",+ p."tags",+ li."createdBy",+ li."createdAt",+ li."updatedBy",+ li."updatedAt"+FROM "part" p+INNER JOIN latest_items li ON li."readableId" = p."id" AND li."companyId" = p."companyId"+LEFT JOIN item_revisions ir ON ir."readableId" = p."id" AND ir."companyId" = p."companyId"+LEFT JOIN (+ SELECT+ "itemId",+ "companyId",+ string_agg(ps."supplierPartId", ',') AS "supplierIds"+ FROM "supplierPart" ps+ GROUP BY "itemId", "companyId"+) ps ON ps."itemId" = li."id" AND ps."companyId" = li."companyId"+LEFT JOIN "unitOfMeasure" uom ON uom.code = li."unitOfMeasureCode" AND uom."companyId" = li."companyId";+++DROP VIEW IF EXISTS "materials";+CREATE OR REPLACE VIEW "materials" WITH (SECURITY_INVOKER=true) AS+WITH latest_items AS (+ SELECT DISTINCT ON (i."readableId", i."companyId")+ i.*,++ mu."modelPath",+ mu."thumbnailPath" as "modelThumbnailPath",+ mu."name" as "modelName",+ mu."size" as "modelSize"+ FROM "item" i+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"+ ORDER BY i."readableId", i."companyId", i."createdAt" DESC NULLS LAST+),+item_revisions AS (+ SELECT+ i."readableId",+ i."companyId",+ json_agg(+ json_build_object(+ 'id', i.id,+ 'revision', i."revision",+ 'methodType', i."defaultMethodType",+ 'type', i."type"+ ) ORDER BY i."createdAt"+ ) as "revisions"+ FROM "item" i+ GROUP BY i."readableId", i."companyId"+)+SELECT+ i."active",+ i."assignee",+ i."defaultMethodType",+ i."description",+ i."itemTrackingType",+ i."name",+ i."replenishmentSystem",+ i."unitOfMeasureCode",+ i."notes",+ i."revision",+ i."readableId",+ i."readableIdWithRevision",+ i."id",+ i."companyId",+ CASE+ WHEN i."thumbnailPath" IS NULL AND i."modelThumbnailPath" IS NOT NULL THEN i."modelThumbnailPath"+ ELSE i."thumbnailPath"+ END as "thumbnailPath",+ i."modelUploadId",++ i."modelPath",+ i."modelName",+ i."modelSize",+ ps."supplierIds",+ uom.name as "unitOfMeasure",+ ir."revisions",+ mf."name" AS "materialForm",+ ms."name" AS "materialSubstance",+ m."finish",+ m."grade",+ m."dimensions",+ m."materialSubstanceId",+ m."materialFormId",+ m."customFields",+ m."tags",+ i."createdBy",+ i."createdAt",+ i."updatedBy",+ i."updatedAt"+FROM "material" m+ INNER JOIN latest_items i ON i."readableId" = m."id" AND i."companyId" = m."companyId"+ LEFT JOIN item_revisions ir ON ir."readableId" = m."id" AND ir."companyId" = i."companyId"+ LEFT JOIN (+ SELECT+ ps."itemId",+ ps."companyId",+ string_agg(ps."supplierPartId", ',') AS "supplierIds"+ FROM "supplierPart" ps+ GROUP BY ps."itemId", ps."companyId"+ ) ps ON ps."itemId" = i."id" AND ps."companyId" = i."companyId"+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"+ LEFT JOIN "unitOfMeasure" uom ON uom.code = i."unitOfMeasureCode" AND uom."companyId" = i."companyId"+ LEFT JOIN "materialForm" mf ON mf."id" = m."materialFormId"+ LEFT JOIN "materialSubstance" ms ON ms."id" = m."materialSubstanceId";++DROP FUNCTION IF EXISTS get_material_details;+CREATE OR REPLACE FUNCTION get_material_details(item_id TEXT)+RETURNS TABLE (+ "active" BOOLEAN,+ "assignee" TEXT,+ "defaultMethodType" "methodType",+ "description" TEXT,+ "itemTrackingType" "itemTrackingType",+ "name" TEXT,+ "replenishmentSystem" "itemReplenishmentSystem",+ "unitOfMeasureCode" TEXT,+ "notes" JSONB,+ "thumbnailPath" TEXT,+ "modelUploadId" TEXT,+ "modelPath" TEXT,+ "modelName" TEXT,+ "modelSize" BIGINT,+ "id" TEXT,+ "companyId" TEXT,+ "readableId" TEXT,+ "revision" TEXT,+ "readableIdWithRevision" TEXT,+ "supplierIds" TEXT,+ "unitOfMeasure" TEXT,+ "revisions" JSON,+ "materialForm" TEXT,+ "materialSubstance" TEXT,+ "finish" TEXT,+ "grade" TEXT,+ "dimensions" TEXT,+ "materialSubstanceId" TEXT,+ "materialFormId" TEXT,+ "customFields" JSONB,+ "tags" TEXT[],+ "createdBy" TEXT,+ "createdAt" TIMESTAMP WITH TIME ZONE,+ "updatedBy" TEXT,+ "updatedAt" TIMESTAMP WITH TIME ZONE+) AS $$+DECLARE+ v_readable_id TEXT;+ v_company_id TEXT;+BEGIN+ -- First get the readableId and companyId for the item+ SELECT i."readableId", i."companyId" INTO v_readable_id, v_company_id+ FROM "item" i+ WHERE i.id = item_id;++ RETURN QUERY+ WITH item_revisions AS (+ SELECT+ json_agg(+ json_build_object(+ 'id', i.id,+ 'revision', i."revision",+ 'methodType', i."defaultMethodType",+ 'type', i."type"+ ) ORDER BY+ i."createdAt"+ ) as "revisions"+ FROM "item" i+ WHERE i."readableId" = v_readable_id+ AND i."companyId" = v_company_id+ )+ SELECT+ i."active",+ i."assignee",+ i."defaultMethodType",+ i."description",+ i."itemTrackingType",+ i."name",+ i."replenishmentSystem",+ i."unitOfMeasureCode",+ i."notes",+ CASE+ WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"+ ELSE i."thumbnailPath"+ END as "thumbnailPath",+ mu.id as "modelUploadId",+ mu."modelPath",+ mu."name" as "modelName",+ mu."size" as "modelSize",+ i."id",+ i."companyId",+ i."readableId",+ i."revision",+ i."readableIdWithRevision",+ ps."supplierIds",+ uom.name as "unitOfMeasure",+ ir."revisions",+ mf."name" AS "materialForm",+ ms."name" AS "materialSubstance",+ m."finish",+ m."grade",+ m."dimensions",+ m."materialSubstanceId",+ m."materialFormId",+ m."customFields",+ m."tags",+ i."createdBy",+ i."createdAt",+ i."updatedBy",+ i."updatedAt"+ FROM "material" m+ LEFT JOIN "item" i ON i."readableId" = m."id" AND i."companyId" = m."companyId"+ LEFT JOIN item_revisions ir ON true+ LEFT JOIN (+ SELECT+ ps."itemId",+ string_agg(ps."supplierPartId", ',') AS "supplierIds"+ FROM "supplierPart" ps+ GROUP BY ps."itemId"+ ) ps ON ps."itemId" = i.id+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"+ LEFT JOIN "unitOfMeasure" uom ON uom.code = i."unitOfMeasureCode" AND uom."companyId" = i."companyId"+ LEFT JOIN "materialForm" mf ON mf."id" = m."materialFormId"+ LEFT JOIN "materialSubstance" ms ON ms."id" = m."materialSubstanceId"+ WHERE i."id" = item_id;+END;+$$ LANGUAGE plpgsql STABLE;++DROP VIEW IF EXISTS "consumables";+CREATE OR REPLACE VIEW "consumables" WITH (SECURITY_INVOKER=true) AS+WITH latest_items AS (+ SELECT DISTINCT ON (i."readableId", i."companyId")+ i.*,+ mu."modelPath",+ mu."thumbnailPath" as "modelThumbnailPath",+ mu."name" as "modelName",+ mu."size" as "modelSize"+ FROM "item" i+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"+ ORDER BY i."readableId", i."companyId", i."createdAt" DESC NULLS LAST+),+item_revisions AS (+ SELECT+ i."readableId",+ i."companyId",+ json_agg(+ json_build_object(+ 'id', i.id,+ 'revision', i."revision",+ 'methodType', i."defaultMethodType",+ 'type', i."type"+ ) ORDER BY i."createdAt"+ ) as "revisions"+ FROM "item" i+ GROUP BY i."readableId", i."companyId"+)+SELECT+ li."active",+ li."assignee",+ li."defaultMethodType",+ li."description",+ li."itemTrackingType",+ li."name",+ li."replenishmentSystem",+ li."unitOfMeasureCode",+ li."notes",+ li."revision",+ li."readableId",+ li."readableIdWithRevision",+ li."id",+ li."companyId",+ CASE+ WHEN li."thumbnailPath" IS NULL AND li."modelThumbnailPath" IS NOT NULL THEN li."modelThumbnailPath"+ ELSE li."thumbnailPath"+ END as "thumbnailPath",+ li."modelUploadId",+ li."modelPath",+ li."modelName",+ li."modelSize",+ ps."supplierIds",+ uom.name as "unitOfMeasure",+ ir."revisions",+ c."customFields",+ c."tags",+ li."createdBy",+ li."createdAt",+ li."updatedBy",+ li."updatedAt"+FROM "consumable" c+ INNER JOIN latest_items li ON li."readableId" = c."id" AND li."companyId" = c."companyId"+LEFT JOIN item_revisions ir ON ir."readableId" = c."id" AND ir."companyId" = li."companyId"+LEFT JOIN (+ SELECT+ "itemId",+ "companyId",+ string_agg(ps."supplierPartId", ',') AS "supplierIds"+ FROM "supplierPart" ps+ GROUP BY "itemId", "companyId"+) ps ON ps."itemId" = li."id" AND ps."companyId" = li."companyId"+LEFT JOIN "unitOfMeasure" uom ON uom.code = li."unitOfMeasureCode" AND uom."companyId" = li."companyId";+++DROP FUNCTION IF EXISTS get_consumable_details;+CREATE OR REPLACE FUNCTION get_consumable_details(item_id TEXT)+RETURNS TABLE (+ "active" BOOLEAN,+ "assignee" TEXT,+ "defaultMethodType" "methodType",+ "description" TEXT,+ "itemTrackingType" "itemTrackingType",+ "name" TEXT,+ "replenishmentSystem" "itemReplenishmentSystem",+ "unitOfMeasureCode" TEXT,+ "notes" JSONB,+ "thumbnailPath" TEXT,+ "modelUploadId" TEXT,+ "modelPath" TEXT,+ "modelName" TEXT,+ "modelSize" BIGINT,+ "id" TEXT,+ "companyId" TEXT,+ "readableId" TEXT,+ "revision" TEXT,+ "readableIdWithRevision" TEXT,+ "supplierIds" TEXT,+ "unitOfMeasure" TEXT,+ "revisions" JSON,+ "customFields" JSONB,+ "tags" TEXT[],+ "createdBy" TEXT,+ "createdAt" TIMESTAMP WITH TIME ZONE,+ "updatedBy" TEXT,+ "updatedAt" TIMESTAMP WITH TIME ZONE+) AS $$+DECLARE+ v_readable_id TEXT;+ v_company_id TEXT;+BEGIN+ -- First get the readableId and companyId for the item+ SELECT i."readableId", i."companyId" INTO v_readable_id, v_company_id+ FROM "item" i+ WHERE i.id = item_id;++ RETURN QUERY+ WITH item_revisions AS (+ SELECT+ json_agg(+ json_build_object(+ 'id', i.id,+ 'revision', i."revision",+ 'methodType', i."defaultMethodType",+ 'type', i."type"+ ) ORDER BY+ i."createdAt"+ ) as "revisions"+ FROM "item" i+ WHERE i."readableId" = v_readable_id+ AND i."companyId" = v_company_id+ )+ SELECT+ i."active",+ i."assignee",+ i."defaultMethodType",+ i."description",+ i."itemTrackingType",+ i."name",+ i."replenishmentSystem",+ i."unitOfMeasureCode",+ i."notes",+ CASE+ WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"+ ELSE i."thumbnailPath"+ END as "thumbnailPath",+ mu.id as "modelUploadId",+ mu."modelPath",+ mu."name" as "modelName",+ mu."size" as "modelSize",+ i."id",+ i."companyId",+ i."readableId",+ i."revision",+ i."readableIdWithRevision",+ ps."supplierIds",+ uom.name as "unitOfMeasure",+ ir."revisions",+ c."customFields",+ c."tags",+ i."createdBy",+ i."createdAt",+ i."updatedBy",+ i."updatedAt"+ FROM "consumable" c+ LEFT JOIN "item" i ON i."readableId" = c."id" AND i."companyId" = c."companyId"+ LEFT JOIN item_revisions ir ON true+ LEFT JOIN (+ SELECT+ ps."itemId",+ string_agg(ps."supplierPartId", ',') AS "supplierIds"+ FROM "supplierPart" ps+ GROUP BY ps."itemId"+ ) ps ON ps."itemId" = i.id+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"+ LEFT JOIN "unitOfMeasure" uom ON uom.code = i."unitOfMeasureCode" AND uom."companyId" = i."companyId"+ WHERE i."id" = item_id;+END;+$$ LANGUAGE plpgsql STABLE;+++DROP FUNCTION IF EXISTS get_tool_details;+CREATE OR REPLACE FUNCTION get_tool_details(item_id TEXT)+RETURNS TABLE (+ "active" BOOLEAN,+ "assignee" TEXT,+ "defaultMethodType" "methodType",+ "description" TEXT,+ "itemTrackingType" "itemTrackingType",+ "name" TEXT,+ "replenishmentSystem" "itemReplenishmentSystem",+ "unitOfMeasureCode" TEXT,+ "notes" JSONB,+ "thumbnailPath" TEXT,+ "modelId" TEXT,+ "modelPath" TEXT,+ "modelName" TEXT,+ "modelSize" BIGINT,+ "id" TEXT,+ "companyId" TEXT,+ "unitOfMeasure" TEXT,+ "readableId" TEXT,+ "revision" TEXT,+ "readableIdWithRevision" TEXT,+ "revisions" JSON,+ "customFields" JSONB,+ "tags" TEXT[],+ "createdBy" TEXT,+ "createdAt" TIMESTAMP WITH TIME ZONE,+ "updatedBy" TEXT,+ "updatedAt" TIMESTAMP WITH TIME ZONE+) AS $$+DECLARE+ v_readable_id TEXT;+ v_company_id TEXT;+BEGIN+ -- First get the readableId and companyId for the item+ SELECT i."readableId", i."companyId" INTO v_readable_id, v_company_id+ FROM "item" i+ WHERE i.id = item_id;++ RETURN QUERY+ WITH item_revisions AS (+ SELECT+ json_agg(+ json_build_object(+ 'id', i.id,+ 'revision', i."revision",+ 'methodType', i."defaultMethodType",+ 'type', i."type"+ ) ORDER BY+ i."createdAt"+ ) as "revisions"+ FROM "item" i+ WHERE i."readableId" = v_readable_id+ AND i."companyId" = v_company_id+ )+ SELECT+ i."active",+ i."assignee",+ i."defaultMethodType",+ i."description",+ i."itemTrackingType",+ i."name",+ i."replenishmentSystem",+ i."unitOfMeasureCode",+ i."notes",+ CASE+ WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"+ ELSE i."thumbnailPath"+ END as "thumbnailPath",+ mu.id as "modelId",+ mu."modelPath",+ mu."name" as "modelName",+ mu."size" as "modelSize",+ i."id",+ i."companyId",+ uom.name as "unitOfMeasure",+ i."readableId",+ i."revision",+ i."readableIdWithRevision",+ ir."revisions",+ t."customFields",+ t."tags",+ i."createdBy",+ i."createdAt",+ i."updatedBy",+ i."updatedAt"+ FROM "tool" t+ LEFT JOIN "item" i ON i."readableId" = t."id" AND i."companyId" = t."companyId"+ LEFT JOIN item_revisions ir ON true+ LEFT JOIN (+ SELECT+ ps."itemId",+ string_agg(ps."supplierPartId", ',') AS "supplierIds"+ FROM "supplierPart" ps+ GROUP BY ps."itemId"+ ) ps ON ps."itemId" = i.id+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"+ LEFT JOIN "unitOfMeasure" uom ON uom.code = i."unitOfMeasureCode" AND uom."companyId" = i."companyId"+ WHERE i."id" = item_id;+END;+$$ LANGUAGE plpgsql;++DROP VIEW IF EXISTS "tools";+CREATE OR REPLACE VIEW "tools" WITH (SECURITY_INVOKER=true) AS+WITH latest_items AS (+ SELECT DISTINCT ON (i."readableId", i."companyId")+ i.*,+ mu.id as "modelUploadId",++ mu."modelPath",+ mu."thumbnailPath" as "modelThumbnailPath",+ mu."name" as "modelName",+ mu."size" as "modelSize"+ FROM "item" i+ LEFT JOIN "modelUpload" mu ON mu.id = i."modelUploadId"+ ORDER BY i."readableId", i."companyId", i."createdAt" DESC NULLS LAST+),+item_revisions AS (+ SELECT+ i."readableId",+ i."companyId",+ json_agg(+ json_build_object(+ 'id', i.id,+ 'revision', i."revision",+ 'methodType', i."defaultMethodType",+ 'type', i."type"+ ) ORDER BY i."createdAt"+ ) as "revisions"+ FROM "item" i+ GROUP BY i."readableId", i."companyId"+)+SELECT+ li."active",+ li."assignee",+ li."defaultMethodType",+ li."description",+ li."itemTrackingType",+ li."name",+ li."replenishmentSystem",+ li."unitOfMeasureCode",+ li."notes",+ li."revision",+ li."readableId",+ li."readableIdWithRevision",+ li."id",+ li."companyId",+ CASE+ WHEN li."thumbnailPath" IS NULL AND li."modelThumbnailPath" IS NOT NULL THEN li."modelThumbnailPath"+ ELSE li."thumbnailPath"+ END as "thumbnailPath",++ li."modelPath",+ li."modelName",+ li."modelSize",+ ps."supplierIds",+ uom.name as "unitOfMeasure",+ ir."revisions",+ t."customFields",+ t."tags",+ li."createdBy",+ li."createdAt",+ li."updatedBy",+ li."updatedAt"+FROM "tool" t+ INNER JOIN latest_items li ON li."readableId" = t."id" AND li."companyId" = t."companyId"+LEFT JOIN item_revisions ir ON ir."readableId" = t."id" AND ir."companyId" = li."companyId"+LEFT JOIN (+ SELECT+ "itemId",+ "companyId",+ string_agg(ps."supplierPartId", ',') AS "supplierIds"+ FROM "supplierPart" ps+ GROUP BY "itemId", "companyId"+) ps ON ps."itemId" = li."id" AND ps."companyId" = li."companyId"+LEFT JOIN "unitOfMeasure" uom ON uom.code = li."unitOfMeasureCode" AND uom."companyId" = li."companyId";++DROP VIEW IF EXISTS "jobs";+CREATE OR REPLACE VIEW "jobs" WITH(SECURITY_INVOKER=true) AS+WITH job_model AS (+ SELECT+ j.id AS job_id,+ j."companyId",+ COALESCE(j."modelUploadId", i."modelUploadId") AS model_upload_id+ FROM "job" j+ INNER JOIN "item" i ON j."itemId" = i."id" AND j."companyId" = i."companyId"+)+SELECT+ j.*,+ CASE+ WHEN j.status = 'Completed' THEN 'Completed'+ WHEN j."status" = 'Cancelled' THEN 'Cancelled'+ WHEN j."dueDate" IS NOT NULL AND j."dueDate" < CURRENT_DATE THEN 'Overdue'+ WHEN j."dueDate" IS NOT NULL AND j."dueDate" = CURRENT_DATE THEN 'Due Today'+ ELSE j."status"+ END as "statusWithDueDate",+ i.name,+ i."readableIdWithRevision" as "itemReadableIdWithRevision",+ i.type as "itemType",+ i.name as "description",+ i."itemTrackingType",+ i.active,+ i."replenishmentSystem",+ mu.id as "modelId",+ mu."autodeskUrn",+ mu."modelPath",+ CASE+ WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"+ ELSE i."thumbnailPath"+ END as "thumbnailPath",+ mu."name" as "modelName",+ mu."size" as "modelSize",+ so."salesOrderId" as "salesOrderReadableId",+ qo."quoteId" as "quoteReadableId"+FROM "job" j+INNER JOIN "item" i ON j."itemId" = i."id" AND j."companyId" = i."companyId"+LEFT JOIN job_model jm ON j.id = jm.job_id AND j."companyId" = jm."companyId"+LEFT JOIN "modelUpload" mu ON mu.id = jm.model_upload_id+LEFT JOIN "salesOrder" so on j."salesOrderId" = so.id AND j."companyId" = so."companyId"+LEFT JOIN "quote" qo ON j."quoteId" = qo.id AND j."companyId" = qo."companyId";\ No newline at end of file