Skip to content

Commit

Permalink
feat(back): GET maps/:mapID/reviews/:reviewID implemented and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
notjoao1 committed Sep 9, 2023
1 parent 285a822 commit a7ff93f
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 2 deletions.
106 changes: 106 additions & 0 deletions apps/backend-e2e/src/maps.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3280,4 +3280,110 @@ describe('Maps', () => {
req.unauthorizedTest('maps/1/reviews', 'get'));
});
});

describe('maps/{mapID}/reviews/{reviewID}', () => {
describe('GET', () => {
let u1, u2, u2Token, map, review;
beforeAll(async () => {
[u1, [u2, u2Token]] = await Promise.all([
db.createUser({ data: { roles: Role.MAPPER } }),
db.createAndLoginUser({ data: { roles: Role.REVIEWER } })
]);

map = await db.createMap({
status: MapStatusNew.PUBLIC_TESTING,
submitter: { connect: { id: u1.id } }
});

review = await prisma.mapReview.create({
data: {
mainText:
'Sync speedshot into sync speedshot, what substance did you take mapper?!?!',
suggestions: [
{
track: 1,
gamemode: Gamemode.RJ,
tier: 2,
comment: 'True',
gameplayRating: 1
}
],
mmap: { connect: { id: map.id } },
reviewer: { connect: { id: u2.id } },
resolved: false
}
});
});

afterAll(() => db.cleanup('mMap', 'user'));

it('should return the requested review', async () => {
const response = await req.get({
url: `maps/${map.id}/reviews/${review.id}`,
status: 200,
validate: MapReviewDto,
token: u2Token
});
expect(response.body).toMatchObject({
mainText: review.mainText,
mapID: map.id,
id: review.id
});
});

it('should return the requested review including author information', async () => {
await req.expandTest({
url: `maps/${map.id}/reviews/${review.id}`,
token: u2Token,
expand: 'reviewer',
validate: MapReviewDto
});
});

it('should return the requested review including map information', async () => {
await req.expandTest({
url: `maps/${map.id}/reviews/${review.id}`,
token: u2Token,
expand: 'map',
validate: MapReviewDto
});
});

// Test that permissions checks are getting called
it('should 403 if the user does not have permission to access to the map', async () => {
await prisma.mMap.update({
where: { id: map.id },
data: { status: MapStatusNew.PRIVATE_TESTING }
});

await req.get({
url: `maps/${map.id}/reviews/${review.id}`,
status: 403,
token: u2Token
});

await prisma.mMap.update({
where: { id: map.id },
data: { status: MapStatusNew.APPROVED }
});
});

it('should return 404 for a nonexistent map', () =>
req.get({
url: `maps/${NULL_ID}/reviews/${review.id}`,
status: 404,
token: u2Token
}));

it('should return 404 for a nonexistent review', () =>
req.get({
url: `maps/${map.id}/reviews/${NULL_ID}`,
status: 404,
token: u2Token
}));

it('should 401 when no access token is provided', () =>
req.unauthorizedTest('maps/1/reviews/1', 'get'));
});
});
});
24 changes: 23 additions & 1 deletion apps/backend/src/app/modules/maps/map-review.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
import {
DtoFactory,
MapReviewDto,
MapReviewGetIdDto,
MapReviewsGetQueryDto,
PagedResponseDto
} from '@momentum/backend/dto';
Expand Down Expand Up @@ -71,4 +73,24 @@ export class MapReviewService {

return new PagedResponseDto(MapReviewDto, [paginatedResponse, totalCount]);
}

async getReview(
mapID: number,
reviewID: number,
userID: number,
query: MapReviewGetIdDto
): Promise<MapReviewDto> {
await this.mapsService.getMapAndCheckReadAccess(mapID, userID);

const review = await this.db.mapReview.findFirst({
where: { id: reviewID, mapID },
include: expandToIncludes(query.expand, {
mappings: [{ expand: 'map', model: 'mmap' }]
})
});

if (!review) throw new NotFoundException('Review not found');

return DtoFactory(MapReviewDto, review);
}
}
28 changes: 28 additions & 0 deletions apps/backend/src/app/modules/maps/maps.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
MapRankGetNumberQueryDto,
MapRanksGetQueryDto,
MapReviewDto,
MapReviewGetIdDto,
MapReviewsGetQueryDto,
MapsCtlGetAllQueryDto,
MapsGetQueryDto,
Expand Down Expand Up @@ -724,6 +725,33 @@ export class MapsController {
return this.mapReviewService.getAllReviews(mapID, userID, query);
}

@Get('/:mapID/reviews/:reviewID')
@ApiOperation({ summary: 'Returns the requested review' })
@ApiParam({
name: 'mapID',
type: Number,
description: 'Target Map ID',
required: true
})
@ApiParam({
name: 'reviewID',
type: Number,
description: 'Target Review ID',
required: true
})
@ApiOkResponse({ description: 'The requested review of the map' })
@ApiNotFoundResponse({
description: 'Either the map or review was not found'
})
getReview(
@Param('mapID', ParseIntSafePipe) mapID: number,
@Param('reviewID', ParseIntSafePipe) reviewID: number,
@LoggedInUser('id') userID: number,
@Query() query?: MapReviewGetIdDto
): Promise<MapReviewDto> {
return this.mapReviewService.getReview(mapID, reviewID, userID, query);
}

//endregion

//#region Private
Expand Down
8 changes: 7 additions & 1 deletion libs/backend/dto/src/queries/map-queries.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
MapReviewsGetExpand,
MapCreditsGetExpand,
MapsGetExpand,
AdminMapsGetAllExpand
AdminMapsGetAllExpand,
MapReviewGetIdQuery
} from '@momentum/constants';
import {
AdminMapsGetAllQuery,
Expand Down Expand Up @@ -181,3 +182,8 @@ export class MapReviewsGetQueryDto
@ExpandQueryProperty(['map', 'reviewer'])
readonly expand?: MapReviewsGetExpand;
}

export class MapReviewGetIdDto implements MapReviewGetIdQuery {
@ExpandQueryProperty(['map', 'reviewer'])
readonly expand?: MapReviewsGetExpand;
}
4 changes: 4 additions & 0 deletions libs/constants/src/types/queries/map-queries.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ export type MapReviewsGetQuery = {
official?: boolean;
expand?: string[];
};

export type MapReviewGetIdQuery = {
expand?: string[];
};

0 comments on commit a7ff93f

Please sign in to comment.