import { Body, Controller, Get, Param, Patch, Post, StreamableFile, UseGuards, } from '@nestjs/common'; import type { UserRole } from '@mento/shared'; import { JwtAuthGuard } from '../auth/jwt-auth.guard'; import { CurrentUser } from '../auth/current-user.decorator'; import { Roles } from '../auth/roles.decorator'; import { RolesGuard } from '../auth/roles.guard'; import { InviteUserDto, UpdateOrgUserDto, UpdateUserCommissionDto, UpdateLogoutTimeDto, UpdateUserRoleDto, UpdateUserStatusDto, UpdateUserVisibilityDto, } from './dto'; import { UsersService } from './users.service'; @Controller('users') @UseGuards(JwtAuthGuard, RolesGuard) export class UsersController { constructor(private readonly usersService: UsersService) {} @Get() list(@CurrentUser() user: { organizationId: string }) { return this.usersService.list(user.organizationId); } @Get(':id/avatar') async avatar( @CurrentUser() user: { organizationId: string }, @Param('id') id: string, ) { const file = await this.usersService.openAvatar(user.organizationId, id); return new StreamableFile(file.stream, { type: file.mimeType }); } @Post() @Roles('owner', 'admin') invite( @CurrentUser() user: { organizationId: string; role: UserRole }, @Body() dto: InviteUserDto, ) { return this.usersService.invite(user.organizationId, user.role, dto); } @Patch(':id') @Roles('owner', 'admin') updateProfile( @CurrentUser() user: { organizationId: string; role: UserRole }, @Param('id') id: string, @Body() dto: UpdateOrgUserDto, ) { return this.usersService.updateProfile( user.organizationId, user.role, id, dto, ); } @Patch(':id/logout-time') @Roles('owner') updateLogoutTime( @CurrentUser() user: { organizationId: string; role: UserRole }, @Param('id') id: string, @Body() dto: UpdateLogoutTimeDto, ) { return this.usersService.updateLogoutTime( user.organizationId, user.role, id, dto.logoutTime ?? null, dto.blockLoginAfterLogout, ); } @Patch(':id/commission') @Roles('owner', 'admin') updateCommission( @CurrentUser() user: { organizationId: string; role: UserRole }, @Param('id') id: string, @Body() dto: UpdateUserCommissionDto, ) { return this.usersService.updateCommission( user.organizationId, user.role, id, dto.commissionPercent, ); } @Patch(':id/status') @Roles('owner', 'admin') updateStatus( @CurrentUser() user: { organizationId: string; userId: string; role: UserRole }, @Param('id') id: string, @Body() dto: UpdateUserStatusDto, ) { return this.usersService.updateStatus( user.organizationId, { userId: user.userId, role: user.role }, id, dto.isActive, ); } @Patch(':id/prior-archive') @Roles('owner') updatePriorArchive( @CurrentUser() user: { organizationId: string; role: UserRole }, @Param('id') id: string, @Body() body: { enabled?: boolean }, ) { return this.usersService.updatePriorArchive( user.organizationId, user.role, id, body.enabled === true, ); } @Patch(':id/visibility') @Roles('owner', 'admin') updateVisibility( @CurrentUser() user: { organizationId: string; role: UserRole }, @Param('id') id: string, @Body() dto: UpdateUserVisibilityDto, ) { return this.usersService.updateVisibility( user.organizationId, user.role, id, dto, ); } @Patch(':id/role') @Roles('owner', 'admin') updateRole( @CurrentUser() user: { organizationId: string; userId: string; role: UserRole }, @Param('id') id: string, @Body() dto: UpdateUserRoleDto, ) { return this.usersService.updateRole( user.organizationId, { userId: user.userId, role: user.role }, id, dto, ); } }