feat(prestashop): sync mapped statuses from Orbito in 1.1.11
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const {
|
||||
STATUS_SYNC_LOCK_TTL_SECONDS,
|
||||
registerStatusSyncHook,
|
||||
syncOrderStatusToPrestashop,
|
||||
} = require('../src/status-sync');
|
||||
|
||||
const ORDER_ID = '507f191e810c19729de860e1';
|
||||
const OLD_STATUS = '507f191e810c19729de860e2';
|
||||
const CURRENT_STATUS = '507f191e810c19729de860e3';
|
||||
|
||||
function dependencies(overrides = {}) {
|
||||
const calls = { setOrderState: [], released: [], locks: [] };
|
||||
const Core = {
|
||||
getLock: async (name, ttl) => {
|
||||
calls.locks.push([name, ttl]);
|
||||
return 'status-lock';
|
||||
},
|
||||
releaseLock: async id => { calls.released.push(id); },
|
||||
getModuleInfo: () => ({ code: 'core/prestashop' }),
|
||||
getInstanceId: () => 'aaaaaaaaaaaaaaaaaaaaaaaa',
|
||||
callModule: async () => ({ data: [{
|
||||
_id: ORDER_ID,
|
||||
statusId: CURRENT_STATUS,
|
||||
source: {
|
||||
moduleCode: 'core/prestashop',
|
||||
moduleId: 'aaaaaaaaaaaaaaaaaaaaaaaa',
|
||||
sourceId: '21',
|
||||
},
|
||||
}] }),
|
||||
registerHook: async () => true,
|
||||
};
|
||||
const deps = {
|
||||
Core,
|
||||
Log: { info() {}, warn() {}, error() {} },
|
||||
PsOrder: { findOne: async () => ({ psOrderId: 21, orbitoOrderId: ORDER_ID }) },
|
||||
loadConfig: async () => ({
|
||||
apiUrl: 'https://shop.test',
|
||||
apiKey: 'secret',
|
||||
statusMapping: [{ psStateId: 7, orbitoStatusId: CURRENT_STATUS }],
|
||||
}),
|
||||
apiFor: () => ({
|
||||
setOrderState: async (...args) => {
|
||||
calls.setOrderState.push(args);
|
||||
return { unchanged: false };
|
||||
},
|
||||
}),
|
||||
...overrides,
|
||||
};
|
||||
return { deps, calls };
|
||||
}
|
||||
|
||||
test('syncs the current Orbito status to the mapped PrestaShop state', async () => {
|
||||
const { deps, calls } = dependencies();
|
||||
|
||||
const result = await syncOrderStatusToPrestashop({
|
||||
orderId: ORDER_ID,
|
||||
statusId: OLD_STATUS,
|
||||
}, deps);
|
||||
|
||||
assert.deepEqual(calls.setOrderState, [[21, 7]]);
|
||||
assert.deepEqual(calls.locks, [[`prestashop-status-sync:${ORDER_ID}`, STATUS_SYNC_LOCK_TTL_SECONDS]]);
|
||||
assert.deepEqual(calls.released, ['status-lock']);
|
||||
assert.equal(result.orbitoStatusId, CURRENT_STATUS);
|
||||
assert.equal(result.synced, true);
|
||||
});
|
||||
|
||||
test('ignores status events for an order owned by another PrestaShop instance', async () => {
|
||||
const { deps, calls } = dependencies();
|
||||
deps.Core.callModule = async () => ({ data: [{
|
||||
_id: ORDER_ID,
|
||||
statusId: CURRENT_STATUS,
|
||||
source: {
|
||||
moduleCode: 'core/prestashop',
|
||||
moduleId: 'bbbbbbbbbbbbbbbbbbbbbbbb',
|
||||
sourceId: '21',
|
||||
},
|
||||
}] });
|
||||
|
||||
const result = await syncOrderStatusToPrestashop({ orderId: ORDER_ID }, deps);
|
||||
|
||||
assert.deepEqual(calls.setOrderState, []);
|
||||
assert.equal(result.reason, 'different_source_instance');
|
||||
assert.deepEqual(calls.locks, []);
|
||||
assert.deepEqual(calls.released, []);
|
||||
});
|
||||
|
||||
test('does not write an unmapped Orbito status to PrestaShop', async () => {
|
||||
const { deps, calls } = dependencies({
|
||||
loadConfig: async () => ({
|
||||
apiUrl: 'https://shop.test',
|
||||
apiKey: 'secret',
|
||||
statusMapping: [{ psStateId: 2, orbitoStatusId: OLD_STATUS }],
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await syncOrderStatusToPrestashop({ orderId: ORDER_ID }, deps);
|
||||
|
||||
assert.deepEqual(calls.setOrderState, []);
|
||||
assert.equal(result.reason, 'status_not_mapped');
|
||||
});
|
||||
|
||||
test('keeps a concurrent event retryable when the per-order lock is occupied', async () => {
|
||||
const { deps } = dependencies();
|
||||
deps.Core.getLock = async () => null;
|
||||
|
||||
await assert.rejects(
|
||||
() => syncOrderStatusToPrestashop({ orderId: ORDER_ID }, deps),
|
||||
/already running/
|
||||
);
|
||||
});
|
||||
|
||||
test('registers the Orbito order status change hook', async () => {
|
||||
const { deps } = dependencies();
|
||||
let registration;
|
||||
deps.Core.registerHook = async (name, handler) => { registration = { name, handler }; };
|
||||
|
||||
await registerStatusSyncHook(deps);
|
||||
|
||||
assert.equal(registration.name, 'event:order:status_changed');
|
||||
assert.equal(typeof registration.handler, 'function');
|
||||
});
|
||||
Reference in New Issue
Block a user