-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserSubmission.ts
More file actions
136 lines (120 loc) · 4.19 KB
/
Copy pathUserSubmission.ts
File metadata and controls
136 lines (120 loc) · 4.19 KB
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
import { sendConfirmationEmail } from './ConfirmationEmail';
const EMAIL_SENT = 'EMAIL_SENT';
const QUOTA_EXCEEDED = 'QUOTA_EXCEEDED';
export interface SubmissionInfo {
fname: string
lname: string
email: string
city: string
reason: string
include_email: string
idempotency_key: string
email_sent: string
retry_count: number
}
export class UserSubmission {
header: any;
rowIndex: number;
sheet: GoogleAppsScript.Spreadsheet.Sheet;
row: SubmissionInfo;
constructor(header: any, rowIndex: number, sheet: GoogleAppsScript.Spreadsheet.Sheet, rowData?: any[]) {
this.header = header;
this.rowIndex = rowIndex;
this.sheet = sheet;
// rowData is optional field, just if the row has already been pulled out into an array so you don't have to
// do additional call to google sheets server
if (!rowData) {
rowData = sheet.getRange(rowIndex, 1, 1, sheet.getDataRange().getLastColumn()).getValues()[0];
}
this.row = this.buildSubmissionInfo(rowData)
};
buildSubmissionInfo = (row: any[]): SubmissionInfo => (
{
fname: row[this.header.FIRST_NAME - 1],
lname: row[this.header.LAST_NAME - 1],
email: row[this.header.EMAIL_ADDRESS - 1],
city: row[this.header.CITY - 1],
reason: row[this.header.REASON - 1],
include_email: row[this.header.EMAIL_INCLUDE - 1],
idempotency_key: row[this.header.IDEMPOTENCY_KEY - 1],
email_sent: row[this.header.EMAIL_SENT - 1],
retry_count: row[this.header.RETRY_COUNT - 1]
}
);
addIdempotencyKey = () => {
if (this.row.idempotency_key === '') {
this.row.idempotency_key = Utilities.getUuid();
this.sheet.getRange(this.rowIndex, this.header.IDEMPOTENCY_KEY, 1, 1).setValue(this.row.idempotency_key);
}
}
setCityTitleCase = () => {
let city = this.row.city.toLowerCase().split(' ');
for (let i = 0; i < city.length; i++) {
city[i] = city[i].charAt(0).toUpperCase() + city[i].slice(1);
}
this.row.city = city.join(' ');
this.sheet.getRange(this.rowIndex, this.header.CITY).setValue(this.row.city);
}
postToLob = () => {
if (!this.row.include_email) {
this.row.email = '';
} else {
this.row.email = this.row.email.toLowerCase();
}
this.setCityTitleCase();
this.addIdempotencyKey();
const url = "https://api.lob.com/v1/postcards";
const data = {
description: "Postcard",
to: toAddress,
from: null,
front: front_tmpl,
back: back_tmpl,
merge_variables: this.row,
};
const options = {
method: 'post',
contentType: 'application/json',
headers: {
Authorization: "Basic " + Utilities.base64Encode(API_KEY + ":"),
'Idempotency-key': this.row.idempotency_key,
},
payload: JSON.stringify(data),
muteHttpExceptions: true
};
try{
// @ts-ignore
const response = UrlFetchApp.fetch(url, options);
const responseCode = response.getResponseCode();
const values = [[new Date(), responseCode]];
if (responseCode === 200.0) {
this.sheet.getRange(this.rowIndex, this.header.SENT_TO_LOB, 1, 2).setValues(values);
} else {
this.sheet.getRange(this.rowIndex, this.header.SENT_TO_LOB, 1, 2).setValues(values);
Logger.log('response: ', response.getContentText());
}
} catch (error) {
Logger.log('error: ', error);
};
};
incrementRetryCount = () => {
this.row.retry_count += 1;
this.sheet.getRange(this.rowIndex, this.header.RETRY_COUNT, 1, 1).setValue(this.row.retry_count);
};
sendConfirmationEmail() {
let emailSentStatus = EMAIL_SENT;
const emailQuotaRemaining = MailApp.getRemainingDailyQuota();
Logger.log("Remaining email quota: " + emailQuotaRemaining);
Logger.log('this.row: ', this.row);
try {
sendConfirmationEmail(this.row)
} catch (err) {
Logger.log('err: ', err);
emailSentStatus = err.toString() === 'Email quota exceeded' ? QUOTA_EXCEEDED : emailSentStatus;
}
this.sheet.getRange(this.rowIndex, this.header.EMAIL_SENT, 1, 1).setValue(emailSentStatus);
};
markFailedEmailSent = () => {
this.sheet.getRange(this.rowIndex, this.header.FAILED_EMAIL_SENT, 1, 1).setValue('Yes');
};
};