Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "isAdmin" BOOLEAN NOT NULL DEFAULT false;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ model User {
pollsCreatedCount Int @default(0)
pollsParticipatedCount Int @default(0)
createdAt DateTime @default(now())
isAdmin Boolean @default(false)
createdPolls Poll[] @relation("PollAuthor")
actions UserAction[]
votes Vote[]
Expand Down
5 changes: 3 additions & 2 deletions src/poll/poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ export class PollService {
async deletePoll(pollId: number, worldID: string) {
const user = await this.databaseService.user.findUnique({
where: { worldID },
select: { id: true },
select: { id: true, isAdmin: true },
})
if (!user) {
throw new UserNotFoundException()
Expand All @@ -561,7 +561,8 @@ export class PollService {
if (!poll) {
throw new PollNotFoundException()
}
if (poll.authorUserId !== user.id) {
// Allow deletion if user is admin or if user is the poll author
if (!user.isAdmin && poll.authorUserId !== user.id) {
throw new UnauthorizedActionException()
}

Expand Down
5 changes: 5 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,9 @@ export class UserController {
async getUserCount(@Query() query: GetCountDto): Promise<number> {
return await this.userService.getUserCount(query)
}

@Get('listAdmins')
async listAdmins(): Promise<string[]> {
return await this.userService.listAdmins()
}
}
4 changes: 4 additions & 0 deletions src/user/user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export class UserDataResponseDto {
@IsString()
@IsOptional()
name?: string | null

@IsBoolean()
@IsNotEmpty()
isAdmin: boolean
}

export class GetUserActivitiesDto {
Expand Down
10 changes: 10 additions & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class UserService {
worldID: true,
name: true,
profilePicture: true,
isAdmin: true,
pollsCreatedCount: true,
pollsParticipatedCount: true,
},
Expand All @@ -110,6 +111,7 @@ export class UserService {
worldID: user.worldID,
worldProfilePic: user.profilePicture,
name: user.name,
isAdmin: user.isAdmin,
}
}

Expand Down Expand Up @@ -456,4 +458,12 @@ export class UserService {

return await this.databaseService.user.count({ where })
}

async listAdmins(): Promise<string[]> {
const admins = await this.databaseService.user.findMany({
where: { isAdmin: true },
select: { worldID: true },
})
return admins.map(admin => admin.worldID)
}
}