File

src/users/users.controller.ts

Prefix

users

Index

Methods

Methods

Async clearTelematics
clearTelematics(orgId: string)
Decorators :
@Delete('customization/telematics')
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiOperation({summary: 'Disconnect the telematics provider', description: 'Wipes provider/credentials/baseUrl from organization.settings.telematics.'})
Parameters :
Name Type Optional
orgId string No
Returns : unknown
Async create
create(orgId: string, dto: CreateUserDto)
Decorators :
@Post()
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiOperation({summary: 'Create a new user'})
Parameters :
Name Type Optional
orgId string No
dto CreateUserDto No
Returns : unknown
Async createInviteCode
createInviteCode(orgId: string, user: any, body: literal type)
Decorators :
@Post('invite-codes')
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiOperation({summary: 'Generate a new invite code'})
Parameters :
Name Type Optional
orgId string No
user any No
body literal type No
Returns : unknown
Async findAll
findAll(orgId: string, query: PaginationParams)
Decorators :
@Get()
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiPagination()
@ApiOperation({summary: 'List all users with pagination'})
Parameters :
Name Type Optional
orgId string No
query PaginationParams No
Returns : unknown
Async findOne
findOne(orgId: string, id: string)
Decorators :
@Get(':id')
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiOperation({summary: 'Get a user by ID (org-scoped)'})
Parameters :
Name Type Optional
orgId string No
id string No
Returns : unknown
Async getCustomization
getCustomization(orgId: string)
Decorators :
@Get('customization/config')
@ApiOperation({summary: 'Get customization config (nav labels, order columns)'})
Parameters :
Name Type Optional
orgId string No
Returns : unknown
Async getMyPreferences
getMyPreferences(userId: string)
Decorators :
@Get('me/preferences')
@ApiOperation({summary: 'Get the current user's notification + UI preferences'})
Parameters :
Name Type Optional
userId string No
Returns : unknown
Async getRolePermissions
getRolePermissions(orgId: string)
Decorators :
@Get('role-permissions/config')
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiOperation({summary: 'Get role-based page access configuration'})
Parameters :
Name Type Optional
orgId string No
Returns : unknown
Async listInviteCodes
listInviteCodes(orgId: string)
Decorators :
@Get('invite-codes')
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiOperation({summary: 'List all invite codes'})
Parameters :
Name Type Optional
orgId string No
Returns : unknown
Async remove
remove(orgId: string, id: string)
Decorators :
@Delete(':id')
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiOperation({summary: 'Delete a user (org-scoped)'})
Parameters :
Name Type Optional
orgId string No
id string No
Returns : unknown
Async removeLogo
removeLogo(orgId: string)
Decorators :
@Delete('customization/logo')
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiOperation({summary: 'Remove organization logo'})
Parameters :
Name Type Optional
orgId string No
Returns : unknown
Async revokeInviteCode
revokeInviteCode(id: string)
Decorators :
@Delete('invite-codes/:id')
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiOperation({summary: 'Revoke an invite code'})
Parameters :
Name Type Optional
id string No
Returns : unknown
Async testTelematics
testTelematics(orgId: string)
Decorators :
@Post('customization/telematics/test')
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiOperation({summary: 'Test the saved telematics provider credentials', description: 'Performs a real auth handshake against the configured provider (currently Mix Telematics) and records the result on the org settings. Use this after saving credentials to verify they work before relying on them in production.'})
Parameters :
Name Type Optional
orgId string No
Returns : unknown
Async update
update(orgId: string, id: string, dto: UpdateUserDto)
Decorators :
@Put(':id')
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiOperation({summary: 'Update a user (org-scoped)'})
Parameters :
Name Type Optional
orgId string No
id string No
dto UpdateUserDto No
Returns : unknown
Async updateCustomization
updateCustomization(orgId: string, user: any, body: UpdateCustomizationDto)
Decorators :
@Put('customization/config')
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiOperation({summary: 'Update customization config'})
Parameters :
Name Type Optional
orgId string No
user any No
body UpdateCustomizationDto No
Returns : unknown
Async updateMyPreferences
updateMyPreferences(userId: string, body: Record)
Decorators :
@Put('me/preferences')
@ApiOperation({summary: 'Update the current user's preferences (partial)'})
Parameters :
Name Type Optional
userId string No
body Record<string | any> No
Returns : unknown
Async updateRolePermissions
updateRolePermissions(orgId: string, body: UpdateRolePermissionsDto)
Decorators :
@Put('role-permissions/config')
@Roles('SUPER_ADMIN', 'ADMIN')
@ApiOperation({summary: 'Update role-based page access configuration'})
Parameters :
Name Type Optional
orgId string No
body UpdateRolePermissionsDto No
Returns : unknown
Async uploadLogo
uploadLogo(orgId: string, file: Express.Multer.File)
Decorators :
@Post('customization/logo')
@Roles('SUPER_ADMIN', 'ADMIN')
@UseInterceptors(undefined)
@ApiConsumes('multipart/form-data')
@ApiOperation({summary: 'Upload organization logo'})
Parameters :
Name Type Optional
orgId string No
file Express.Multer.File No
Returns : unknown
import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiConsumes } from '@nestjs/swagger';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { UpdateRolePermissionsDto } from './dto/role-permissions.dto';
import { UpdateCustomizationDto } from './dto/customization.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { Roles } from '../auth/decorators/roles.decorator';
import { CurrentUser } from '../auth/decorators/current-user.decorator';
import { ApiPagination } from '../common/decorators/api-pagination.decorator';
import { PaginationParams } from '../common/utils/pagination.util';

@ApiTags('Users')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, RolesGuard)
@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Post()
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiOperation({ summary: 'Create a new user' })
  async create(
    @CurrentUser('organizationId') orgId: string,
    @Body() dto: CreateUserDto,
  ) {
    return this.usersService.create(orgId, dto);
  }

  @Get()
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiPagination()
  @ApiOperation({ summary: 'List all users with pagination' })
  async findAll(
    @CurrentUser('organizationId') orgId: string,
    @Query() query: PaginationParams,
  ) {
    return this.usersService.findAll(orgId, query);
  }

  @Get(':id')
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiOperation({ summary: 'Get a user by ID (org-scoped)' })
  async findOne(
    @CurrentUser('organizationId') orgId: string,
    @Param('id') id: string,
  ) {
    return this.usersService.findOne(orgId, id);
  }

  @Put(':id')
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiOperation({ summary: 'Update a user (org-scoped)' })
  async update(
    @CurrentUser('organizationId') orgId: string,
    @Param('id') id: string,
    @Body() dto: UpdateUserDto,
  ) {
    return this.usersService.update(orgId, id, dto);
  }

  @Delete(':id')
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiOperation({ summary: 'Delete a user (org-scoped)' })
  async remove(
    @CurrentUser('organizationId') orgId: string,
    @Param('id') id: string,
  ) {
    return this.usersService.remove(orgId, id);
  }

  // ── Role Permissions ──────────────────────────────

  @Get('role-permissions/config')
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiOperation({ summary: 'Get role-based page access configuration' })
  async getRolePermissions(@CurrentUser('organizationId') orgId: string) {
    return this.usersService.getRolePermissions(orgId);
  }

  @Put('role-permissions/config')
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiOperation({ summary: 'Update role-based page access configuration' })
  async updateRolePermissions(
    @CurrentUser('organizationId') orgId: string,
    @Body() body: UpdateRolePermissionsDto,
  ) {
    return this.usersService.updateRolePermissions(orgId, body);
  }

  // ── Invite Codes ──────────────────────────────

  @Get('invite-codes')
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiOperation({ summary: 'List all invite codes' })
  async listInviteCodes(@CurrentUser('organizationId') orgId: string) {
    return this.usersService.listInviteCodes(orgId);
  }

  @Post('invite-codes')
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiOperation({ summary: 'Generate a new invite code' })
  async createInviteCode(
    @CurrentUser('organizationId') orgId: string,
    @CurrentUser() user: any,
    @Body() body: { role?: string; expiresInDays?: number },
  ) {
    return this.usersService.createInviteCode(orgId, user, body);
  }

  // ── Customization ──────────────────────────────

  @Get('customization/config')
  @ApiOperation({ summary: 'Get customization config (nav labels, order columns)' })
  async getCustomization(@CurrentUser('organizationId') orgId: string) {
    return this.usersService.getCustomization(orgId);
  }

  @Put('customization/config')
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiOperation({ summary: 'Update customization config' })
  async updateCustomization(
    @CurrentUser('organizationId') orgId: string,
    @CurrentUser() user: any,
    @Body() body: UpdateCustomizationDto,
  ) {
    return this.usersService.updateCustomization(orgId, body, user);
  }

  @Post('customization/telematics/test')
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiOperation({
    summary: 'Test the saved telematics provider credentials',
    description:
      'Performs a real auth handshake against the configured provider (currently Mix Telematics) and records the result on the org settings. Use this after saving credentials to verify they work before relying on them in production.',
  })
  async testTelematics(@CurrentUser('organizationId') orgId: string) {
    return this.usersService.testTelematicsConnection(orgId);
  }

  @Delete('customization/telematics')
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiOperation({
    summary: 'Disconnect the telematics provider',
    description: 'Wipes provider/credentials/baseUrl from organization.settings.telematics.',
  })
  async clearTelematics(@CurrentUser('organizationId') orgId: string) {
    return this.usersService.clearTelematics(orgId);
  }

  @Delete('invite-codes/:id')
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiOperation({ summary: 'Revoke an invite code' })
  async revokeInviteCode(@Param('id') id: string) {
    return this.usersService.revokeInviteCode(id);
  }

  // ── Logo upload (admin) ──────────────────────────────────────────

  @Post('customization/logo')
  @Roles('SUPER_ADMIN', 'ADMIN')
  @UseInterceptors(
    FileInterceptor('file', { limits: { fileSize: 2 * 1024 * 1024 } }),
  )
  @ApiConsumes('multipart/form-data')
  @ApiOperation({ summary: 'Upload organization logo' })
  async uploadLogo(
    @CurrentUser('organizationId') orgId: string,
    @UploadedFile() file: Express.Multer.File,
  ) {
    return this.usersService.uploadLogo(orgId, file);
  }

  @Delete('customization/logo')
  @Roles('SUPER_ADMIN', 'ADMIN')
  @ApiOperation({ summary: 'Remove organization logo' })
  async removeLogo(@CurrentUser('organizationId') orgId: string) {
    return this.usersService.removeLogo(orgId);
  }

  // ── Per-user preferences (any authenticated user) ────────────────

  @Get('me/preferences')
  @ApiOperation({ summary: 'Get the current user\'s notification + UI preferences' })
  async getMyPreferences(@CurrentUser('id') userId: string) {
    return this.usersService.getMyPreferences(userId);
  }

  @Put('me/preferences')
  @ApiOperation({ summary: 'Update the current user\'s preferences (partial)' })
  async updateMyPreferences(
    @CurrentUser('id') userId: string,
    @Body() body: Record<string, any>,
  ) {
    return this.usersService.updateMyPreferences(userId, body);
  }
}

results matching ""

    No results matching ""