-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLambdaEntry.js
76 lines (61 loc) · 2.38 KB
/
LambdaEntry.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 { createServer, proxy } = require('aws-serverless-express');
const { init } = require('./dist/App');
const { Environment } = require('./dist/helpers/Environment');
const { Pool } = require('@churchapps/apihelper');
const AWS = require('aws-sdk');
const { Logger } = require('./dist/helpers/Logger');
const { SocketHelper } = require('./dist/helpers/SocketHelper');
const { ApiGatewayManagementApi } = require('aws-sdk');
let gwManagement = new ApiGatewayManagementApi({ apiVersion: '2020-04-16', endpoint: Environment.socketUrl });
Environment.init(process.env.APP_ENV).then(() => {
Pool.initPool();
gwManagement = new ApiGatewayManagementApi({ apiVersion: '2020-04-16', endpoint: Environment.socketUrl });
});
async function logMessage(message) {
const wl = new Logger();
wl.error(message);
await wl.flush();
}
const checkPool = async () => {
if (!Environment.connectionString) {
await Environment.init(process.env.APP_ENV)
Pool.initPool();
}
}
module.exports.timer15Min = async (event, context) => {
return await NotificationHelper.sendEmailNotifications("individual");
}
module.exports.timerMidnight = async (event, context) => {
return await NotificationHelper.sendEmailNotifications("daily");
}
module.exports.universal = function universal(event, context) {
checkPool().then(() => {
init().then(app => {
const server = createServer(app);
return proxy(server, event, context);
});
});
}
module.exports.handleWeb = function handleWeb(event, context) {
AWS.config.update({ region: 'us-east-2' });
checkPool().then(() => {
init().then(app => {
const server = createServer(app);
return proxy(server, event, context);
});
});
}
module.exports.handleSocket = async function handleSocket(event) {
const rc = event.requestContext;
const eventType = rc.eventType;
const connectionId = rc.connectionId;
await checkPool();
if (eventType == "DISCONNECT") await SocketHelper.handleDisconnect(connectionId) //; Repositories.getCurrent().connection.deleteForSocket(connectionId);
else if (eventType == "MESSAGE") {
const payload = { churchId: "", conversationId: "", action: "socketId", data: rc.connectionId }
//try {
await gwManagement.postToConnection({ ConnectionId: rc.connectionId, Data: JSON.stringify(payload) }).promise();
//} catch (e) { logMessage(e.toString()); }
}
return { statusCode: 200, body: 'success' }
}