-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
62 lines (53 loc) · 1.56 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
import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
import dotenv from "dotenv";
import jwt from "jsonwebtoken";
import Service from "./service";
import { Root, StatusEnum } from "./interface";
import {
NotificationError,
NotificationErrorTypeEnum,
errorHandlingMiddleware,
} from "./error";
dotenv.config();
const app = express();
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const PORT = process.env.PORT || 8111;
app.use(cors());
app.use(errorHandlingMiddleware);
app.post("/generate-token", async (req, res, next) => {
try {
const { secretKey } = req.body;
if (secretKey === process.env.SECRET_KEY) {
const token = jwt.sign({}, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_EXPIRES_IN,
});
res.status(StatusEnum.SUCCESS).json({ token });
} else {
throw new NotificationError(NotificationErrorTypeEnum.UNAUTHORIZED);
}
} catch (error) {
next(error);
}
});
app.post("/send-notification", Service.authenticate, async (req, res, next) => {
try {
const message: Root = req.body;
const notificationMessage = message.text
? message.text
: Service.getNotificationMessage(message);
if (notificationMessage)
await Service.sendNotification(notificationMessage);
res
.status(StatusEnum.SUCCESS)
.json({ message: "✅ Notification sent successfully!" });
} catch (error) {
next(error);
}
});
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});