-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (63 loc) · 2.08 KB
/
index.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
const fs = require("fs/promises");
const { PDFDocument } = require("pdf-lib");
const AWS = require("aws-sdk");
const path = require("path");
exports.handler = async (event) => {
try {
console.log("event object", event);
const data = await fs.readFile("medicare-form.pdf");
const pdfDoc = await PDFDocument.load(data);
const form = pdfDoc.getForm();
let firstNameResponse = "Johnny";
let lastNameResponse = "Depp";
const firstName = form.getTextField("firstname");
const lastName = form.getTextField("lastname");
const spouseFirstname = form.getTextField("spouseFirstname");
const spouseLastname = form.getTextField("spouseLastname");
if (
event &&
event.ApplicantInfo &&
event.ApplicantInfo.Name
) {
firstNameResponse = event.ApplicantInfo.Name.First;
lastNameResponse = event.ApplicantInfo.Name.Last;
firstName.setText(firstNameResponse);
lastName.setText(lastNameResponse);
} else {
firstName.setText("Johnny");
lastName.setText("Depp");
}
if (event && event.SpouseInfo2 && event.SpouseInfo2.Name) {
spouseFirstname.setText(event.SpouseInfo2.Name.First);
spouseLastname.setText(event.SpouseInfo2.Name.Last);
} else {
spouseFirstname.setText("Black");
spouseLastname.setText("Canary");
}
console.log("setting up S3...");
AWS.config.update({ region: "us-east-1" });
s3 = new AWS.S3({ apiVersion: "2006-03-01" });
const uploadParams = {
Bucket: "medicareforms3",
Key: path.basename(
`${firstNameResponse.toLowerCase()}-${lastNameResponse.toLowerCase()}-medicare.pdf`
),
Body: await pdfDoc.save(),
};
console.log("sending file to S3...");
const response = await s3.upload(uploadParams).promise();
console.log("success..", response.Location);
return {
statusCode: 200,
body: JSON.stringify({
response: "File uploaded..." + response.Location,
}),
};
} catch (e) {
console.error(e);
return {
statusCode: 200,
body: JSON.stringify({ error: e }),
};
}
};