- 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
import Airtable from 'airtable';
import createPromosFile from '../createPromosFile';
import createPagesFile from '../createPagesFile';
import getSKUFile from '../getSKUFile';
import createCrescendoFile from '../createCrescendoFile';
import { replaceTimeFormatArray } from '../replaceTimeFormat';
import replacePromoCodeIdWithPromoCode from '../replacePromoCodeIdWithPromoCode';
import ProductArray from '../Util/ProductArray';
import { HomschoolProductArray } from '../Util/ProductArray';
import { createImages, createSitewidePromoImages } from '../FileHandler';
const configObject = { view: 'Grid view' };
const getRSI = dataSourceStrategy =>
new Promise((resolve, reject) => {
console.log('getRSI key', dataSourceStrategy.key);
console.log('getRSI base', dataSourceStrategy.base);
const RSIArray = [];
const base = new Airtable({ apiKey: dataSourceStrategy.key }).base(
dataSourceStrategy.base
);
base('RSI')
.select(configObject)
.eachPage(
function page(records, fetchNextPage) {
records.forEach(function(record) {
RSIArray.push({
RSI: 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'),
ProductFilter: record.get('ProductFilter')
});
});
fetchNextPage();
},
function done(err) {
if (err) {
console.error(err);
reject(err);
}
resolve(RSIArray);
}
);
});
const getPromo = dataSourceStrategy =>
new Promise((resolve, reject) => {
const PromoArray = [];
const base = new Airtable({ apiKey: dataSourceStrategy.key }).base(
dataSourceStrategy.base
);
base('PROMO')
.select(configObject)
.eachPage(
function page(records, fetchNextPage) {
records.forEach(record => {
var tempObject = {};
let processProductArray;
if (dataSourceStrategy.filepath.match(/homeschool/))
processProductArray = HomschoolProductArray;
else processProductArray = ProductArray;
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);
}
);
});
const getPromoCodes = dataSourceStrategy =>
new Promise((resolve, reject) => {
const PromoCodeObject = {};
const base = new Airtable({ apiKey: dataSourceStrategy.key }).base(
dataSourceStrategy.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);
}
);
});
const getCrescendo = dataSourceStrategy =>
new Promise((resolve, reject) => {
const base = new Airtable({ apiKey: dataSourceStrategy.key }).base(
dataSourceStrategy.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);
}
);
});
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;
};
const getAllImages = (rsiArray, dataSourceStrategy) => {
for (let i = 0; i < rsiArray.length; i++) {
createImages(rsiArray[i], dataSourceStrategy);
}
};
/**
* These are the images & criteo feed that get
* changed with each campaign. If a new assets is introduced
* to a page, just create it in the `Name` column. Us devs
* won't have to do anything to accomodate.
*/
const getAssets = dataSourceStrategy => {
new Promise((resolve, reject) => {
const base = await new Airtable({ apiKey: dataSourceStrategy.key }).base(
dataSourceStrategy.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 = async dataSourceStrategy => {
// If it's the 'assets' market, we dont pull RSI models, etc.
if (dataSourceStrategy.market == 'assets') {
// Put sitewide assets in sitewide-promos directory
const assets = await getAssets(dataSourceStrategy);
createSitewidePromoImages(assets, dataSourceStrategy);
return;
}
Promise.all([
getRSI(dataSourceStrategy),
getPromo(dataSourceStrategy),
getPromoCodes(dataSourceStrategy),
getCrescendo(dataSourceStrategy),
getAssets(dataSourceStrategy)
])
.then(async ([rsiArray, promoArray, PromoCodeObject, crescendosObject, getAssets]) => {
rsiArray = replacePromoIdWithPromoName(rsiArray, promoArray);
rsiArray = replaceTimeFormatArray(rsiArray);
let processProductArray;
if (dataSourceStrategy.filepath.match(/homeschool/)) {
processProductArray = HomschoolProductArray;
} else {
processProductArray = ProductArray;
}
promoArray = replacePromoCodeIdWithPromoCode(
promoArray,
PromoCodeObject,
processProductArray
);
createPagesFile(rsiArray, dataSourceStrategy);
createPromosFile(promoArray, dataSourceStrategy);
getSKUFile(dataSourceStrategy);
Object.keys(crescendosObject).forEach(k => {
createCrescendoFile(k, crescendosObject[k], dataSourceStrategy);
});
if (
typeof dataSourceStrategy.getImages != 'undefined' &&
dataSourceStrategy.getImages == 'yes'
) {
getAllImages(rsiArray, dataSourceStrategy);
}
})
.catch(e => {
console.log(e);
});
};
export default AirtableHandler;