260316:1347 Refactor to NestJS 11 #2
Build and Deploy / deploy (push) Failing after 2m19s

This commit is contained in:
admin
2026-03-16 13:47:35 +07:00
parent a75ba3105f
commit f13861f02e
17 changed files with 544 additions and 563 deletions
@@ -3,20 +3,45 @@ import {
NotFoundException,
ConflictException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Like } from 'typeorm';
import { InjectRepository, InjectEntityManager } from '@nestjs/typeorm';
import { Repository, Like, EntityManager } from 'typeorm';
import { Contract } from './entities/contract.entity';
import { CreateContractDto } from './dto/create-contract.dto.js';
import { UpdateContractDto } from './dto/update-contract.dto.js';
import { Project } from '../project/entities/project.entity';
@Injectable()
export class ContractService {
constructor(
@InjectRepository(Contract)
private readonly contractRepo: Repository<Contract>
private readonly contractRepo: Repository<Contract>,
@InjectEntityManager()
private readonly entityManager: EntityManager
) {}
/**
* Helper to resolve projectId (ID or UUID) to internal INT ID
*/
async resolveProjectId(projectId: number | string): Promise<number> {
if (typeof projectId === 'number') return projectId;
const num = Number(projectId);
if (!isNaN(num)) return num;
const project = await this.entityManager.findOne(Project, {
where: { uuid: projectId as string },
select: ['id'],
});
if (!project) {
throw new NotFoundException(`Project with UUID ${projectId} not found`);
}
return project.id;
}
async create(dto: CreateContractDto) {
const internalProjectId = await this.resolveProjectId(dto.projectId);
const existing = await this.contractRepo.findOne({
where: { contractCode: dto.contractCode },
});
@@ -25,7 +50,7 @@ export class ContractService {
`Contract Code "${dto.contractCode}" already exists`
);
}
const contract = this.contractRepo.create(dto);
const contract = this.contractRepo.create({ ...dto, projectId: internalProjectId });
return this.contractRepo.save(contract);
}
@@ -33,6 +58,11 @@ export class ContractService {
const { search, projectId, page = 1, limit = 100 } = params || {};
const skip = (page - 1) * limit;
let internalProjectId = undefined;
if (projectId) {
internalProjectId = await this.resolveProjectId(projectId);
}
const findOptions: any = {
relations: ['project'],
order: { contractCode: 'ASC' },
@@ -47,21 +77,20 @@ export class ContractService {
searchConditions.push({ contractName: Like(`%${search}%`) });
}
if (projectId) {
// Combine project filter with search if exists
if (internalProjectId) {
if (searchConditions.length > 0) {
findOptions.where = searchConditions.map((cond) => ({
...cond,
projectId,
projectId: internalProjectId,
}));
} else {
findOptions.where = { projectId };
findOptions.where = { projectId: internalProjectId };
}
} else {
if (searchConditions.length > 0) {
findOptions.where = searchConditions;
} else {
delete findOptions.where; // No filters
delete findOptions.where;
}
}
@@ -99,6 +128,9 @@ export class ContractService {
async update(uuid: string, dto: UpdateContractDto) {
const contract = await this.findOneByUuid(uuid);
if (dto.projectId) {
dto.projectId = await this.resolveProjectId(dto.projectId);
}
Object.assign(contract, dto);
return this.contractRepo.save(contract);
}