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
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 19 additions & 8 deletions src/controllers/WeeklySummaryEmailAssignmentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ const WeeklySummaryEmailAssignmentController = function (
return;
}

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

const newAssignment = new WeeklySummaryEmailAssignment({
email,
email: normalizedEmail,
assignedTo: user._id,
});

await newAssignment.save();
const assignment = await WeeklySummaryEmailAssignment.find({ email })
const assignment = await WeeklySummaryEmailAssignment.find({ email: normalizedEmail })
.populate('assignedTo')
.exec();

Expand Down Expand Up @@ -79,23 +80,33 @@ const WeeklySummaryEmailAssignmentController = function (
});
}

const normalizedEmail = email.toLowerCase().trim();
const user = await userProfile.findOne().where('email').equals(normalizedEmail);
const updateFields = {
email: normalizedEmail,
};
if (user?._id) {
updateFields.assignedTo = user._id;
}

const updateAssignment = await WeeklySummaryEmailAssignment.findOneAndUpdate(
{ _id: id },
{
email,
},
updateFields,
{
new: true,
},
);
).populate('assignedTo');

if (!updateAssignment) {
res.status(404).send('Assignment not found');
return;
}

res.status(200).send(updateAssignment);
res.status(200).send({ assignment: updateAssignment });
} catch (error) {
if (error?.code === 11000) {
return res.status(409).send('Email already assigned');
}
res.status(500).send(error);
}
};
Expand Down
105 changes: 105 additions & 0 deletions src/controllers/WeeklySummaryEmailAssignmentController.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const WeeklySummaryEmailAssignmentController = require('./WeeklySummaryEmailAssignmentController');

const makeMockRes = () => ({
status: jest.fn().mockReturnThis(),
send: jest.fn(),
json: jest.fn(),
});

describe('WeeklySummaryEmailAssignmentController', () => {
const makeUserProfileQueryMock = (resolvedValue) => ({
findOne: jest.fn().mockReturnValue({
where: jest.fn().mockReturnValue({
equals: jest.fn().mockResolvedValue(resolvedValue),
}),
}),
});

beforeEach(() => {
jest.clearAllMocks();
});

describe('updateWeeklySummaryEmailAssignment', () => {
it('updates only email when no matching user profile is found for the edited email', async () => {
const updatedAssignment = {
_id: 'assignment-id',
email: 'new@example.com',
assignedTo: { _id: 'existing-user-id', firstName: 'Existing', lastName: 'User' },
};
const populate = jest.fn().mockResolvedValue(updatedAssignment);
const WeeklySummaryEmailAssignment = {
findOneAndUpdate: jest.fn().mockReturnValue({ populate }),
};
const userProfile = makeUserProfileQueryMock(null);

const controller = WeeklySummaryEmailAssignmentController(
WeeklySummaryEmailAssignment,
userProfile,
);
const req = { params: { id: 'assignment-id' }, body: { email: 'new@example.com' } };
const res = makeMockRes();

await controller.updateWeeklySummaryEmailAssignment(req, res);

expect(userProfile.findOne).toHaveBeenCalledWith();
expect(WeeklySummaryEmailAssignment.findOneAndUpdate).toHaveBeenCalledWith(
{ _id: 'assignment-id' },
{ email: 'new@example.com' },
{ new: true },
);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.send).toHaveBeenCalledWith({ assignment: updatedAssignment });
});

it('updates both email and assignedTo and returns populated assignment payload', async () => {
const updatedAssignment = {
_id: 'assignment-id',
email: 'updated@example.com',
assignedTo: { _id: 'user-id', firstName: 'Updated', lastName: 'User' },
};
const populate = jest.fn().mockResolvedValue(updatedAssignment);
const WeeklySummaryEmailAssignment = {
findOneAndUpdate: jest.fn().mockReturnValue({ populate }),
};
const userProfile = makeUserProfileQueryMock({ _id: 'user-id' });

const controller = WeeklySummaryEmailAssignmentController(
WeeklySummaryEmailAssignment,
userProfile,
);
const req = { params: { id: 'assignment-id' }, body: { email: 'updated@example.com' } };
const res = makeMockRes();

await controller.updateWeeklySummaryEmailAssignment(req, res);

expect(WeeklySummaryEmailAssignment.findOneAndUpdate).toHaveBeenCalledWith(
{ _id: 'assignment-id' },
{ email: 'updated@example.com', assignedTo: 'user-id' },
{ new: true },
);
expect(populate).toHaveBeenCalledWith('assignedTo');
expect(res.status).toHaveBeenCalledWith(200);
expect(res.send).toHaveBeenCalledWith({ assignment: updatedAssignment });
});

it('returns 409 when the edited email already exists', async () => {
const populate = jest.fn().mockRejectedValue({ code: 11000 });
const WeeklySummaryEmailAssignment = {
findOneAndUpdate: jest.fn().mockReturnValue({ populate }),
};
const userProfile = makeUserProfileQueryMock({ _id: 'user-id' });

const controller = WeeklySummaryEmailAssignmentController(
WeeklySummaryEmailAssignment,
userProfile,
);
const req = { params: { id: 'assignment-id' }, body: { email: 'existing@example.com' } };
const res = makeMockRes();

await controller.updateWeeklySummaryEmailAssignment(req, res);

expect(res.status).toHaveBeenCalledWith(409);
expect(res.send).toHaveBeenCalledWith('Email already assigned');
});
});
});
Loading