Wed Jan 04 2017
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
  • 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
/*
 * Handles all routes regarding pages.
 * I changed up my brackets/braces style here
 * to be very Java-like, for shits and giggles.
 * Let us know what you think by emailing  Vinoth
 * at vjohn@rosettastone.com
 * :D
 */


import fs from 'fs-extra'
import prettyjson from 'json-format'
import logger from '../logger'
import cfg from '../config'
import Validate from '../validate'

export default class Pages
{
  

  static getPages(ctx, next)
  {
    ctx.body = fs.readFileSync(cfg.localUris.pages, 'utf8')
  }


  /*
   * Creates a new page in pages.json
   * First it checks to see if an object with the
   * same name exists. If not, write the object to
   * pages.json
   */
  
  static createPage(ctx, next)
  {
    const data = ctx.request.body.fields
    const pages = fs.readJsonSync(cfg.localUris.pages, 'utf8')

    if (pages.some(p => p.name == data.name))
    {
      return ctx.body = { result: 0, payload: "Page exists. Try a different name." }
    }

    const validate = new Validate()
    var result

    if (validate.page(data) === true)
    {
      if (Pages._upsertPage(pages, data))
      {
        const env = data.inprod == '' ? 'www.stg.' 
        const link = `http://${env}rosettastone.com/lp/sbsr/${data.name}`        
        result = {reuslt: 1, payload: `Page <a href='${link}'>created</a>!`}
        logger.info(`--user -- created landing page ${data.name}`)
      } 
      else
      {
        result = {reuslt: 0, payload: 'Could not write to pages.json.'}
        logger.info(`--user -- created landing page ${data.name}`)
      }
    }
    else
    {
      result = {reuslt: 0, payload: 'Some invalid data was passed'}
      logger.warn(`--user -- tried to create page ${data.name} but passed bad params.`)
    }

    return ctx.body = result
  }


  
  /*
   * Update pages.json
   * First it deletes the object we are updating
   * (identified by its 'name' value), then
   * append the new one and finally, write to file.
   */
  
  static updatePage(ctx, next)
  {
    const data = ctx.request.body.fields
    var pages = fs.readJsonSync(cfg.localUris.pages], 'utf8')
    pages = pages.filter(p => p.name != data.name)

    if (validate.page(data) === true)
    {
      if (Pages._upsertPage(pages, data))
      {
        const env = data.inprod == '' ? 'www.stg.' 
        const link = `http://${env}rosettastone.com/lp/sbsr/${data.name}`
        result = { reuslt: 1, payload: `Page <a href='${link}'>updated</a>.` }
      } 
      else
      {
        result = { reuslt: 0, payload: 'Could not write to pages.json.' }
      }
    }
    else
    {
      result = { reuslt: 0, payload: 'Some invalid data was passed' }
      logger.warn(`--user -- tried to create page ${data.name} but passed bad params.`)
    }

    return ctx.body = result
  }


  
  /*
   * Will insert a pages object into pages.json,
   * Doesn't have if the same object exists or not,
   * that is up to the caller to decide
   *
   * @param {array} pages
   *   An array containing all the existing pages obj.
   *   It will be written to pages.json with the new
   *   page (`data` param) appended to it.
   *
   * @param {obj} data
   *   An object containing page data (name, promo, etc)
   *   These are the information visible on a web page.
   */
  
  static _upsertPage(pages, data)
  {
    pages.push({
      "name": data.name,
      "promoset": data.promoset,
      "mastheadtype": data.mastheadtype,
      "mastheadimage": data.mastheadimage,
      "strikethrough": [],
      "tophtml": data.tophtml,
      "bottomhtml": data.bottomhtml,
      "phone": data.phone,
      "expiration": data.expiration,
      "crescendo": data.crescendo,
      "js": data.js,
      "css": data.css,
      "metadata": {
        "constant": data.constant === 'true',
        "inprod": data.inprod === 'true',
        "market": data.market
      }
    })
    try
    {
      fs.writeFileSync(cfg.localUris.pages, prettyjson(pages))
      return true
    }
    catch(e)
    {      
      logger.error(`-- user -- attemped to create page ${data.name}, got error '${e}'`)
      return false
    }
  }


}