720 lines
24 KiB
JavaScript
720 lines
24 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const Sequelize = require('sequelize');
|
|
const OrderDeliveryMigrationService = require('../../../../app/service/systems/order_delivery_migration');
|
|
const { matchesWhere } = require('../../../support/order_service_test_helper');
|
|
|
|
function cloneRow(row) {
|
|
return {
|
|
...(row || {}),
|
|
};
|
|
}
|
|
|
|
function buildOrderRows(count, overrides = {}) {
|
|
return Array.from({ length: count }, (_, index) => ({
|
|
id: index + 1,
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
delivery_order_code: `OD-${String(index + 1).padStart(5, '0')}`,
|
|
step: 4,
|
|
outbound_status: 'fin',
|
|
delivery_time: new Date('2026-04-12T00:00:00Z'),
|
|
updated_at: '2026-04-12 00:00:00',
|
|
...overrides,
|
|
}));
|
|
}
|
|
|
|
function buildItemRows(orderRows, overrides = {}) {
|
|
return (Array.isArray(orderRows) ? orderRows : []).map((orderRow, index) => ({
|
|
id: index + 10001,
|
|
order_id: orderRow.id,
|
|
actual_price: 1.00,
|
|
retail_price: 2.00,
|
|
ext_code: `EXT-${String(index + 1).padStart(5, '0')}`,
|
|
inventory_type: 'default',
|
|
item_code: `ITEM-${String(index + 1).padStart(5, '0')}`,
|
|
item_id: index + 20001,
|
|
item_name: `商品-${String(index + 1).padStart(5, '0')}`,
|
|
order_line_no: `LINE-${String(index + 1).padStart(5, '0')}`,
|
|
owner_code: 'OWNER-001',
|
|
plan_qty: 1,
|
|
source_order_code: orderRow.delivery_order_code,
|
|
sub_source_order_code: null,
|
|
extend_props: {
|
|
source: 'migration-test',
|
|
},
|
|
stock_id: index + 30001,
|
|
created_at: '2026-04-12 00:00:00',
|
|
updated_at: '2026-04-12 00:00:00',
|
|
...overrides,
|
|
}));
|
|
}
|
|
|
|
function buildDispatchKey(row) {
|
|
return [ row.delivery_order_code, row.store_id, row.warehouse_id ].join('::');
|
|
}
|
|
|
|
function buildRetryableDatabaseError(message, code = 1205) {
|
|
const error = new Error(message);
|
|
error.name = 'SequelizeDatabaseError';
|
|
error.code = code;
|
|
error.parent = {
|
|
code,
|
|
errno: code,
|
|
};
|
|
|
|
return error;
|
|
}
|
|
|
|
function createTransaction(calls) {
|
|
return {
|
|
committed: false,
|
|
rolledBack: false,
|
|
operations: [],
|
|
touchedModels: new Set(),
|
|
async commit() {
|
|
this.committed = true;
|
|
calls.commitCount += 1;
|
|
|
|
for (const model of this.touchedModels) {
|
|
if (typeof model.commitOperations === 'function') {
|
|
model.commitOperations(this.operations);
|
|
}
|
|
}
|
|
},
|
|
async rollback() {
|
|
this.rolledBack = true;
|
|
calls.rollbackCount += 1;
|
|
this.operations.length = 0;
|
|
this.touchedModels.clear();
|
|
},
|
|
};
|
|
}
|
|
|
|
function applyOperations(rows, operations, options = {}) {
|
|
let currentRows = Array.isArray(rows) ? rows.map(cloneRow) : [];
|
|
const keySelector = typeof options.keySelector === 'function'
|
|
? options.keySelector
|
|
: row => row && row.id;
|
|
const assignInsertedId = Boolean(options.assignInsertedId);
|
|
|
|
for (const operation of Array.isArray(operations) ? operations : []) {
|
|
if (operation.type === 'bulkCreate') {
|
|
const existingKeys = new Set(currentRows
|
|
.map(row => keySelector(row))
|
|
.filter(value => value !== undefined && value !== null && value !== ''));
|
|
let nextId = currentRows.reduce((maxId, row) => {
|
|
const rowId = Number(row && row.id);
|
|
return Number.isFinite(rowId) && rowId > maxId ? rowId : maxId;
|
|
}, 0) + 1;
|
|
|
|
for (const row of Array.isArray(operation.rows) ? operation.rows : []) {
|
|
const nextRow = cloneRow(row);
|
|
|
|
if (assignInsertedId && (!Number.isFinite(Number(nextRow.id)) || Number(nextRow.id) <= 0)) {
|
|
nextRow.id = nextId;
|
|
nextId += 1;
|
|
}
|
|
|
|
const rowKey = keySelector(nextRow);
|
|
|
|
if (operation.ignoreDuplicates && rowKey !== undefined && rowKey !== null && rowKey !== '' && existingKeys.has(rowKey)) {
|
|
continue;
|
|
}
|
|
|
|
currentRows.push(nextRow);
|
|
|
|
if (rowKey !== undefined && rowKey !== null && rowKey !== '') {
|
|
existingKeys.add(rowKey);
|
|
}
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
if (operation.type === 'destroy') {
|
|
currentRows = currentRows.filter(row => !matchesWhere(row, operation.where));
|
|
continue;
|
|
}
|
|
|
|
if (operation.type === 'update') {
|
|
currentRows = currentRows.map(row => {
|
|
if (!matchesWhere(row, operation.where)) {
|
|
return row;
|
|
}
|
|
|
|
return {
|
|
...row,
|
|
...(operation.values || {}),
|
|
};
|
|
});
|
|
}
|
|
}
|
|
|
|
return currentRows;
|
|
}
|
|
|
|
function createTransactionalModel(rows, capture, options = {}) {
|
|
let storedRows = Array.isArray(rows) ? rows.map(cloneRow) : [];
|
|
const modelId = options.modelId || 'model';
|
|
const keySelector = typeof options.keySelector === 'function'
|
|
? options.keySelector
|
|
: row => row && row.id;
|
|
const assignInsertedId = Boolean(options.assignInsertedId);
|
|
|
|
const model = {
|
|
async findAll(query = {}) {
|
|
capture.findAllOptions.push({
|
|
modelId,
|
|
where: query.where,
|
|
limit: query.limit,
|
|
order: query.order,
|
|
transaction: query.transaction,
|
|
});
|
|
|
|
const transactionOperations = query.transaction
|
|
? query.transaction.operations.filter(operation => operation.modelId === modelId)
|
|
: [];
|
|
let visibleRows = applyOperations(storedRows, transactionOperations, {
|
|
keySelector,
|
|
assignInsertedId,
|
|
});
|
|
|
|
visibleRows = visibleRows.filter(row => matchesWhere(row, query.where));
|
|
|
|
if (Array.isArray(query.order) && query.order.length > 0) {
|
|
const [ column, direction ] = query.order[0];
|
|
const sortFactor = String(direction || 'ASC').toUpperCase() === 'DESC' ? -1 : 1;
|
|
|
|
visibleRows = [ ...visibleRows ].sort((left, right) => {
|
|
const leftValue = left[column];
|
|
const rightValue = right[column];
|
|
|
|
if (leftValue === rightValue) {
|
|
return 0;
|
|
}
|
|
|
|
return leftValue > rightValue ? sortFactor : -sortFactor;
|
|
});
|
|
}
|
|
|
|
const limitValue = query.limit === undefined ? visibleRows.length : Number.parseInt(query.limit, 10);
|
|
const limit = Number.isFinite(limitValue) ? Math.max(limitValue, 0) : visibleRows.length;
|
|
|
|
return visibleRows.slice(0, limit).map(cloneRow);
|
|
},
|
|
async bulkCreate(rowsToCreate, query = {}) {
|
|
capture.bulkCreateOptions.push({
|
|
modelId,
|
|
transaction: query.transaction,
|
|
ignoreDuplicates: query.ignoreDuplicates,
|
|
});
|
|
capture.bulkCreateRows.push({
|
|
modelId,
|
|
rows: rowsToCreate.map(cloneRow),
|
|
});
|
|
|
|
const transactionOperations = query.transaction
|
|
? query.transaction.operations.filter(operation => operation.modelId === modelId)
|
|
: [];
|
|
const visibleRows = applyOperations(storedRows, transactionOperations, {
|
|
keySelector,
|
|
assignInsertedId,
|
|
});
|
|
const stagedRows = rowsToCreate.map(cloneRow);
|
|
|
|
if (assignInsertedId) {
|
|
let nextId = visibleRows.reduce((maxId, row) => {
|
|
const rowId = Number(row && row.id);
|
|
return Number.isFinite(rowId) && rowId > maxId ? rowId : maxId;
|
|
}, 0) + 1;
|
|
|
|
for (const row of stagedRows) {
|
|
if (!Number.isFinite(Number(row.id)) || Number(row.id) <= 0) {
|
|
row.id = nextId;
|
|
nextId += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (query.transaction) {
|
|
query.transaction.operations.push({
|
|
modelId,
|
|
type: 'bulkCreate',
|
|
rows: stagedRows,
|
|
ignoreDuplicates: Boolean(query.ignoreDuplicates),
|
|
});
|
|
query.transaction.touchedModels.add(model);
|
|
} else {
|
|
storedRows = applyOperations(storedRows, [{
|
|
modelId,
|
|
type: 'bulkCreate',
|
|
rows: stagedRows,
|
|
ignoreDuplicates: Boolean(query.ignoreDuplicates),
|
|
}], {
|
|
keySelector,
|
|
assignInsertedId,
|
|
});
|
|
}
|
|
|
|
return stagedRows.map(cloneRow);
|
|
},
|
|
async destroy(query = {}) {
|
|
capture.destroyOptions.push({
|
|
modelId,
|
|
where: query.where,
|
|
transaction: query.transaction,
|
|
});
|
|
|
|
if (query.transaction) {
|
|
query.transaction.operations.push({
|
|
modelId,
|
|
type: 'destroy',
|
|
where: query.where || {},
|
|
});
|
|
query.transaction.touchedModels.add(model);
|
|
|
|
return 0;
|
|
}
|
|
|
|
const beforeCount = storedRows.length;
|
|
storedRows = storedRows.filter(row => !matchesWhere(row, query.where));
|
|
|
|
return beforeCount - storedRows.length;
|
|
},
|
|
async update(values, query = {}) {
|
|
capture.updateOptions.push({
|
|
modelId,
|
|
values: {
|
|
...(values || {}),
|
|
},
|
|
where: query.where,
|
|
transaction: query.transaction,
|
|
});
|
|
capture.updateRows.push({
|
|
modelId,
|
|
values: {
|
|
...(values || {}),
|
|
},
|
|
where: query.where,
|
|
});
|
|
|
|
const transactionOperations = query.transaction
|
|
? query.transaction.operations.filter(operation => operation.modelId === modelId)
|
|
: [];
|
|
const visibleRows = applyOperations(storedRows, transactionOperations, {
|
|
keySelector,
|
|
assignInsertedId,
|
|
});
|
|
const affectedCount = visibleRows.filter(row => matchesWhere(row, query.where)).length;
|
|
|
|
if (query.transaction) {
|
|
query.transaction.operations.push({
|
|
modelId,
|
|
type: 'update',
|
|
values: {
|
|
...(values || {}),
|
|
},
|
|
where: query.where || {},
|
|
});
|
|
query.transaction.touchedModels.add(model);
|
|
|
|
return [ affectedCount ];
|
|
}
|
|
|
|
storedRows = applyOperations(storedRows, [{
|
|
modelId,
|
|
type: 'update',
|
|
values: {
|
|
...(values || {}),
|
|
},
|
|
where: query.where || {},
|
|
}], {
|
|
keySelector,
|
|
assignInsertedId,
|
|
});
|
|
|
|
return [ affectedCount ];
|
|
},
|
|
commitOperations(operations) {
|
|
const modelOperations = Array.isArray(operations)
|
|
? operations.filter(operation => operation.modelId === modelId)
|
|
: [];
|
|
|
|
storedRows = applyOperations(storedRows, modelOperations, {
|
|
keySelector,
|
|
assignInsertedId,
|
|
});
|
|
},
|
|
get rows() {
|
|
return storedRows.map(cloneRow);
|
|
},
|
|
};
|
|
|
|
return model;
|
|
}
|
|
|
|
function createHarness(options = {}) {
|
|
const capture = {
|
|
findAllOptions: [],
|
|
bulkCreateOptions: [],
|
|
bulkCreateRows: [],
|
|
destroyOptions: [],
|
|
updateOptions: [],
|
|
updateRows: [],
|
|
commitCount: 0,
|
|
rollbackCount: 0,
|
|
};
|
|
|
|
const sourceOrderModel = createTransactionalModel(options.sourceRows, capture, {
|
|
modelId: 'sourceOrder',
|
|
keySelector: row => row && row.id,
|
|
});
|
|
const targetOrderModel = createTransactionalModel(options.targetRows || [], capture, {
|
|
modelId: 'targetOrder',
|
|
keySelector: row => buildDispatchKey(row),
|
|
assignInsertedId: true,
|
|
});
|
|
const sourceItemModel = createTransactionalModel(options.sourceItemRows || [], capture, {
|
|
modelId: 'sourceItem',
|
|
keySelector: row => row && row.id,
|
|
});
|
|
const targetItemModel = createTransactionalModel(options.targetItemRows || [], capture, {
|
|
modelId: 'targetItem',
|
|
keySelector: row => row && row.id,
|
|
});
|
|
|
|
const transactionCalls = [];
|
|
const ctx = {
|
|
app: {
|
|
Sequelize,
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
debug() {},
|
|
},
|
|
model: {
|
|
async transaction() {
|
|
const transaction = createTransaction(capture);
|
|
transactionCalls.push(transaction);
|
|
return transaction;
|
|
},
|
|
OrderDeliveryFactory: {
|
|
async getCurrentModel() {
|
|
return sourceOrderModel;
|
|
},
|
|
async getDispatchModel() {
|
|
return targetOrderModel;
|
|
},
|
|
async listManagedTableNames() {
|
|
return [ 'wms_order_delivery_100000' ];
|
|
},
|
|
},
|
|
OrderItemsFactory: {
|
|
async getCurrentModel() {
|
|
return sourceItemModel;
|
|
},
|
|
async getDispatchModel() {
|
|
return targetItemModel;
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const service = new OrderDeliveryMigrationService(ctx);
|
|
|
|
return {
|
|
service,
|
|
capture,
|
|
transactionCalls,
|
|
sourceOrderModel,
|
|
targetOrderModel,
|
|
sourceItemModel,
|
|
targetItemModel,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/order_delivery_migration.test.js', () => {
|
|
it('should only migrate outbound rows that match the outbound condition', async () => {
|
|
const sourceRows = [
|
|
{
|
|
id: 1,
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
delivery_order_code: 'OD-00001',
|
|
step: 4,
|
|
outbound_status: 'fin',
|
|
delivery_time: new Date('2026-04-12T00:00:00Z'),
|
|
updated_at: '2026-04-12 00:00:00',
|
|
},
|
|
{
|
|
id: 2,
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
delivery_order_code: 'OD-00002',
|
|
step: 4,
|
|
outbound_status: 'padding',
|
|
delivery_time: new Date('2026-04-12T00:00:00Z'),
|
|
updated_at: '2026-04-12 00:00:00',
|
|
},
|
|
{
|
|
id: 3,
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
delivery_order_code: 'OD-00003',
|
|
step: 3,
|
|
outbound_status: 'fin',
|
|
delivery_time: new Date('2026-04-12T00:00:00Z'),
|
|
updated_at: '2026-04-12 00:00:00',
|
|
},
|
|
{
|
|
id: 4,
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
delivery_order_code: 'OD-00004',
|
|
step: 9,
|
|
outbound_status: 'fin',
|
|
delivery_time: new Date('2026-04-12T00:00:00Z'),
|
|
updated_at: '2026-04-12 00:00:00',
|
|
},
|
|
{
|
|
id: 5,
|
|
warehouse_id: 100000,
|
|
store_id: 1000000,
|
|
delivery_order_code: 'OD-00005',
|
|
step: 4,
|
|
outbound_status: 'none',
|
|
delivery_time: null,
|
|
updated_at: '2026-04-12 00:00:00',
|
|
},
|
|
];
|
|
const { service, capture, sourceOrderModel, targetOrderModel, sourceItemModel, targetItemModel } = createHarness({
|
|
sourceRows,
|
|
sourceItemRows: buildItemRows(sourceRows),
|
|
});
|
|
|
|
const result = await service.syncOutboundOrdersToDelivery({
|
|
warehouseIds: [ 100000 ],
|
|
});
|
|
|
|
assert.equal(result.totalSelected, 3);
|
|
assert.equal(result.totalCreated, 3);
|
|
assert.equal(result.results[0].selectedCount, 3);
|
|
assert.equal(result.results[0].createdCount, 3);
|
|
assert.deepEqual(capture.findAllOptions[0].where.outbound_status[Sequelize.Op.in], [ 'fin', 'padding' ]);
|
|
assert.deepEqual(capture.findAllOptions[0].where.step[Sequelize.Op.in], [ 4, 9 ]);
|
|
assert.equal(capture.bulkCreateRows.length, 2);
|
|
assert.equal(capture.bulkCreateRows[0].modelId, 'targetOrder');
|
|
assert.equal(capture.bulkCreateRows[1].modelId, 'targetItem');
|
|
assert.equal(capture.bulkCreateRows[0].rows[0].dispatch_lane, 'm4');
|
|
assert.equal(capture.bulkCreateRows[0].rows[0].id, 1);
|
|
assert.equal(sourceOrderModel.rows.length, 2);
|
|
assert.equal(targetOrderModel.rows.length, 3);
|
|
assert.equal(sourceItemModel.rows.length, 2);
|
|
assert.equal(targetItemModel.rows.length, 3);
|
|
});
|
|
|
|
it('should migrate order items into dispatch table and delete source rows in the same transaction', async () => {
|
|
const sourceRows = buildOrderRows(2);
|
|
const { service, capture, transactionCalls, sourceOrderModel, targetOrderModel, sourceItemModel, targetItemModel } = createHarness({
|
|
sourceRows,
|
|
sourceItemRows: buildItemRows(sourceRows),
|
|
});
|
|
|
|
const result = await service.syncOutboundOrdersToDelivery({
|
|
warehouseIds: [ 100000 ],
|
|
});
|
|
|
|
assert.equal(result.totalSelected, 2);
|
|
assert.equal(result.totalCreated, 2);
|
|
assert.equal(transactionCalls.length, 1);
|
|
assert.equal(transactionCalls[0].committed, true);
|
|
assert.equal(transactionCalls[0].rolledBack, false);
|
|
assert.equal(capture.findAllOptions.every(option => option.transaction === transactionCalls[0]), true);
|
|
assert.equal(capture.bulkCreateOptions.every(option => option.transaction === transactionCalls[0]), true);
|
|
assert.equal(capture.destroyOptions.every(option => option.transaction === transactionCalls[0]), true);
|
|
assert.equal(sourceOrderModel.rows.length, 0);
|
|
assert.equal(targetOrderModel.rows.length, 2);
|
|
assert.equal(sourceItemModel.rows.length, 0);
|
|
assert.equal(targetItemModel.rows.length, 2);
|
|
|
|
assert.deepEqual(targetOrderModel.rows.map(row => row.id).sort((a, b) => a - b), [ 1, 2 ]);
|
|
const targetOrderIdSet = new Set(targetOrderModel.rows.map(row => row.id));
|
|
assert.equal(targetItemModel.rows.every(row => targetOrderIdSet.has(Number(row.order_id))), true);
|
|
});
|
|
|
|
it('should cap each batch at 1000 rows and execute the insert inside one transaction', async () => {
|
|
const sourceRows = buildOrderRows(1005);
|
|
const { service, capture, transactionCalls, targetOrderModel, sourceOrderModel, targetItemModel, sourceItemModel } = createHarness({
|
|
sourceRows,
|
|
sourceItemRows: buildItemRows(sourceRows),
|
|
});
|
|
|
|
const result = await service.syncOutboundOrdersToDelivery({
|
|
warehouseIds: [ 100000 ],
|
|
});
|
|
|
|
assert.equal(result.totalSelected, 1000);
|
|
assert.equal(result.totalCreated, 1000);
|
|
assert.equal(result.results[0].selectedCount, 1000);
|
|
assert.equal(capture.findAllOptions[0].limit, 1000);
|
|
assert.equal(capture.bulkCreateRows[0].rows.length, 1000);
|
|
assert.equal(capture.bulkCreateRows[1].rows.length, 1000);
|
|
assert.equal(transactionCalls.length, 1);
|
|
assert.equal(transactionCalls[0].committed, true);
|
|
assert.equal(transactionCalls[0].rolledBack, false);
|
|
assert.equal(capture.findAllOptions.every(option => option.transaction === transactionCalls[0]), true);
|
|
assert.equal(capture.bulkCreateOptions.every(option => option.transaction === transactionCalls[0]), true);
|
|
assert.equal(capture.destroyOptions.every(option => option.transaction === transactionCalls[0]), true);
|
|
assert.equal(targetOrderModel.rows.length, 1000);
|
|
assert.equal(targetItemModel.rows.length, 1000);
|
|
assert.equal(sourceOrderModel.rows.length, 5);
|
|
assert.equal(sourceItemModel.rows.length, 5);
|
|
});
|
|
|
|
it('should skip already migrated orders on repeated runs', async () => {
|
|
const sourceRows = buildOrderRows(2);
|
|
const { service, targetOrderModel, targetItemModel, sourceOrderModel, sourceItemModel } = createHarness({
|
|
sourceRows,
|
|
sourceItemRows: buildItemRows(sourceRows),
|
|
});
|
|
|
|
const firstResult = await service.syncOutboundOrdersToDelivery({
|
|
warehouseIds: [ 100000 ],
|
|
});
|
|
const secondResult = await service.syncOutboundOrdersToDelivery({
|
|
warehouseIds: [ 100000 ],
|
|
});
|
|
|
|
assert.equal(firstResult.totalCreated, 2);
|
|
assert.equal(secondResult.totalCreated, 0);
|
|
assert.equal(targetOrderModel.rows.length, 2);
|
|
assert.equal(targetItemModel.rows.length, 2);
|
|
assert.equal(sourceOrderModel.rows.length, 0);
|
|
assert.equal(sourceItemModel.rows.length, 0);
|
|
assert.equal(new Set(targetOrderModel.rows.map(row => row.delivery_order_code)).size, 2);
|
|
assert.deepEqual(targetOrderModel.rows.map(row => row.id).sort((a, b) => a - b), [ 1, 2 ]);
|
|
});
|
|
|
|
it('should rollback the whole batch when any selected source row lacks dispatch key fields', async () => {
|
|
const sourceRows = buildOrderRows(2);
|
|
delete sourceRows[1].store_id;
|
|
|
|
const { service, capture, transactionCalls, sourceOrderModel, targetOrderModel, sourceItemModel, targetItemModel } = createHarness({
|
|
sourceRows,
|
|
sourceItemRows: buildItemRows(sourceRows),
|
|
});
|
|
|
|
await assert.rejects(
|
|
service.syncOutboundOrdersToDelivery({
|
|
warehouseIds: [ 100000 ],
|
|
}),
|
|
/缺少 delivery_order_code\/store_id\/warehouse_id/
|
|
);
|
|
|
|
assert.equal(transactionCalls.length, 1);
|
|
assert.equal(transactionCalls[0].committed, false);
|
|
assert.equal(transactionCalls[0].rolledBack, true);
|
|
assert.equal(capture.bulkCreateRows.length, 0);
|
|
assert.equal(capture.destroyOptions.length, 0);
|
|
assert.equal(sourceOrderModel.rows.length, 2);
|
|
assert.equal(targetOrderModel.rows.length, 0);
|
|
assert.equal(sourceItemModel.rows.length, 2);
|
|
assert.equal(targetItemModel.rows.length, 0);
|
|
});
|
|
|
|
it('should rollback source and target changes when item migration fails', async () => {
|
|
const sourceRows = buildOrderRows(1);
|
|
const { service, sourceOrderModel, targetOrderModel, sourceItemModel, targetItemModel, transactionCalls } = createHarness({
|
|
sourceRows,
|
|
sourceItemRows: buildItemRows(sourceRows),
|
|
});
|
|
|
|
targetItemModel.bulkCreate = async () => {
|
|
throw new Error('item migration failed');
|
|
};
|
|
|
|
await assert.rejects(
|
|
service.syncOutboundOrdersToDelivery({
|
|
warehouseIds: [ 100000 ],
|
|
}),
|
|
/item migration failed/
|
|
);
|
|
|
|
assert.equal(transactionCalls.length, 1);
|
|
assert.equal(transactionCalls[0].committed, false);
|
|
assert.equal(transactionCalls[0].rolledBack, true);
|
|
assert.equal(sourceOrderModel.rows.length, 1);
|
|
assert.equal(targetOrderModel.rows.length, 0);
|
|
assert.equal(sourceItemModel.rows.length, 1);
|
|
assert.equal(targetItemModel.rows.length, 0);
|
|
});
|
|
|
|
it('should mark source orders as timeout without changing step when normal migration hits retryable lock errors', async () => {
|
|
const sourceRows = buildOrderRows(1, {
|
|
step: 9,
|
|
});
|
|
const { service, capture, transactionCalls, sourceOrderModel, targetOrderModel, sourceItemModel, targetItemModel } = createHarness({
|
|
sourceRows,
|
|
sourceItemRows: buildItemRows(sourceRows),
|
|
});
|
|
|
|
targetOrderModel.bulkCreate = async () => {
|
|
throw buildRetryableDatabaseError('Lock wait timeout exceeded; try restarting transaction', 1205);
|
|
};
|
|
|
|
const result = await service.syncOutboundOrdersToDelivery({
|
|
warehouseIds: [ 100000 ],
|
|
});
|
|
|
|
assert.equal(result.totalSelected, 1);
|
|
assert.equal(result.totalCreated, 0);
|
|
assert.equal(result.totalTimeout, 1);
|
|
assert.equal(result.results[0].retryableFailure, true);
|
|
assert.equal(result.results[0].retryableFailureType, 'lock_wait_timeout');
|
|
assert.equal(result.results[0].timeoutCount, 1);
|
|
assert.equal(transactionCalls.length, 1);
|
|
assert.equal(transactionCalls[0].committed, false);
|
|
assert.equal(transactionCalls[0].rolledBack, true);
|
|
assert.equal(capture.updateOptions.length, 1);
|
|
assert.equal(capture.updateOptions[0].values.outbound_status, 'timeout');
|
|
assert.equal('step' in capture.updateOptions[0].values, false);
|
|
assert.equal(sourceOrderModel.rows.length, 1);
|
|
assert.equal(sourceOrderModel.rows[0].outbound_status, 'timeout');
|
|
assert.equal(sourceOrderModel.rows[0].step, 9);
|
|
assert.equal(targetOrderModel.rows.length, 0);
|
|
assert.equal(sourceItemModel.rows.length, 1);
|
|
assert.equal(targetItemModel.rows.length, 0);
|
|
});
|
|
|
|
it('should retry only timeout outbound rows in the long task', async () => {
|
|
const sourceRows = buildOrderRows(2);
|
|
sourceRows[0].outbound_status = 'timeout';
|
|
sourceRows[1].outbound_status = 'fin';
|
|
const { service, capture, sourceOrderModel, targetOrderModel, sourceItemModel, targetItemModel } = createHarness({
|
|
sourceRows,
|
|
sourceItemRows: buildItemRows(sourceRows),
|
|
});
|
|
|
|
const result = await service.retryTimeoutOutboundOrdersToDelivery({
|
|
warehouseIds: [ 100000 ],
|
|
});
|
|
|
|
assert.equal(result.totalSelected, 1);
|
|
assert.equal(result.totalCreated, 1);
|
|
assert.equal(result.totalTimeout, 0);
|
|
assert.deepEqual(capture.findAllOptions[0].where.step[Sequelize.Op.in], [ 4, 9 ]);
|
|
assert.deepEqual(capture.findAllOptions[0].where.outbound_status[Sequelize.Op.in], [ 'timeout' ]);
|
|
assert.equal(sourceOrderModel.rows.length, 1);
|
|
assert.equal(sourceOrderModel.rows[0].outbound_status, 'fin');
|
|
assert.equal(targetOrderModel.rows.length, 1);
|
|
assert.equal(targetOrderModel.rows[0].id, 1);
|
|
assert.equal(sourceItemModel.rows.length, 1);
|
|
assert.equal(targetItemModel.rows.length, 1);
|
|
assert.equal(targetItemModel.rows[0].order_id, targetOrderModel.rows[0].id);
|
|
});
|
|
});
|