- 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
import fs from 'fs'
//import { mirrorConstantPages } from './functions'
import config from './config'
import moment from 'moment'
/*
 * Home handler. Simply list
 * our scheduled flips. 
 */
export function home(ctx, next) {
  ctx.body = 'Hello Koa';
}
/*
 * Grabs list of all page names.
 * ie, ['sitewide', 'rmsitewide', etc]
 * If ?ui=true is passed as query parameters,
 * it means we are requesting it for a dropdown()
 * element for semantic-ui, so we format it accordingly.
 * http://semantic-ui.com/modules/dropdown.html#/examples
 *
 * @returns {list} list of page names
 */
export function getPages(ctx, next) {  
  
  ctx.body = fs.readFileSync(config.localUris.pages, 'utf8')
  return
  if (ctx.request.query.filter == 'constant') {
    pages = Object.keys(pages).map(page =>  {
      if (!pages[page].metadata.constant) return page
    })
    .filter(page => page !== undefined)
  }
  
  if (!Array.isArray(pages)) pages = Object.keys(pages)
  
  
  // Format for semantic-ui if ?ui=true
  // TODO do the formatting on client. Or get rid of semantic ui
  if (ctx.request.query.ui) {
    var result = {}      
    result.success = true
    result.results = pages.map(p => {
      return {name: p, value: p, text: p}
    })
    pages = result
  } 
  ctx.body = JSON.stringify(pages)
}
 /*
 * Grabs list of all promoset names.
 * ie, ['q2_199', 'q2_209', etc]
 * If ?ui=true is passed as query parameters,
 * it means we are requesting it for a dropdown()
 * element for semantic-ui, so we format it accordingly.
 * http://semantic-ui.com/modules/dropdown.html#/examples
 *
 * @returns {list} list of promo names
 */
export function getPromoNames(ctx, next) {  
  
  var promos = fs.readFileSync(config.localUris.promos, 'utf8')
  promos = Object.keys(JSON.parse(promos))
  var ret = JSON.stringify(promos)
  
  // Format for semantic-ui if ?ui=true
  if (ctx.request.query.ui) {
    var result = {}  
    result.success = true
    result.results = promos.map(p => {
      return {name: p, value: p, text: p}
    })
    ret = JSON.stringify(result)    
  }
  ctx.body = ret
}
/*
 * Create flip.
 * Will create a file called flip.json and a parent directory
 * with the date as its name. This file (flip.json) holds 
 * all the information related to the flip.
 * The script that actually compiles and uploads the files
 * for the flip will read flip.json.
 */
export function createFlip(ctx, next) {
  
  const data = ctx.request.body.fields
  const assets = ctx.request.body.files['assets[]'] 
  
  // for (let file in assets["assets"]) {
  //   // size, path, name, type
  //   console.log(file.name)
  // }
  
  
  const flip = {}
  flip.name = data.name
  flip.promoset = data.promoset
  flip.date = new Date(data.date)
  flip.constants = JSON.parse(data.constants)
  flip.mobileRto = data['mobile-rto']
  flip.mobileRtoUrl = data['mobile-rto-url']
  flip.homepageRto = data['homepage-rto']
  flip.homepageRtoUrl = data['homepage-rto-url']  
  
  
  
  // Formate date. It will be directory's name in data/flips
  var date = moment(flip.date).format('MMM DD YYYY, HH:mm')
  
  // Means we have more than 1 file
  if (Array.isArray(assets)) {
    for (let file in assets) {
      let tmp = assets[file].path
      let dst = `../data/${assets[file].name}`
      fs.renameSync(tmp, dst )
    }
  }
  
  ctx.body = 'flip done'
}