Skip to content

Commit fde2003

Browse files
committed
Use secure cloud retrieval for coupon code
1 parent d961ca3 commit fde2003

File tree

8 files changed

+462
-216
lines changed

8 files changed

+462
-216
lines changed

index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const T = new Twitter({
99
const statusText = require('./lib/statusText')
1010

1111
module.exports = (status = statusText()) => {
12-
console.log()
1312
console.log(`[${colors.green('✔')}] Status prepared: ${colors.cyan(status)}`)
1413
if (process.env.TRAVIS_EVENT_TYPE === 'cron') {
1514
T.post('statuses/update', { status })

lib/couponCode.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
const z85 = require('z85')
1+
const request = require('sync-request')
22

3-
const months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
4-
5-
function toMMMYY (date) {
6-
const month = date.getMonth()
7-
const year = date.getFullYear()
8-
return months[month] + year.toString().substring(2, 4)
9-
}
10-
11-
module.exports = (discount, date = new Date()) => {
12-
const coupon = toMMMYY(date) + '-' + discount
13-
return z85.encode(coupon)
3+
module.exports = (discount) => {
4+
var res = request('GET', 'https://5j4d1u7jhf.execute-api.eu-west-1.amazonaws.com/default/JuicyCouponFunc')
5+
if (res.statusCode > 200) {
6+
throw new Error(`AWS lookup failed with HTTP error ${res.statusCode}`)
7+
} else {
8+
const coupons = JSON.parse(res.body)
9+
return coupons[(discount / 10) - 1]
10+
}
1411
}

lib/statusText.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const couponCode = require('./couponCode')
22
const expirationDate = require('./expirationDate')
33
const randomDiscount = require('./randomDiscount')
44

5-
module.exports = (discount = randomDiscount(), date = new Date()) => {
6-
const coupon = couponCode(discount, date)
7-
const expiration = expirationDate(date)
5+
module.exports = (discount = randomDiscount()) => {
6+
const coupon = couponCode(discount)
7+
const expiration = expirationDate(new Date())
88

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

0 commit comments

Comments
 (0)