mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-28 05:30:55 +00:00
refactor(bookings): replace RFQ/quotation flow with submit, staff review, approval routing, and payment stubs
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
Param,
|
||||
ParseUUIDPipe,
|
||||
Post,
|
||||
Query,
|
||||
Put,
|
||||
} from "@nestjs/common";
|
||||
import { ApiOperation, ApiTags } from "@nestjs/swagger";
|
||||
@@ -27,6 +28,19 @@ export class BackofficeController {
|
||||
return this.backofficeService.createOrganizationUser(organizationId, dto);
|
||||
}
|
||||
|
||||
@Get("organizations/:orgId/employees")
|
||||
@ApiOperation({ summary: "Get deduplicated organization employees for backoffice" })
|
||||
getOrganizationEmployees(
|
||||
@Param("orgId", ParseUUIDPipe) organizationId: string,
|
||||
@Query("skip") skip?: string,
|
||||
@Query("take") take?: string,
|
||||
) {
|
||||
return this.backofficeService.getOrganizationEmployees(organizationId, {
|
||||
skip,
|
||||
take,
|
||||
});
|
||||
}
|
||||
|
||||
@Get("organizations/:orgId/employee-users/:userId/roles")
|
||||
@ApiOperation({ summary: "Get org-scoped roles assigned to an employee user" })
|
||||
getEmployeeUserRoles(
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { hashPassword } from "@tria-plc/api-common/utils/argon";
|
||||
import { EUserStatus } from "@tria-plc/api-common/utils/enums/user.enum";
|
||||
import { DataSource, In, IsNull, Repository } from "typeorm";
|
||||
import { DataSource, EntityManager, In, IsNull, Repository } from "typeorm";
|
||||
|
||||
import { Employee, Organization, UserCredential } from "@tria-plc/iamapi-common";
|
||||
import { Role } from "@tria-plc/iamapi-common/entities/iam/user/role.entity";
|
||||
@@ -21,10 +21,14 @@ const RESERVED_ROLE_KEYS = new Set([
|
||||
"unit_admin",
|
||||
]);
|
||||
const DEFAULT_USER_PASSWORD = "12345678";
|
||||
const ORGANIZATION_ADMIN_ROLE_KEY = "organization_admin";
|
||||
const EDR_ORG_MANAGER_ROLE_KEY = "edr_org_manager";
|
||||
|
||||
@Injectable()
|
||||
export class BackofficeService {
|
||||
constructor(
|
||||
@InjectRepository(Employee)
|
||||
private readonly employeeRepository: Repository<Employee>,
|
||||
@InjectRepository(Organization)
|
||||
private readonly organizationRepository: Repository<Organization>,
|
||||
@InjectRepository(Role)
|
||||
@@ -51,6 +55,7 @@ export class BackofficeService {
|
||||
const email = dto.email.trim().toLowerCase();
|
||||
const username = dto.username.trim().toLowerCase();
|
||||
const phoneNumber = dto.phoneNumber?.trim() || undefined;
|
||||
const assignOrganizationAdmin = dto.assignOrganizationAdmin === true;
|
||||
const name = {
|
||||
en: dto.name.en.trim(),
|
||||
...(dto.name.am?.trim() ? { am: dto.name.am.trim() } : {}),
|
||||
@@ -168,6 +173,16 @@ export class BackofficeService {
|
||||
throw new NotFoundException("employee_create_failed");
|
||||
}
|
||||
|
||||
const userId = user.id;
|
||||
|
||||
if (!userId) {
|
||||
throw new NotFoundException("user_create_failed");
|
||||
}
|
||||
|
||||
if (assignOrganizationAdmin) {
|
||||
await this.ensureOrganizationAdminAccess(manager, organizationId, userId);
|
||||
}
|
||||
|
||||
return employee;
|
||||
});
|
||||
}
|
||||
@@ -201,6 +216,45 @@ export class BackofficeService {
|
||||
}));
|
||||
}
|
||||
|
||||
async getOrganizationEmployees(
|
||||
organizationId: string,
|
||||
query: { skip?: string; take?: string },
|
||||
) {
|
||||
const organizationExists = await this.organizationRepository.exists({
|
||||
where: { id: organizationId },
|
||||
});
|
||||
|
||||
if (!organizationExists) {
|
||||
throw new NotFoundException("organization_not_found");
|
||||
}
|
||||
|
||||
const take = Number.parseInt(query.take ?? "1000", 10);
|
||||
const skip = Number.parseInt(query.skip ?? "0", 10);
|
||||
|
||||
const employees = await this.employeeRepository.find({
|
||||
where: {
|
||||
organizationId,
|
||||
isCurrent: true,
|
||||
},
|
||||
relations: {
|
||||
user: true,
|
||||
employeePositions: {
|
||||
position: true,
|
||||
},
|
||||
},
|
||||
order: {
|
||||
createdAt: "DESC",
|
||||
},
|
||||
});
|
||||
|
||||
const deduplicated = this.mergeEmployeesByUser(employees);
|
||||
|
||||
return {
|
||||
count: deduplicated.length,
|
||||
items: deduplicated.slice(skip, skip + take),
|
||||
};
|
||||
}
|
||||
|
||||
async replaceEmployeeUserRoles(
|
||||
organizationId: string,
|
||||
userId: string,
|
||||
@@ -268,4 +322,104 @@ export class BackofficeService {
|
||||
throw new NotFoundException("user_not_found_in_organization");
|
||||
}
|
||||
}
|
||||
|
||||
private mergeEmployeesByUser(employees: Employee[]) {
|
||||
const employeesByUserId = new Map<string, Employee>();
|
||||
|
||||
for (const employee of employees) {
|
||||
const userId = employee.userId;
|
||||
const employeeId = employee.id;
|
||||
|
||||
if (!userId) {
|
||||
if (employeeId) {
|
||||
employeesByUserId.set(employeeId, employee);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const existing = employeesByUserId.get(userId);
|
||||
|
||||
if (!existing) {
|
||||
employeesByUserId.set(userId, employee);
|
||||
continue;
|
||||
}
|
||||
|
||||
const existingPositions = existing.employeePositions ?? [];
|
||||
const nextPositions = employee.employeePositions ?? [];
|
||||
const mergedEmployeePositions = Array.from(
|
||||
new Map(
|
||||
[...existingPositions, ...nextPositions].map((employeePosition) => [
|
||||
employeePosition.id,
|
||||
employeePosition,
|
||||
]),
|
||||
).values(),
|
||||
);
|
||||
|
||||
employeesByUserId.set(userId, {
|
||||
...existing,
|
||||
...employee,
|
||||
id: existing.id,
|
||||
user: existing.user ?? employee.user,
|
||||
userId,
|
||||
name: existing.name ?? employee.name,
|
||||
status: existing.status ?? employee.status,
|
||||
employeePositions: mergedEmployeePositions,
|
||||
});
|
||||
}
|
||||
|
||||
return [...employeesByUserId.values()];
|
||||
}
|
||||
|
||||
private async ensureOrganizationAdminAccess(
|
||||
manager: EntityManager,
|
||||
organizationId: string,
|
||||
userId: string,
|
||||
) {
|
||||
const roles = await manager.getRepository(Role).find({
|
||||
where: [
|
||||
{ key: ORGANIZATION_ADMIN_ROLE_KEY },
|
||||
{ key: EDR_ORG_MANAGER_ROLE_KEY },
|
||||
],
|
||||
select: { id: true, key: true },
|
||||
});
|
||||
|
||||
const requiredRoles = [ORGANIZATION_ADMIN_ROLE_KEY, EDR_ORG_MANAGER_ROLE_KEY].map((key) => {
|
||||
const role = roles.find((item) => item.key === key);
|
||||
|
||||
if (!role?.id) {
|
||||
throw new NotFoundException(`required_role_not_seeded:${key}`);
|
||||
}
|
||||
|
||||
return {
|
||||
id: role.id,
|
||||
key: role.key,
|
||||
};
|
||||
});
|
||||
|
||||
const existingRoleIds = new Set(
|
||||
(
|
||||
await manager.getRepository(UserRole).find({
|
||||
where: {
|
||||
userId,
|
||||
organizationId,
|
||||
},
|
||||
select: { roleId: true },
|
||||
})
|
||||
).map((userRole) => userRole.roleId),
|
||||
);
|
||||
|
||||
const rolesToInsert = requiredRoles
|
||||
.filter((role) => !existingRoleIds.has(role.id))
|
||||
.map((role) => ({
|
||||
userId,
|
||||
roleId: role.id,
|
||||
organizationId,
|
||||
}));
|
||||
|
||||
if (!rolesToInsert.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
await manager.getRepository(UserRole).insert(rolesToInsert);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsEmail, IsObject, IsOptional, IsString, MinLength } from "class-validator";
|
||||
import { IsBoolean, IsEmail, IsObject, IsOptional, IsString, MinLength } from "class-validator";
|
||||
|
||||
class CreateOrganizationUserNameDto {
|
||||
@ApiProperty()
|
||||
@@ -31,4 +31,9 @@ export class CreateOrganizationUserDto {
|
||||
@ApiProperty({ type: CreateOrganizationUserNameDto })
|
||||
@IsObject()
|
||||
name!: CreateOrganizationUserNameDto;
|
||||
|
||||
@ApiProperty({ required: false, default: false })
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
assignOrganizationAdmin?: boolean;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user