Update sync ERP

This commit is contained in:
andrew.ng 2026-06-30 10:03:16 +07:00
parent 56b5bd3d79
commit 64948505e7
5 changed files with 38 additions and 2 deletions

View File

@ -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
}

View File

@ -3,6 +3,7 @@ import logger from '@adonisjs/core/services/logger'
import type Product from '#models/product' import type Product from '#models/product'
import type { SupplierPricePoint } from '#models/history' import type { SupplierPricePoint } from '#models/history'
import axios from 'axios' import axios from 'axios'
import { convertCondition } from '#helpers/condition'
interface ErpProductListParams { interface ErpProductListParams {
limit?: number limit?: number
@ -49,7 +50,7 @@ export default class ErpService {
limit: 50, limit: 50,
skip: 0, skip: 0,
order: 'updatedAt desc', order: 'updatedAt desc',
where: { _q: product.sku, condition: product.condition }, where: { _q: product.sku, condition: convertCondition(product.condition) },
}, },
} }

View File

@ -3,6 +3,7 @@ import ProductService from '#services/product_service'
import LogService from '#services/log_service' import LogService from '#services/log_service'
import ErpService, { type ErpProductItem } from '#services/erp_service' import ErpService, { type ErpProductItem } from '#services/erp_service'
import { enqueueProductUpserts } from '#services/queue_service' import { enqueueProductUpserts } from '#services/queue_service'
import { convertCondition } from '#helpers/condition'
export interface SyncSummary { 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. */ /** 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 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). // 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 summary.skipped += page.items.length - valid.length
if (valid.length > 0) { if (valid.length > 0) {

View File

@ -17,6 +17,7 @@
"#controllers/*": "./app/controllers/*.js", "#controllers/*": "./app/controllers/*.js",
"#models/*": "./app/models/*.js", "#models/*": "./app/models/*.js",
"#services/*": "./app/services/*.js", "#services/*": "./app/services/*.js",
"#helpers/*": "./app/helpers/*.js",
"#validators/*": "./app/validators/*.js", "#validators/*": "./app/validators/*.js",
"#middleware/*": "./app/middleware/*.js", "#middleware/*": "./app/middleware/*.js",
"#exceptions/*": "./app/exceptions/*.js", "#exceptions/*": "./app/exceptions/*.js",

View File

@ -7,6 +7,7 @@
"#controllers/*": ["./app/controllers/*.js"], "#controllers/*": ["./app/controllers/*.js"],
"#models/*": ["./app/models/*.js"], "#models/*": ["./app/models/*.js"],
"#services/*": ["./app/services/*.js"], "#services/*": ["./app/services/*.js"],
"#helpers/*": ["./app/helpers/*.js"],
"#validators/*": ["./app/validators/*.js"], "#validators/*": ["./app/validators/*.js"],
"#middleware/*": ["./app/middleware/*.js"], "#middleware/*": ["./app/middleware/*.js"],
"#exceptions/*": ["./app/exceptions/*.js"], "#exceptions/*": ["./app/exceptions/*.js"],