diff --git a/onecgiar-pr-server/src/api/delete-recover-data/delete-recover-data.service.ts b/onecgiar-pr-server/src/api/delete-recover-data/delete-recover-data.service.ts index 636e8b09c..b42af47a6 100644 --- a/onecgiar-pr-server/src/api/delete-recover-data/delete-recover-data.service.ts +++ b/onecgiar-pr-server/src/api/delete-recover-data/delete-recover-data.service.ts @@ -3,7 +3,6 @@ import { ReturnResponse, ReturnResponseDto, } from '../../shared/handlers/error.utils'; - import { IpsrRepository } from '../ipsr/ipsr.repository'; import { InnovationPackagingExpertRepository } from '../ipsr/innovation-packaging-experts/repositories/innovation-packaging-expert.repository'; import { ResultIpExpertisesRepository } from '../ipsr/innovation-packaging-experts/repositories/result-ip-expertises.repository'; @@ -76,6 +75,8 @@ import { ResultCountriesSubNationalRepository } from '../results/result-countrie import { KnowledgeProductFairBaselineRepository } from '../results/knowledge_product_fair_baseline/knowledge_product_fair_baseline.repository'; import { EvidenceTypeEnum } from '../../shared/constants/evidence-type.enum'; import { EnvironmentExtractor } from '../../shared/utils/environment-extractor'; +import { ResultsByInstitution } from '../results/results_by_institutions/entities/results_by_institution.entity'; +import { ResultInstitutionsBudget } from '../results/result_budget/entities/result_institutions_budget.entity'; @Injectable() export class DeleteRecoverDataService { @@ -417,6 +418,7 @@ export class DeleteRecoverDataService { resultAfterbefore, new_result_level_id, new_result_type_id, + user, ); //updating elastic search @@ -478,6 +480,7 @@ export class DeleteRecoverDataService { result: Result, new_result_level: ResultLevelEnum, new_result_type: ResultTypeEnum, + user: TokenDto, ): Promise> { try { const returnDelete = await this.deleteDataByNewResultType( @@ -502,6 +505,7 @@ export class DeleteRecoverDataService { new_result_level, result.result_type_id, result.result_level_id, + user, ); if (returnMigration.statusCode >= 300) { @@ -542,6 +546,7 @@ export class DeleteRecoverDataService { _new_result_level: ResultLevelEnum, old_result_type: ResultTypeEnum, _old_result_level: ResultLevelEnum, + user: TokenDto, ) { try { if (ResultTypeEnum.KNOWLEDGE_PRODUCT == old_result_type) { @@ -563,6 +568,29 @@ export class DeleteRecoverDataService { }); } + if (new_result_type == ResultTypeEnum.INNOVATION_DEVELOPMENT) { + const partners: ResultsByInstitution[] = + await this._resultByIntitutionsRepository.find({ + where: { + result_id, + institution_roles_id: InstitutionRoleEnum.PARTNER, + is_active: true, + }, + }); + + const partnerBudgets: ResultInstitutionsBudget[] = partners.map((p) => { + const partnerBudget = new ResultInstitutionsBudget(); + + partnerBudget.result_institution_id = p.id; + partnerBudget.created_by = user.id; + partnerBudget.is_active = true; + + return partnerBudget; + }); + + await this._resultInstitutionsBudgetRepository.save(partnerBudgets); + } + return this._returnResponse.format({ message: `The result with code ${result_id} has been migrated`, response: result_id, diff --git a/onecgiar-pr-server/src/api/results/results_by_institutions/results_by_institutions.service.ts b/onecgiar-pr-server/src/api/results/results_by_institutions/results_by_institutions.service.ts index 0544d7046..d05f22a55 100644 --- a/onecgiar-pr-server/src/api/results/results_by_institutions/results_by_institutions.service.ts +++ b/onecgiar-pr-server/src/api/results/results_by_institutions/results_by_institutions.service.ts @@ -227,7 +227,12 @@ export class ResultsByInstitutionsService { try { const incomingResult = await this._resultRepository.findOne({ where: { id: data.result_id }, - relations: { result_by_institution_array: { delivery: true } }, + relations: { + result_by_institution_array: { + delivery: true, + result_institution_budget_array: true, + }, + }, }); if (!incomingResult) { throw { @@ -660,6 +665,7 @@ export class ResultsByInstitutionsService { const toUpdate = oldInstitutions.filter( (i) => !removed.some((r) => r.id === i.id), ); + const updatedNewBudgets: ResultInstitutionsBudget[] = []; for (const institutionToUpdate of toUpdate) { const newData = incomingInstitutions.find( @@ -671,9 +677,46 @@ export class ResultsByInstitutionsService { institutionToUpdate.institutions_id = newData.institutions_id; institutionToUpdate.is_leading_result = newData.is_leading_result; } + + if (isInnoDev) { + if (institutionToUpdate.result_institution_budget_array?.length > 0) { + const activeBudgets = + institutionToUpdate.result_institution_budget_array.filter( + (b) => b.is_active, + ); + let workingBudget: ResultInstitutionsBudget; + if (activeBudgets?.length > 0) { + workingBudget = activeBudgets.shift(); + + activeBudgets.forEach((b) => { + b.is_active = false; + b.last_updated_by = userId; + updatedNewBudgets.push(b); + }); + } else { + workingBudget = new ResultInstitutionsBudget(); + + workingBudget.created_by = userId; + workingBudget.result_institution_id = institutionToUpdate.id; + } + + workingBudget.is_active = true; + workingBudget.last_updated_by = userId; + updatedNewBudgets.push(workingBudget); + } else { + const newBudget = new ResultInstitutionsBudget(); + + newBudget.created_by = userId; + newBudget.result_institution_id = institutionToUpdate.id; + newBudget.is_active = true; + + updatedNewBudgets.push(newBudget); + } + } } await this._resultByIntitutionsRepository.save(toUpdate); + await this._resultInstitutionsBudgetRepository.save(updatedNewBudgets); //handling deliveries from added and updated result_by_institutions for (const toUpdateDeliveries of toUpdate.concat(added)) {