diff --git a/backend/app/helpers/condition.ts b/backend/app/helpers/condition.ts new file mode 100644 index 0000000..b518293 --- /dev/null +++ b/backend/app/helpers/condition.ts @@ -0,0 +1,32 @@ +/** + * Quy đổi condition gốc từ ERP về condition chuẩn dùng trong hệ thống. + * + * Quy tắc: + * NIB, NOB -> NEW + * USEB -> USED + */ + +/** Các condition gốc từ ERP được phép đồng bộ / lưu trữ. */ +export const SYNCABLE_CONDITIONS = ['NIB', 'NOB', 'USEB'] as const + +/** + * Convert condition gốc -> condition chuẩn (NEW/USED). + * Trả về `null` nếu condition không nằm trong danh sách hỗ trợ (NIB/NOB/USEB). + */ +export function convertCondition(condition?: string | null): string | null { + const normalized = (condition ?? '').trim().toUpperCase() + switch (normalized) { + case 'NIB': + case 'NOB': + return 'NEW' + case 'USEB': + return 'USED' + default: + return null + } +} + +/** Condition gốc có thuộc danh sách được phép đồng bộ (NIB/NOB/USEB) hay không. */ +export function isSyncableCondition(condition?: string | null): boolean { + return convertCondition(condition) !== null +} diff --git a/backend/app/services/erp_service.ts b/backend/app/services/erp_service.ts index d36ad23..e53c70b 100644 --- a/backend/app/services/erp_service.ts +++ b/backend/app/services/erp_service.ts @@ -3,6 +3,7 @@ import logger from '@adonisjs/core/services/logger' import type Product from '#models/product' import type { SupplierPricePoint } from '#models/history' import axios from 'axios' +import { convertCondition } from '#helpers/condition' interface ErpProductListParams { limit?: number @@ -49,7 +50,7 @@ export default class ErpService { limit: 50, skip: 0, order: 'updatedAt desc', - where: { _q: product.sku, condition: product.condition }, + where: { _q: product.sku, condition: convertCondition(product.condition) }, }, } diff --git a/backend/app/services/sync_service.ts b/backend/app/services/sync_service.ts index b3db5ac..7ebc4ce 100644 --- a/backend/app/services/sync_service.ts +++ b/backend/app/services/sync_service.ts @@ -3,6 +3,7 @@ import ProductService from '#services/product_service' import LogService from '#services/log_service' import ErpService, { type ErpProductItem } from '#services/erp_service' import { enqueueProductUpserts } from '#services/queue_service' +import { convertCondition } from '#helpers/condition' export interface SyncSummary { /** Tổng số bản ghi ERP tự báo cáo (field `total`) — CHỈ tham khảo, không tin cậy. */ @@ -75,7 +76,7 @@ export default class SyncService { summary.fetched += page.items.length // Bỏ qua item rác không có sku (sku rỗng gây trùng unique key -> lỗi insert). - const valid = page.items.filter((i) => i.sku && i.sku.trim() !== '') + const valid = page.items.filter((i) => i.sku && i.sku.trim() !== '' && i.condition && ['NIB', 'NOB', 'USEB'].includes(i.condition.toUpperCase())) summary.skipped += page.items.length - valid.length if (valid.length > 0) { diff --git a/backend/package.json b/backend/package.json index 5d5e176..4469087 100644 --- a/backend/package.json +++ b/backend/package.json @@ -17,6 +17,7 @@ "#controllers/*": "./app/controllers/*.js", "#models/*": "./app/models/*.js", "#services/*": "./app/services/*.js", + "#helpers/*": "./app/helpers/*.js", "#validators/*": "./app/validators/*.js", "#middleware/*": "./app/middleware/*.js", "#exceptions/*": "./app/exceptions/*.js", diff --git a/backend/tsconfig.json b/backend/tsconfig.json index 0d9f4f2..95d09df 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -7,6 +7,7 @@ "#controllers/*": ["./app/controllers/*.js"], "#models/*": ["./app/models/*.js"], "#services/*": ["./app/services/*.js"], + "#helpers/*": ["./app/helpers/*.js"], "#validators/*": ["./app/validators/*.js"], "#middleware/*": ["./app/middleware/*.js"], "#exceptions/*": ["./app/exceptions/*.js"],