first commit
This commit is contained in:
130
apps/api-gateway/src/controllers/family.controller.ts
Normal file
130
apps/api-gateway/src/controllers/family.controller.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { Body, Controller, Delete, ForbiddenException, Get, Headers, Param, Patch, Post } from '@nestjs/common';
|
||||
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
|
||||
import { ApiBearerAuth, ApiBody, ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
|
||||
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
import { CoreGrpcService } from '../core-grpc.service';
|
||||
|
||||
import { getAuthorizedUserId } from '../document-access';
|
||||
|
||||
import { AddFamilyMemberDto, CreateFamilyGroupDto, RespondFamilyInviteDto, SendFamilyInviteDto, UpdateFamilyGroupDto } from '../dto/identity.dto';
|
||||
|
||||
|
||||
|
||||
@ApiTags('Семья')
|
||||
|
||||
@ApiBearerAuth()
|
||||
|
||||
@Controller('family')
|
||||
|
||||
export class FamilyController {
|
||||
|
||||
constructor(
|
||||
|
||||
private readonly core: CoreGrpcService,
|
||||
|
||||
private readonly jwt: JwtService
|
||||
|
||||
) {}
|
||||
|
||||
|
||||
|
||||
private async auth(authorization?: string) {
|
||||
|
||||
return getAuthorizedUserId(this.jwt, this.core, authorization);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Post('groups')
|
||||
|
||||
@ApiOperation({ summary: 'Создать семейную группу' })
|
||||
|
||||
@ApiBody({ type: CreateFamilyGroupDto })
|
||||
|
||||
async createGroup(@Headers('authorization') authorization: string | undefined, @Body() dto: CreateFamilyGroupDto) {
|
||||
|
||||
const userId = await this.auth(authorization);
|
||||
|
||||
if (dto.ownerId !== userId) {
|
||||
|
||||
throw new ForbiddenException('Можно создавать семью только от своего имени');
|
||||
|
||||
}
|
||||
|
||||
return firstValueFrom(this.core.family.CreateFamilyGroup(dto));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Get('users/:userId/groups')
|
||||
|
||||
@ApiOperation({ summary: 'Список семей пользователя' })
|
||||
|
||||
async listGroups(@Headers('authorization') authorization: string | undefined, @Param('userId') userId: string) {
|
||||
|
||||
const requesterId = await this.auth(authorization);
|
||||
|
||||
if (requesterId !== userId) {
|
||||
|
||||
throw new ForbiddenException('Можно просматривать только свои семьи');
|
||||
|
||||
}
|
||||
|
||||
return firstValueFrom(
|
||||
|
||||
this.core.family.ListFamilyGroups({ userId }).pipe(
|
||||
|
||||
map((response) => {
|
||||
|
||||
const payload = response as { groups?: unknown[] };
|
||||
|
||||
return { groups: payload.groups ?? [] };
|
||||
|
||||
})
|
||||
|
||||
)
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Get('groups/:groupId')
|
||||
|
||||
@ApiOperation({ summary: 'Получить семейную группу' })
|
||||
|
||||
async getGroup(@Headers('authorization') authorization: string | undefined, @Param('groupId') groupId: string) {
|
||||
|
||||
const requesterId = await this.auth(authorization);
|
||||
|
||||
return firstValueFrom(this.core.family.GetFamilyGroup({ requesterId, groupId }));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Patch('groups/:groupId')
|
||||
|
||||
@ApiOperation({ summary: 'Обновить семейную группу' })
|
||||
|
||||
async updateGroup(
|
||||
|
||||
@Headers('authorization') authorization: string | undefined,
|
||||
|
||||
@Param('groupId') groupId: string,
|
||||
|
||||
@Body() dto: UpdateFamilyGroupDto
|
||||
|
||||
) {
|
||||
|
||||
const requesterId = await this.auth(authorization);
|
||||
|
||||
Reference in New Issue
Block a user