Skip to content

Commit

Permalink
Merge pull request #363 from AllianceBioversityCIAT/staging
Browse files Browse the repository at this point in the history
🐛fix(Budget): saving missing budget on section 3 save and result type change
  • Loading branch information
davinzifc authored Dec 4, 2024
2 parents 21b2dee + 378d28f commit edf3b7d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -417,6 +418,7 @@ export class DeleteRecoverDataService {
resultAfterbefore,
new_result_level_id,
new_result_type_id,
user,
);

//updating elastic search
Expand Down Expand Up @@ -478,6 +480,7 @@ export class DeleteRecoverDataService {
result: Result,
new_result_level: ResultLevelEnum,
new_result_type: ResultTypeEnum,
user: TokenDto,
): Promise<ReturnResponseDto<any>> {
try {
const returnDelete = await this.deleteDataByNewResultType(
Expand All @@ -502,6 +505,7 @@ export class DeleteRecoverDataService {
new_result_level,
result.result_type_id,
result.result_level_id,
user,
);

if (returnMigration.statusCode >= 300) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -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)) {
Expand Down

0 comments on commit edf3b7d

Please sign in to comment.