Skip to content

Commit

Permalink
Merge pull request #159 from chingu-x/bug/no-check-on-voyageTeamMemberId
Browse files Browse the repository at this point in the history
Bug: no check on voyage team member
  • Loading branch information
Ajen07 authored Jun 7, 2024
2 parents f09f28a + 47664d6 commit 9e4ad27
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Another example [here](https://co-pilot.dev/changelog)
- Fix unit tests where mocked req doesn't match new CustomRequest type ([#122](https://github.com/chingu-x/chingu-dashboard-be/pull/122))
- Fix bug with reading roles after reseeding causes the db to not recognize the tokens stored by the user's browser ([#134](https://github.com/chingu-x/chingu-dashboard-be/pull/134))
- Fix form responses giving error and not inserting values when the boolean value is false ([#156](https://github.com/chingu-x/chingu-dashboard-be/pull/156))
- Fix a bug for check on voyageTeamMemberId ([#159](https://github.com/chingu-x/chingu-dashboard-be/pull/159))

### Removed

Expand Down
31 changes: 31 additions & 0 deletions src/pipes/voyage-team-member-validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
BadRequestException,
Injectable,
PipeTransform,
Scope,
Inject,
} from "@nestjs/common";
import { REQUEST } from "@nestjs/core";
import { CustomRequest } from "src/global/types/CustomRequest";

@Injectable({ scope: Scope.REQUEST })
export class VoyageTeamMemberValidationPipe implements PipeTransform {
constructor(@Inject(REQUEST) private request: CustomRequest) {}
transform(value: any): any {
const voyageTeamMemberId: number = value.voyageTeamMemberId;

const voyageTeams = this.request.user.voyageTeams;

const isTeamMember = voyageTeams.some(
(teams) => teams.memberId === voyageTeamMemberId,
);

if (!isTeamMember) {
throw new BadRequestException(
"User is not in the specified team, check voyageTeamId or voyageTeamMemberId is correct.",
);
}

return value;
}
}
3 changes: 2 additions & 1 deletion src/sprints/sprints.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from "../global/responses/errors";
import { FormResponse, ResponseResponse } from "../forms/forms.response";
import { CreateCheckinFormDto } from "./dto/create-checkin-form.dto";
import { VoyageTeamMemberValidationPipe } from "../pipes/voyage-team-member-validation";

@Controller()
@ApiTags("Voyage - Sprints")
Expand Down Expand Up @@ -530,7 +531,7 @@ export class SprintsController {
type: ConflictErrorResponse,
})
addCheckinFormResponse(
@Body(new FormInputValidationPipe())
@Body(new FormInputValidationPipe(), VoyageTeamMemberValidationPipe)
createCheckinFormResponse: CreateCheckinFormDto,
) {
return this.sprintsService.addCheckinFormResponse(
Expand Down
22 changes: 19 additions & 3 deletions test/sprints.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ describe("Sprints Controller (e2e)", () => {
.post(sprintCheckinUrl)
.set("Cookie", accessToken)
.send({
voyageTeamMemberId: 2, // voyageTeamMemberId 1 is already in the seed
voyageTeamMemberId: 4, // voyageTeamMemberId 1 is already in the seed
sprintId: 1,
responses: [
{
Expand Down Expand Up @@ -1045,7 +1045,7 @@ describe("Sprints Controller (e2e)", () => {
.post(sprintCheckinUrl)
.set("Cookie", accessToken)
.send({
voyageTeamMemberId: 1,
voyageTeamMemberId: 4,
sprintId: 1,
responses: [
{
Expand All @@ -1062,7 +1062,7 @@ describe("Sprints Controller (e2e)", () => {
.post(sprintCheckinUrl)
.set("Cookie", accessToken)
.send({
voyageTeamMemberId: 1,
voyageTeamMemberId: 4,
sprintId: 1,
responses: [
{
Expand All @@ -1081,5 +1081,21 @@ describe("Sprints Controller (e2e)", () => {
expect(responseGroupAfter).toEqual(responseGroupBefore);
expect(checkinsAfter).toEqual(checkinsBefore);
});
it("should return 400 if the user doesnot belong to the voyage team", async () => {
await request(app.getHttpServer())
.post(sprintCheckinUrl)
.set("Cookie", accessToken)
.send({
voyageTeamMemberId: 5,
sprintId: 1,
responses: [
{
questionId: questions[0].id,
text: "Text input value",
},
],
})
.expect(400);
});
});
});

0 comments on commit 9e4ad27

Please sign in to comment.