2022-12-27 14:40:33 +00:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
Param,
|
|
|
|
|
Post,
|
|
|
|
|
UseGuards,
|
|
|
|
|
Body,
|
|
|
|
|
UseInterceptors,
|
|
|
|
|
UploadedFile,
|
|
|
|
|
Res,
|
|
|
|
|
BadRequestException,
|
2024-01-19 11:23:40 +00:00
|
|
|
Put,
|
2022-12-27 14:40:33 +00:00
|
|
|
} from '@nestjs/common';
|
|
|
|
|
import { Response, Express } from 'express';
|
2021-07-19 08:57:57 +00:00
|
|
|
import { OrganizationUsersService } from 'src/services/organization_users.service';
|
|
|
|
|
import { decamelizeKeys } from 'humps';
|
|
|
|
|
import { JwtAuthGuard } from '../../src/modules/auth/jwt-auth.guard';
|
2021-07-22 07:25:29 +00:00
|
|
|
import { AppAbility } from 'src/modules/casl/casl-ability.factory';
|
2021-07-21 16:57:04 +00:00
|
|
|
import { PoliciesGuard } from 'src/modules/casl/policies.guard';
|
|
|
|
|
import { CheckPolicies } from 'src/modules/casl/check_policies.decorator';
|
2022-05-05 07:08:42 +00:00
|
|
|
import { User as UserEntity } from 'src/entities/user.entity';
|
|
|
|
|
import { User } from 'src/decorators/user.decorator';
|
2022-04-20 09:16:57 +00:00
|
|
|
import { InviteNewUserDto } from '../dto/invite-new-user.dto';
|
2022-06-02 09:50:51 +00:00
|
|
|
import { OrganizationsService } from '@services/organizations.service';
|
2022-12-27 14:40:33 +00:00
|
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
2021-07-19 08:57:57 +00:00
|
|
|
|
2022-12-27 14:40:33 +00:00
|
|
|
const MAX_CSV_FILE_SIZE = 1024 * 1024 * 1; // 1MB
|
2021-07-19 08:57:57 +00:00
|
|
|
@Controller('organization_users')
|
|
|
|
|
export class OrganizationUsersController {
|
2022-06-02 09:50:51 +00:00
|
|
|
constructor(
|
|
|
|
|
private organizationUsersService: OrganizationUsersService,
|
|
|
|
|
private organizationsService: OrganizationsService
|
|
|
|
|
) {}
|
2021-07-19 08:57:57 +00:00
|
|
|
|
2021-07-19 09:36:34 +00:00
|
|
|
// Endpoint for inviting new organization users
|
2021-07-22 09:41:50 +00:00
|
|
|
@UseGuards(JwtAuthGuard, PoliciesGuard)
|
2022-05-05 07:08:42 +00:00
|
|
|
@CheckPolicies((ability: AppAbility) => ability.can('inviteUser', UserEntity))
|
2021-07-19 09:36:34 +00:00
|
|
|
@Post()
|
2022-05-05 07:08:42 +00:00
|
|
|
async create(@User() user, @Body() inviteNewUserDto: InviteNewUserDto) {
|
2022-08-16 02:14:03 +00:00
|
|
|
await this.organizationsService.inviteNewUser(user, inviteNewUserDto);
|
|
|
|
|
return;
|
2021-07-19 08:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-27 14:40:33 +00:00
|
|
|
@UseGuards(JwtAuthGuard, PoliciesGuard)
|
|
|
|
|
@CheckPolicies((ability: AppAbility) => ability.can('inviteUser', UserEntity))
|
|
|
|
|
@UseInterceptors(FileInterceptor('file'))
|
|
|
|
|
@Post('upload_csv')
|
|
|
|
|
async bulkUploadUsers(@User() user, @UploadedFile() file: Express.Multer.File, @Res() res: Response) {
|
|
|
|
|
if (file.size > MAX_CSV_FILE_SIZE) {
|
|
|
|
|
throw new BadRequestException('File size cannot be greater than 2MB');
|
|
|
|
|
}
|
|
|
|
|
await this.organizationsService.bulkUploadUsers(user, file.buffer, res);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-22 07:25:29 +00:00
|
|
|
@UseGuards(JwtAuthGuard, PoliciesGuard)
|
2022-05-05 07:08:42 +00:00
|
|
|
@CheckPolicies((ability: AppAbility) => ability.can('archiveUser', UserEntity))
|
2021-07-19 09:36:34 +00:00
|
|
|
@Post(':id/archive')
|
2022-06-17 11:39:13 +00:00
|
|
|
async archive(@User() user, @Param('id') id: string) {
|
|
|
|
|
await this.organizationUsersService.archive(id, user.organizationId);
|
|
|
|
|
return;
|
2021-07-19 09:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-19 11:23:40 +00:00
|
|
|
@UseGuards(JwtAuthGuard, PoliciesGuard)
|
|
|
|
|
@CheckPolicies((ability: AppAbility) => ability.can('updateUser', UserEntity))
|
|
|
|
|
@Put(':id')
|
|
|
|
|
async updateUser(@Param('id') id: string, @Body() updateUserDto) {
|
|
|
|
|
await this.organizationUsersService.updateOrgUser(id, updateUserDto);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-30 20:41:10 +00:00
|
|
|
@UseGuards(JwtAuthGuard, PoliciesGuard)
|
2022-05-05 07:08:42 +00:00
|
|
|
@CheckPolicies((ability: AppAbility) => ability.can('archiveUser', UserEntity))
|
2021-12-30 20:41:10 +00:00
|
|
|
@Post(':id/unarchive')
|
2022-05-05 07:08:42 +00:00
|
|
|
async unarchive(@User() user, @Param('id') id: string) {
|
2022-06-17 11:39:13 +00:00
|
|
|
await this.organizationUsersService.unarchive(user, id);
|
|
|
|
|
return;
|
2021-12-30 20:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
2022-04-20 09:16:57 +00:00
|
|
|
// Deprecated
|
2021-07-21 16:57:04 +00:00
|
|
|
@UseGuards(JwtAuthGuard, PoliciesGuard)
|
2022-05-05 07:08:42 +00:00
|
|
|
@CheckPolicies((ability: AppAbility) => ability.can('changeRole', UserEntity))
|
2021-07-19 09:41:33 +00:00
|
|
|
@Post(':id/change_role')
|
2022-05-05 07:08:42 +00:00
|
|
|
async changeRole(@Param('id') id, @Body('role') role) {
|
|
|
|
|
const result = await this.organizationUsersService.changeRole(id, role);
|
2021-07-19 09:41:33 +00:00
|
|
|
return decamelizeKeys({ result });
|
|
|
|
|
}
|
2021-07-19 08:57:57 +00:00
|
|
|
}
|