-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-inventory.js
181 lines (152 loc) · 5.48 KB
/
sync-inventory.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict';
const config = require( 'config' );
const DABC = require( './dabc' );
const WooCommerceAPI = require( 'woocommerce-api' );
const UntappdClient = require( 'node-untappd' );
const querystring = require( 'querystring' );
const PromisePool = require( 'promise-pool-executor' );
const _ = require( 'lodash' );
const parseLinkHeader = require( 'parse-link-header' );
const WooCommerce = new WooCommerceAPI( {
url: config.get( 'woocommerce.url' ),
consumerKey: config.get( 'woocommerce.key' ),
consumerSecret: config.get( 'woocommerce.secret' ),
wpAPI: true,
version: 'wc/v2',
} );
const WordPress = new WooCommerceAPI( {
url: config.get( 'woocommerce.url' ),
consumerKey: config.get( 'woocommerce.key' ),
consumerSecret: config.get( 'woocommerce.secret' ),
wpAPI: true,
version: 'wp/v2',
} );
let storeMarkerIdMap = {};
const getProducts = ( page = 1 ) => new Promise( ( resolve, reject ) => {
WooCommerce.get(
// TODO: only get beers not marked special order by DABC
'products?' + querystring.stringify( {
per_page: 100,
page,
fields: [ 'id', 'title', 'meta.untappd_id', 'stores' ].join( ',' ),
} ),
( err, res, body ) => {
if ( err ) {
reject( err );
} else {
try {
const json = JSON.parse( body );
const links = parseLinkHeader( _.get( res, 'headers.link', '' ) );
const response = {
products: json,
};
if ( links.next ) {
response.next = () => getProducts( page + 1 );
}
resolve( response );
} catch ( syntaxError ) {
console.error( body );
reject( syntaxError );
}
}
}
);
} );
const getStoreMapMarkers = ( page = 1 ) => new Promise( ( resolve, reject ) => {
WordPress.get(
'stores?' + querystring.stringify( { per_page: 100, page } ),
( err, res, body ) => {
if ( err ) {
reject( err );
} else {
try {
const json = JSON.parse( body );
resolve( {
results: json,
totalPages: Number.parseInt( res.headers['x-wp-totalpages'] ),
} );
} catch ( syntaxError ) {
console.error( body );
reject( syntaxError );
}
}
}
);
} );
const getAllStoreMapMarkers = () => {
return getStoreMapMarkers().then( ( response ) => {
if ( 1 == response.totalPages ) {
return response.results;
}
const tasks = [ { results: response.results } ];
for ( let i = 0; i < ( response.totalPages - 1 ); i++ ) {
tasks.push( getStoreMapMarkers( i + 2 ) );
}
return Promise.all( tasks ).then( ( resultSets ) => _.flatMap( resultSets, 'results' ) );
} );
};
const getStoresBySKU = ( sku ) => new Promise( ( resolve, reject ) => {
DABC.getBeerInventory( sku, ( err, inventory ) => {
if ( err ) {
return reject( err );
}
const storeNumbers = inventory.stores.map( ( store ) => Number.parseInt( store.store ) );
resolve( storeNumbers );
} );
} );
const updateBeerStores = ( beerID, storeNumbers ) => new Promise( ( resolve, reject ) => {
WordPress.put(
'product/' + beerID,
{ stores: storeNumbers },
( err, res, body ) => {
if ( err ) {
reject( err );
} else {
try {
const json = JSON.parse( body );
resolve( json );
} catch ( syntaxError ) {
console.error( body );
reject( syntaxError );
}
}
}
);
} );
const processBeers = ( beers ) => {
// Create a pool with a concurrency limit
const pool = new PromisePool.PromisePoolExecutor( {
frequencyLimit: 15,
frequencyWindow: 5000,
} );
return pool.addEachTask( {
data: beers,
generator: ( beer ) => {
console.log( `Processing ${beer.name}.` );
return getStoresBySKU( beer.sku )
.then( ( storeNumbers ) => {
console.log( `${beer.name} available in stores:`, storeNumbers.join( ', ' ) );
return storeNumbers.map( ( number ) => storeMarkerIdMap[ number ] || false ).filter( Boolean )
} )
.then( ( storeMarkerIds ) => updateBeerStores( beer.id, storeMarkerIds ) )
.then( () => console.log( `${beer.name} stores updated.` ) );
}
} ).promise().then( ( results ) => {
console.log( results.length + ' beers processed.' );
} );
};
const processProductPage = ( page ) => {
processBeers( page.products ).then( () => {
if ( page.next ) {
page.next().then( processProductPage );
}
} );
};
getAllStoreMapMarkers().then( ( markers ) => {
markers.forEach( ( marker ) => {
const storeNumber = Number.parseInt( marker.slug.split( '-' ).pop() );
if ( isNaN( storeNumber ) ) return;
storeMarkerIdMap[ storeNumber ] = marker.id;
} );
getProducts().then( ( page ) => processProductPage( page ) );
} );