This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
103 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Role, UserModel } from "@/models/User"; | ||
import * as db from "@test-util/MongoMemory"; | ||
import { openSession, deleteRequest } from "@test-util/SessionSetUp"; | ||
import { StatusCodes } from "http-status-codes"; | ||
import { SessionDto, UserDto } from "@/models/dto"; | ||
|
||
beforeAll(db.connect); | ||
afterEach(db.clear); | ||
afterAll(db.close); | ||
|
||
const endpoint = "/user/bonds/"; | ||
|
||
describe("Calling DELETE " + endpoint, () => { | ||
|
||
let session: SessionDto; | ||
let user: UserDto; | ||
|
||
beforeEach((done) => { | ||
openSession((response) => { | ||
session = response.session; | ||
user = response.user; | ||
if (session && user) { | ||
done(); | ||
} | ||
}); | ||
}); | ||
|
||
it("should remove the bond if it's correct", async () => { | ||
const keeper = await UserModel.findById(user._id); | ||
keeper.role = Role.Keeper; | ||
await keeper.save(); | ||
const patient = await UserModel.create({ | ||
googleId: "patient", | ||
role: Role.Patient | ||
}); | ||
await patient.bondWith(keeper); | ||
|
||
await deleteRequest(endpoint + patient._id, session.auth) | ||
.send() | ||
.expect(StatusCodes.NO_CONTENT); | ||
|
||
const storedPatient = await UserModel.findById(patient._id); | ||
expect(storedPatient.bonds.length).toBe(0); | ||
const storedKeeper = await UserModel.findById(user._id); | ||
expect(storedKeeper.cared).toBeUndefined(); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import { StatusCodes } from "http-status-codes"; | ||
import * as UserService from "@/services/UserService"; | ||
import { UserDto } from "@/models/dto"; | ||
import { IdParam } from "@/shared/values"; | ||
import * as NotificationService from "@/services/NotificationService"; | ||
import { Action } from "@/models/Notification"; | ||
import { Role } from "@/models/User"; | ||
import { GLOBAL } from "@/sockets/global"; | ||
import { io } from "@server"; | ||
|
||
/** | ||
* Removes the bond of the user with the specified user | ||
* @param req request with the bond id | ||
* @param res carried response | ||
* @param next invokation of the next middleware to use in case of error | ||
* @returns the response with the success or error confirmation message | ||
*/ | ||
export const removeBond = async ( | ||
req: Request<IdParam>, | ||
res: Response, | ||
next: NextFunction): Promise<void|Response<UserDto>> => | ||
{ | ||
let patientId: string; | ||
return UserService.getRole(req.sessionId) | ||
.then((role) => { | ||
patientId = role === Role.Patient ? req.sessionId : req.params.id; | ||
return UserService.unbond(req.sessionId, req.params.id); | ||
}) | ||
.then(() => { | ||
res.status(StatusCodes.NO_CONTENT).send(); | ||
return NotificationService.create(Action.BOND_DELETED, patientId); | ||
}) | ||
.then((notification) => { | ||
io.to(`${GLOBAL}:${patientId}`).emit(notification.event, notification.dto()) | ||
}) | ||
.catch(next); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters