Setting the file. One moment.
Pw Gift Cards Client · Rp Source Wordpress · wix/skills · Skills Docs
ContentsBack to the top of the page lib/pw-gift-cards-client.js
lib/ pw-gift-cards-client.js
JavaScript · 67 lines · 3 KB
10 if ( ! body || ! Array. isArray (body.cards)) throw new Error ( `page ${ requestedPage }: cards must be an array` );
11 if ( ! Number. isInteger (body.total) || body.total < 0 ) throw new Error ( `page ${ requestedPage }: total must be a non-negative integer` );
12 if (body.page !== undefined && Number (body.page) !== requestedPage) throw new Error ( `page ${ requestedPage }: response page does not match request` );
13 }
14
15 function normalizeBalance ( value , cardNumber , page ) {
16 if ( typeof value !== 'string' && typeof value !== 'number' ) {
17 throw new Error ( `page ${ page }: balance for ${ cardNumber } must be a decimal string or number` );
18 }
19 const decimal = typeof value === 'string' ? value. trim () : String (value);
20 if ( ! / ^ \d + (?: \. \d {1,2} ) ?$ / . test (decimal) || ! Number. isFinite ( Number (decimal))) {
21 throw new Error ( `page ${ page }: balance for ${ cardNumber } must be finite, non-negative, and have at most 2 decimal places` );
22 }
23 return decimal;
24 }
25
26 async function extract ({ httpClient , siteBaseUrl , auth , extractionRoute = '/wix-migration-helper/v1/pw-gift-cards' } = {}) {
27 if ( ! httpClient || typeof httpClient.get !== 'function' ) throw new Error ( 'httpClient.get is required' );
28 if ( ! siteBaseUrl || typeof siteBaseUrl !== 'string' ) throw new Error ( 'siteBaseUrl is required' );
29
30 const perPage = 200 ;
31 const base = siteBaseUrl. replace ( / \/ $ / , '' );
32 const route = extractionRoute. startsWith ( '/' ) ? extractionRoute : `/${ extractionRoute }` ;
33 const remainingBalanceByCard = {};
34 let page = 1 ;
35 let expectedTotal = null ;
36 let sourceCount = 0 ;
37
38 while (expectedTotal === null || sourceCount < expectedTotal) {
39 const response = await httpClient. get ( `${ base }/wp-json${ route }` , {
40 params: { page, per_page: perPage },
41 auth,
42 });
43 const body = bodyOf (response);
44 assertPage (body, page);
45 if (expectedTotal === null ) expectedTotal = body.total;
46 if (body.total !== expectedTotal) throw new Error ( `page ${ page }: total changed during extraction` );
47 if (body.cards. length === 0 && sourceCount < expectedTotal) throw new Error ( `page ${ page }: short read before total was reached` );
48
49 for ( const card of body.cards) {
50 if ( ! card || typeof card.number !== 'string' || card.number. length === 0 ) throw new Error ( `page ${ page }: card number is required` );
51 if ( Object . prototype .hasOwnProperty. call (remainingBalanceByCard, card.number)) throw new Error ( `page ${ page }: duplicate card number ${ card . number }` );
52 remainingBalanceByCard[card.number] = normalizeBalance (card.balance, card.number, page);
53 }
54 sourceCount += body.cards. length ;
55 if (sourceCount > expectedTotal) throw new Error ( `page ${ page }: fetched more cards than total` );
56 page += 1 ;
57 }
58
59 return {
60 remainingBalanceByCard,
61 sourceCount,
62 expectedTotal: expectedTotal === null ? 0 : expectedTotal,
63 reconciled: sourceCount === (expectedTotal === null ? 0 : expectedTotal),
64 };
65 }
66
67 module . exports = { HANDLER_VERSION, extract, normalizeBalance };