inventory

This commit is contained in:
2026-03-14 21:23:22 -05:00
parent d21e2e3c0b
commit 472c36915c
14 changed files with 730 additions and 1 deletions

View File

@@ -9,7 +9,7 @@ Current foundation scope includes:
- CRM customers and vendors with create/edit/detail workflows
- CRM search, filtering, status tagging, and reseller hierarchy
- CRM contact history, account contacts, and shared attachments
- inventory item master and BOM foundation
- inventory item master, BOM, warehouse, and stock-location foundation
- file storage and PDF rendering
## Workspace
@@ -85,7 +85,10 @@ The current inventory foundation supports:
- SKU, description, type, status, unit-of-measure, sellable/purchasable, default cost, and notes fields
- BOM header and BOM line editing directly on the item form
- BOM detail display with component SKU, name, quantity, unit, notes, and position
- protected warehouse list, detail, create, and edit flows
- nested stock-location management inside each warehouse record
- seeded sample inventory items and a starter assembly BOM during bootstrap
- seeded sample warehouse and stock locations during bootstrap
This module introduces `inventory.read` and `inventory.write` permissions. After updating the code, restart the server against the migrated database so bootstrap can upsert the new permissions onto the default administrator role.
@@ -109,6 +112,7 @@ As of March 14, 2026, the latest committed domain migrations include:
- CRM commercial terms and account contacts
- CRM lifecycle stages and operational metadata
- inventory item master and BOM foundation
- warehouse and stock-location foundation
## UI Notes

View File

@@ -9,6 +9,7 @@ const links = [
{ to: "/crm/customers", label: "Customers" },
{ to: "/crm/vendors", label: "Vendors" },
{ to: "/inventory/items", label: "Inventory" },
{ to: "/inventory/warehouses", label: "Warehouses" },
{ to: "/planning/gantt", label: "Gantt" },
];

View File

@@ -27,6 +27,9 @@ import type {
InventoryItemStatus,
InventoryItemSummaryDto,
InventoryItemType,
WarehouseDetailDto,
WarehouseInput,
WarehouseSummaryDto,
} from "@mrp/shared/dist/inventory/types.js";
export class ApiError extends Error {
@@ -315,6 +318,32 @@ export const api = {
token
);
},
getWarehouses(token: string) {
return request<WarehouseSummaryDto[]>("/api/v1/inventory/warehouses", undefined, token);
},
getWarehouse(token: string, warehouseId: string) {
return request<WarehouseDetailDto>(`/api/v1/inventory/warehouses/${warehouseId}`, undefined, token);
},
createWarehouse(token: string, payload: WarehouseInput) {
return request<WarehouseDetailDto>(
"/api/v1/inventory/warehouses",
{
method: "POST",
body: JSON.stringify(payload),
},
token
);
},
updateWarehouse(token: string, warehouseId: string, payload: WarehouseInput) {
return request<WarehouseDetailDto>(
`/api/v1/inventory/warehouses/${warehouseId}`,
{
method: "PUT",
body: JSON.stringify(payload),
},
token
);
},
getGanttDemo(token: string) {
return request<{ tasks: GanttTaskDto[]; links: GanttLinkDto[] }>("/api/v1/gantt/demo", undefined, token);
},

View File

