From 7c92c0d6144035acca962400bf817c1c7b24b84a Mon Sep 17 00:00:00 2001 From: Ramin Date: Tue, 3 Jun 2025 22:07:20 +0330 Subject: [PATCH 1/7] add isAdmin to user --- prisma/schema.prisma | 1 + 1 file changed, 1 insertion(+) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 6e8df3f..f3b744c 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -12,6 +12,7 @@ model User { worldID String @unique name String? profilePicture String? + isAdmin Boolean @default(false) pollsCreatedCount Int @default(0) pollsParticipatedCount Int @default(0) createdAt DateTime @default(now()) From 4f3f1d7d916a0aad406d7b337c04c834c31e5cbb Mon Sep 17 00:00:00 2001 From: Ramin Date: Tue, 3 Jun 2025 22:08:40 +0330 Subject: [PATCH 2/7] add isAdmin to deletePoll --- src/poll/poll.service.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/poll/poll.service.ts b/src/poll/poll.service.ts index 0322c92..f099189 100644 --- a/src/poll/poll.service.ts +++ b/src/poll/poll.service.ts @@ -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() @@ -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() } From 1377e93d7ae544a88c0f59609d27c13b51433174 Mon Sep 17 00:00:00 2001 From: Ramin Date: Tue, 3 Jun 2025 22:09:06 +0330 Subject: [PATCH 3/7] add listAdmins --- src/user/user.controller.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index bb5d869..6539e7c 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -71,4 +71,10 @@ export class UserController { async getUserCount(@Query() query: GetCountDto): Promise { return await this.userService.getUserCount(query) } + + @Get('listAdmins') + @Public() + async listAdmins(): Promise { + return await this.userService.listAdmins() + } } From 92547eb6f46020d9cbb8cf0697e64747331db511 Mon Sep 17 00:00:00 2001 From: Ramin Date: Tue, 3 Jun 2025 22:09:20 +0330 Subject: [PATCH 4/7] add isAdmin to UserDataResponseDto --- src/user/user.dto.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/user/user.dto.ts b/src/user/user.dto.ts index 7f95fe2..4af634a 100644 --- a/src/user/user.dto.ts +++ b/src/user/user.dto.ts @@ -40,6 +40,10 @@ export class UserDataResponseDto { @IsString() @IsOptional() name?: string | null + + @IsBoolean() + @IsNotEmpty() + isAdmin: boolean } export class GetUserActivitiesDto { From 8e03147daef10b2fbced93d73eefbe3d47748339 Mon Sep 17 00:00:00 2001 From: Ramin Date: Tue, 3 Jun 2025 22:09:43 +0330 Subject: [PATCH 5/7] add isAdmin to getUserData --- src/user/user.service.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/user/user.service.ts b/src/user/user.service.ts index fc4d3d0..c405ad6 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -97,6 +97,7 @@ export class UserService { worldID: true, name: true, profilePicture: true, + isAdmin: true, pollsCreatedCount: true, pollsParticipatedCount: true, }, @@ -110,6 +111,7 @@ export class UserService { worldID: user.worldID, worldProfilePic: user.profilePicture, name: user.name, + isAdmin: user.isAdmin, } } @@ -456,4 +458,12 @@ export class UserService { return await this.databaseService.user.count({ where }) } + + async listAdmins(): Promise { + const admins = await this.databaseService.user.findMany({ + where: { isAdmin: true }, + select: { worldID: true }, + }) + return admins.map(admin => admin.worldID) + } } From 38090494fcd42f3582bd49608255a8b399d26f5b Mon Sep 17 00:00:00 2001 From: Ramin Date: Wed, 4 Jun 2025 15:22:21 +0330 Subject: [PATCH 6/7] remove Public from listAdmins --- src/user/user.controller.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index 6539e7c..74fbb92 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -73,7 +73,6 @@ export class UserController { } @Get('listAdmins') - @Public() async listAdmins(): Promise { return await this.userService.listAdmins() } From b5a6f9e8128e47a293ad354f1e059e1b907563ce Mon Sep 17 00:00:00 2001 From: Ramin Date: Wed, 4 Jun 2025 17:24:45 +0330 Subject: [PATCH 7/7] add isAdmin migration --- .../20250604135348_add_is_admin_to_user/migration.sql | 2 ++ prisma/schema.prisma | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 prisma/migrations/20250604135348_add_is_admin_to_user/migration.sql diff --git a/prisma/migrations/20250604135348_add_is_admin_to_user/migration.sql b/prisma/migrations/20250604135348_add_is_admin_to_user/migration.sql new file mode 100644 index 0000000..7db3f39 --- /dev/null +++ b/prisma/migrations/20250604135348_add_is_admin_to_user/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "User" ADD COLUMN "isAdmin" BOOLEAN NOT NULL DEFAULT false; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index f3b744c..38206de 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -12,10 +12,10 @@ model User { worldID String @unique name String? profilePicture String? - isAdmin Boolean @default(false) pollsCreatedCount Int @default(0) pollsParticipatedCount Int @default(0) createdAt DateTime @default(now()) + isAdmin Boolean @default(false) createdPolls Poll[] @relation("PollAuthor") actions UserAction[] votes Vote[]