Skip to content

Commit

Permalink
feat: add clear completionist badges admin endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
GiyoMoon committed Aug 18, 2024
1 parent 3011db7 commit 8275b97
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,10 @@ export class AdminController {
): Promise<void> {
return this._adminService.broadcastNotification(uuid, notification);
}

@UseGuards(AuthGuard)
@Delete('completionists')
public clearCompletionists(@UserDecorator('id') uuid: string): Promise<void> {
return this._adminService.clearCompletionists(uuid);
}
}
23 changes: 23 additions & 0 deletions src/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,27 @@ export class AdminService {

return;
}

public async clearCompletionists(uuid: string) {
const requestingUser = await this._userRepository.findOne(uuid, {
relations: ['roles'],
});

if (!this._hasPermission(requestingUser.roles, PERMISSION.ADMIN)) {
throw new HttpException('Permission denied.', HttpStatus.FORBIDDEN);
}

const users = await this._userRepository.find({
relations: ['badges'],
});

for (const user of users) {
if (user.badges.some((badge) => badge.id.startsWith('completionist'))) {
user.badges = user.badges.filter(
(badge) => !badge.id.startsWith('completionist'),
);
await this._userRepository.save(user);
}
}
}
}

0 comments on commit 8275b97

Please sign in to comment.