Skip to content

Commit

Permalink
feat(back): allow reviewers to request unlimited maps in submission
Browse files Browse the repository at this point in the history
Closes #1071
  • Loading branch information
tsa96 committed Nov 17, 2024
1 parent d0ecff5 commit 800e0cc
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
47 changes: 47 additions & 0 deletions apps/backend-e2e/src/maps.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4161,6 +4161,53 @@ describe('Maps', () => {
);
});

it('should limit -1 take to 100 for non-reviewers', async () => {
// Magic number sorry, if additional maps are added above in setup
// this'll break.
const newMaps = await db
.createMaps(98, {
status: MapStatus.PUBLIC_TESTING
})
.then((maps) => maps.map((m) => m.id));

await req.get({
url: 'maps/submissions',
status: 200,
query: { take: -1 },
validatePaged: { type: MapDto, returnCount: 100, totalCount: 101 },
token: u1Token
});

await prisma.mMap.deleteMany({ where: { id: { in: newMaps } } });
});

it('should not limit -1 take reviewers', async () => {
await prisma.user.update({
where: { id: u1.id },
data: { roles: Role.REVIEWER }
});

const newMaps = await db
.createMaps(97, {
status: MapStatus.PUBLIC_TESTING
})
.then((maps) => maps.map((m) => m.id));

await req.get({
url: 'maps/submissions',
status: 200,
query: { take: -1 },
validatePaged: { type: MapDto, returnCount: 101, totalCount: 101 },
token: u1Token
});

await prisma.mMap.deleteMany({ where: { id: { in: newMaps } } });
await prisma.user.update({
where: { id: u1.id },
data: { roles: 0 }
});
});

it('should 401 when no access token is provided', () =>
req.unauthorizedTest('maps/submissions', 'get'));
});
Expand Down
9 changes: 7 additions & 2 deletions apps/backend/src/app/dto/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,13 @@ export function SkipQueryProperty(def: number): PropertyDecorator {
* Decorator collection for take queries
* @param def - The default `take` value
* @param max - The maximum allowed `take` value
* @param min - The minimum allowed `take` value
*/
export function TakeQueryProperty(def: number, max = 100): PropertyDecorator {
export function TakeQueryProperty(
def: number,
max = 100,
min = 0
): PropertyDecorator {
return applyDecorators(
ApiPropertyOptional({
name: 'take',
Expand All @@ -241,7 +246,7 @@ export function TakeQueryProperty(def: number, max = 100): PropertyDecorator {
description: 'Take this many records'
}),
TypeDecorator(() => Number),
Min(0),
Min(min),
IsInt(),
Max(max),
IsOptional()
Expand Down
3 changes: 3 additions & 0 deletions apps/backend/src/app/dto/queries/map-queries.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ export class MapsGetAllSubmissionQueryDto
extends MapsGetAllBaseQueryDto
implements MapsGetAllSubmissionQuery
{
@TakeQueryProperty(10, 100, -1)
override readonly take = -1;

@ExpandQueryProperty([
'leaderboards',
'info',
Expand Down
10 changes: 9 additions & 1 deletion apps/backend/src/app/modules/maps/maps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export class MapsService {
): Promise<PagedResponseDto<MapDto>> {
// Where
const where: Prisma.MMapWhereInput = {};
let take: number | undefined = query.take;
if (query.search) where.name = { contains: query.search };
if (query.searchStartsWith)
where.name = { startsWith: query.searchStartsWith };
Expand Down Expand Up @@ -187,6 +188,13 @@ export class MapsService {

if (roles == null) throw new BadRequestException();

// Allow unlimited take for reviewers and above
if (take === -1) {
take = Bitflags.has(roles, CombinedRoles.REVIEWER_AND_ABOVE)
? undefined
: 100;
}

// Logic here is a nightmare, for a breakdown of permissions see
// MapsService.getMapAndCheckReadAccess.
const filter = query.filter;
Expand Down Expand Up @@ -363,7 +371,7 @@ export class MapsService {
include,
orderBy: { createdAt: 'desc' },
skip: query.skip,
take: query.take
take
});

if (incPB || incWR) {
Expand Down

0 comments on commit 800e0cc

Please sign in to comment.