- add optional description to scenario and environment entities - expose description in create/update DTOs, MCP tools, and export DTOs - render description as markdown on detail pages - add description editor (CodeEditor) to create/edit forms for both resources
118 lines
3.2 KiB
TypeScript
118 lines
3.2 KiB
TypeScript
import {
|
|
ConflictException,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from "@nestjs/common";
|
|
import { InjectRepository } from "@nestjs/typeorm";
|
|
import { Repository } from "typeorm";
|
|
import {
|
|
PaginatedResult,
|
|
PaginationQueryDto,
|
|
} from "../common/dto/pagination.dto";
|
|
import { CreateEnvironmentDto } from "./dto/create-environment.dto";
|
|
import { EnvironmentExportDto } from "./dto/environment-export.dto";
|
|
import { UpdateEnvironmentDto } from "./dto/update-environment.dto";
|
|
import { EnvironmentEntity } from "./environment.entity";
|
|
|
|
export type EnvironmentOrderBy = "id" | "name" | "createdAt" | "updatedAt";
|
|
|
|
@Injectable()
|
|
export class EnvironmentService {
|
|
constructor(
|
|
@InjectRepository(EnvironmentEntity)
|
|
private readonly repo: Repository<EnvironmentEntity>,
|
|
) {}
|
|
|
|
async create(dto: CreateEnvironmentDto): Promise<EnvironmentEntity> {
|
|
const existing = await this.repo.findOneBy({ name: dto.name });
|
|
if (existing) {
|
|
throw new ConflictException(`Environment "${dto.name}" already exists`);
|
|
}
|
|
return this.repo.save(this.repo.create(dto));
|
|
}
|
|
|
|
async findAll(
|
|
query: PaginationQueryDto<EnvironmentOrderBy> = {},
|
|
): Promise<PaginatedResult<EnvironmentEntity>> {
|
|
const page = query.page ?? 1;
|
|
const limit = query.limit ?? 20;
|
|
const orderBy = query.orderBy ?? "id";
|
|
const orderDir = query.orderDir ?? "ASC";
|
|
const [data, total] = await this.repo.findAndCount({
|
|
order: { [orderBy]: orderDir },
|
|
skip: (page - 1) * limit,
|
|
take: limit,
|
|
});
|
|
return { data, total, page, limit };
|
|
}
|
|
|
|
async findOne(id: string): Promise<EnvironmentEntity> {
|
|
const env = await this.repo.findOneBy({ id });
|
|
if (!env) throw new NotFoundException(`Environment ${id} not found`);
|
|
return env;
|
|
}
|
|
|
|
async update(
|
|
id: string,
|
|
dto: UpdateEnvironmentDto,
|
|
): Promise<EnvironmentEntity> {
|
|
const env = await this.findOne(id);
|
|
Object.assign(env, dto);
|
|
return this.repo.save(env);
|
|
}
|
|
|
|
async remove(id: string): Promise<void> {
|
|
await this.findOne(id);
|
|
await this.repo.delete(id);
|
|
}
|
|
|
|
exportEnvironment(env: EnvironmentEntity): EnvironmentExportDto {
|
|
return {
|
|
kind: "environment",
|
|
id: env.id,
|
|
name: env.name,
|
|
description: env.description ?? undefined,
|
|
data: env.data,
|
|
};
|
|
}
|
|
|
|
async importEnvironment(
|
|
dto: EnvironmentExportDto,
|
|
): Promise<EnvironmentEntity> {
|
|
if (dto.id) {
|
|
const existing = await this.repo.findOneBy({ id: dto.id });
|
|
if (existing) {
|
|
Object.assign(existing, {
|
|
name: dto.name,
|
|
description: dto.description ?? null,
|
|
data: dto.data,
|
|
});
|
|
return this.repo.save(existing);
|
|
}
|
|
return this.repo.save(
|
|
this.repo.create({
|
|
id: dto.id,
|
|
name: dto.name,
|
|
description: dto.description ?? null,
|
|
data: dto.data,
|
|
}),
|
|
);
|
|
}
|
|
const byName = await this.repo.findOneBy({ name: dto.name });
|
|
if (byName) {
|
|
Object.assign(byName, {
|
|
description: dto.description ?? null,
|
|
data: dto.data,
|
|
});
|
|
return this.repo.save(byName);
|
|
}
|
|
return this.repo.save(
|
|
this.repo.create({
|
|
name: dto.name,
|
|
description: dto.description ?? null,
|
|
data: dto.data,
|
|
}),
|
|
);
|
|
}
|
|
}
|