From caf517123bf9ee51f56c0bd65d5019aaaa76050f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EB=8B=A4=ED=98=84=20=28Dahyeon=20Park=29?= Date: Sun, 12 Apr 2026 23:46:00 +0900 Subject: [PATCH] feat(api): add user profile endpoints --- .../src/modules/user/dto/me-response.dto.ts | 22 ++++ .../user/dto/refresh-riot-account.dto.ts | 13 ++ apps/api/src/modules/user/user.controller.ts | 120 ++++++++++++++++++ apps/api/src/modules/user/user.module.ts | 9 ++ 4 files changed, 164 insertions(+) create mode 100644 apps/api/src/modules/user/dto/me-response.dto.ts create mode 100644 apps/api/src/modules/user/dto/refresh-riot-account.dto.ts create mode 100644 apps/api/src/modules/user/user.controller.ts create mode 100644 apps/api/src/modules/user/user.module.ts diff --git a/apps/api/src/modules/user/dto/me-response.dto.ts b/apps/api/src/modules/user/dto/me-response.dto.ts new file mode 100644 index 00000000..0bd09bbf --- /dev/null +++ b/apps/api/src/modules/user/dto/me-response.dto.ts @@ -0,0 +1,22 @@ +import type { + CommunityDocument, + UserDocument, +} from '../../global/database/schema'; + +export interface MeIdentityDto { + id: string; + entraOid: string; + tenantId?: string; + email?: string; + displayName?: string; +} + +export interface MeMembershipDto { + user: UserDocument; + community: CommunityDocument | null; +} + +export interface MeResponseDto { + identity: MeIdentityDto | null; + memberships: MeMembershipDto[]; +} diff --git a/apps/api/src/modules/user/dto/refresh-riot-account.dto.ts b/apps/api/src/modules/user/dto/refresh-riot-account.dto.ts new file mode 100644 index 00000000..02d9fd2d --- /dev/null +++ b/apps/api/src/modules/user/dto/refresh-riot-account.dto.ts @@ -0,0 +1,13 @@ +import { IsBoolean, IsIn, IsOptional } from 'class-validator'; +import { RIOT_PLATFORM_ROUTES } from '../../riot/riot.types'; +import type { RiotPlatformRoute } from '../../riot/riot.types'; + +export class RefreshRiotAccountDto { + @IsOptional() + @IsBoolean() + force?: boolean; + + @IsOptional() + @IsIn(RIOT_PLATFORM_ROUTES) + platform?: RiotPlatformRoute; +} diff --git a/apps/api/src/modules/user/user.controller.ts b/apps/api/src/modules/user/user.controller.ts new file mode 100644 index 00000000..9b614966 --- /dev/null +++ b/apps/api/src/modules/user/user.controller.ts @@ -0,0 +1,120 @@ +import { + Body, + Controller, + ForbiddenException, + Get, + Param, + Post, + Req, + UnauthorizedException, +} from '@nestjs/common'; +import type { Request } from 'express'; +import { AllowPublicAccess } from '../auth/allow-public-access.decorator'; +import '../auth/auth-request'; +import { CurrentUser } from '../auth/current-user.decorator'; +import { ConnectionService } from '../global/database/connection.service'; +import type { + IdentityDocument, + UserDocument, +} from '../global/database/schema'; +import { RiotAccountEnrichmentService } from '../riot/riot-account-enrichment.service'; +import type { MeResponseDto } from './dto/me-response.dto'; +import { RefreshRiotAccountDto } from './dto/refresh-riot-account.dto'; + +@Controller('me') +export class UserController { + constructor( + private readonly enrichment: RiotAccountEnrichmentService, + private readonly connectionService: ConnectionService, + ) {} + + @Get() + @AllowPublicAccess() + public async me(@Req() req: Request): Promise { + if (req.identity) { + return this.resolveMeByIdentity(req.identity); + } + if (req.user) { + return { + identity: null, + memberships: [ + { + user: req.user, + community: await this.findCommunity(req.user), + }, + ], + }; + } + return { identity: null, memberships: [] }; + } + + @Post('riot-accounts/:puuid/refresh') + public async refreshRiotAccount( + @CurrentUser() user: UserDocument | undefined, + @Req() req: Request, + @Param('puuid') puuid: string, + @Body() body: RefreshRiotAccountDto, + ) { + const resolvedUser = + user ?? (req.identity ? await this.findUserByIdentityAndPuuid(req.identity, puuid) : null); + if (!resolvedUser) { + throw new UnauthorizedException('Authentication required.'); + } + if (!resolvedUser.riotAccounts.some((account) => account.puuid === puuid)) { + throw new ForbiddenException('Riot account does not belong to user.'); + } + + return this.enrichment.enrichRiotAccount({ + userId: resolvedUser._id, + puuid, + options: { + force: body.force === true, + platform: body.platform, + }, + }); + } + + private async resolveMeByIdentity( + identity: IdentityDocument, + ): Promise { + const UserModel = await this.connectionService.getModel('user'); + const CommunityModel = await this.connectionService.getModel('community'); + const users = await UserModel.find({ identityId: identity._id }).exec(); + const communities = await CommunityModel.find({ + _id: { $in: users.map((user) => user.communityId) }, + }).exec(); + const communityById = new Map( + communities.map((community) => [community._id.toString(), community]), + ); + + return { + identity: { + id: identity._id.toString(), + entraOid: identity.entraOid, + tenantId: identity.tenantId, + email: identity.email, + displayName: identity.displayName, + }, + memberships: users.map((user) => ({ + user, + community: communityById.get(user.communityId.toString()) ?? null, + })), + }; + } + + private async findCommunity(user: UserDocument) { + const CommunityModel = await this.connectionService.getModel('community'); + return CommunityModel.findById(user.communityId).exec(); + } + + private async findUserByIdentityAndPuuid( + identity: IdentityDocument, + puuid: string, + ): Promise { + const UserModel = await this.connectionService.getModel('user'); + return UserModel.findOne({ + identityId: identity._id, + 'riotAccounts.puuid': puuid, + }).exec(); + } +} diff --git a/apps/api/src/modules/user/user.module.ts b/apps/api/src/modules/user/user.module.ts new file mode 100644 index 00000000..66ca9ce0 --- /dev/null +++ b/apps/api/src/modules/user/user.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { RiotModule } from '../riot/riot.module'; +import { UserController } from './user.controller'; + +@Module({ + imports: [RiotModule], + controllers: [UserController], +}) +export class UserModule {}