import { Inject, Injectable,BadRequestException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { ClockIDGenService } from 'src/utils/service/clockIDGenUtil.service'; import { EntityManager, IsNull, Repository } from 'typeorm'; import { OrganizationData } from '../entity/organization.entity'; // import { BrandRepository } from '../repository/brand.repository'; import { WBSCodeGenService } from 'src/utils/service/wbsCodeGen.service'; import { SchoolRepository } from '../repository/school.repository'; @Injectable() export class OrganizationService { constructor( @InjectRepository(OrganizationData) private readonly organizationRepository: Repository, private readonly idGen: ClockIDGenService, private readonly wbcCodeGenService: WBSCodeGenService, private readonly schoolRepository: SchoolRepository, ) {} async create( organizationDto: Partial, loggedInUser?: any, manager?: EntityManager, ): Promise { const repo = this.organizationRepository; const isBrand = organizationDto.type === 'BRN'; organizationDto.enterprise_id = loggedInUser.enterprise_id; if (isBrand) { // BRN case const parentOrgId = organizationDto.parent_id; if (!parentOrgId) { throw new Error('Parent Org ID is required to create a brand.'); } const parentOrg = await repo.findOne({ where: { id: parentOrgId }, }); if (!parentOrg) { throw new Error('Parent organization not found.'); } const existingBrands = await repo.find({ where: { type: 'BRN', parent_id: parentOrgId, }, select: ['wbs_code'], }); const brandCodes = existingBrands .map((b) => b.wbs_code?.split('.')[1]) .filter(Boolean) .map(Number); const nextBrnCode = brandCodes.length ? Math.max(...brandCodes) + 1 : 1; const brnWbsCode = this.wbcCodeGenService.padCode(nextBrnCode); organizationDto.wbs_code = `${parentOrg.wbs_code}.${brnWbsCode}`; } else { // ORG case const existingOrgs = await repo.find({ where: { type: IsNull(), }, select: ['wbs_code'], }); const orgCodes = existingOrgs .map((org) => org.wbs_code) .filter(Boolean) .map(Number); const nextOrgCode = orgCodes.length ? Math.max(...orgCodes) + 1 : 1; organizationDto.wbs_code = this.wbcCodeGenService.padCode(nextOrgCode); } organizationDto.code = this.idGen.idGenerator('ORG'); // organizationDto.status = 'ACTIVE'; organizationDto.created_date = new Date(); const organization = repo.create(organizationDto); return await repo.save(organization); } async findAll(manager?: EntityManager): Promise { const repo = manager ? manager.getRepository(OrganizationData) : this.organizationRepository; return await repo.find(); } async findOne( id: number, manager?: EntityManager, ): Promise { const repo = manager ? manager.getRepository(OrganizationData) : this.organizationRepository; return await repo.findOne({ where: { id } }); } async findById(id: number | undefined) { return await this.organizationRepository.find({ where: { id } }); } // async update( // id: number, // organizationDto: Partial, // // manager?: EntityManager, // ): Promise { // // const repo = manager // // ? manager.getRepository(OrganizationData) // // : this.organizationRepository; // const repo = this.organizationRepository; // await repo.update(id, organizationDto); // return await repo.findOne({ where: { id } }); // } async update( organizationDto: Partial, userData: any, ): Promise { const orgId = typeof organizationDto.id === 'number' ? organizationDto.id : parseInt(String(organizationDto.id).trim(), 10); if (!Number.isInteger(orgId)) { throw new BadRequestException('Invalid organization id'); } const { id: _ignoreId, sessionToken, appcode, email_id, level_id, level_type, ...dbData } = organizationDto as any; if (!dbData || Object.keys(dbData).length === 0) { throw new BadRequestException('No valid fields to update'); } await this.organizationRepository.update(orgId, dbData); return this.organizationRepository.findOne({ where: { id: orgId } }); } async remove(id: number, manager?: EntityManager): Promise { const repo = manager ? manager.getRepository(OrganizationData) : this.organizationRepository; await repo.delete(id); } async getOrganizationHierarchy( userId: number, appCode: string, orgId: number, ) { return await this.schoolRepository.getUserContextDropdown( userId, appCode, orgId, ); } }