Skip to content

Commit

Permalink
Merge branch 'restructure-email-backend'
Browse files Browse the repository at this point in the history
  • Loading branch information
Aakarsh-Lohani committed Jul 31, 2024
2 parents c874810 + 4849b9d commit 3d70ddc
Show file tree
Hide file tree
Showing 41 changed files with 1,199 additions and 314 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
node_modules
/.angular/cache
/.angular/cache
Front-end/.angular
.vscode/extensions.json
Front-end/.vscode/extensions.json
61 changes: 0 additions & 61 deletions Back-end/Controllers/Email.controller.js

This file was deleted.

8 changes: 8 additions & 0 deletions Back-end/Models/Auth.model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import mongoose from "mongoose";
import bcrypt from "bcryptjs";

/**
* Represents the authentication schema for the user.
*
* @typedef {Object} AuthSchema
* @property {string} email - The email of the user.
* @property {string} password - The password of the user.
* @property {string} [role=Admin] - The role of the user. Defaults to "Admin".
*/
const AuthSchema = new mongoose.Schema({
email: {
type: String,
Expand Down
10 changes: 10 additions & 0 deletions Back-end/Models/Template.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ const variable = {
}
}

/**
* TemplateSchema represents the schema for a template in the application.
*
* @typedef {Object} TemplateSchema
* @property {String} templateId - The unique identifier for the template.
* @property {String} Subject - The subject of the template.
* @property {String} Content - The content of the template.
* @property {String} type - The type of the template (email, sms, whatsapp).
* @property {Array} variables - An array of variables used in the template.
*/
const TemplateSchema = new mongoose.Schema({
templateId: {
type: String,
Expand Down
11 changes: 8 additions & 3 deletions Back-end/email-backend/config/emailConfig.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import NodemailerService from "../services/nodemailerService.js";
import SendgridService from "../services/sendgridService.js";

/**
* Function to get the appropriate email service based on the environment variable.
* @returns {Object} - An instance of the selected email service.
* @throws {Error} - If the EMAIL_SERVICE environment variable is invalid.
*/

const getEmailService=()=>{
switch (process.env.EMAIL_SERVICE){
const getEmailService = () => {
switch (process.env.EMAIL_SERVICE) {
case 'nodemailer':
return new NodemailerService();
case 'sendgrid':
return new SendgridService();
default:
throw new Error("Invalid Email service selected")
throw new Error("Invalid Email service selected");
}
};

Expand Down
5 changes: 4 additions & 1 deletion Back-end/email-backend/controllers/historyController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import EmailHistory from '../models/emailHistory.js';
/**
* Fetch all email history from the database.
* @returns {Array} - An array of email history records.
*/

// Fetch all history from the database
export const getHistory = async () => {
try {
const history = await EmailHistory.find();
Expand Down
42 changes: 42 additions & 0 deletions Back-end/email-backend/controllers/templateController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import EmailTemplate from '../models/template.js';

// Fetch all templates from the database
/**
* Retrieves all email templates from the database.
* @returns {Promise<Array>} A promise that resolves to an array of email templates.
*/
export const getTemplates = async () => {
try {
const templates = await EmailTemplate.find();
Expand All @@ -12,6 +16,11 @@ export const getTemplates = async () => {
};

// Select a template by ID
/**
* Retrieves a template by its ID.
* @param {string} id - The ID of the template to retrieve.
* @returns {Promise<Object|null>} - A promise that resolves to the template object if found, or null if not found.
*/
export const getTemplateById = async (id) => {
try {
const template = await EmailTemplate.findById(id);
Expand All @@ -23,6 +32,13 @@ export const getTemplateById = async (id) => {
};

// Create a new template
/**
* Creates a new email template.
* @param {string} name - The name of the template.
* @param {string} subject - The subject of the email template.
* @param {string} body - The body content of the email template.
* @returns {Promise<Object|null>} - A promise that resolves to the newly created template object, or null if an error occurs.
*/
export const createTemplate = async (name, subject, body) => {
const newTemplate = new EmailTemplate({
name,
Expand All @@ -38,3 +54,29 @@ export const createTemplate = async (name, subject, body) => {
return null;
}
};


/**
* Updates an existing email template.
* @param {Object} template - The template object to update.
* @returns {Promise<Object|null>} - A promise that resolves to the updated template object, or null if an error occurs.
*/
export const updateTemplate = async (template) => {
try {
const existingTemplate = await EmailTemplate.findById(template._id);
if (!existingTemplate) {
console.error('Template not found in update');
return null;
}

existingTemplate.name = template.name;
existingTemplate.subject = template.subject;
existingTemplate.body = template.body;

await existingTemplate.save();
return existingTemplate;
} catch (error) {
console.error('Error updating template:', error);
return null;
}
};
85 changes: 55 additions & 30 deletions Back-end/email-backend/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import User from '../models/userModel.js';

// Controller to fetch all users
/**
* Get all users.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @returns {Promise<void>} - A promise that resolves with the fetched users or an error message.
*/

export const getUsers = async (req, res) => {
try {
const users = await User.find();
Expand All @@ -10,35 +17,53 @@ export const getUsers = async (req, res) => {
res.status(500).json({ message: 'Failed to fetch users' });
}
};
// Controller to create a new user



// Controller to create a new user
/**
* Create a new user.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @returns {Promise<void>} - A promise that resolves when the user is created.
*/

export const createUser = async (req, res) => {
const { name, email, otherField } = req.body;

const newUser = new User({
name,
email,
otherField
});

try {
const savedUser = await newUser.save();
res.status(201).json(savedUser);
} catch (error) {
console.error('Error creating user:', error);
res.status(500).json({ message: 'Failed to create user' });
}
};

// Controller to upload user data
export const uploadUserData = async (req, res) => {
try {
const users = req.body; // Assuming req.body is an array of users

const savedUsers = await User.insertMany(users);
res.status(201).json(savedUsers);
} catch (error) {
console.error('Error uploading users:', error);
res.status(500).json({ message: 'Failed to upload users' });
}
};
const { name, email, otherField } = req.body;

const newUser = new User({
name,
email,
otherField
});

try {
const savedUser = await newUser.save();
res.status(201).json(savedUser);
} catch (error) {
console.error('Error creating user:', error);
res.status(500).json({ message: 'Failed to create user' });
}
};


// Controller to upload user data
/**
* Uploads user data to the database.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @returns {Promise<void>} - A Promise that resolves when the user data is uploaded.
*/

export const uploadUserData = async (req, res) => {
try {
const users = req.body; // Assuming req.body is an array of users

const savedUsers = await User.insertMany(users);
res.status(201).json(savedUsers);
} catch (error) {
console.error('Error uploading users:', error);
res.status(500).json({ message: 'Failed to upload users' });
}
};

11 changes: 11 additions & 0 deletions Back-end/email-backend/models/emailHistory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import mongoose from 'mongoose';

/**
* Represents the schema for email history.
*
* @typedef {Object} EmailHistorySchema
* @property {string} username - The username associated with the email.
* @property {string} email - The email address.
* @property {string} status - The status of the email.
* @property {string} time - The time when the email was sent.
* @property {string} date - The date when the email was sent.
*/

const emailHistorySchema = new mongoose.Schema({
username: String,
email: String,
Expand Down
9 changes: 9 additions & 0 deletions Back-end/email-backend/models/template.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import mongoose from 'mongoose';

/**
* Represents a template schema.
*
* @typedef {Object} TemplateSchema
* @property {string} name - The name of the template.
* @property {string} subject - The subject of the template.
* @property {string} body - The body of the template.
* @property {Date} createdAt - The creation date of the template.
*/
const templateSchema = new mongoose.Schema({
name: {
type: String,
Expand Down
9 changes: 9 additions & 0 deletions Back-end/email-backend/models/userModel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import mongoose from 'mongoose';

/**
* Represents the user schema.
*
* @typedef {Object} UserSchema
* @property {string} name - The name of the user.
* @property {string} email - The email of the user.
* @property {number} age - The age of the user.
* @property {string} address - The address of the user.
*/
const userSchema = new mongoose.Schema({
name: String,
email: String,
Expand Down
4 changes: 4 additions & 0 deletions Back-end/email-backend/routes/emailHistoryRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const router = express.Router();
// Route to fetch all history
router.get('/history', async (req, res) => {
try {
/**
* Represents the email history.
* @type {Array}
*/
const history = await getHistory();
res.status(200).json(history);
} catch (error) {
Expand Down
Loading

0 comments on commit 3d70ddc

Please sign in to comment.