Mon Dec 24 2018
Copied to clipboard! Copy reply
  • 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

import { createFile } from './FileHandler';
import prettyjson from 'pretty-json-stringify'

/**
 * This function creates a products.json file, which holds
 * the products we sell on landing pages.
 * 
 * @param {Array} productsArray - Array of prdoucts from Airtable
 * @param {Array} languagesArray - Array of languages from Airtable
 * @param {Object} config - App's config obj
 */

const createProductsFile = (productsArray, languagesArray, config) => {
  
  var productsArray = languagesArray.map(langObject => 
        {
          // We format products into types.
          var formattedProducts = {        
              // subscription: []
              // dictionary: []
              //...       
          }
    
          // Puts product type into `formattedProducts`
          for (let obj of productsArray)
          {
            if (!Object.keys(formattedProducts).includes(obj.type))
            {
              formattedProducts[obj.type] = {}
            }
          }
    
          // TODO it's a v3 lang -- currently comes undefined because its blank in airtable 
          if (typeof langObject.products == 'undefined')
          {
            return
          }
          
          for (let productId of langObject.products)
          {
            // find this productId in productsArray
            let productObj = productsArray.filter(o => o.airtableId == productId)[0]
            /*
            formattedProducts[productObj.type].push({
              name: productObj.shortName,
              msrp: productObj.msrp,
              //sku: productObj.sku
              //type: productObj.type
              // longName: productObj.longName
            })
            */
           formattedProducts[productObj.type][productObj.shortName] = 5 //langObject.code

          }
          langObject.products = formattedProducts
          delete langObject.airtableId
          return langObject
         
  })


  
  // Objects for products.json
  //let contentForFile = `[ ${products.join(',').trim(' ')} ]`;
  
  //console.log(contentForFile)
  
  let dstPath = `${config.dstpath}/models/json/skus.json`
  createFile(dstPath, prettyjson(productsArray, {tab: '  '}));
  
};

export default createProductsFile;