File size: 2,947 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';
/*

 * MOCK Order Management extension + flexfield inventory (O2C-INT-003 stand-in; DR-O2C-INT-003 says the mock "should pre-seed an

 * equivalent object exposing the same output keys": om_extensions, page_extensions). NOT Oracle software. The extensions below are

 * SYNTHETIC examples that really execute in the mock order flow; the pod's real inventory and count are POD-REQUIRED.

 * Each entry declares how to trigger it and the observable effect, so a test can iterate the inventory generically.

 */
const { err } = require('../../core/errors');

const OM_EXTENSIONS = [
  { name: 'ZZ_DEFAULT_SHIP_INSTRUCTIONS (MOCK)', event: 'On Save', kind: 'default',
    trigger: {}, effect: { kind: 'attribute', label: 'Shipping Instructions', value: 'ZZ Deliver to dock 3 (MOCK)' } },
  { name: 'ZZ_PARTNER_ORDER_NEEDS_PO (MOCK)', event: 'On Start of Submission Request', kind: 'validation',
    trigger: { salesChannel: 'Partner', customerPO: '' }, effect: { kind: 'message', text: 'ZZ: Partner orders require a customer purchase order (MOCK extension).' } },
  { name: 'ZZ_LOG_SUBMISSION (MOCK)', event: 'On End of Submission Request', kind: 'log',
    trigger: {}, effect: { kind: 'log', table: 'Extension Log', text: 'ZZ_LOG_SUBMISSION (MOCK): submission recorded' } },
];
const PAGE_EXTENSIONS = [
  { name: 'ZZ Sales Channel (MOCK)', type: 'flexfield', context: 'Order Header', field: 'salesChannel', label: 'ZZ Sales Channel (MOCK)', values: ['Direct', 'Partner'] },
];
const inventory = () => ({ source: 'MOCK (synthetic; pod inventory is POD-REQUIRED)', om_extensions: OM_EXTENSIONS, page_extensions: PAGE_EXTENSIONS });

// Hooks called by store.js. Disabled extensions (e.g. by a mutation check) simply do not run.
let disabled = new Set((process.env.MOCK_O2C_DISABLED_EXTENSIONS || '').split(',').filter(Boolean));
const on = (n) => !disabled.has(n);
function onSave(o, b) {
  for (const f of PAGE_EXTENSIONS) if (b[f.field] !== undefined && b[f.field] !== '') {
    if (!f.values.includes(b[f.field])) throw err(400, 'VALIDATION', `${f.label}: value ${b[f.field]} is not valid.`);
    o.ext = { ...(o.ext || {}), [f.field]: b[f.field] };
  }
  if (on('ZZ_DEFAULT_SHIP_INSTRUCTIONS (MOCK)') && !o.shippingInstructions) o.shippingInstructions = OM_EXTENSIONS[0].effect.value;
  o.extensionLog = o.extensionLog || [];
}
function onStartOfSubmission(o) {
  if (on('ZZ_PARTNER_ORDER_NEEDS_PO (MOCK)') && (o.ext || {}).salesChannel === 'Partner' && !o.customerPO) throw err(400, 'VALIDATION', OM_EXTENSIONS[1].effect.text);
}
function onEndOfSubmission(o) {
  if (on('ZZ_LOG_SUBMISSION (MOCK)')) (o.extensionLog = o.extensionLog || []).push({ extension: 'ZZ_LOG_SUBMISSION (MOCK)', event: 'On End of Submission Request', message: OM_EXTENSIONS[2].effect.text, at: new Date().toISOString() });
}
module.exports = { inventory, PAGE_EXTENSIONS, onSave, onStartOfSubmission, onEndOfSubmission };