-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
139 lines (130 loc) · 4.2 KB
/
index.ts
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
import { Application, Router, RouterContext } from "https://deno.land/x/oak/mod.ts";
import { oakCors } from "https://deno.land/x/cors/mod.ts";
const MAILJET_NEXT_API_URL = 'https://api.mailjet.com/v3.1'
const headers = new Headers();
headers.set('Content-Type', 'application/json')
headers.set('Authorization', 'Basic ' + btoa(Deno.env.get('MJ_APIKEY_PUBLIC') + ":" + Deno.env.get('MJ_APIKEY_PRIVATE')))
const sendEmail = (messages: any): Promise<any> => {
return fetch(MAILJET_NEXT_API_URL + '/send', {
method: "POST",
headers,
body: JSON.stringify({
Messages: messages,
}),
});
}
const betterParseInt = (val: unknown) => {
if (typeof val === 'string') {
return parseInt(val.replace(/\D/g, ''), 10);
}
if (typeof val === 'number') {
return val;
}
}
const router = new Router();
router
.post("/", async (context: RouterContext) => {
try {
const body = await context.request.body().value
const entities = body.entities
.filter((entity: any) => entity['text'] && Array.isArray(entity['text']) && entity['text'].length)
.map((entity: any) => ({ [entity['name']]: entity['text'].map(betterParseInt) }))
const message = JSON.stringify(entities)
let Filename = 'result.json'
if (body.metadata && body.metadata.total_page && body.metadata.total_page.length && body.metadata.total_page[0].document_name) {
Filename = body.metadata?.total_page[0]?.document_name?.replace('.pdf', '.json')
}
const response = await sendEmail([
{
From: {
Email: '[email protected]',
Name: 'The Green Alternative - Test'
},
To: [
{
Email: Deno.env.get('TO_EMAIL'),
Name: 'Test',
},
],
Subject: 'Hook received',
TextPart: 'JSON file attached with the hook data.',
Attachments: [
{
ContentType: 'application/json',
Filename,
Base64Content: btoa(unescape(encodeURIComponent(message))),
}
],
},
])
context.response.status = 200
context.response.headers.set("Content-Type", "application/json")
const jsonRes = await response.json()
if (!response.ok) {
console.error(jsonRes)
}
context.response.body = {
success: response.ok,
...(!response.ok && { jsonRes })
}
} catch (error) {
console.error(error)
context.response.status = 400
context.response.headers.set("Content-Type", "application/json")
context.response.body = {
error
}
}
})
.post("/pure", async (context: RouterContext) => {
try {
const body = await context.request.body().value
const message = JSON.stringify(body)
const Filename = 'result.json'
const response = await sendEmail([
{
From: {
Email: '[email protected]',
Name: 'The Green Alternative - Test'
},
To: [
{
Email: Deno.env.get('TO_EMAIL'),
Name: 'Test',
},
],
Subject: 'Hook received',
TextPart: 'JSON file attached with the hook data.',
Attachments: [
{
ContentType: 'application/json',
Filename,
Base64Content: btoa(unescape(encodeURIComponent(message))),
}
],
},
])
context.response.status = 200
context.response.headers.set("Content-Type", "application/json")
const jsonRes = await response.json()
if (!response.ok) {
console.error(jsonRes)
}
context.response.body = {
success: response.ok,
...(!response.ok && { jsonRes })
}
} catch (error) {
console.error(error)
context.response.status = 400
context.response.headers.set("Content-Type", "application/json")
context.response.body = {
error
}
}
});
const app = new Application();
app.use(oakCors()); // Enable CORS for All Routes
app.use(router.routes());
console.info("CORS-enabled web server listening on port 8000");
await app.listen({ port: 8000 });