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 19, 2024
1 parent ed5b42b commit 22cc545
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 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 @@ -4155,6 +4155,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
7 changes: 5 additions & 2 deletions apps/backend/src/app/dto/queries/map-queries.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class MapsGetAllBaseQueryDto extends QueryDto {
@SkipQueryProperty(0)
skip = 0;

@TakeQueryProperty(100)
take = 100;
@TakeQueryProperty(10, 100)
take = 10;

@StringQueryProperty({
description: 'Filter by partial map name match (contains)',
Expand Down Expand Up @@ -133,6 +133,9 @@ export class MapsGetAllSubmissionQueryDto
extends MapsGetAllBaseQueryDto
implements MapsGetAllSubmissionQuery
{
@TakeQueryProperty(10, 100, -1)
override readonly take = 10;

@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 22cc545

Please sign in to comment.