-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-purchase.js
158 lines (138 loc) · 4.33 KB
/
get-purchase.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
if (!process.env.NETLIFY) {
require("dotenv").config();
}
const { GoogleSpreadsheet } = require("google-spreadsheet");
const {
GOOGLE_SERVICE_ACCOUNT_EMAIL,
GOOGLE_PRIVATE_KEY,
GOOGLE_SPREADSHEET_ID_FROM_URL,
} = process.env;
if (!GOOGLE_SERVICE_ACCOUNT_EMAIL)
throw new Error("No GOOGLE_SERVICE_ACCOUNT_EMAIL env var set");
if (!GOOGLE_PRIVATE_KEY) throw new Error("No GOOGLE_PRIVATE_KEY env var set");
if (!GOOGLE_SPREADSHEET_ID_FROM_URL)
throw new Error("No GOOGLE_SPREADSHEET_ID_FROM_URL env var set");
exports.handler = async (event) => {
if (event.httpMethod !== "POST") {
return {
statusCode: 405,
body: "Method Not Allowed",
headers: { Allow: "POST" },
};
}
const { reference_no = null } = JSON.parse(event.body);
if (!reference_no) {
let error = {
statusCode: 422,
body: "Reference is Required!",
};
return error;
}
try {
const doc = new GoogleSpreadsheet(GOOGLE_SPREADSHEET_ID_FROM_URL);
await doc.useServiceAccountAuth({
client_email: GOOGLE_SERVICE_ACCOUNT_EMAIL,
private_key: GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n"),
});
await doc.loadInfo();
var purchase_sheet = doc.sheetsById[1];
if (!purchase_sheet) {
await doc.addSheet({
headerValues: [
"reference_no",
"deliverable",
"sent",
"courier",
"tracking_no",
"remarks",
"pm_link",
"payment_id",
"paid",
"date_paid",
"mop",
"currency",
"net_amount",
"fee",
"payout_date",
"referral_code",
"referral_fee",
"received",
"order_details",
"receiver_name",
"receiver_phone",
"notes",
"delivery_address",
"payer_name",
"payer_email",
"payer_phone",
"billing_address",
],
sheetId: 1,
gridProperties: {
rowCount: 1000,
columnCount: 27,
},
title: "Purchases",
});
purchase_sheet = doc.sheetsById[1];
}
try {
await purchase_sheet.loadHeaderRow();
} catch (e) {
await purchase_sheet.resize({ rowCount: 1000, columnCount: 27 });
await purchase_sheet.setHeaderRow([
"reference_no",
"deliverable",
"sent",
"courier",
"tracking_no",
"remarks",
"pm_link",
"payment_id",
"paid",
"date_paid",
"mop",
"currency",
"net_amount",
"fee",
"payout_date",
"referral_code",
"referral_fee",
"received",
"order_details",
"receiver_name",
"receiver_phone",
"notes",
"delivery_address",
"payer_name",
"payer_email",
"payer_phone",
"billing_address",
]);
}
const rows = await purchase_sheet.getRows();
const rowIndex = rows.findIndex((x) => x.reference_no == reference_no);
if (rowIndex === -1) {
let error = {
statusCode: 400,
body: JSON.stringify({ error: "Reference Number Not Found!" }),
};
return error;
}
let header = purchase_sheet.headerValues;
const rowData = {};
header.forEach((element) => {
rowData[element] = rows[rowIndex][element];
});
return {
statusCode: 200,
body: JSON.stringify(rowData),
};
} catch (e) {
console.log(e.toString());
return {
statusCode: 500,
body: e.toString(),
};
}
};