Skip to content

Commit

Permalink
Merge pull request #624 from code4romania/fix/438-1
Browse files Browse the repository at this point in the history
fix: [438] prevent errors while attempting to delete undefined reports
  • Loading branch information
dragos1195 authored Aug 29, 2024
2 parents ff8eb83 + 80d006d commit 098ab71
Showing 1 changed file with 48 additions and 32 deletions.
80 changes: 48 additions & 32 deletions backend/src/modules/organization/services/organization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import { OrganizationReportService } from './organization-report.service';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { EVENTS } from 'src/modules/notifications/constants/events.contants';
import RestrictOngEvent from 'src/modules/notifications/events/restrict-ong-event.class';
import { isBefore } from 'date-fns';
import * as Sentry from '@sentry/node';

@Injectable()
export class OrganizationService {
Expand Down Expand Up @@ -1304,45 +1304,59 @@ export class OrganizationService {
await queryRunner.manager.delete(Organization, organizationId);

// 2. delete report
const reportIds = organization.organizationReport.reports.map(
(report) => report.id,
);
await queryRunner.manager.delete(Report, reportIds);
if (organization.organizationReport?.reports.length) {
const reportIds = organization.organizationReport.reports.map(
(report) => report.id,
);
await queryRunner.manager.delete(Report, reportIds);
}

const inverstorIds = organization.organizationReport.investors.map(
(investor) => investor.id,
);
await queryRunner.manager.delete(Investor, inverstorIds);
if (organization.organizationReport?.investors.length) {
const inverstorIds = organization.organizationReport.investors.map(
(investor) => investor.id,
);
await queryRunner.manager.delete(Investor, inverstorIds);
}

const partnerIds = organization.organizationReport.partners.map(
(partner) => partner.id,
);
await queryRunner.manager.delete(Partner, partnerIds);
if (organization.organizationReport?.partners.length) {
const partnerIds = organization.organizationReport.partners.map(
(partner) => partner.id,
);
await queryRunner.manager.delete(Partner, partnerIds);
}

await queryRunner.manager.delete(
OrganizationReport,
organization.organizationReportId,
);
if (organization.organizationReportId) {
await queryRunner.manager.delete(
OrganizationReport,
organization.organizationReportId,
);
}

// 3. delete financial
const organizationFinancialIds = organization.organizationFinancial.map(
(financial) => financial.id,
);
await queryRunner.manager.delete(
OrganizationFinancial,
organizationFinancialIds,
);
if (organization.organizationFinancial.length) {
const organizationFinancialIds = organization.organizationFinancial.map(
(financial) => financial.id,
);
await queryRunner.manager.delete(
OrganizationFinancial,
organizationFinancialIds,
);
}

// 4. delete delete legal
await queryRunner.manager.delete(
Contact,
organization.organizationLegal.legalReprezentativeId,
);
if (organization.organizationLegal.legalReprezentativeId) {
await queryRunner.manager.delete(
Contact,
organization.organizationLegal.legalReprezentativeId,
);
}

const directorsIds = organization.organizationLegal.directors.map(
(director) => director.id,
);
await queryRunner.manager.delete(Contact, directorsIds);
if (organization.organizationLegal.directors.length) {
const directorsIds = organization.organizationLegal.directors.map(
(director) => director.id,
);
await queryRunner.manager.delete(Contact, directorsIds);
}

await queryRunner.manager.delete(
OrganizationLegal,
Expand All @@ -1366,6 +1380,8 @@ export class OrganizationService {
// since we have errors lets rollback the changes we made
await queryRunner.rollbackTransaction();

Sentry.captureException(error);

this.logger.error({
error: { error },
...ORGANIZATION_ERRORS.DELETE.ONG,
Expand Down

0 comments on commit 098ab71

Please sign in to comment.