Skip to content

Commit 3dcfa64

Browse files
1998-Added info to service and added json datat return
1 parent c68c116 commit 3dcfa64

4 files changed

Lines changed: 41 additions & 5 deletions

File tree

src/backend/src/controllers/users.controllers.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,14 @@ export default class UsersController {
151151
const { personalGmail, personalZoomLink, availability } = req.body;
152152
const user = await getCurrentUser(res);
153153

154-
await UsersService.setUserScheduleSettings(user, personalGmail, personalZoomLink, availability);
155-
156-
res.status(200).json({ message: `Successfully updated schedule settings for user ${user.userId}.` });
154+
const editedScheduleSettings = await UsersService.setUserScheduleSettings(
155+
user,
156+
personalGmail,
157+
personalZoomLink,
158+
availability
159+
);
160+
161+
return res.status(200).json(editedScheduleSettings);
157162
} catch (error: unknown) {
158163
next(error);
159164
}

src/backend/src/routes/users.routes.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@ userRouter.post(
3232
nonEmptyString(body('phoneNumber')),
3333
UsersController.setUserSecureSettings
3434
);
35+
3536
userRouter.post(
3637
'/schedule-settings/set',
37-
nonEmptyString(body('personalGmail')),
38-
nonEmptyString(body('personalZoomLink')),
38+
nonEmptyString(body('personalGmail')).isEmail(),
39+
nonEmptyString(body('personalZoomLink')).isURL(),
3940
body('availability').isArray(),
4041
intMinZero(body('availibility.*')),
42+
validateInputs,
4143
UsersController.setUserScheduleSettings
4244
);
45+
4346
userRouter.get('/:userId/secure-settings', UsersController.getUserSecureSettings);
4447

4548
export default userRouter;

src/backend/src/services/users.services.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,28 @@ export default class UsersService {
340340
return userScheduleSettingsTransformer(scheduleSettings);
341341
}
342342

343+
/**
344+
*
345+
* @param user the user to set the schedule settings for
346+
* @param personalGmail the user's personal gmail
347+
* @param personalZoomLink the user's personal zoom link
348+
* @param availability the user's availibility
349+
* @returns the id of the user's schedule settings
350+
*/
343351
static async setUserScheduleSettings(
344352
user: User,
345353
personalGmail: string,
346354
personalZoomLink: string,
347355
availability: number[]
348356
): Promise<string> {
357+
const existingUser = await prisma.schedule_Settings.findFirst({
358+
where: { personalGmail, userId: { not: user.userId } } // excludes the current user from check
359+
});
360+
361+
if (existingUser) {
362+
throw new HttpException(400, 'Email already in use');
363+
}
364+
349365
const newUserScheduleSettings = await prisma.schedule_Settings.upsert({
350366
where: { userId: user.userId },
351367
update: {

src/backend/tests/users.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,17 @@ describe('Users', () => {
175175

176176
expect(res).toBe(batmanUserScheduleSettings.drScheduleSettingsId);
177177
});
178+
179+
test('setting same email does not work', async () => {
180+
vi.spyOn(prisma.schedule_Settings, 'findFirst').mockResolvedValue(batmanScheduleSettings);
181+
await expect(() =>
182+
UsersService.setUserScheduleSettings(
183+
batmanWithScheduleSettings,
184+
batmanScheduleSettings.personalGmail,
185+
batmanScheduleSettings.personalZoomLink,
186+
batmanScheduleSettings.availability
187+
)
188+
).rejects.toThrow(new HttpException(400, 'Email already in use'));
189+
});
178190
});
179191
});

0 commit comments

Comments
 (0)