-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (133 loc) · 3.57 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
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
140
141
142
const express = require("express");
const app = express();
const mime = require("mime-types");
const fs = require("fs");
const path = require("path");
const jimp = require("jimp");
const joi = require("@hapi/joi");
const { MongoClient } = require("mongodb");
const stream = require("stream");
(async function () {
require("dotenv").config();
const hostname = "localhost";
const port = process.env.PORT || 3000;
const uri = process.env.DB_CONN;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.use(express.urlencoded());
app.use(express.static(__dirname + "/public"));
try {
await client.connect();
} catch (err) {
console.error(err);
} finally {
console.log("Connected to db");
}
app.all("/", (req, res, next) => {
res.redirect("./events.html");
});
app.post("/generateCertificate", async (req, res, next) => {
try {
var getObj = {
userEmail: req.body.user.email,
};
var checkedEmail = await validateEmail(getObj);
if (!checkedEmail) {
res.statusCode = 422;
res.send("Invalid Email Address");
res.end();
} else {
var participantName = await findOneListingByEmail(client, checkedEmail);
if (participantName) {
var certImage = await createCertificate(participantName);
const fileName = "Upskill-certificate.png";
var file = Buffer.from(certImage, "base64");
const mimeType = mime.lookup(file);
var readStream = new stream.PassThrough();
readStream.end(file);
res.writeHead(200, {
"Content-Disposition": `attachment; filename="${fileName}"`,
"Content-Type": mimeType,
});
readStream.pipe(res);
} else {
res.statusCode = 404;
res.send("Certificate not available");
res.end();
}
}
} catch (err) {
console.error(err);
}
});
app.listen(port, () => {
console.log(`listening on port: ${port}`);
});
})();
async function validateEmail(getObj) {
try {
const schema = joi.object({
userEmail: joi.string().email(),
});
const value = await schema.validateAsync(getObj);
if (value.userEmail) {
return value.userEmail;
} else {
return;
}
} catch (err) {
console.log(err);
}
}
async function findOneListingByEmail(client, userEmail) {
try {
const cursor = await client
.db()
.collection("events")
.aggregate(
{
$match: {
id: "upskill_attendance",
"presentRegistrants.Email": userEmail,
},
},
{ $unwind: "$presentRegistrants" },
{
$match: {
"presentRegistrants.Email": userEmail,
},
},
{ "presentRegistrants.$": 1 }
)
.toArray();
if (!cursor || cursor.length < 1) {
return;
}
var obj = cursor[0].presentRegistrants;
if (obj.Name) {
return obj.Name;
} else {
return;
}
} catch (err) {
console.log(err);
}
}
async function createCertificate(participantName) {
const certPath = path.join(
__dirname,
"/public/res/img/certificates/upskill.png"
);
const fontPath = path.join(__dirname, "/public/res/fonts/alexa-font.fnt");
try {
const cert = await jimp.read(certPath);
const font = await jimp.loadFont(jimp.FONT_SANS_128_BLACK);
cert.print(font, 1350, 1200, participantName);
const certImage = await cert.getBufferAsync(jimp.MIME_PNG);
return certImage;
} catch (err) {
console.error(err);
}
}