feat(api): add user profile endpoints
This commit is contained in:
1 parent
d4b790fd86
commit
caf517123b
4 files changed
+164
No files matched your search
@@ -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[];
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<MeResponseDto> {
|
||||
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<MeResponseDto> {
|
||||
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<UserDocument | null> {
|
||||
const UserModel = await this.connectionService.getModel('user');
|
||||
return UserModel.findOne({
|
||||
identityId: identity._id,
|
||||
'riotAccounts.puuid': puuid,
|
||||
}).exec();
|
||||
}
|
||||
}
|
||||
@@ -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 {}
|
||||
Reference in new issue
Block a user