-
Notifications
You must be signed in to change notification settings - Fork 0
/
toggle-received.js
105 lines (93 loc) · 3.16 KB
/
toggle-received.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
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: JSON.stringify({
message: "Reference Number 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();
const purchase_sheet = doc.sheetsById[1];
const rows = await purchase_sheet.getRows();
const rowIndex = rows.findIndex((x) => x.reference_no == reference_no);
if (rowIndex == -1) {
let error = {
statusCode: 404,
body: JSON.stringify({
message: "Reference Number Not Found!",
}),
};
return error;
}
let message =
"Oops!Request Failed. We Already Marked This Transaction as DONE";
if (
(rows[rowIndex].remarks === "DONE" && rows[rowIndex].received == "yes") ||
(rows[rowIndex].remarks === "done" && rows[rowIndex].received == "yes")
) {
let error = {
statusCode: 400,
body: JSON.stringify({
message,
}),
};
return error;
} else if (
(rows[rowIndex].remarks === "DONE" && rows[rowIndex].received == "no") ||
(rows[rowIndex].remarks === "done" && rows[rowIndex].received == "no")
) {
rows[rowIndex].received = "yes";
message = "Transaction is DONE Already and marked as Received!";
} else {
if (rows[rowIndex].received == "no" || rows[rowIndex].received == false) {
rows[rowIndex].received = "yes";
message = "Order Marked As Received!";
} else {
rows[rowIndex].received = "no";
message = "Order Unmarked as Received!";
}
}
await rows[rowIndex].save();
return {
statusCode: 200,
body: JSON.stringify({
message,
}),
};
} catch (e) {
return {
statusCode: 500,
body: e.toString(),
};
}
};