Database Design
Multi-Factory Supportโ
The multi-factory schema enables scoping core entities (purchase orders, customer shipments, work orders) to specific factories and controlling user access per factory with granular permissions.
ERDโ
Design Decisionsโ
Factoryis the central entity identified by a uniquefactoryCodeUserFactoryAccessis a junction table with a unique constraint on(userId, factoryId)and anAccessLevelenum (viewer,editor,admin)Userhas both a directfactoryId(default factory) and granular per-factory access viaUserFactoryAccessPurchaseOrder,CustomerShipment, andWorkOrdereach have a nullablefactoryIdto support gradual migration of existing data- On factory deletion: entity FKs are SET NULL (preserves records), while
UserFactoryAccessrows CASCADE DELETE - On user deletion:
UserFactoryAccessrows also CASCADE DELETE
Core Order Lifecycleโ
The primary business flow: purchase orders contain customer shipments, which contain work orders. Each work order references a style, color, and size.
ERDโ
Design Decisionsโ
PurchaseOrderis the top-level container โ shipments and work orders both reference itWorkOrderhas FKs to bothPurchaseOrderandCustomerShipment, withpoIdcurrently nullable during migration- Soft deletes are used across all three entities (
isDeleted,deletedAt,deletedBy) CustomerShipmenttracks cancellation state withcancelledShipmentflag and replacement shipment referencesShipmentLabelStatustracks the label lifecycle:PendingโAcknowledgedโPrintedโShipped(orSuspended)- Bilingual support:
StyleandColorhave bothCnandEnname fields
Material Reference Dataโ
Styles have associated garment measurements (per size) and material construction notes. These tables power the work order spec sheets.
ERDโ
Design Decisionsโ
GarmentMeasurementis a junction table with a unique constraint on(styleId, measurementId, size)โ each style has one measurement value per measurement type per sizeMaterialConstructionis a junction table with a unique constraint on(styleId, materialId)โ each style has one set of construction notes per materialMeasurementandMaterialusesortOrderto control display ordering in the UI- Both junction tables cascade delete when their parent
Styleis deleted - All reference tables support bilingual (
Cn/En) fields