-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.js
192 lines (182 loc) · 7.54 KB
/
interface.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const database = require('/opt/database'),
qs = require('qs'),
_ = require('lodash'),
{ DateTime } = require('luxon');
const ALLOWED_RULESETS = ['gift-card', 'brand-v-brand', 'big-cash', 'daily-sweeps'];
const ALLOWED_SITES = ['2017', '1311'];
const ALLOWED_FREQUENCIES = ['daily', 'weekly', 'monthly', 'quarterly', 'annually'];
const ALLOWED_TIMEZONES = ['America/New_York', 'America/Los_Angeles', 'UTC'];
exports.handler = async (event, context, callback) => {
let params = parseParameters(event);
if (params['action'] == 'get_promotions') {
let dbh = await database.connect();
return dbh.query("select consumer_pathways_contest_id, utm_content, human_readable_name, ruleset, frequency, dedupe_on_email, status, timezone, site, start_date, end_date from consumer_pathways_contests where (status or end_date > curdate());")
.then(res => {
for (let i = 0; i < res.length; i++) {
if (res[i]['start_date']) {
res[i]['start_date'] = res[i]['start_date'].toISOString().slice(0, 10);
}
if (res[i]['end_date']) {
res[i]['end_date'] = res[i]['end_date'].toISOString().slice(0, 10);
}
}
console.log(res[0]);
return {
statusCode: 200,
body: JSON.stringify({ promotions: res })
};
})
.catch(err => console.log(err))
.finally(() => dbh.close());
}
else if (params['action'] == 'add_promotion') {
if (!ALLOWED_SITES.includes(params.site)) {
return {
statusCode: 400,
body: 'Invalid Site',
};
}
if (!ALLOWED_RULESETS.includes(params.ruleset)) {
return {
statusCode: 400,
body: 'Invalid Ruleset',
};
}
if (!ALLOWED_FREQUENCIES.includes(params.frequency)) {
return {
statusCode: 400,
body: 'Invalid Frequency',
};
}
if (!ALLOWED_TIMEZONES.includes(params.timezone)) {
return {
statusCode: 400,
body: 'Invalid Timezone',
};
}
//find the first valid start date on or after the given start date
let start_date = DateTime.fromISO(params.start_date);
if (params.frequency == 'weekly') {
if (start_date.weekday != 1) {
start_date = start_date.plus({ 'days': 8 - start_date.weekday })
}
}
else if (params.frequency == 'monthly') {
if (start_date.get('day') != 1) {
start_date = start_date.plus({ 'months': 1 }).set({ 'day': 1 });
}
}
else if (params.frequency == 'quarterly') {
if (start_date.get('day') != 1) {
start_date = start_date.plus({ 'months': 1 }).set({ 'day': 1 });
}
if (start_date.get('month') % 3 == 0) {
start_date = start_date.plus({ "months": 1 })
}
else if (start_date.get('month') % 3 == 2) {
start_date = start_date.plus({ "months": 2 })
}
}
else if (params.frequency == 'annually') {
if (!(start_date.get('month') == 10 && start_date.get('day') == 1)) {
if (start_date.get('month') >= 10) {
start_date = start_date.plus({ 'years': 1 })
}
start_date = start_date.set({ 'month': 10, 'day': 1 });
}
}
let dbh = await database.connect();
return dbh.query("INSERT INTO consumer_pathways_contests (utm_content, human_readable_name, ruleset, frequency, dedupe_on_email, timezone, site, start_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
[params.utm_content, params.name, params.ruleset, params.frequency, params.dedupe, params.timezone, params.site, start_date.toISODate()])
.then(res => {
return {
statusCode: 200,
body: JSON.stringify({ promotions: res })
};
})
.catch(err => console.log(err))
.finally(() => dbh.close());
}
else if (params['action'] == 'end_promotion') {
console.log({ params });
let dbh = await database.connect();
return dbh.query("SELECT frequency FROM consumer_pathways_contests WHERE consumer_pathways_contest_id = ?", [params.contest_id])
.then(res => {
if (res.length != 1) {
throw "Invalid Contest ID";
}
let frequency = res[0].frequency;
//find the first valid start date on or after the given start date
let end_date = DateTime.fromISO(params.end_date);
if (frequency == 'weekly') {
if (end_date.weekday != 7) {
end_date = end_date.plus({ 'days': 7 - end_date.weekday })
}
}
else if (frequency == 'monthly') {
end_date = end_date.plus({ 'months': 1 }).set({ 'day': 1 }).minus({ "days": 1 });
}
else if (frequency == 'quarterly') {
end_date = end_date.plus({ 'months': 1 }).set({ 'day': 1 });
if (end_date.get('month') % 3 == 0) {
end_date = end_date.plus({ "months": 1 })
}
else if (end_date.get('month') % 3 == 2) {
end_date = end_date.plus({ "months": 2 })
}
end_date = end_date.minus({ 'days': 1 })
}
else if (frequency == 'annually') {
if (!(end_date.get('month') == 10 && end_date.get('day') == 1)) {
if (end_date.get('month') >= 10) {
end_date = end_date.plus({ 'years': 1 })
}
end_date = end_date.set({ 'month': 10, 'day': 1 });
}
end_date = end_date.minus({ "days": 1 })
}
return dbh.query("UPDATE consumer_pathways_contests SET status = 0, end_date = ? WHERE consumer_pathways_contest_id = ?", [end_date.toISODate(), params['consumer_pathways_contest_id']])
}).then(res => {
return {
statusCode: 200,
body: JSON.stringify({ promotions: res })
};
})
.catch(err => {
console.log(err);
return {
statusCode: 400,
body: res
};
})
.finally(() => dbh.close());
}
else {
return {
statusCode: 400,
body: 'Invalid Action',
};
}
}
function parseParameters(event) {
let params;
if (event.queryStringParameters) {
params = event.queryStringParameters;
if (!params.user_ip) {
params.user_ip = event.headers['X-Forwarded-For'];
}
if (!params.user_agent) {
params.user_agent = event.headers['User-Agent'];
}
}
else if (event.body) {
let body = event.body;
if (event.isBase64Encoded) {
let buff = new Buffer(body, 'base64');
body = buff.toString('ascii');
console.log({ body });
}
params = qs.parse(body);
}
return params;
}