mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 04:50:54 +00:00
108 lines
4.1 KiB
TypeScript
108 lines
4.1 KiB
TypeScript
import { PaginatedResponse } from '@edr/types';
|
|
import { BadRequestException, Inject, Injectable, NotFoundException } from '@nestjs/common';
|
|
import { DataSource } from 'typeorm';
|
|
import { CreateApprovalRuleDto } from '../dto/create-approval-rule.dto';
|
|
import { ListApprovalRulesQueryDto } from '../dto/list-rule-engine-query.dto';
|
|
import { ReorderItemsDto } from '../dto/reorder-items.dto';
|
|
import { UpdateApprovalRuleDto } from '../dto/update-approval-rule.dto';
|
|
import { ApprovalRule } from '../entities/approval-rule.entity';
|
|
import {
|
|
APPROVAL_RULES_REPOSITORY,
|
|
IApprovalRulesRepository,
|
|
} from '../interfaces/approval-rules.repository.interface';
|
|
import { DisplayOrderService } from './display-order.service';
|
|
|
|
@Injectable()
|
|
export class ApprovalRulesService {
|
|
constructor(
|
|
@Inject(APPROVAL_RULES_REPOSITORY)
|
|
private readonly repository: IApprovalRulesRepository,
|
|
private readonly displayOrder: DisplayOrderService,
|
|
private readonly dataSource: DataSource,
|
|
) {}
|
|
|
|
/** List approval rules — standard paginated envelope with server-side search. */
|
|
async findAll(query: ListApprovalRulesQueryDto): Promise<PaginatedResponse<ApprovalRule>> {
|
|
return this.repository.findPaged(query);
|
|
}
|
|
|
|
/** Get approval chain for a cargo type flag. */
|
|
async findChain(requiresDirectorApproval: boolean): Promise<ApprovalRule[]> {
|
|
return this.repository.findChainForCargo(requiresDirectorApproval);
|
|
}
|
|
|
|
/**
|
|
* IAM position types, for the approval-step role picker. A chain step names
|
|
* the position type that must approve it, so this is the vocabulary an admin
|
|
* builds chains from. Read straight from the shared `iam` schema — the same
|
|
* pattern the freight API already uses for `iam.users`.
|
|
*/
|
|
async listPositionTypes(): Promise<Array<{ label: string; value: string }>> {
|
|
const rows = await this.dataSource.query<
|
|
Array<{ key: string; label: string }>
|
|
>(`SELECT key, COALESCE(name->>'en', key) AS label
|
|
FROM iam.position_types
|
|
ORDER BY 2`);
|
|
return rows.map((row) => ({ label: row.label, value: row.key }));
|
|
}
|
|
|
|
/** Get an approval rule by ID. */
|
|
async findById(id: string): Promise<ApprovalRule> {
|
|
const entity = await this.repository.findById(id);
|
|
if (!entity) throw new NotFoundException(`Approval rule ${id} not found`);
|
|
return entity;
|
|
}
|
|
|
|
/** Create an approval rule step. */
|
|
async create(dto: CreateApprovalRuleDto): Promise<ApprovalRule> {
|
|
if (dto.stepOrder !== undefined && dto.insertAfterId) {
|
|
throw new BadRequestException('Cannot set both stepOrder and insertAfterId');
|
|
}
|
|
|
|
const scopeWhere = { requiresDirectorApproval: dto.requiresDirectorApproval };
|
|
const stepOrder = await this.displayOrder.resolveCreateOrder(ApprovalRule, 'stepOrder', {
|
|
explicitOrder: dto.stepOrder,
|
|
insertAfterId: dto.insertAfterId,
|
|
scopeWhere,
|
|
});
|
|
|
|
return this.repository.create({
|
|
requiresDirectorApproval: dto.requiresDirectorApproval,
|
|
stepOrder,
|
|
requiredRole: dto.requiredRole,
|
|
actionLabel: dto.actionLabel,
|
|
blocksRole: dto.blocksRole,
|
|
});
|
|
}
|
|
|
|
/** Update an approval rule. */
|
|
async update(id: string, dto: UpdateApprovalRuleDto): Promise<ApprovalRule> {
|
|
await this.findById(id);
|
|
const updated = await this.repository.update(id, dto);
|
|
if (!updated) throw new NotFoundException(`Approval rule ${id} not found`);
|
|
return updated;
|
|
}
|
|
|
|
/** Soft-delete an approval rule. */
|
|
async remove(id: string): Promise<void> {
|
|
await this.findById(id);
|
|
await this.repository.softDelete(id);
|
|
}
|
|
|
|
async reorder(dto: ReorderItemsDto): Promise<void> {
|
|
if (dto.requiresDirectorApproval === undefined) {
|
|
throw new BadRequestException('requiresDirectorApproval is required for approval rule reorder');
|
|
}
|
|
await this.displayOrder.reorderByIds(ApprovalRule, 'stepOrder', dto.ids, {
|
|
requiresDirectorApproval: dto.requiresDirectorApproval,
|
|
});
|
|
}
|
|
|
|
async moveOrder(id: string, direction: 'up' | 'down'): Promise<void> {
|
|
const rule = await this.findById(id);
|
|
await this.displayOrder.moveOne(ApprovalRule, 'stepOrder', id, direction, {
|
|
requiresDirectorApproval: rule.requiresDirectorApproval,
|
|
});
|
|
}
|
|
}
|