Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions Backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const loginService = require("./service/login");
const verifyService = require("./service/verify");
const updateService = require("./service/update");
const scoreService = require("./service/score");
const mailService = require("./service/mail");
const util = require("./utils/util");

const healthPath = "/health";
Expand All @@ -11,42 +12,40 @@ const loginPath = "/login";
const verifyPath = "/verify";
const scorePath = "/score";
const updatePath = "/update";
const mailPath = "/sendmail";

exports.handler = async (event) => {
console.log("Request Event : ", event);
let response;
switch (true) {
case event.httpMethod === "GET" && event.path === healthPath:
console.log("healthEvent");
response = util.buildResponse(200);
break;
case event.httpMethod === "POST" && event.path === registerPath:
console.log("registerEvent");
const registerBody = JSON.parse(event.body);
response = await registerService.register(registerBody);
break;
case event.httpMethod === "POST" && event.path === loginPath:
console.log("loginEvent");
const loginBody = JSON.parse(event.body);
response = await loginService.login(loginBody);
break;
case event.httpMethod === "POST" && event.path === verifyPath:
console.log("verifyEvent");
const verifyBody = JSON.parse(event.body);
response = verifyService.verify(verifyBody);
break;
case event.httpMethod === "PUT" && event.path === updatePath:
console.log("updateEvent");
const updateBody = JSON.parse(event.body);
response = updateService.update(updateBody);
break;
case event.httpMethod === "POST" && event.path === scorePath:
console.log("scoreEvent");
const scoreBody = JSON.parse(event.body);
response = scoreService.score(scoreBody);
break;
case event.httpMethod === "POST" && event.path === mailPath:
const emailBody = JSON.parse(event.body);
response = mailService.mail(emailBody);
break;
default:
console.log("nullEvent");
response = util.buildResponse(404, "404 Not Found!");
}
return response;
Expand Down
79 changes: 79 additions & 0 deletions Backend/service/mail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const AWS = require("aws-sdk");
AWS.config.update({
region: "ap-south-1",
});
const util = require("../utils/util");
const bcrypt = require("bcryptjs");
const auth = require("../utils/auth");
const dynamodb = new AWS.DynamoDB.DocumentClient();
const userTable = "User_Data";
const amazonSes = new AWS.SES({ region: "ap-south-1" });

async function mail(requestBody) {
const to = requestBody.to;
const subject = requestBody.subject;
const body = requestBody.body;
const username = requestBody.username;
if (!username || !to || !body) {
return util.buildResponse(401, {
message: "Missing fields!",
});
}
const dynamoUser = await getUser(username);
if (!dynamoUser || !dynamoUser.username) {
return util.buildResponse(403, {
message: "user does not exist",
});
}
const params = {
Source: "hithu203@gmail.com",
Destination: {
ToAddresses: [to],
},
Message: {
Body: {
Text: {
Data: body,
},
},
Subject: {
Data: subject,
},
},
};
try {
const result = await amazonSes.sendEmail(params).promise();
console.log(result);
return util.buildResponse(200, {
message: "Email sent successfully",
});
} catch (error) {
console.log(error);
return util.buildResponse(500, {
message: "Please enter verified email address",
});
}
}

async function getUser(username) {
const params = {
TableName: userTable,
Key: {
username: username,
},
};
return await dynamodb
.get(params)
.promise()
.then(
(response) => {
console.log("Response received!", response.Item);
return response.Item;
},
(error) => {
console.error("There is an error: ", error);
}
);
}

module.exports.mail = mail;