@@ -18,6 +18,9 @@ import { GanttPage } from "./modules/gantt/GanttPage";
import { InventoryDetailPage } from "./modules/inventory/InventoryDetailPage";
import { InventoryFormPage } from "./modules/inventory/InventoryFormPage";
import { InventoryItemsPage } from "./modules/inventory/InventoryItemsPage";
import { WarehouseDetailPage } from "./modules/inventory/WarehouseDetailPage";
import { WarehouseFormPage } from "./modules/inventory/WarehouseFormPage";
import { WarehousesPage } from "./modules/inventory/WarehousesPage";
import { ThemeProvider } from "./theme/ThemeProvider";
import "./index.css";
@@ -50,6 +53,8 @@ const router = createBrowserRouter([
children: [
{ path: "/inventory/items", element: <InventoryItemsPage /> },
{ path: "/inventory/items/:itemId", element: <InventoryDetailPage /> },
{ path: "/inventory/warehouses", element: <WarehousesPage /> },
{ path: "/inventory/warehouses/:warehouseId", element: <WarehouseDetailPage /> },
],
},
{
@@ -66,6 +71,8 @@ const router = createBrowserRouter([
children: [
{ path: "/inventory/items/new", element: <InventoryFormPage mode="create" /> },
{ path: "/inventory/items/:itemId/edit", element: <InventoryFormPage mode="edit" /> },
{ path: "/inventory/warehouses/new", element: <WarehouseFormPage mode="create" /> },
{ path: "/inventory/warehouses/:warehouseId/edit", element: <WarehouseFormPage mode="edit" /> },
],
},
{

View File

@@ -0,0 +1,90 @@
import type { WarehouseDetailDto, WarehouseLocationDto } from "@mrp/shared/dist/inventory/types.js";
import { permissions } from "@mrp/shared";
import { useEffect, useState } from "react";
import { Link, useParams } from "react-router-dom";
import { useAuth } from "../../auth/AuthProvider";
import { api, ApiError } from "../../lib/api";
export function WarehouseDetailPage() {
const { token, user } = useAuth();
const { warehouseId } = useParams();
const [warehouse, setWarehouse] = useState<WarehouseDetailDto | null>(null);
const [status, setStatus] = useState("Loading warehouse...");
const canManage = user?.permissions.includes(permissions.inventoryWrite) ?? false;
useEffect(() => {
if (!token || !warehouseId) {
return;
}
api
.getWarehouse(token, warehouseId)
.then((nextWarehouse) => {
setWarehouse(nextWarehouse);
setStatus("Warehouse loaded.");
})
.catch((error: unknown) => {
const message = error instanceof ApiError ? error.message : "Unable to load warehouse.";
setStatus(message);
});
}, [token, warehouseId]);
if (!warehouse) {
return <div className="rounded-[28px] border border-line/70 bg-surface/90 p-8 text-sm text-muted shadow-panel">{status}</div>;
}
return (
<section className="space-y-4">
<div className="rounded-[28px] border border-line/70 bg-surface/90 p-6 shadow-panel 2xl:p-7">
<div className="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
<div>
<p className="text-xs font-semibold uppercase tracking-[0.24em] text-muted">Warehouse Detail</p>
<h3 className="mt-3 text-3xl font-bold text-text">{warehouse.code}</h3>
<p className="mt-2 text-base text-text">{warehouse.name}</p>
<p className="mt-3 text-sm text-muted">Last updated {new Date(warehouse.updatedAt).toLocaleString()}.</p>
</div>
<div className="flex flex-wrap gap-3">
<Link to="/inventory/warehouses" className="inline-flex items-center justify-center rounded-2xl border border-line/70 px-4 py-3 text-sm font-semibold text-text">
Back to warehouses
</Link>
{canManage ? (
<Link to={`/inventory/warehouses/${warehouse.id}/edit`} className="inline-flex items-center justify-center rounded-2xl bg-brand px-5 py-3 text-sm font-semibold text-white">
Edit warehouse
</Link>
) : null}
</div>
</div>
</div>
<div className="grid gap-4 xl:grid-cols-[minmax(0,0.85fr)_minmax(0,1.15fr)]">
<article className="rounded-[28px] border border-line/70 bg-surface/90 p-6 shadow-panel 2xl:p-7">
<p className="text-xs font-semibold uppercase tracking-[0.24em] text-muted">Notes</p>
<p className="mt-4 whitespace-pre-line text-sm leading-7 text-text">{warehouse.notes || "No warehouse notes recorded."}</p>
<div className="mt-8 rounded-2xl border border-line/70 bg-page/70 px-4 py-4 text-sm text-muted">
Created {new Date(warehouse.createdAt).toLocaleDateString()}
</div>
</article>
<section className="rounded-[28px] border border-line/70 bg-surface/90 p-6 shadow-panel 2xl:p-7">
<p className="text-xs font-semibold uppercase tracking-[0.24em] text-muted">Locations</p>
<h4 className="mt-3 text-xl font-bold text-text">Stock locations</h4>
{warehouse.locations.length === 0 ? (
<div className="mt-6 rounded-3xl border border-dashed border-line/70 bg-page/60 px-6 py-12 text-center text-sm text-muted">
No stock locations have been defined for this warehouse yet.
</div>
) : (
<div className="mt-6 grid gap-3 xl:grid-cols-2">
{warehouse.locations.map((location: WarehouseLocationDto) => (
<article key={location.id} className="rounded-3xl border border-line/70 bg-page/60 px-4 py-4">
<div className="text-sm font-semibold text-text">{location.code}</div>
<div className="mt-1 text-sm text-text">{location.name}</div>
<div className="mt-2 text-xs leading-6 text-muted">{location.notes || "No notes."}</div>
</article>
))}
</div>
)}
</section>
</div>
</section>
);
}

View File

@@ -0,0 +1,172 @@
import type { WarehouseInput, WarehouseLocationInput } from "@mrp/shared/dist/inventory/types.js";
import { useEffect, useState } from "react";
import { Link, useNavigate, useParams } from "react-router-dom";
import { useAuth } from "../../auth/AuthProvider";
import { api, ApiError } from "../../lib/api";
import { emptyWarehouseInput, emptyWarehouseLocationInput } from "./config";
export function WarehouseFormPage({ mode }: { mode: "create" | "edit" }) {
const navigate = useNavigate();
const { warehouseId } = useParams();
const { token } = useAuth();
const [form, setForm] = useState<WarehouseInput>(emptyWarehouseInput);
const [status, setStatus] = useState(mode === "create" ? "Create a new warehouse." : "Loading warehouse...");
const [isSaving, setIsSaving] = useState(false);
useEffect(() => {
if (mode !== "edit" || !token || !warehouseId) {
return;
}
api
.getWarehouse(token, warehouseId)
.then((warehouse) => {
setForm({
code: warehouse.code,
name: warehouse.name,
notes: warehouse.notes,
locations: warehouse.locations.map((location: WarehouseLocationInput) => ({
code: location.code,
name: location.name,
notes: location.notes,
})),
});
setStatus("Warehouse loaded.");
})
.catch((error: unknown) => {
const message = error instanceof ApiError ? error.message : "Unable to load warehouse.";
setStatus(message);
});
}, [mode, token, warehouseId]);
function updateField<Key extends keyof WarehouseInput>(key: Key, value: WarehouseInput[Key]) {
setForm((current: WarehouseInput) => ({ ...current, [key]: value }));
}
function updateLocation(index: number, nextLocation: WarehouseLocationInput) {
setForm((current: WarehouseInput) => ({
...current,
locations: current.locations.map((location: WarehouseLocationInput, locationIndex: number) =>
locationIndex === index ? nextLocation : location
),
}));
}
function addLocation() {
setForm((current: WarehouseInput) => ({
...current,
locations: [...current.locations, emptyWarehouseLocationInput],
}));
}
function removeLocation(index: number) {
setForm((current: WarehouseInput) => ({
...current,
locations: current.locations.filter((_location: WarehouseLocationInput, locationIndex: number) => locationIndex !== index),
}));
}
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
if (!token) {
return;
}
setIsSaving(true);
setStatus("Saving warehouse...");
try {
const saved =
mode === "create" ? await api.createWarehouse(token, form) : await api.updateWarehouse(token, warehouseId ?? "", form);
navigate(`/inventory/warehouses/${saved.id}`);
} catch (error: unknown) {
const message = error instanceof ApiError ? error.message : "Unable to save warehouse.";
setStatus(message);
setIsSaving(false);
}
}
return (
<form className="space-y-6" onSubmit={handleSubmit}>
<section className="rounded-[28px] border border-line/70 bg-surface/90 p-6 shadow-panel 2xl:p-7">
<div className="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
<div>
<p className="text-xs font-semibold uppercase tracking-[0.24em] text-muted">Warehouse Editor</p>
<h3 className="mt-3 text-2xl font-bold text-text">{mode === "create" ? "New Warehouse" : "Edit Warehouse"}</h3>
</div>
<Link
to={mode === "create" ? "/inventory/warehouses" : `/inventory/warehouses/${warehouseId}`}
className="inline-flex items-center justify-center rounded-2xl border border-line/70 px-4 py-3 text-sm font-semibold text-text"
>
Cancel
</Link>
</div>
</section>
<section className="space-y-5 rounded-[28px] border border-line/70 bg-surface/90 p-6 shadow-panel 2xl:p-7">
<div className="grid gap-4 xl:grid-cols-2">
<label className="block">
<span className="mb-2 block text-sm font-semibold text-text">Warehouse code</span>
<input value={form.code} onChange={(event) => updateField("code", event.target.value)} className="w-full rounded-2xl border border-line/70 bg-page px-4 py-3 text-text outline-none transition focus:border-brand" />
</label>
<label className="block">
<span className="mb-2 block text-sm font-semibold text-text">Warehouse name</span>
<input value={form.name} onChange={(event) => updateField("name", event.target.value)} className="w-full rounded-2xl border border-line/70 bg-page px-4 py-3 text-text outline-none transition focus:border-brand" />
</label>
</div>
<label className="block">
<span className="mb-2 block text-sm font-semibold text-text">Notes</span>
<textarea value={form.notes} onChange={(event) => updateField("notes", event.target.value)} rows={4} className="w-full rounded-3xl border border-line/70 bg-page px-4 py-3 text-text outline-none transition focus:border-brand" />
</label>
</section>
<section className="rounded-[28px] border border-line/70 bg-surface/90 p-6 shadow-panel 2xl:p-7">
<div className="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
<div>
<p className="text-xs font-semibold uppercase tracking-[0.24em] text-muted">Locations</p>
<h4 className="mt-3 text-xl font-bold text-text">Internal stock locations</h4>
</div>
<button type="button" onClick={addLocation} className="inline-flex items-center justify-center rounded-2xl border border-line/70 px-4 py-3 text-sm font-semibold text-text">
Add location
</button>
</div>
{form.locations.length === 0 ? (
<div className="mt-5 rounded-3xl border border-dashed border-line/70 bg-page/60 px-6 py-10 text-center text-sm text-muted">
No locations added yet.
</div>
) : (
<div className="mt-5 space-y-4">
{form.locations.map((location: WarehouseLocationInput, index: number) => (
<div key={`${location.code}-${index}`} className="rounded-3xl border border-line/70 bg-page/60 p-4">
<div className="grid gap-4 xl:grid-cols-[0.7fr_1fr_auto]">
<label className="block">
<span className="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-muted">Code</span>
<input value={location.code} onChange={(event) => updateLocation(index, { ...location, code: event.target.value })} className="w-full rounded-2xl border border-line/70 bg-surface px-4 py-3 text-text outline-none transition focus:border-brand" />
</label>
<label className="block">
<span className="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-muted">Name</span>
<input value={location.name} onChange={(event) => updateLocation(index, { ...location, name: event.target.value })} className="w-full rounded-2xl border border-line/70 bg-surface px-4 py-3 text-text outline-none transition focus:border-brand" />
</label>
<div className="flex items-end">
<button type="button" onClick={() => removeLocation(index)} className="rounded-2xl border border-rose-400/40 px-4 py-3 text-sm font-semibold text-rose-700 dark:text-rose-300">
Remove
</button>
</div>
</div>
<label className="mt-4 block">
<span className="mb-2 block text-xs font-semibold uppercase tracking-[0.16em] text-muted">Notes</span>
<input value={location.notes} onChange={(event) => updateLocation(index, { ...location, notes: event.target.value })} className="w-full rounded-2xl border border-line/70 bg-surface px-4 py-3 text-text outline-none transition focus:border-brand" />
</label>
</div>
))}
</div>
)}
<div className="mt-6 flex flex-col gap-3 rounded-2xl border border-line/70 bg-page/70 px-4 py-4 sm:flex-row sm:items-center sm:justify-between">
<span className="min-w-0 text-sm text-muted">{status}</span>
<button type="submit" disabled={isSaving} className="rounded-2xl bg-brand px-5 py-3 text-sm font-semibold text-white disabled:cursor-not-allowed disabled:opacity-60">
{isSaving ? "Saving..." : mode === "create" ? "Create warehouse" : "Save changes"}
</button>
</div>
</section>
</form>
);
}

View File

@@ -0,0 +1,82 @@
import { permissions } from "@mrp/shared";
import type { WarehouseSummaryDto } from "@mrp/shared/dist/inventory/types.js";
import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import { useAuth } from "../../auth/AuthProvider";
import { api, ApiError } from "../../lib/api";
export function WarehousesPage() {
const { token, user } = useAuth();
const [warehouses, setWarehouses] = useState<WarehouseSummaryDto[]>([]);
const [status, setStatus] = useState("Loading warehouses...");
const canManage = user?.permissions.includes(permissions.inventoryWrite) ?? false;
useEffect(() => {
if (!token) {
return;
}
api
.getWarehouses(token)
.then((nextWarehouses) => {
setWarehouses(nextWarehouses);
setStatus(`${nextWarehouses.length} warehouse(s) available.`);
})
.catch((error: unknown) => {
const message = error instanceof ApiError ? error.message : "Unable to load warehouses.";
setStatus(message);
});
}, [token]);
return (
<section className="rounded-[28px] border border-line/70 bg-surface/90 p-6 shadow-panel 2xl:p-7">
<div className="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
<div>
<p className="text-xs font-semibold uppercase tracking-[0.24em] text-muted">Inventory</p>
<h3 className="mt-3 text-2xl font-bold text-text">Warehouses</h3>
<p className="mt-2 max-w-2xl text-sm text-muted">Physical warehouse records and their internal stock locations.</p>
</div>
{canManage ? (
<Link to="/inventory/warehouses/new" className="inline-flex items-center justify-center rounded-2xl bg-brand px-5 py-3 text-sm font-semibold text-white">
New warehouse
</Link>
) : null}
</div>
<div className="mt-6 rounded-2xl border border-line/70 bg-page/60 px-4 py-3 text-sm text-muted">{status}</div>
{warehouses.length === 0 ? (
<div className="mt-6 rounded-3xl border border-dashed border-line/70 bg-page/60 px-6 py-12 text-center text-sm text-muted">
No warehouses have been added yet.
</div>
) : (
<div className="mt-6 overflow-hidden rounded-2xl border border-line/70">
<table className="min-w-full divide-y divide-line/70 text-sm">
<thead className="bg-page/80 text-left text-muted">
<tr>
<th className="px-4 py-3">Code</th>
<th className="px-4 py-3">Name</th>
<th className="px-4 py-3">Locations</th>
<th className="px-4 py-3">Updated</th>
</tr>
</thead>
<tbody className="divide-y divide-line/70 bg-surface">
{warehouses.map((warehouse) => (
<tr key={warehouse.id} className="transition hover:bg-page/70">
<td className="px-4 py-3 font-semibold text-text">
<Link to={`/inventory/warehouses/${warehouse.id}`} className="hover:text-brand">
{warehouse.code}
</Link>
</td>
<td className="px-4 py-3 text-muted">{warehouse.name}</td>
<td className="px-4 py-3 text-muted">{warehouse.locationCount}</td>
<td className="px-4 py-3 text-muted">{new Date(warehouse.updatedAt).toLocaleDateString()}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</section>
);
}

View File

@@ -4,6 +4,8 @@ import {
inventoryUnitsOfMeasure,
type InventoryBomLineInput,
type InventoryItemInput,
type WarehouseInput,
type WarehouseLocationInput,
type InventoryItemStatus,
type InventoryItemType,
type InventoryUnitOfMeasure,
@@ -31,6 +33,19 @@ export const emptyInventoryItemInput: InventoryItemInput = {
bomLines: [],
};
export const emptyWarehouseLocationInput: WarehouseLocationInput = {
code: "",
name: "",
notes: "",
};
export const emptyWarehouseInput: WarehouseInput = {
code: "",
name: "",
notes: "",
locations: [],
};
export const inventoryTypeOptions: Array<{ value: InventoryItemType; label: string }> = [
{ value: "PURCHASED", label: "Purchased" },
{ value: "MANUFACTURED", label: "Manufactured" },

View File

@@ -0,0 +1,23 @@
CREATE TABLE "Warehouse" (
"id" TEXT NOT NULL PRIMARY KEY,
"code" TEXT NOT NULL,
"name" TEXT NOT NULL,
"notes" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
CREATE TABLE "WarehouseLocation" (
"id" TEXT NOT NULL PRIMARY KEY,
"warehouseId" TEXT NOT NULL,
"code" TEXT NOT NULL,
"name" TEXT NOT NULL,
"notes" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
CONSTRAINT "WarehouseLocation_warehouseId_fkey" FOREIGN KEY ("warehouseId") REFERENCES "Warehouse" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE UNIQUE INDEX "Warehouse_code_key" ON "Warehouse"("code");
CREATE UNIQUE INDEX "WarehouseLocation_warehouseId_code_key" ON "WarehouseLocation"("warehouseId", "code");
CREATE INDEX "WarehouseLocation_warehouseId_idx" ON "WarehouseLocation"("warehouseId");

View File

@@ -119,6 +119,16 @@ model InventoryItem {
usedInBomLines InventoryBomLine[] @relation("InventoryBomComponent")
}
model Warehouse {
id String @id @default(cuid())
code String @unique
name String
notes String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
locations WarehouseLocation[]
}
model Customer {
id String @id @default(cuid())
name String
@@ -169,6 +179,20 @@ model InventoryBomLine {
@@index([componentItemId])
}
model WarehouseLocation {
id String @id @default(cuid())
warehouseId String
code String
name String
notes String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
warehouse Warehouse @relation(fields: [warehouseId], references: [id], onDelete: Cascade)
@@unique([warehouseId, code])
@@index([warehouseId])
}
model Vendor {
id String @id @default(cuid())
name String

View File

@@ -230,4 +230,33 @@ export async function bootstrapAppData() {
],
});
}
if ((await prisma.warehouse.count()) === 0) {
await prisma.warehouse.create({
data: {
code: "MAIN",
name: "Main Warehouse",
notes: "Primary stocking location for finished goods and purchased materials.",
locations: {
create: [
{
code: "RECV",
name: "Receiving",
notes: "Initial inbound inspection and receipt staging.",
},
{
code: "STOCK-A1",
name: "Aisle A1",
notes: "General rack storage for standard material.",
},
{
code: "FG-STAGE",
name: "Finished Goods Staging",
notes: "Outbound-ready finished assemblies.",
},
],
},
},
});
}
}

View File

@@ -7,10 +7,14 @@ import { fail, ok } from "../../lib/http.js";
import { requirePermissions } from "../../lib/rbac.js";
import {
createInventoryItem,
createWarehouse,
getInventoryItemById,
getWarehouseById,
listInventoryItemOptions,
listInventoryItems,
listWarehouses,
updateInventoryItem,
updateWarehouse,
} from "./service.js";
const bomLineSchema = z.object({
@@ -41,6 +45,19 @@ const inventoryListQuerySchema = z.object({
type: z.enum(inventoryItemTypes).optional(),
});
const warehouseLocationSchema = z.object({
code: z.string().trim().min(1).max(64),
name: z.string().trim().min(1).max(160),
notes: z.string(),
});
const warehouseSchema = z.object({
code: z.string().trim().min(1).max(64),
name: z.string().trim().min(1).max(160),
notes: z.string(),
locations: z.array(warehouseLocationSchema),
});
function getRouteParam(value: unknown) {
return typeof value === "string" ? value : null;
}
@@ -113,3 +130,49 @@ inventoryRouter.put("/items/:itemId", requirePermissions([permissions.inventoryW
return ok(response, item);
});
inventoryRouter.get("/warehouses", requirePermissions([permissions.inventoryRead]), async (_request, response) => {
return ok(response, await listWarehouses());
});
inventoryRouter.get("/warehouses/:warehouseId", requirePermissions([permissions.inventoryRead]), async (request, response) => {
const warehouseId = getRouteParam(request.params.warehouseId);
if (!warehouseId) {
return fail(response, 400, "INVALID_INPUT", "Warehouse id is invalid.");
}
const warehouse = await getWarehouseById(warehouseId);
if (!warehouse) {
return fail(response, 404, "WAREHOUSE_NOT_FOUND", "Warehouse was not found.");
}
return ok(response, warehouse);
});
inventoryRouter.post("/warehouses", requirePermissions([permissions.inventoryWrite]), async (request, response) => {
const parsed = warehouseSchema.safeParse(request.body);
if (!parsed.success) {
return fail(response, 400, "INVALID_INPUT", "Warehouse payload is invalid.");
}
return ok(response, await createWarehouse(parsed.data), 201);
});
inventoryRouter.put("/warehouses/:warehouseId", requirePermissions([permissions.inventoryWrite]), async (request, response) => {
const warehouseId = getRouteParam(request.params.warehouseId);
if (!warehouseId) {
return fail(response, 400, "INVALID_INPUT", "Warehouse id is invalid.");
}
const parsed = warehouseSchema.safeParse(request.body);
if (!parsed.success) {
return fail(response, 400, "INVALID_INPUT", "Warehouse payload is invalid.");
}
const warehouse = await updateWarehouse(warehouseId, parsed.data);
if (!warehouse) {
return fail(response, 404, "WAREHOUSE_NOT_FOUND", "Warehouse was not found.");
}
return ok(response, warehouse);
});

View File

@@ -3,6 +3,11 @@ import type {
InventoryBomLineInput,
InventoryItemDetailDto,
InventoryItemInput,
WarehouseDetailDto,
WarehouseInput,
WarehouseLocationDto,
WarehouseLocationInput,
WarehouseSummaryDto,
InventoryItemStatus,
InventoryItemSummaryDto,
InventoryItemType,
@@ -41,6 +46,23 @@ type InventoryDetailRecord = {
bomLines: BomLineRecord[];
};
type WarehouseLocationRecord = {
id: string;
code: string;
name: string;
notes: string;
};
type WarehouseDetailRecord = {
id: string;
code: string;
name: string;
notes: string;
createdAt: Date;
updatedAt: Date;
locations: WarehouseLocationRecord[];
};
function mapBomLine(record: BomLineRecord): InventoryBomLineDto {
return {
id: record.id,
@@ -54,6 +76,15 @@ function mapBomLine(record: BomLineRecord): InventoryBomLineDto {
};
}
function mapWarehouseLocation(record: WarehouseLocationRecord): WarehouseLocationDto {
return {
id: record.id,
code: record.code,
name: record.name,
notes: record.notes,
};
}
function mapSummary(record: {
id: string;
sku: string;
@@ -102,6 +133,37 @@ function mapDetail(record: InventoryDetailRecord): InventoryItemDetailDto {
};
}
function mapWarehouseSummary(record: {
id: string;
code: string;
name: string;
updatedAt: Date;
_count: { locations: number };
}): WarehouseSummaryDto {
return {
id: record.id,
code: record.code,
name: record.name,
locationCount: record._count.locations,
updatedAt: record.updatedAt.toISOString(),
};
}
function mapWarehouseDetail(record: WarehouseDetailRecord): WarehouseDetailDto {
return {
...mapWarehouseSummary({
id: record.id,
code: record.code,
name: record.name,
updatedAt: record.updatedAt,
_count: { locations: record.locations.length },
}),
notes: record.notes,
createdAt: record.createdAt.toISOString(),
locations: record.locations.map(mapWarehouseLocation),
};
}
interface InventoryListFilters {
query?: string;
status?: InventoryItemStatus;
@@ -138,6 +200,16 @@ function normalizeBomLines(bomLines: InventoryBomLineInput[]) {
.filter((line) => line.componentItemId.trim().length > 0);
}
function normalizeWarehouseLocations(locations: WarehouseLocationInput[]) {
return locations
.map((location) => ({
code: location.code.trim(),
name: location.name.trim(),
notes: location.notes,
}))
.filter((location) => location.code.length > 0 && location.name.length > 0);
}
async function validateBomLines(parentItemId: string | null, bomLines: InventoryBomLineInput[]) {
const normalized = normalizeBomLines(bomLines);
@@ -325,3 +397,87 @@ export async function updateInventoryItem(itemId: string, payload: InventoryItem
return mapDetail(item);
}
export async function listWarehouses() {
const warehouses = await prisma.warehouse.findMany({
include: {
_count: {
select: {
locations: true,
},
},
},
orderBy: [{ code: "asc" }],
});
return warehouses.map(mapWarehouseSummary);
}
export async function getWarehouseById(warehouseId: string) {
const warehouse = await prisma.warehouse.findUnique({
where: { id: warehouseId },
include: {
locations: {
orderBy: [{ code: "asc" }],
},
},
});
return warehouse ? mapWarehouseDetail(warehouse) : null;
}
export async function createWarehouse(payload: WarehouseInput) {
const locations = normalizeWarehouseLocations(payload.locations);
const warehouse = await prisma.warehouse.create({
data: {
code: payload.code.trim(),
name: payload.name.trim(),
notes: payload.notes,
locations: locations.length
? {
create: locations,
}
: undefined,
},
include: {
locations: {
orderBy: [{ code: "asc" }],
},
},
});
return mapWarehouseDetail(warehouse);
}
export async function updateWarehouse(warehouseId: string, payload: WarehouseInput) {
const existingWarehouse = await prisma.warehouse.findUnique({
where: { id: warehouseId },
});
if (!existingWarehouse) {
return null;
}
const locations = normalizeWarehouseLocations(payload.locations);
const warehouse = await prisma.warehouse.update({
where: { id: warehouseId },
data: {
code: payload.code.trim(),
name: payload.name.trim(),
notes: payload.notes,
locations: {
deleteMany: {},
create: locations,
},
},
include: {
locations: {
orderBy: [{ code: "asc" }],
},
},
});
return mapWarehouseDetail(warehouse);
}

View File

@@ -31,6 +31,40 @@ export interface InventoryItemOptionDto {
name: string;
}
export interface WarehouseLocationDto {
id: string;
code: string;
name: string;
notes: string;
}
export interface WarehouseLocationInput {
code: string;
name: string;
notes: string;
}
export interface WarehouseSummaryDto {
id: string;
code: string;
name: string;
locationCount: number;
updatedAt: string;
}
export interface WarehouseDetailDto extends WarehouseSummaryDto {
notes: string;
createdAt: string;
locations: WarehouseLocationDto[];
}
export interface WarehouseInput {
code: string;
name: string;
notes: string;
locations: WarehouseLocationInput[];
}
export interface InventoryItemSummaryDto {
id: string;
sku: string;