Skip to content

Commit

Permalink
Merge pull request #989 from OneCommunityGlobal/Abdel-modal-assign-bl…
Browse files Browse the repository at this point in the history
…ue-square-emails

Abdel modal assign blue square emails
  • Loading branch information
one-community authored Jun 14, 2024
2 parents 9ee17da + fe8cfd0 commit a3c33d8
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
67 changes: 67 additions & 0 deletions src/controllers/BlueSquareEmailAssignmentController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const BlueSquareEmailAssignmentController = function (BlueSquareEmailAssignment, userProfile) {
const getBlueSquareEmailAssignment = async function (req, res) {
try {
const assignments = await BlueSquareEmailAssignment.find().populate('assignedTo').exec()
res.status(200).send(assignments);
} catch (error) {
console.log(error)
res.status(500).send(error);
}
};

const setBlueSquareEmailAssignment = async function (req, res) {
try {
const { email } = req.body;

if (!email) {
res.status(400).send('bad request');
return;
}

const user = await userProfile.findOne({ email });
if (!userProfile) {
return res.status(400).send('User profile not found');
}

const newAssignment = new BlueSquareEmailAssignment({
email,
assignedTo: user._id,
});
await newAssignment.save();
const assignment = await BlueSquareEmailAssignment.find({email}).populate('assignedTo').exec()

res.status(200).send(assignment[0]);
} catch (error) {
res.status(500).send(error);
}
};

const deleteBlueSquareEmailAssignment = async function (req, res) {
try {
const { id } = req.params;

if (!id) {
res.status(400).send('bad request');
return;
}

const deletedAssignment = await BlueSquareEmailAssignment.findOneAndDelete({ _id: id });
if (!deletedAssignment) {
res.status(404).send('Assignment not found');
return;
}

res.status(200).send({id});
} catch (error) {
res.status(500).send(error);
}
};

return {
getBlueSquareEmailAssignment,
setBlueSquareEmailAssignment,
deleteBlueSquareEmailAssignment,
};
};

module.exports = BlueSquareEmailAssignmentController;
19 changes: 18 additions & 1 deletion src/helpers/userHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const reportHelper = require('./reporthelper')();
const emailSender = require('../utilities/emailSender');
const logger = require('../startup/logger');
const token = require('../models/profileInitialSetupToken');
const BlueSquareEmailAssignment = require('../models/BlueSquareEmailAssignment');
const cache = require('../utilities/nodeCache')();
const timeOffRequest = require('../models/timeOffRequest');
const notificationService = require('../services/notificationService');
Expand Down Expand Up @@ -726,11 +727,27 @@ const userHelper = function () {
administrativeContent,
);
}

let emailsBCCs;
const blueSquareBCCs = await BlueSquareEmailAssignment.find()
.populate('assignedTo')
.exec();
if (blueSquareBCCs.length > 0) {
emailsBCCs = blueSquareBCCs.map((assignment) => {
if (assignment.assignedTo.isActive === true) {
return assignment.email
}
});
} else {
emailsBCCs = null;
}


emailSender(
status.email,
'New Infringement Assigned',
emailBody,
null,
emailsBCCs,
'[email protected]',
status.email,
null,
Expand Down
10 changes: 10 additions & 0 deletions src/models/BlueSquareEmailAssignment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const mongoose = require("mongoose");

const { Schema } = mongoose;

const BlueSquareEmailAssignmentSchema = new Schema({
email: { type: String, required: true, unique: true },
assignedTo: { type: mongoose.SchemaTypes.ObjectId, ref: 'userProfile', required: true }
});

module.exports = mongoose.model("BlueSquareEmailAssignment", BlueSquareEmailAssignmentSchema, "BlueSquareEmailAssignments");
18 changes: 18 additions & 0 deletions src/routes/BlueSquareEmailAssignmentRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const express = require('express');

const routes = function (BlueSquareEmailAssignment,userProfile) {
const BlueSquareEmailAssignmentRouter = express.Router();
const controller = require('../controllers/BlueSquareEmailAssignmentController')(BlueSquareEmailAssignment,userProfile);

BlueSquareEmailAssignmentRouter.route('/AssignBlueSquareEmail')
.get(controller.getBlueSquareEmailAssignment)
.post(controller.setBlueSquareEmailAssignment)

BlueSquareEmailAssignmentRouter.route('/AssignBlueSquareEmail/:id')
.delete(controller.deleteBlueSquareEmailAssignment);


return BlueSquareEmailAssignmentRouter;
};

module.exports = routes;
4 changes: 4 additions & 0 deletions src/startup/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const rolePreset = require('../models/rolePreset');
const ownerMessage = require('../models/ownerMessage');
// Title
const title = require('../models/title');
const blueSquareEmailAssignment = require('../models/BlueSquareEmailAssignment');

const weeklySummaryAIPrompt = require('../models/weeklySummaryAIPrompt');
const profileInitialSetuptoken = require('../models/profileInitialSetupToken');
Expand Down Expand Up @@ -119,6 +120,8 @@ const bmToolRouter = require('../routes/bmdashboard/bmToolRouter')(buildingTool,
const bmEquipmentRouter = require('../routes/bmdashboard/bmEquipmentRouter')(buildingEquipment);
const bmIssueRouter = require('../routes/bmdashboard/bmIssueRouter')(buildingIssue);

const blueSquareEmailAssignmentRouter = require('../routes/BlueSquareEmailAssignmentRouter')(blueSquareEmailAssignment,userProfile)


module.exports = function (app) {
app.use('/api', forgotPwdRouter);
Expand Down Expand Up @@ -156,6 +159,7 @@ module.exports = function (app) {
app.use('/api', titleRouter);
app.use('/api', timeOffRequestRouter);
app.use('/api', followUpRouter);
app.use('/api', blueSquareEmailAssignmentRouter)
// bm dashboard
app.use('/api/bm', bmLoginRouter);
app.use('/api/bm', bmMaterialsRouter);
Expand Down

0 comments on commit a3c33d8

Please sign in to comment.