Skip to content

API & Services Reference

WFP MVP không expose REST API public. Toàn bộ I/O đi qua Supabase client + service layer trong src/services/. Trang này tham chiếu services chính.

Tổng quan service layer

ServiceFilePhụ trách
adjustmentServiceadjustmentService.tsCRUD adjustment
forecastServiceforecastService.tsRead forecast_results, trigger nightly
reviewServicereviewService.tsState machine NEW→SEEN→ACCEPTED/REJECTED
reportServicereportService.tsAggregate cho warehouse/customer reports
configServiceconfigService.tsRead/write config_values
auditServiceauditService.tsInsert audit_log (insert-only)
historicalImportServicehistoricalImportService.tsParse + upsert historical_orders
customerServicecustomerService.tsCustomer CRUD + tier
warehouseServicewarehouseService.tsWarehouse CRUD

Convention chung

  • Mọi service trả về typed result (Zod-validated khi cross boundary)
  • Error: ném Error với message tiếng Việt + code field
  • Audit: service tự ghi audit row khi có side-effect
  • Auth: supabase.auth.getUser() ở mọi insert/update

Hook layer (React Query)

HookFileService gọi
useForecasthooks/useForecast.tsforecastService
useAdjustmentshooks/useAdjustments.tsadjustmentService
useConfighooks/useConfig.tsconfigService
useCustomershooks/useCustomers.tscustomerService
useWarehouseshooks/useWarehouses.tswarehouseService

Engine functions (pure)

engine/forecast/generator.ts

ts
function generate(input: GenerateInput): GenerateOutput
Input fieldTypeNote
baselineBaselineResolutiontừ historical-aggregator
monthly_multipliernumber ≥ 0từ config
event_multipliernumber ≥ 0từ config
Output fieldType
forecast_qtynumber (rounded)
raw_qtynumber (pre-round)
baseline_avgnumber
resolved_event_typeEventType
fallback_usedboolean
p85number | null
sample_sizenumber

engine/wlu/calculator.ts

FunctionMô tả
uphLookupFromMap(map, warehouseId)Wrap UPH config thành lookup
computeWlu(input)WLU/đơn (phút)
computeTotalWorkload(input)Tổng phút nhân lực
minutesToHeadcount(min, shift=480)Headcount cho ca 8h

Supabase tables (DDL summary)

Xem chi tiết trong Database. Liệt kê quick:

TablePKFK chính
warehousesid
customersid
customer_warehouseidcustomer_id, warehouse_id
historical_ordersidcustomer_warehouse_id
forecast_adjustmentsidcustomer_warehouse_id
forecast_resultsidcustomer_warehouse_id
config_valuesidconfig_definitions.id
audit_logidactor_id (auth.users)

RPC (Postgres functions)

RPCPurpose
rpc_run_nightly_forecast()Trigger nightly compute (chạy bởi pg_cron)
rpc_accept_adjustment(adjustment_id, audited_qty, reason)State change + qty edit + 2 audit row atomically
rpc_reject_adjustment(adjustment_id, reason)State → REJECTED + audit

Implementation đầy đủ: xem migration M5_review.sql trong supabase-schema.md.

Examples

ts
import { adjustmentService } from '@/services/adjustmentService';

await adjustmentService.submit({
  customerCode: 'LOREAL_VN',
  warehouseCode: 'HCM_01',
  targetDate: '2026-06-15',
  qty: 5500,
  eventType: 'MID_MONTH',
  note: 'Mid-Month sale, KOL livestream',
});
// → row state=NEW, audit_log insert SUBMIT_ADJUSTMENT
ts
import { reviewService } from '@/services/reviewService';

await reviewService.accept({
  adjustmentId: '...',
  auditedQty: 5200,           // optional override
  reason: 'Buffer xuống 5200, ổn hơn',
});
// → state=ACCEPTED, 2 audit rows
ts
import { useForecast } from '@/hooks/useForecast';

const { data, isLoading } = useForecast({
  warehouseId,
  from: '2026-06-01',
  to: '2026-06-14',
});