Skip to content

Commit

Permalink
Use secure cloud retrieval for coupon code
Browse files Browse the repository at this point in the history
  • Loading branch information
bkimminich committed Feb 20, 2019
1 parent d961ca3 commit fde2003
Show file tree
Hide file tree
Showing 8 changed files with 462 additions and 216 deletions.
1 change: 0 additions & 1 deletion index.js
Expand Up @@ -9,7 +9,6 @@ const T = new Twitter({
const statusText = require('./lib/statusText')

module.exports = (status = statusText()) => {
console.log()
console.log(`[${colors.green('✔')}] Status prepared: ${colors.cyan(status)}`)
if (process.env.TRAVIS_EVENT_TYPE === 'cron') {
T.post('statuses/update', { status })
Expand Down
21 changes: 9 additions & 12 deletions lib/couponCode.js
@@ -1,14 +1,11 @@
const z85 = require('z85')
const request = require('sync-request')

const months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']

function toMMMYY (date) {
const month = date.getMonth()
const year = date.getFullYear()
return months[month] + year.toString().substring(2, 4)
}

module.exports = (discount, date = new Date()) => {
const coupon = toMMMYY(date) + '-' + discount
return z85.encode(coupon)
module.exports = (discount) => {
var res = request('GET', 'https://5j4d1u7jhf.execute-api.eu-west-1.amazonaws.com/default/JuicyCouponFunc')
if (res.statusCode > 200) {
throw new Error(`AWS lookup failed with HTTP error ${res.statusCode}`)
} else {
const coupons = JSON.parse(res.body)
return coupons[(discount / 10) - 1]
}
}
6 changes: 3 additions & 3 deletions lib/statusText.js
Expand Up @@ -2,9 +2,9 @@ const couponCode = require('./couponCode')
const expirationDate = require('./expirationDate')
const randomDiscount = require('./randomDiscount')

module.exports = (discount = randomDiscount(), date = new Date()) => {
const coupon = couponCode(discount, date)
const expiration = expirationDate(date)
module.exports = (discount = randomDiscount()) => {
const coupon = couponCode(discount)
const expiration = expirationDate(new Date())

const texts = [
`[🤖] Enjoy ${discount}% off all our juicy products with this #coupon code: ${coupon} (valid until ${expiration})`,
Expand Down

0 comments on commit fde2003

Please sign in to comment.