Skip to content

Commit 0bd1992

Browse files
one-communityAmalesh-AAshritaCherlapallyVenk-rgbVenkataramanan Venkateswaran
authored
Backend Release to Main [2.51] (#1679)
* Amalesh - BmDashboard Unit tests (#1461) * First * Second * Third * Fourth * Fourth * Amalesh - Unit Tests for faqController, jobsController, titleController, timeEntryController (#1487) * First File * Second File * Third File * Final * Unit tests for faqController.js - Added * Unit tests for faqController.js - Added * Unit tests for jobsController.js - Added * Unit tests for jobsController.js - Added * Unit tests for titleController.js - Added * Unit tests for timeEntryController.js - Added * Ashrita update consumable form (#1262) * Revert "Backend Release to Main [2.11]" * Revert "Revert "Backend Release to Main [2.11]"" * Updated-Consumable-Form --------- Co-authored-by: One Community <one-community@users.noreply.github.com> * Revert "Amalesh - BmDashboard Unit tests" (#1675) Revert "Amalesh - BmDashboard Unit tests (#1461)" This reverts commit 775cebc. * Venkataramanan fix: teams tab to display with no members (#1673) * fix: teams tab to display with no members * fix: failing tests --------- Co-authored-by: Venkataramanan Venkateswaran <venk@Venkataramanans-MacBook-Air.local> * Vamsi Krishna - Added 'viewTaskExtensionCount' permission to Owner & Admin (#1326) Added viewTaskExtensionCount permission to Owner&Admin * Manoj cost prediction line chart (#1528) * Models, router,controller * new endpoint forprojectIDs * new endpoint forprojectIDs * no date range options * setup files and routes * Beckend Poiints logic implemented * backup * backup * replaced pprediction using ml-regression library * duplicatefix * fix: update package-lock.json to resolve npm ci issues * fix: regenerate package-lock.json with Node.js 20 to resolve Azure storage blob compatibility * fix: use working package-lock.json from development branch to resolve npm ci issues * Sundar: fix test failure in createTestPermissions.js (#1680) fix(tests): fix test failures Co-authored-by: Sundar Machani <sundarmachani@Sundars-MacBook-Pro.local> --------- Co-authored-by: Amalesh Arivanan <92014450+Amalesh-A@users.noreply.github.com> Co-authored-by: Ashrita Cherlapally <160983565+AshritaCherlapally@users.noreply.github.com> Co-authored-by: Venkataramanan <59522101+Venk-rgb@users.noreply.github.com> Co-authored-by: Venkataramanan Venkateswaran <venk@Venkataramanans-MacBook-Air.local> Co-authored-by: Vamsi Krishna Rolla <63103226+vamsikrishna1704@users.noreply.github.com> Co-authored-by: Manoj99Q <144982472+Manoj99Q@users.noreply.github.com> Co-authored-by: Sundar Machani <76655727+sundarmachani@users.noreply.github.com> Co-authored-by: Sundar Machani <sundarmachani@Sundars-MacBook-Pro.local>
1 parent 23d47c3 commit 0bd1992

31 files changed

Lines changed: 2544 additions & 277 deletions
Lines changed: 106 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,111 @@
1-
const WeeklySummaryEmailAssignmentController = function (WeeklySummaryEmailAssignment, userProfile) {
2-
const getWeeklySummaryEmailAssignment = async function (req, res) {
3-
try {
4-
const assignments = await WeeklySummaryEmailAssignment.find().populate('assignedTo').exec();
5-
res.status(200).send(assignments);
6-
} catch (error) {
7-
res.status(500).send(error);
1+
const WeeklySummaryEmailAssignmentController = function (
2+
WeeklySummaryEmailAssignment,
3+
userProfile,
4+
) {
5+
const getWeeklySummaryEmailAssignment = async function (req, res) {
6+
try {
7+
const assignments = await WeeklySummaryEmailAssignment.find().populate('assignedTo').exec();
8+
res.status(200).send(assignments);
9+
} catch (error) {
10+
res.status(500).send(error);
11+
}
12+
};
13+
14+
const setWeeklySummaryEmailAssignment = async function (req, res) {
15+
try {
16+
const { email } = req.body;
17+
18+
if (!email) {
19+
res.status(400).send('bad request');
20+
return;
821
}
9-
};
10-
11-
const setWeeklySummaryEmailAssignment = async function (req, res) {
12-
try {
13-
const { email } = req.body;
14-
15-
if (!email) {
16-
res.status(400).send('bad request');
17-
return;
18-
}
19-
20-
const user = await userProfile.findOne({ email });
21-
if (!user) {
22-
return res.status(400).send('User profile not found');
23-
}
24-
25-
const newAssignment = new WeeklySummaryEmailAssignment({
26-
email,
27-
assignedTo: user._id,
28-
});
29-
30-
await newAssignment.save();
31-
const assignment = await WeeklySummaryEmailAssignment.find({ email }).populate('assignedTo').exec();
32-
33-
res.status(200).send(assignment[0]);
34-
} catch (error) {
35-
res.status(500).send(error);
22+
23+
const user = await userProfile.findOne({ email });
24+
if (!user) {
25+
return res.status(400).send('User profile not found');
3626
}
37-
};
38-
39-
const deleteWeeklySummaryEmailAssignment = async function (req, res) {
40-
try {
41-
const { id } = req.params;
42-
43-
if (!id) {
44-
res.status(400).send('bad request');
45-
return;
46-
}
47-
48-
const deletedAssignment = await WeeklySummaryEmailAssignment.findOneAndDelete({ _id: id });
49-
if (!deletedAssignment) {
50-
res.status(404).send('Assignment not found');
51-
return;
52-
}
53-
54-
res.status(200).send({ id });
55-
} catch (error) {
56-
res.status(500).send(error);
27+
28+
const newAssignment = new WeeklySummaryEmailAssignment({
29+
email,
30+
assignedTo: user._id,
31+
});
32+
33+
await newAssignment.save();
34+
const assignment = await WeeklySummaryEmailAssignment.find({ email })
35+
.populate('assignedTo')
36+
.exec();
37+
38+
res.status(200).send(assignment[0]);
39+
} catch (error) {
40+
res.status(500).send(error);
41+
}
42+
};
43+
44+
const deleteWeeklySummaryEmailAssignment = async function (req, res) {
45+
try {
46+
const { id } = req.params;
47+
48+
if (!id) {
49+
res.status(400).send('bad request');
50+
return;
5751
}
58-
};
59-
60-
const updateWeeklySummaryEmailAssignment = async function (req, res) {
61-
try{
62-
const { id } = req.params;
63-
const { email } = req.body;
64-
65-
if (!id || (!email)) {
66-
res.status(400).send('bad request');
67-
return;
68-
}
69-
70-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
71-
if (!emailRegex.test(email)) {
72-
return res.status(400).json({
73-
error: 'Invalid email format'
74-
});
75-
}
76-
77-
const updateAssignment = await WeeklySummaryEmailAssignment.findOneAndUpdate(
78-
{ _id: id },
79-
{
80-
email,
81-
},
82-
{
83-
new: true
84-
}
85-
);
86-
87-
if (!updateAssignment) {
88-
res.status(404).send('Assignment not found');
89-
return;
90-
}
91-
92-
res.status(200).send(updateAssignment);
93-
94-
} catch (error) {
95-
res.status(500).send(error);
52+
53+
const deletedAssignment = await WeeklySummaryEmailAssignment.findOneAndDelete({ _id: id });
54+
if (!deletedAssignment) {
55+
res.status(404).send('Assignment not found');
56+
return;
9657
}
97-
};
98-
99-
return {
100-
getWeeklySummaryEmailAssignment,
101-
setWeeklySummaryEmailAssignment,
102-
deleteWeeklySummaryEmailAssignment,
103-
updateWeeklySummaryEmailAssignment
104-
};
58+
59+
res.status(200).send({ id });
60+
} catch (error) {
61+
res.status(500).send(error);
62+
}
10563
};
106-
107-
module.exports = WeeklySummaryEmailAssignmentController;
108-
64+
65+
const updateWeeklySummaryEmailAssignment = async function (req, res) {
66+
try {
67+
const { id } = req.params;
68+
const { email } = req.body;
69+
70+
if (!id || !email) {
71+
res.status(400).send('bad request');
72+
return;
73+
}
74+
75+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
76+
if (!emailRegex.test(email)) {
77+
return res.status(400).json({
78+
error: 'Invalid email format',
79+
});
80+
}
81+
82+
const updateAssignment = await WeeklySummaryEmailAssignment.findOneAndUpdate(
83+
{ _id: id },
84+
{
85+
email,
86+
},
87+
{
88+
new: true,
89+
},
90+
);
91+
92+
if (!updateAssignment) {
93+
res.status(404).send('Assignment not found');
94+
return;
95+
}
96+
97+
res.status(200).send(updateAssignment);
98+
} catch (error) {
99+
res.status(500).send(error);
100+
}
101+
};
102+
103+
return {
104+
getWeeklySummaryEmailAssignment,
105+
setWeeklySummaryEmailAssignment,
106+
deleteWeeklySummaryEmailAssignment,
107+
updateWeeklySummaryEmailAssignment,
108+
};
109+
};
110+
111+
module.exports = WeeklySummaryEmailAssignmentController;

src/controllers/automation/sentryController.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const sentryService = require('../../services/automation/sentryService');
33
const appAccessService = require('../../services/automation/appAccessService');
44
const { checkAppAccess } = require('./utils');
55

6-
76
// Controller function to invite a user
87
async function inviteUser(req, res) {
98
const { targetUser } = req.body;
@@ -15,11 +14,16 @@ async function inviteUser(req, res) {
1514
if (!checkAppAccess(requestor.role)) {
1615
res.status(403).send({ message: 'Unauthorized request' });
1716
return;
18-
}
17+
}
1918

2019
try {
2120
const invitation = await sentryService.inviteUser(targetUser.email);
22-
await appAccessService.upsertAppAccess(targetUser.targetUserId, 'sentry', 'invited', targetUser.email);
21+
await appAccessService.upsertAppAccess(
22+
targetUser.targetUserId,
23+
'sentry',
24+
'invited',
25+
targetUser.email,
26+
);
2327
res.status(201).json({ message: 'Invitation sent', data: invitation });
2428
} catch (error) {
2529
res.status(500).json({ message: error.message });
@@ -50,8 +54,7 @@ async function removeUser(req, res) {
5054
}
5155
}
5256

53-
5457
module.exports = {
5558
inviteUser,
5659
removeUser,
57-
};
60+
};
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
const bmEquipmentController = require('../bmEquipmentController');
2+
3+
describe('bmEquipmentController', () => {
4+
let mockBuildingEquipment;
5+
let controller;
6+
let mockReq;
7+
let mockRes;
8+
9+
beforeEach(() => {
10+
// Mock BuildingEquipment model
11+
mockBuildingEquipment = {
12+
findById: jest.fn(),
13+
find: jest.fn(),
14+
findOne: jest.fn(),
15+
findOneAndUpdate: jest.fn(),
16+
create: jest.fn(),
17+
findByIdAndUpdate: jest.fn(),
18+
};
19+
20+
// Initialize controller with mock model
21+
controller = bmEquipmentController(mockBuildingEquipment);
22+
23+
// Mock request and response objects
24+
mockReq = {
25+
params: {},
26+
body: {},
27+
query: {},
28+
};
29+
mockRes = {
30+
status: jest.fn().mockReturnThis(),
31+
send: jest.fn(),
32+
json: jest.fn(),
33+
};
34+
});
35+
36+
describe('fetchSingleEquipment', () => {
37+
it('should fetch a single equipment by ID successfully', async () => {
38+
const mockEquipment = { _id: '123', name: 'Test Equipment' };
39+
const mockPopulate = jest.fn().mockReturnThis();
40+
const mockExec = jest.fn().mockResolvedValue(mockEquipment);
41+
42+
mockBuildingEquipment.findById.mockReturnValue({
43+
populate: mockPopulate,
44+
exec: mockExec,
45+
});
46+
47+
mockReq.params.equipmentId = '123';
48+
await controller.fetchSingleEquipment(mockReq, mockRes);
49+
50+
expect(mockBuildingEquipment.findById).toHaveBeenCalledWith('123');
51+
expect(mockRes.status).toHaveBeenCalledWith(200);
52+
expect(mockRes.send).toHaveBeenCalledWith(mockEquipment);
53+
});
54+
55+
it('should handle errors when fetching single equipment', async () => {
56+
const error = new Error('Database error');
57+
const mockPopulate = jest.fn().mockReturnThis();
58+
const mockExec = jest.fn().mockRejectedValue(error);
59+
60+
mockBuildingEquipment.findById.mockReturnValue({
61+
populate: mockPopulate,
62+
exec: mockExec,
63+
});
64+
65+
mockReq.params.equipmentId = '123';
66+
await controller.fetchSingleEquipment(mockReq, mockRes);
67+
await Promise.resolve();
68+
69+
expect(mockRes.status).toHaveBeenCalledWith(500);
70+
expect(mockRes.send).toHaveBeenCalledWith(error);
71+
});
72+
});
73+
74+
describe('fetchBMEquipments', () => {
75+
it('should fetch all building equipments successfully', async () => {
76+
const mockEquipments = [{ _id: '1' }, { _id: '2' }];
77+
const mockPopulate = jest.fn().mockReturnThis();
78+
const mockExec = jest.fn().mockResolvedValue(mockEquipments);
79+
80+
mockBuildingEquipment.find.mockReturnValue({
81+
populate: mockPopulate,
82+
exec: mockExec,
83+
});
84+
85+
await controller.fetchBMEquipments(mockReq, mockRes);
86+
87+
expect(mockBuildingEquipment.find).toHaveBeenCalled();
88+
expect(mockRes.status).toHaveBeenCalledWith(200);
89+
expect(mockRes.send).toHaveBeenCalledWith(mockEquipments);
90+
});
91+
92+
it('should handle errors when fetching all equipments', async () => {
93+
const error = new Error('Database error');
94+
const mockPopulate = jest.fn().mockReturnThis();
95+
const mockExec = jest.fn().mockRejectedValue(error);
96+
97+
mockBuildingEquipment.find.mockReturnValue({
98+
populate: mockPopulate,
99+
exec: mockExec,
100+
});
101+
102+
await controller.fetchBMEquipments(mockReq, mockRes);
103+
await Promise.resolve();
104+
105+
expect(mockRes.status).toHaveBeenCalledWith(500);
106+
expect(mockRes.send).toHaveBeenCalledWith(error);
107+
});
108+
});
109+
110+
describe('bmPurchaseEquipments', () => {
111+
it('should create new equipment purchase record when equipment does not exist', async () => {
112+
const purchaseData = {
113+
projectId: '123',
114+
equipmentId: '456',
115+
quantity: 2,
116+
priority: 'high',
117+
estTime: '2 weeks',
118+
desc: 'Test description',
119+
makeModel: 'Test Model',
120+
requestor: { requestorId: '789' },
121+
};
122+
123+
mockReq.body = purchaseData;
124+
mockBuildingEquipment.findOne.mockResolvedValue(null);
125+
mockBuildingEquipment.create.mockResolvedValue({});
126+
127+
await controller.bmPurchaseEquipments(mockReq, mockRes);
128+
129+
expect(mockBuildingEquipment.create).toHaveBeenCalled();
130+
expect(mockRes.status).toHaveBeenCalledWith(201);
131+
});
132+
});
133+
});

0 commit comments

Comments
 (0)