mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-30 01:48:12 +00:00
implement rule engine module with dynamic booking evaluation, 7 entities, and Postman endpoints
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import { ConflictException, Inject, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { CreatePriorityRuleDto } from '../dto/create-priority-rule.dto';
|
||||
import { UpdatePriorityRuleDto } from '../dto/update-priority-rule.dto';
|
||||
import { PriorityRule } from '../entities/priority-rule.entity';
|
||||
import {
|
||||
IPriorityRulesRepository,
|
||||
PRIORITY_RULES_REPOSITORY,
|
||||
} from '../interfaces/priority-rules.repository.interface';
|
||||
|
||||
@Injectable()
|
||||
export class PriorityRulesService {
|
||||
constructor(
|
||||
@Inject(PRIORITY_RULES_REPOSITORY)
|
||||
private readonly repository: IPriorityRulesRepository,
|
||||
) {}
|
||||
|
||||
/** List priority rules with pagination. */
|
||||
async findAll(filter: {
|
||||
isActive?: boolean;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
}): Promise<{ data: PriorityRule[]; meta: { total: number; page: number; pageSize: number; totalPages: number } }> {
|
||||
const page = filter.page ?? 1;
|
||||
const pageSize = filter.pageSize ?? 20;
|
||||
const where: Record<string, unknown> = {};
|
||||
if (filter.isActive !== undefined) where.isActive = filter.isActive;
|
||||
|
||||
const [data, total] = await this.repository.findAndCount({
|
||||
where,
|
||||
order: { priorityType: 'ASC' },
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
});
|
||||
return { data, meta: { total, page, pageSize, totalPages: Math.ceil(total / pageSize) } };
|
||||
}
|
||||
|
||||
/** Get a single priority rule by ID. */
|
||||
async findById(id: string): Promise<PriorityRule> {
|
||||
const entity = await this.repository.findById(id);
|
||||
if (!entity) throw new NotFoundException(`Priority rule ${id} not found`);
|
||||
return entity;
|
||||
}
|
||||
|
||||
/** Create a new priority rule. */
|
||||
async create(dto: CreatePriorityRuleDto): Promise<PriorityRule> {
|
||||
const existing = await this.repository.findAll({ where: { priorityType: dto.priorityType } });
|
||||
if (existing.length > 0) {
|
||||
throw new ConflictException(`Priority rule for type "${dto.priorityType}" already exists`);
|
||||
}
|
||||
return this.repository.create({
|
||||
priorityType: dto.priorityType,
|
||||
ruleName: dto.ruleName,
|
||||
description: dto.description ?? null,
|
||||
activationCondition: dto.activationCondition ?? null,
|
||||
bonusPoints: dto.bonusPoints,
|
||||
isActive: dto.isActive ?? false,
|
||||
});
|
||||
}
|
||||
|
||||
/** Update an existing priority rule. */
|
||||
async update(id: string, dto: UpdatePriorityRuleDto): Promise<PriorityRule> {
|
||||
await this.findById(id);
|
||||
const updated = await this.repository.update(id, dto);
|
||||
if (!updated) throw new NotFoundException(`Priority rule ${id} not found`);
|
||||
return updated;
|
||||
}
|
||||
|
||||
/** Soft-delete a priority rule. */
|
||||
async remove(id: string): Promise<void> {
|
||||
await this.findById(id);
|
||||
await this.repository.softDelete(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user