Setting the file. One moment.
Order Invoice Contract · Rp Target Wix · wix/skills · Skills Docs
ContentsBack to the top of the page 41.10
Multilingual Restore
Bundled file Order Payment Mapping
lib/ order-invoice-contract.js
JavaScript · 90 lines · 4 KB
// full requirement -- two views of one contract, disagreeing, with nothing to notice.
15 //
16 // So: validate on read, freeze what goes out, and hand back clones where callers might mutate.
17
18 const path = require ( 'node:path' );
19 const fs = require ( 'node:fs' );
20 const crypto = require ( 'node:crypto' );
21
22 const ENTITY_PATH = path. join (__dirname, '..' , 'domains' , 'ecom' , 'entities' , 'order-invoice-document.json' );
23
24 // Semantic roles, so callers ask for "the field that holds the preserved file" rather than
25 // retyping a key. Every hardcoded key in a writer is a chance for the two to drift.
26 const ROLES = Object. freeze ({
27 number: 'replatformSourceInvoiceNumber' ,
28 fileId: 'replatformSourceInvoiceFileId' ,
29 url: 'replatformSourceInvoiceUrl' ,
30 });
31
32 let cached = null ;
33
34 function load () {
35 if (cached) return cached;
36 const entity = JSON . parse (fs. readFileSync ( ENTITY_PATH , 'utf8' ));
37 const requirement = (entity.setupRequirements || [])
38 . find (( entry ) => entry && entry.kind === 'extendedFieldSchema' );
39 if ( ! requirement) {
40 throw new Error ( 'order-invoice-document.json declares no extendedFieldSchema setup requirement' );
41 }
42 for ( const role of Object. values ( ROLES )) {
43 if ( ! requirement.fields. some (( field ) => field.key === role)) {
44 throw new Error ( `order-invoice-document.json is missing the ${ role } field; the writer requires it` );
45 }
46 }
47 // The version comes from the GENERIC stamper, not a second algorithm here. Two implementations
48 // of one hash is how the adapter and the shipped producer stamped the same contract differently
49 // -- and a mismatched stamp fails exactly like a forged one, so the duplication was worse than
50 // no stamp at all. What it covers, and why, is documented at that function.
51 cached = Object. freeze ({
52 namespace: requirement.namespace,
53 version: require ( './data-extension-schema.js' ). extendedFieldContractVersion (requirement),
54 fields: Object. freeze (requirement.fields. map (( field ) => Object. freeze ({ ... field }))),
55 });
56 return cached;
57 }
58
59 // A fresh, independent copy every time -- the caller may do what it likes with it.
60 function requirement () {
61 const entity = JSON . parse (fs. readFileSync ( ENTITY_PATH , 'utf8' ));
62 const found = JSON . parse ( JSON . stringify ((entity.setupRequirements || [])
63 . find (( entry ) => entry && entry.kind === 'extendedFieldSchema' )));
64 // Stamped, so everything downstream can be traced back to the contract that produced it.
65 found.contractVersion = load ().version;
66 return found;
67 }
68
69 function version () {
70 return load ().version;
71 }
72
73 function fields () {
74 return load ().fields;
75 }
76
77 function writePaths ( namespace ) {
78 const ns = namespace || load ().namespace;
79 return load ().fields. map (( field ) => `extendedFields.namespaces.${ ns }.${ field . key }` );
80 }
81
82 // The declared cap for a role, so a writer never carries its own copy of a length.
83 function maxLengthFor ( role ) {
84 const key = ROLES [role] || role;
85 const field = load ().fields. find (( entry ) => entry.key === key);
86 if ( ! field) throw new Error ( `no invoice field for role "${ role }"` );
87 return field.maxLength;
88 }
89
90 module . exports = { ROLES, fields, requirement, version, writePaths, maxLengthFor, ENTITY_PATH };