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
refactor: services for multi-tenant - inventory
Brad Barbinf84168ccca83
diff --git a/apps/carbon/app/modules/inventory/inventory.service.ts b/apps/carbon/app/modules/inventory/inventory.service.tsindex 2ce33abb8a..d19ba8805e 100644--- a/apps/carbon/app/modules/inventory/inventory.service.ts+++ b/apps/carbon/app/modules/inventory/inventory.service.ts@@ -29,6 +29,7 @@ export async function deleteShippingMethod(export async function getReceipts(client: SupabaseClient<Database>,+ companyId: number,args: GenericQueryFilters & {search: string | null;}@@ -38,6 +39,7 @@ export async function getReceipts(.select("*", {count: "exact",})+ .eq("companyId", companyId).neq("sourceDocumentId", "");if (args.search) {@@ -79,6 +81,7 @@ export async function getShippingMethod(export async function getShippingMethods(client: SupabaseClient<Database>,+ companyId: number,args: GenericQueryFilters & {search: string | null;}@@ -88,6 +91,7 @@ export async function getShippingMethods(.select("*", {count: "exact",})+ .eq("companyId", companyId).eq("active", true);if (args.search) {@@ -102,18 +106,26 @@ export async function getShippingMethods(return query;}-export async function getShippingMethodsList(client: SupabaseClient<Database>) {+export async function getShippingMethodsList(+ client: SupabaseClient<Database>,+ companyId: number+) {return client.from("shippingMethod").select("id, name")+ .eq("companyId", companyId).eq("active", true).order("name", { ascending: true });}-export async function getShippingTermsList(client: SupabaseClient<Database>) {+export async function getShippingTermsList(+ client: SupabaseClient<Database>,+ companyId: number+) {return client.from("shippingTerm").select("id, name")+ .eq("companyId", companyId).eq("active", true).order("name", { ascending: true });}@@ -123,6 +135,7 @@ export async function upsertReceipt(receipt:| (Omit<z.infer<typeof receiptValidator>, "id" | "receiptId"> & {receiptId: string;+ companyId: number;createdBy: string;customFields?: Json;})@@ -151,6 +164,7 @@ export async function upsertShippingMethod(client: SupabaseClient<Database>,shippingMethod:| (Omit<z.infer<typeof shippingMethodValidator>, "id"> & {+ companyId: number;createdBy: string;customFields?: Json;})diff --git a/apps/carbon/app/routes/x+/customer+/_layout.tsx b/apps/carbon/app/routes/x+/customer+/_layout.tsxindex 04acbacabc..d47d2806cf 100644--- a/apps/carbon/app/routes/x+/customer+/_layout.tsx+++ b/apps/carbon/app/routes/x+/customer+/_layout.tsx@@ -37,8 +37,8 @@ export async function loader({ request }: LoaderFunctionArgs) {getCustomerTypes(client),getCustomerStatuses(client),getPaymentTermsList(client, companyId),- getShippingMethodsList(client),- getShippingTermsList(client),+ getShippingMethodsList(client, companyId),+ getShippingTermsList(client, companyId),]);return json({diff --git a/apps/carbon/app/routes/x+/inventory+/receipts.tsx b/apps/carbon/app/routes/x+/inventory+/receipts.tsxindex 6977fd3f9f..1245e5667b 100644--- a/apps/carbon/app/routes/x+/inventory+/receipts.tsx+++ b/apps/carbon/app/routes/x+/inventory+/receipts.tsx@@ -28,7 +28,7 @@ export async function loader({ request }: LoaderFunctionArgs) {getGenericQueryFilters(searchParams);const [receipts, locations] = await Promise.all([- getReceipts(client, {+ getReceipts(client, companyId, {search,limit,offset,diff --git a/apps/carbon/app/routes/x+/inventory+/shipping-methods.new.tsx b/apps/carbon/app/routes/x+/inventory+/shipping-methods.new.tsxindex d41848bd94..521674782f 100644--- a/apps/carbon/app/routes/x+/inventory+/shipping-methods.new.tsx+++ b/apps/carbon/app/routes/x+/inventory+/shipping-methods.new.tsx@@ -24,7 +24,7 @@ export async function loader({ request }: LoaderFunctionArgs) {export async function action({ request }: ActionFunctionArgs) {assertIsPost(request);- const { client, userId } = await requirePermissions(request, {+ const { client, companyId, userId } = await requirePermissions(request, {create: "inventory",});@@ -41,6 +41,7 @@ export async function action({ request }: ActionFunctionArgs) {const insertShippingMethod = await upsertShippingMethod(client, {...data,+ companyId,createdBy: userId,customFields: setCustomFields(formData),});diff --git a/apps/carbon/app/routes/x+/inventory+/shipping-methods.tsx b/apps/carbon/app/routes/x+/inventory+/shipping-methods.tsxindex 01acc43fa4..d4d3bea23e 100644--- a/apps/carbon/app/routes/x+/inventory+/shipping-methods.tsx+++ b/apps/carbon/app/routes/x+/inventory+/shipping-methods.tsx@@ -29,7 +29,7 @@ export async function loader({ request }: LoaderFunctionArgs) {getGenericQueryFilters(searchParams);const [shippingMethods] = await Promise.all([- getShippingMethods(client, {+ getShippingMethods(client, companyId, {search,limit,offset,diff --git a/apps/carbon/app/routes/x+/purchase-order+/$orderId.delivery.tsx b/apps/carbon/app/routes/x+/purchase-order+/$orderId.delivery.tsxindex 686f7b6369..1d32d22f6f 100644--- a/apps/carbon/app/routes/x+/purchase-order+/$orderId.delivery.tsx+++ b/apps/carbon/app/routes/x+/purchase-order+/$orderId.delivery.tsx@@ -14,14 +14,14 @@ import {} from "~/modules/purchasing";import { requirePermissions } from "~/services/auth/auth.server";import { flash } from "~/services/session.server";+import type { ListItem } from "~/types";import { getCustomFields, setCustomFields } from "~/utils/form";import { assertIsPost } from "~/utils/http";import { path } from "~/utils/path";import { error, success } from "~/utils/result";-import type { ListItem } from "~/types";export async function loader({ request, params }: LoaderFunctionArgs) {- const { client } = await requirePermissions(request, {+ const { client, companyId } = await requirePermissions(request, {view: "purchasing",});@@ -31,8 +31,8 @@ export async function loader({ request, params }: LoaderFunctionArgs) {const [purchaseOrderDelivery, shippingMethods, shippingTerms] =await Promise.all([getPurchaseOrderDelivery(client, orderId),- getShippingMethodsList(client),- getShippingTermsList(client),+ getShippingMethodsList(client, companyId),+ getShippingTermsList(client, companyId),]);if (purchaseOrderDelivery.error) {diff --git a/apps/carbon/app/routes/x+/receipt+/new.tsx b/apps/carbon/app/routes/x+/receipt+/new.tsxindex 5ba872c704..fab4fce5fd 100644--- a/apps/carbon/app/routes/x+/receipt+/new.tsx+++ b/apps/carbon/app/routes/x+/receipt+/new.tsx@@ -27,6 +27,7 @@ export async function loader({ request }: LoaderFunctionArgs) {id: string;}>("create-receipt-from-purchase-order", {body: {+ companyId,locationId: defaults.data?.locationId,purchaseOrderId: sourceDocumentId,receiptId: undefined,@@ -49,6 +50,7 @@ export async function loader({ request }: LoaderFunctionArgs) {id: string;}>("create-receipt-default", {body: {+ companyId,locationId: defaults.data?.locationId,userId: userId,},diff --git a/apps/carbon/app/routes/x+/sales-order+/$orderId.shipment.tsx b/apps/carbon/app/routes/x+/sales-order+/$orderId.shipment.tsxindex ef8924b1d4..8ac66f9756 100644--- a/apps/carbon/app/routes/x+/sales-order+/$orderId.shipment.tsx+++ b/apps/carbon/app/routes/x+/sales-order+/$orderId.shipment.tsx@@ -14,14 +14,14 @@ import {} from "~/modules/sales";import { requirePermissions } from "~/services/auth/auth.server";import { flash } from "~/services/session.server";+import type { ListItem } from "~/types";import { getCustomFields, setCustomFields } from "~/utils/form";import { assertIsPost } from "~/utils/http";import { path } from "~/utils/path";import { error, success } from "~/utils/result";-import type { ListItem } from "~/types";export async function loader({ request, params }: LoaderFunctionArgs) {- const { client } = await requirePermissions(request, {+ const { client, companyId } = await requirePermissions(request, {view: "sales",});@@ -31,8 +31,8 @@ export async function loader({ request, params }: LoaderFunctionArgs) {const [salesOrderShipment, shippingMethods, shippingTerms] =await Promise.all([getSalesOrderShipment(client, orderId),- getShippingMethodsList(client),- getShippingTermsList(client),+ getShippingMethodsList(client, companyId),+ getShippingTermsList(client, companyId),]);if (salesOrderShipment.error) {diff --git a/apps/carbon/app/routes/x+/supplier+/_layout.tsx b/apps/carbon/app/routes/x+/supplier+/_layout.tsxindex 57cad9596d..2cd7cd78d5 100644--- a/apps/carbon/app/routes/x+/supplier+/_layout.tsx+++ b/apps/carbon/app/routes/x+/supplier+/_layout.tsx@@ -37,8 +37,8 @@ export async function loader({ request }: LoaderFunctionArgs) {getSupplierTypes(client),getSupplierStatuses(client),getPaymentTermsList(client, companyId),- getShippingMethodsList(client),- getShippingTermsList(client),+ getShippingMethodsList(client, companyId),+ getShippingTermsList(client, companyId),]);return json({diff --git a/packages/database/supabase/functions/create-receipt-default/index.ts b/packages/database/supabase/functions/create-receipt-default/index.tsindex 92f9d82d9e..16a23b7c86 100644--- a/packages/database/supabase/functions/create-receipt-default/index.ts+++ b/packages/database/supabase/functions/create-receipt-default/index.ts@@ -17,11 +17,12 @@ serve(async (req: Request) => {if (req.method === "OPTIONS") {return new Response("ok", { headers: corsHeaders });}- const { locationId, userId } = await req.json();+ const { companyId, locationId, userId } = await req.json();let receiptId;try {if (!userId) throw new Error("Payload is missing userId");+ if (!companyId) throw new Error("Payload is missing companyId");await db.transaction().execute(async (trx) => {receiptId = await getNextSequence(trx, "receipt");@@ -29,6 +30,7 @@ serve(async (req: Request) => {.insertInto("receipt").values({receiptId,+ companyId: Number(companyId),locationId: locationId,createdBy: userId,})diff --git a/packages/database/supabase/functions/create-receipt-from-purchase-order/index.ts b/packages/database/supabase/functions/create-receipt-from-purchase-order/index.tsindex b18b86260d..34d349e950 100644--- a/packages/database/supabase/functions/create-receipt-from-purchase-order/index.ts+++ b/packages/database/supabase/functions/create-receipt-from-purchase-order/index.ts@@ -19,6 +19,7 @@ serve(async (req: Request) => {return new Response("ok", { headers: corsHeaders });}const {+ companyId,locationId,purchaseOrderId,receiptId: existingReceiptId,@@ -28,6 +29,7 @@ serve(async (req: Request) => {try {if (!purchaseOrderId) throw new Error("Payload is missing purchaseOrderId");if (!userId) throw new Error("Payload is missing userId");+ if (!companyId) throw new Error("Payload is missing companyId");const client = getSupabaseServiceRole(req.headers.get("Authorization"));@@ -79,6 +81,7 @@ serve(async (req: Request) => {acc.push({lineId: d.id,+ companyId: Number(companyId),partId: d.partId,orderQuantity: d.purchaseQuantity * (d.conversionFactor ?? 1),outstandingQuantity: outstandingQuantity * (d.conversionFactor ?? 1),@@ -128,6 +131,7 @@ serve(async (req: Request) => {sourceDocument: "Purchase Order",sourceDocumentId: purchaseOrder.data.id,sourceDocumentReadableId: purchaseOrder.data.purchaseOrderId,+ companyId: Number(companyId),locationId: locationId,createdBy: userId,})