fix(cleaner): guard missing session refresh config

This commit is contained in:
Misaka
2026-04-24 19:11:21 +08:00
parent e39fc87869
commit 664f26d63f
2 changed files with 25 additions and 5 deletions

View File

@@ -27,6 +27,7 @@ import type { InsertMaterialDetailInput, InsertOrderInput } from '../../types/cl
import type { OrderMapping } from '../../types/order-resolver.types' import type { OrderMapping } from '../../types/order-resolver.types'
const log = createLogger('CleanerApplicationService') const log = createLogger('CleanerApplicationService')
const DEFAULT_SESSION_REFRESH_ORDER_THRESHOLD = 160
export class CleanerApplicationService { export class CleanerApplicationService {
async runCleaner( async runCleaner(
@@ -148,9 +149,8 @@ export class CleanerApplicationService {
log.info('Login successful') log.info('Login successful')
const totalOrders = validOrderNumbers.length const totalOrders = validOrderNumbers.length
const cleanerConfig = configManager.getConfig().cleaner
const effectiveSessionRefreshOrderThreshold = const effectiveSessionRefreshOrderThreshold =
input.sessionRefreshOrderThreshold ?? cleanerConfig.sessionRefreshOrderThreshold this.resolveSessionRefreshOrderThreshold(input, configManager)
this.sendProgress(eventSender, 'ERP 登录成功', (1 / (1 + totalOrders)) * 100, { this.sendProgress(eventSender, 'ERP 登录成功', (1 / (1 + totalOrders)) * 100, {
phase: 'login', phase: 'login',
currentOrderIndex: 0, currentOrderIndex: 0,
@@ -455,9 +455,10 @@ export class CleanerApplicationService {
: result.errors.length > 0 : result.errors.length > 0
? AuditStatus.FAILURE ? AuditStatus.FAILURE
: AuditStatus.SUCCESS : AuditStatus.SUCCESS
const cleanerConfig = ConfigManager.getInstance().getConfig().cleaner const effectiveSessionRefreshOrderThreshold = this.resolveSessionRefreshOrderThreshold(
const effectiveSessionRefreshOrderThreshold = input,
input.sessionRefreshOrderThreshold ?? cleanerConfig.sessionRefreshOrderThreshold ConfigManager.getInstance()
)
logAuditWithCurrentUser(AuditAction.CLEAN, 'MATERIAL_PLAN', status, { logAuditWithCurrentUser(AuditAction.CLEAN, 'MATERIAL_PLAN', status, {
orderCount, orderCount,
@@ -471,6 +472,17 @@ export class CleanerApplicationService {
}) })
} }
private resolveSessionRefreshOrderThreshold(
input: CleanerInput,
configManager: Pick<ConfigManager, 'getConfig'>
): number {
return (
input.sessionRefreshOrderThreshold ??
configManager.getConfig().cleaner?.sessionRefreshOrderThreshold ??
DEFAULT_SESSION_REFRESH_ORDER_THRESHOLD
)
}
/** /**
* Save attempt results to database: update order statuses, insert material details, * Save attempt results to database: update order statuses, insert material details,
* and update execution status. * and update execution status.

View File

@@ -279,6 +279,14 @@ describe('CleanerApplicationService', () => {
expect(result.ordersProcessed).toBe(1) expect(result.ordersProcessed).toBe(1)
}) })
it('should fall back to default session refresh threshold when cleaner config is missing', async () => {
const eventSender: any = { send: vi.fn() }
await service.runCleaner(eventSender, makeInput())
expect(lastCleanerInput?.sessionRefreshOrderThreshold).toBe(160)
})
it('should close ERP browser on success', async () => { it('should close ERP browser on success', async () => {
await service.runCleaner({ send: vi.fn() } as any, makeInput()) await service.runCleaner({ send: vi.fn() } as any, makeInput())