Files
yara724-api/src/management/management.controller.ts
T

195 lines
5.8 KiB
TypeScript

import { ClientWorkingHoursService } from "src/client/working-hours/client-working-hours.service";
import {
Body,
Controller,
Get,
Param,
Patch,
Post,
Query,
UseGuards,
UsePipes,
ValidationPipe,
} from "@nestjs/common";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
import { LocalActorAuthGuard } from "src/auth/guards/actor-local.guard";
import { RolesGuard } from "src/auth/guards/role.guard";
import { Roles } from "src/decorators/roles.decorator";
import { CurrentUser } from "src/decorators/user.decorator";
import { RoleEnum } from "src/Types&Enums/role.enum";
import { SuperAdminGuard } from "src/super-admin/guards/super-admin.guard";
import { CreateBranchDto } from "src/client/dto/create-branch.dto";
import {
CreateManagedAccountDto,
CreateManagedClientDto,
CreateManagedStaffDto,
ManagedListQueryDto,
SetManagedStaffActiveDto,
UpdateManagedAccountDto,
UpdateManagedBranchDto,
UpdateManagedClientDto,
UpdateManagedStaffDto,
} from "./management.dto";
import { ManagementService } from "./management.service";
const validation = new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
});
@ApiTags("super-admin-management")
@ApiBearerAuth()
@UseGuards(SuperAdminGuard)
@UsePipes(validation)
@Controller("super-admin/management")
export class SuperAdminManagementController {
constructor(
private management: ManagementService,
private readonly workingHours: ClientWorkingHoursService,
) {}
@Get("working-hours")
readHours(@Query("clientId") clientId: string) {
return this.workingHours.read(clientId);
}
@Patch("working-hours")
updateHours(@Query("clientId") clientId: string, @Body() body: unknown) {
return this.workingHours.update(clientId, body);
}
@Get("clients") clients() {
return this.management.listClients();
}
@Post("clients") createClient(@Body() body: CreateManagedClientDto) {
return this.management.saveClient(body);
}
@Patch("clients/:id") updateClient(
@Param("id") id: string,
@Body() body: UpdateManagedClientDto,
) {
return this.management.saveClient(body, id);
}
@Get("clients/:clientId/accounts") accounts(
@Param("clientId") id: string,
@Query() query: ManagedListQueryDto,
) {
return this.management.listStaff(id, query, true);
}
@Post("clients/:clientId/accounts") createAccount(
@Param("clientId") id: string,
@Body() body: CreateManagedAccountDto,
) {
return this.management.createStaff(id, body, true);
}
@Patch("clients/:clientId/accounts/:id") updateAccount(
@Param("clientId") client: string,
@Param("id") id: string,
@Body() body: UpdateManagedAccountDto,
) {
return this.management.updateStaff(
client,
RoleEnum.COMPANY,
id,
body,
true,
);
}
@Get("staff") staff(@Query() query: ManagedListQueryDto) {
return this.management.listStaff(query.clientId, query);
}
@Post("staff") createStaff(
@Query("clientId") client: string,
@Body() body: CreateManagedStaffDto,
) {
return this.management.createStaff(client, body);
}
@Patch("staff/:role/:id") updateStaff(
@Query("clientId") client: string,
@Param("role") role: string,
@Param("id") id: string,
@Body() body: UpdateManagedStaffDto,
) {
return this.management.updateStaff(client, role, id, body);
}
@Get("branches") branches(@Query("clientId") client: string) {
return this.management.listBranches(client);
}
@Patch("staff/:role/:id/status") setStaffActive(
@Query("clientId") client: string,
@Param("role") role: string,
@Param("id") id: string,
@Body() body: SetManagedStaffActiveDto,
) {
return this.management.setStaffActive(client, role, id, body.isActive);
}
@Post("branches") createBranch(
@Query("clientId") client: string,
@Body() body: CreateBranchDto,
) {
return this.management.saveBranch(client, body);
}
@Patch("branches/:id") updateBranch(
@Query("clientId") client: string,
@Param("id") id: string,
@Body() body: UpdateManagedBranchDto,
) {
return this.management.saveBranch(client, body, id);
}
}
@ApiTags("insurer-management")
@ApiBearerAuth()
@UseGuards(LocalActorAuthGuard, RolesGuard)
@Roles(RoleEnum.COMPANY)
@UsePipes(validation)
@Controller("expert-insurer/management")
export class InsurerManagementController {
constructor(
private management: ManagementService,
private readonly workingHours: ClientWorkingHoursService,
) {}
@Get("working-hours")
readHours(@CurrentUser() actor: { clientKey: string }) {
return this.workingHours.read(actor.clientKey);
}
@Patch("working-hours")
updateHours(@CurrentUser() actor: { clientKey: string }, @Body() body: unknown) {
return this.workingHours.update(actor.clientKey, body);
}
@Get("staff") staff(
@CurrentUser() actor,
@Query() query: ManagedListQueryDto,
) {
return this.management.listStaff(actor.clientKey, query);
}
@Post("staff") createStaff(
@CurrentUser() actor,
@Body() body: CreateManagedStaffDto,
) {
return this.management.createStaff(actor.clientKey, body);
}
@Patch("staff/:role/:id") updateStaff(
@CurrentUser() actor,
@Param("role") role: string,
@Param("id") id: string,
@Body() body: UpdateManagedStaffDto,
) {
return this.management.updateStaff(actor.clientKey, role, id, body);
}
@Get("branches") branches(@CurrentUser() actor) {
return this.management.listBranches(actor.clientKey);
}
@Post("branches") createBranch(
@CurrentUser() actor,
@Body() body: CreateBranchDto,
) {
return this.management.saveBranch(actor.clientKey, body);
}
@Patch("branches/:id") updateBranch(
@CurrentUser() actor,
@Param("id") id: string,
@Body() body: UpdateManagedBranchDto,
) {
return this.management.saveBranch(actor.clientKey, body, id);
}
}