fix(prestashop): import customer messages across checkout sources (1.1.8)
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const Module = require('node:module');
|
||||
const path = require('node:path');
|
||||
|
||||
function loadImporter(Core, PsOrder) {
|
||||
const importerPath = path.resolve(__dirname, '../src/OrderImporter.js');
|
||||
const originalLoad = Module._load;
|
||||
Module._load = function(request, parent, isMain) {
|
||||
if (request === '@orbito/api-lib') return { Core, Log: { debug() {}, info() {}, warn() {}, error() {} } };
|
||||
if (request === './models/PsOrder') return PsOrder;
|
||||
return originalLoad.call(this, request, parent, isMain);
|
||||
};
|
||||
delete require.cache[importerPath];
|
||||
try {
|
||||
return require(importerPath);
|
||||
} finally {
|
||||
Module._load = originalLoad;
|
||||
}
|
||||
}
|
||||
|
||||
test('imports only public PrestaShop customer messages as customer_message notes', async () => {
|
||||
let createPayload;
|
||||
const Core = {
|
||||
getModuleInfo: () => ({ code: 'core/prestashop' }),
|
||||
callModule: async (module, method, params) => {
|
||||
if (module === 'core/orders' && method === 'status.search') {
|
||||
return { results: [{ _id: '000000000000000000000001', positionInGroup: 1 }] };
|
||||
}
|
||||
if (module === 'core/products' && method === 'upsert') return {};
|
||||
if (module === 'core/orders' && method === 'create') {
|
||||
createPayload = params;
|
||||
return { _id: 'order-1' };
|
||||
}
|
||||
throw new Error(`Unexpected core call: ${module}.${method}`);
|
||||
},
|
||||
};
|
||||
const { importFromPSOrder } = loadImporter(Core, { findOne: async () => null, update: async () => {} });
|
||||
const order = {
|
||||
id: '1', id_cart: '15', reference: 'PS-1', current_state: '1', id_customer: '5', id_address_delivery: '6',
|
||||
id_currency: '1', id_carrier: '0', payment: 'Card', date_add: '2026-01-01 10:00:00',
|
||||
total_discounts: '0', total_paid_real: '0', total_shipping_tax_excl: '0', associations: {
|
||||
order_rows: [{ product_id: '10', product_attribute_id: '0', product_name: 'Product', product_reference: 'SKU', product_ean13: '', unit_price_tax_excl: '10', unit_price_tax_incl: '12.3', product_quantity: '1' }],
|
||||
},
|
||||
};
|
||||
const api = {
|
||||
isBLSupported: async () => false,
|
||||
makeRequest: async endpoint => {
|
||||
if (endpoint === 'orders/1') return { order };
|
||||
if (endpoint === 'customers/5') return { customer: { firstname: 'Ada', lastname: 'Lovelace', email: 'ada@test' } };
|
||||
if (endpoint === 'addresses/6') return { address: { firstname: 'Ada', lastname: 'Lovelace', address1: 'Street 1', city: 'Warsaw', postcode: '00-001', id_country: '1' } };
|
||||
throw new Error(`Unexpected API request: ${endpoint}`);
|
||||
},
|
||||
getProduct: async () => ({ name: 'Product', weight: '1', type: 'standard', associations: {} }),
|
||||
getProductCoverImageUrl: async () => null,
|
||||
getCurrency: async () => ({ iso_code: 'PLN' }),
|
||||
getCountry: async () => ({ iso_code: 'PL' }),
|
||||
getCustomerNotes: async (orderId, cartId, giftMessage) => {
|
||||
assert.equal(orderId, '1');
|
||||
assert.equal(cartId, '15');
|
||||
assert.equal(giftMessage, undefined);
|
||||
return ['Proszę zadzwonić przed dostawą', 'Tekst z checkoutu'];
|
||||
},
|
||||
getStockAvailabilities: async () => [],
|
||||
getManufacturer: async () => null,
|
||||
};
|
||||
|
||||
await importFromPSOrder('1', api, { _id: 'default', orderStatus: '000000000000000000000001', statusMapping: [] });
|
||||
|
||||
assert.deepEqual(createPayload.notes, [
|
||||
{ text: 'Proszę zadzwonić przed dostawą', type: 'customer_message' },
|
||||
{ text: 'Tekst z checkoutu', type: 'customer_message' },
|
||||
]);
|
||||
});
|
||||
@@ -73,6 +73,7 @@ test('order import upserts verified metadata for the ordered product and pack it
|
||||
getProductCoverImageUrl: async () => null,
|
||||
getCurrency: async () => ({ iso_code: 'PLN' }),
|
||||
getCountry: async () => ({ iso_code: 'PL' }),
|
||||
getCustomerNotes: async () => [],
|
||||
getStockAvailabilities: async () => [],
|
||||
getManufacturer: async () => null,
|
||||
};
|
||||
|
||||
@@ -107,3 +107,89 @@ test('does not turn an unsafe reference filter into an empty combination match',
|
||||
|
||||
assert.equal(await api.findProductCombinationsByReference('1', 'SKU[1]'), null);
|
||||
});
|
||||
|
||||
test('collects public customer notes from order, cart and customer thread without cross-order leakage', async t => {
|
||||
const originalFetch = Core.fetch;
|
||||
t.after(() => { Core.fetch = originalFetch; });
|
||||
const calls = [];
|
||||
Core.fetch = async url => {
|
||||
const parsed = new URL(url);
|
||||
calls.push(parsed);
|
||||
const orderId = parsed.searchParams.get('filter[id_order]');
|
||||
const cartId = parsed.searchParams.get('filter[id_cart]');
|
||||
const threadId = parsed.searchParams.get('filter[id_customer_thread]');
|
||||
if (parsed.pathname.endsWith('/messages') && orderId === '[7]') return response(200, {
|
||||
messages: [
|
||||
{ id: '1', id_order: '7', id_cart: '11', private: '0', id_employee: '0', message: 'Dostawa rano' },
|
||||
{ id: '2', id_order: '7', id_cart: '11', private: '1', id_employee: '0', message: 'Prywatna' },
|
||||
{ id: '3', id_order: '7', id_cart: '11', private: '0', id_employee: '5', message: 'Pracownik' },
|
||||
{ id: '4', id_order: '8', id_cart: '11', private: '0', id_employee: '0', message: 'Obce zamówienie' },
|
||||
{ id: '7', id_order: '7', id_cart: '11', private: '0', id_employee: '0', message: 42 },
|
||||
{ id: '8', id_order: '7', id_cart: '11', private: '0', id_employee: '0', message: {} },
|
||||
{ id: '9', id_order: '7', id_cart: '11', private: '0', id_employee: '0', message: ' ' },
|
||||
{ id: '10', id_order: '7', id_cart: '11', private: '0', id_employee: '0', message: 'Proszę o kontakt\r\npo 17:00' },
|
||||
],
|
||||
});
|
||||
if (parsed.pathname.endsWith('/messages') && cartId === '[11]') return response(200, {
|
||||
messages: [
|
||||
{ id: '1', id_order: '7', id_cart: '11', private: '0', id_employee: '0', message: 'Dostawa rano' },
|
||||
{ id: '5', id_order: '0', id_cart: '11', private: 'false', id_employee: '', message: 'Kod bramy: 1234' },
|
||||
{ id: '6', id_order: '8', id_cart: '11', private: '0', id_employee: '0', message: 'Drugi order z koszyka' },
|
||||
],
|
||||
});
|
||||
if (parsed.pathname.endsWith('/customer_threads')) return response(200, {
|
||||
customer_threads: [{ id: '30', id_order: '7' }, { id: '31', id_order: '8' }],
|
||||
});
|
||||
if (parsed.pathname.endsWith('/customer_messages') && threadId === '[30]') return response(200, {
|
||||
customer_messages: [
|
||||
{ id: '40', id_customer_thread: '30', id_employee: '0', message: 'Dostawa rano' },
|
||||
{ id: '41', id_customer_thread: '30', id_employee: '2', message: 'Odpowiedź pracownika' },
|
||||
{ id: '42', id_customer_thread: '31', id_employee: '0', message: 'Obcy wątek' },
|
||||
{ id: '43', id_customer_thread: '30', id_employee: '0', private: '2', message: 'Prywatny niestandardowy' },
|
||||
{ id: '44', id_customer_thread: '30', id_employee: '0', message: 'Proszę o kontakt\npo 17:00' },
|
||||
{ id: '45', id_customer_thread: '30', id_employee: '0', message: 'Dzwonek nie działa' },
|
||||
],
|
||||
});
|
||||
assert.fail(`Unexpected endpoint: ${url}`);
|
||||
};
|
||||
const api = new PrestashopApi('https://shop.example', 'secret', true, { maxGetAttempts: 1 });
|
||||
|
||||
assert.deepEqual(await api.getCustomerNotes('7', '11', 'Dla Ani'), [
|
||||
'Wiadomość dołączona do prezentu: Dla Ani',
|
||||
'Dostawa rano',
|
||||
'Proszę o kontakt\r\npo 17:00',
|
||||
'Dzwonek nie działa',
|
||||
'Kod bramy: 1234',
|
||||
]);
|
||||
assert.equal(calls.filter(call => call.pathname.endsWith('/customer_messages')).length, 1);
|
||||
});
|
||||
|
||||
test('keeps notes from available sources when one customer-note endpoint is forbidden', async t => {
|
||||
const originalFetch = Core.fetch;
|
||||
t.after(() => { Core.fetch = originalFetch; });
|
||||
Core.fetch = async url => {
|
||||
const parsed = new URL(url);
|
||||
if (parsed.pathname.endsWith('/messages') && parsed.searchParams.get('filter[id_order]') === '[7]') {
|
||||
return response(403, { errors: [] });
|
||||
}
|
||||
if (parsed.pathname.endsWith('/messages') && parsed.searchParams.get('filter[id_cart]') === '[11]') {
|
||||
return response(200, { messages: [{ id_cart: '11', id_order: '0', private: '0', id_employee: '0', message: 'Checkout działa' }] });
|
||||
}
|
||||
if (parsed.pathname.endsWith('/customer_threads')) return response(200, { customer_threads: [] });
|
||||
assert.fail(`Unexpected endpoint: ${url}`);
|
||||
};
|
||||
const api = new PrestashopApi('https://shop.example', 'secret', true, { maxGetAttempts: 1 });
|
||||
|
||||
assert.deepEqual(await api.getCustomerNotes('7', '11'), ['Checkout działa']);
|
||||
});
|
||||
|
||||
test('retains gift_message when every customer-note endpoint is unavailable', async t => {
|
||||
const originalFetch = Core.fetch;
|
||||
t.after(() => { Core.fetch = originalFetch; });
|
||||
Core.fetch = async () => response(403, { errors: [] });
|
||||
const api = new PrestashopApi('https://shop.example', 'secret', true, { maxGetAttempts: 1 });
|
||||
|
||||
assert.deepEqual(await api.getCustomerNotes('7', '11', 'Dla Ani'), [
|
||||
'Wiadomość dołączona do prezentu: Dla Ani',
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user