- 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
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
import fs from 'fs-extra'
import Airtable from 'airtable';
import createPromosFile from '../createPromosFile';
import createPagesFile from '../createPagesFile';
import createCrescendoFile from '../createCrescendoFile';
import { replaceTimeFormatArray } from '../replaceTimeFormat';
import replacePromoCodeIdWithPromoCode from '../replacePromoCodeIdWithPromoCode';
import ProductArray from '../Util/ProductArray';
import { createMastheadImages, createSitewidePromoImages } from '../FileHandler';
import _ from 'lodash'
const configObject = { view: 'Grid view' };
/****************************************
* Download Airtable's RSI tab
****************************************/
const getRSI = config =>
new Promise((resolve, reject) => {
console.log(`[${config.market}] Downloading RSIs from Airtable..`)
const RSIArray = [];
const base = new Airtable({ apiKey: config.key }).base(config.base)
base('RSI')
.select(configObject)
.eachPage(
function page(records, fetchNextPage) {
records.forEach(function(record) {
RSIArray.push({
Id: record.get('RSI'),
PromoNode: record.get('PromoNode'),
MastheadImage: record.get('MastheadImage'),
MobileMastheadImage: record.get('MobileMastheadImage'),
ExpirationDate: record.get('ExpirationDate'),
Campaign: record.get('Campaign'),
BonusMonths: record.get('BonusMonths'),
Bogo: record.get('Bogo'),
ProductFilter: record.get('ProductFilter'),
Js: record.get('Js'),
Css: record.get('Css')
});
});
fetchNextPage();
},
function done(err) {
if (err) {
console.error(err);
reject(err);
}
resolve(RSIArray);
}
);
});
/****************************************
* Download Airtable's PROMO tab
****************************************/
const getPromo = config =>
new Promise((resolve, reject) => {
console.log(`[${config.market}] Downloading PROMO from Airtable..`)
const PromoArray = [];
const base = new Airtable({ apiKey: config.key }).base(config.base);
base('PROMO')
.select(configObject)
.eachPage((records, fetchNextPage) => {
records.forEach(record => {
let tempObject = {};
let processProductArray;
processProductArray = ProductArray[config.market];
for (let i = 0; i < processProductArray.length; i++)
{
Object.assign(tempObject, {
[processProductArray[i]]:
typeof record.get(processProductArray[i]) !== 'undefined'
? record.get(processProductArray[i])[0]
: 'undefined'
});
}
Object.assign(tempObject, {
id: record.id,
PromoNodeName: record.get('PromoNodeName')
});
PromoArray.push(tempObject);
});
fetchNextPage();
},
function done(err)
{
if (err)
{
reject(err);
}
resolve(PromoArray);
}
);
});
/****************************************
* Download Airtable's PROMO _CODES tab
****************************************/
const getPromoCodes = config =>
new Promise((resolve, reject) => {
console.log(`[${config.market}] Downloading PROMO_CODES from Airtable..`)
const PromoCodeObject = {};
const base = new Airtable({ apiKey: config.key }).base(config.base);
base('PROMO_CODES')
.select(configObject)
.eachPage(
function page(records, fetchNextPage) {
records.forEach(function(record) {
Object.assign(PromoCodeObject, {
[record.id]: {
code: record.get('PROMOCODE'),
price: record.get('PRICE')
}
});
});
fetchNextPage();
},
function done(err) {
if (err) {
reject(err);
}
resolve(PromoCodeObject);
}
);
});
/****************************************
* Download Airtable's CRESCENDO tab
****************************************/
const getCrescendo = config =>
new Promise((resolve, reject) => {
console.log(`[${config.market}] Downloading CRESCENDO from Airtable..`)
const base = new Airtable({ apiKey: config.key }).base( config.base )
const CrescendoArray = [];
// crescendos = { valentine: [ {clockEnd:...}, {clockEnd:...} ] }
const crescendos = {};
base('CRESCENDO')
.select(configObject)
.eachPage(
function page(records, fetchNextPage) {
records.forEach(function(record) {
CrescendoArray.push({
name: record.get('name'),
crescendoDate: record.get('crescendo-date'),
expirationDate: record.get('expiration-date'),
clockEnd: record.get('clock-end'),
violatorText: record.get('violator-text')
});
});
fetchNextPage();
CrescendoArray.forEach(o => {
if (typeof crescendos[o.name] == 'undefined') {
crescendos[o.name] = [];
}
crescendos[o.name].push(o);
});
},
err => {
if (err) {
reject(err);
}
resolve(crescendos);
}
);
});
/****************************************
* Download Airtable's Products tab
****************************************/
const getProducts = config =>
new Promise((resolve, reject) => {
console.log(`[${config.market}] Downloading PRODUCTS from Airtable..`)
const base = new Airtable({ apiKey: config.key }).base( config.base )
const ProductsArray = [];
base('PRODUCTS')
.select(configObject)
.eachPage(
function page(records, fetchNextPage) {
records.forEach(function(record) {
ProductsArray.push({
shortName: record.get('ShortName'),
longName: record.get('LongName'),
msrp: record.get('Msrp'),
languages: record.get('Languages'),
type: record.get('Type'),
airtableId: record.id
});
});
fetchNextPage();
},
err => {
if (err) {
reject(err);
}
resolve(ProductsArray);
}
);
});
/****************************************
* Download Airtable's Languages tab
****************************************/
const getLanguages = config =>
new Promise((resolve, reject) => {
console.log(`[${config.market}] Downloading LANGUAGES from Airtable..`)
const base = new Airtable({ apiKey: config.key }).base(config.base);
const LanguagesArray = [];
base('LANGUAGES')
.select(configObject)
.eachPage((records, fetchNextPage) => {
records.forEach(record => {
return LanguagesArray.push({
code: record.get('Code'),
language: record.get('Language'),
version: record.get('Version'),
products: record.get('Products'),
airtableId: record.id
});
});
fetchNextPage();
},
function done(err) {
if (err) {
reject(err);
}
resolve(LanguagesArray);
}
);
});
const replacePromoIdWithPromoName = (rsiArray, promoArray) => {
const promosDict = {};
for (let i = 0; i < promoArray.length; i++)
{
promosDict[promoArray[i].id] = promoArray[i].PromoNodeName;
}
rsiArray = rsiArray.map(rsi => {
rsi.PromoNode = promosDict[rsi.PromoNode[0]];
return rsi;
});
return rsiArray;
};
/**
* These are the images that get
* changed with each campaign. If a new assets is introduced
* to a page, just create it in the `Name` column in Airtable.
* We devs won't have to do anything here to accomodate.
* This has to be its own Airtable Base, not just a column
* inside US base
*/
const getAssets = config => {
return new Promise((resolve, reject) => {
console.log('Getting SITEWIDE ASSETS from Airtable..')
const base = new Airtable({ apiKey: config.key }).base(
config.base
);
const files = [];
base('ASSETS')
.select(configObject)
.eachPage(
(records, fetchNextPage) => {
records.forEach(record => {
files.push({
name: record.get('Name'),
file: record.get('File')
});
});
fetchNextPage();
},
function done(err) {
if (err) {
console.error(err);
reject(err);
}
resolve(files.filter(f => typeof f.file != 'undefined'));
}
);
});
};
const AirtableHandler = config => {
// If it's the 'assets' market, we dont pull RSI models, etc. Just
// Get our sitewide assets (buynow.jpg, freedemo.jpg, etc)
// and put sitewide assets in sitewide-promos directory
if (config.market == 'assets')
{
getAssets(config).then(files => createSitewidePromoImages(files, config))
return;
}
Promise.all([
getRSI(config),
getPromo(config),
getPromoCodes(config),
getCrescendo(config),
getProducts(config),
getLanguages(config)
])
.then(([
rsiArray,
promoArray,
PromoCodeObject,
crescendosObject,
productsArray,
languagesArray
]) => {
rsiArray = replacePromoIdWithPromoName(rsiArray, promoArray);
rsiArray = replaceTimeFormatArray(rsiArray);
let processProductArray = ProductArray[config.market];
promoArray = replacePromoCodeIdWithPromoCode(promoArray, PromoCodeObject, processProductArray);
console.log(productsArray[0])
console.log('--- Languages Array:')
console.log(languagesArray[0])
// Each id in a product.languages array, is an airtableId in languagesArray[obj].airtableId
for (let langObject of languagesArray)
{
var tmpLangObject = {
code: null,
language: null,
version: null,
products: {
// subscription: []
// dictionary: []
//...
}
}
// Puts product type into `tmpLangObject.products`
for (let obj of productsArray)
{
if (!Object.keys(tmpLangObject.products).includes(obj.type))
{
tmpLangObject.products[obj.type] = []
}
}
// it's a v3 lang
if (typeof langObject.products == 'undefined')
{
break
}
for (let productId of langObject.products)
{
// find this productId in productsArray
let productObj = productsArray.filter(o => o.airtableId == productId)[0]
//console.log(productObj)
//console.log(`type: ${productObj.code}`)
tmpLangObject.products[productObj.type].push({
name: productObj.shortName,
msrp: productObj.msrp,
type: productObj.type
// longName: productObj.longName
})
tmpLangObject.code = langObject.code
tmpLangObject.version = langObject.version
// Each productId is an 'airtableId' in a langObject
}
langObject.products = tmpLangObject
}
console.log(languagesArray)
return
/* languageaarray
{ code: 'vie',
language: 'Vietnamese',
version: '4',
products:
[ 'recHDYQV2kFZJ8PZK',
'recJC4nv2LBursfKR',
'recfhbC5sXBb7z9vv',
'recL5I0DTM0XPvhH4',
'rec0R5Rh1PVP48s2n',
'recF5yqzajmzQMl97',
'recxQHigXYhUAX16C',
'recLxYYsOzTq3rH5k',
'recpOfs418YONyqHD',
'recqWBOp2iRCBwHSy' ],
airtableId: 'reczrnLjfSZnBkwLi' }
*/
//let langs = _.keyBy(languagesObject, 'type')
// Language object holds lang info and products (product object)
//console.log(langs)
/*
'chi': {
language: 'Chinese',
products: {
subscription: {
1: { sku: 123, msrp: 199},
3: { sku: 123, msrp: 199}
}
}
]
}
*/
let prods = productsArray.map( prodObject => {
for (let i = 0; i < prodObject.languages.length; i++)
{
let langId = prodObject.languages[i]
// console.log('----')
// /prodNode[productName] = productsArray[ prodNode[productName] ]
}
return prodObject;
})
//console.log(prods)
// Create our pages.json file
createPagesFile(rsiArray, config);
// Create our promos.json file
createPromosFile(promoArray, config);
// Create our products.json file
//createProductsFile(productsObject, config)
// We don't store SKUs and MSRPs in Airtable. We store in locally.
// In this repo's data/{market}/skus.json file.
// Move it to ../data-rsi/{market}/skus.json so the RSI parser can use it
fs.copySync(`./data/${config.market}/skus.json`, `${config.dstpath}/models/json/skus.json`)
// Create all our crescendo files
Object.keys(crescendosObject)
.forEach(k => createCrescendoFile(k, crescendosObject[k], config));
// Get all masthead images
if (typeof config.getImages != 'undefined' && config.getImages == 'yes')
{
console.log(`[${config.market}] Creating masthead images...`)
for (let i = 0; i < rsiArray.length; i++)
{
createMastheadImages(rsiArray[i], config);
}
}
})
.catch(e => console.log(e) );
};
export default AirtableHandler;