|
@@ -1,40 +1,53 @@
|
|
|
-import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common';
|
|
|
|
|
|
|
+import { Injectable, NotFoundException, BadRequestException, Inject, forwardRef } from '@nestjs/common';
|
|
|
|
|
+
|
|
|
import { MongoCoreService } from '../../mongo/mongo-core.service';
|
|
import { MongoCoreService } from '../../mongo/mongo-core.service';
|
|
|
import { BlockRepository } from '../repositories/block.repository';
|
|
import { BlockRepository } from '../repositories/block.repository';
|
|
|
import { Block } from '../schemas/site.schema';
|
|
import { Block } from '../schemas/site.schema';
|
|
|
import { PhaseRepository } from '../repositories/phase.repository';
|
|
import { PhaseRepository } from '../repositories/phase.repository';
|
|
|
|
|
+import { SiteRepository } from '../repositories/site.repository';
|
|
|
import { FFBProductionService } from '../../FFB/services/ffb-production.service';
|
|
import { FFBProductionService } from '../../FFB/services/ffb-production.service';
|
|
|
|
|
|
|
|
@Injectable()
|
|
@Injectable()
|
|
|
export class BlockService {
|
|
export class BlockService {
|
|
|
private repo: BlockRepository;
|
|
private repo: BlockRepository;
|
|
|
private phaseRepo: PhaseRepository;
|
|
private phaseRepo: PhaseRepository;
|
|
|
|
|
+ private siteRepo: SiteRepository;
|
|
|
|
|
|
|
|
constructor(
|
|
constructor(
|
|
|
private readonly mongoCore: MongoCoreService,
|
|
private readonly mongoCore: MongoCoreService,
|
|
|
|
|
+ @Inject(forwardRef(() => FFBProductionService))
|
|
|
private readonly ffbService: FFBProductionService,
|
|
private readonly ffbService: FFBProductionService,
|
|
|
) { }
|
|
) { }
|
|
|
|
|
|
|
|
|
|
+
|
|
|
private async getRepos() {
|
|
private async getRepos() {
|
|
|
if (!this.repo) {
|
|
if (!this.repo) {
|
|
|
const db = await this.mongoCore.getDb();
|
|
const db = await this.mongoCore.getDb();
|
|
|
this.repo = new BlockRepository(db);
|
|
this.repo = new BlockRepository(db);
|
|
|
this.phaseRepo = new PhaseRepository(db);
|
|
this.phaseRepo = new PhaseRepository(db);
|
|
|
|
|
+ this.siteRepo = new SiteRepository(db);
|
|
|
await this.repo.init();
|
|
await this.repo.init();
|
|
|
await this.phaseRepo.init();
|
|
await this.phaseRepo.init();
|
|
|
|
|
+ await this.siteRepo.init();
|
|
|
}
|
|
}
|
|
|
- return { repo: this.repo, phaseRepo: this.phaseRepo };
|
|
|
|
|
|
|
+ return { repo: this.repo, phaseRepo: this.phaseRepo, siteRepo: this.siteRepo };
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async create(block: Block): Promise<Block> {
|
|
async create(block: Block): Promise<Block> {
|
|
|
- const { repo, phaseRepo } = await this.getRepos();
|
|
|
|
|
|
|
+ const { repo, phaseRepo, siteRepo } = await this.getRepos();
|
|
|
|
|
|
|
|
- // Parent Validation: Check if Phase exists
|
|
|
|
|
|
|
+ // 1. Validate Phase exists
|
|
|
const phase = await phaseRepo.findById(block.phaseId);
|
|
const phase = await phaseRepo.findById(block.phaseId);
|
|
|
if (!phase) {
|
|
if (!phase) {
|
|
|
throw new BadRequestException(`Phase with ID ${block.phaseId} does not exist.`);
|
|
throw new BadRequestException(`Phase with ID ${block.phaseId} does not exist.`);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 2. Validate Site exists
|
|
|
|
|
+ const site = await siteRepo.findById(phase.siteId);
|
|
|
|
|
+ if (!site) {
|
|
|
|
|
+ throw new BadRequestException(`Site with ID ${phase.siteId} (from Phase ${block.phaseId}) does not exist.`);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return repo.create(block);
|
|
return repo.create(block);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -49,14 +62,19 @@ export class BlockService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async update(id: string, update: Partial<Block>): Promise<void> {
|
|
async update(id: string, update: Partial<Block>): Promise<void> {
|
|
|
- const { repo, phaseRepo } = await this.getRepos();
|
|
|
|
|
|
|
+ const { repo, phaseRepo, siteRepo } = await this.getRepos();
|
|
|
|
|
|
|
|
const block = await repo.findById(id);
|
|
const block = await repo.findById(id);
|
|
|
if (!block) throw new NotFoundException('Block not found');
|
|
if (!block) throw new NotFoundException('Block not found');
|
|
|
|
|
|
|
|
if (update.phaseId) {
|
|
if (update.phaseId) {
|
|
|
|
|
+ // 1. Validate Phase exists
|
|
|
const phase = await phaseRepo.findById(update.phaseId);
|
|
const phase = await phaseRepo.findById(update.phaseId);
|
|
|
if (!phase) throw new BadRequestException(`Phase with ID ${update.phaseId} does not exist.`);
|
|
if (!phase) throw new BadRequestException(`Phase with ID ${update.phaseId} does not exist.`);
|
|
|
|
|
+
|
|
|
|
|
+ // 2. Validate Site exists
|
|
|
|
|
+ const site = await siteRepo.findById(phase.siteId);
|
|
|
|
|
+ if (!site) throw new BadRequestException(`Site with ID ${phase.siteId} (from Phase ${update.phaseId}) does not exist.`);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
await repo.update(id, update);
|
|
await repo.update(id, update);
|