Skip to content

Commit

Permalink
feat(api): Refactor importDataProvidedByPartner and add event logs
Browse files Browse the repository at this point in the history
  • Loading branch information
alepefe committed Jan 16, 2025
1 parent 61c9c34 commit f0bcfe2
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions api/src/modules/import/import.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,34 +85,46 @@ export class ImportService {
}

async importDataProvidedByPartner(fileBuffers: Buffer[], userId: string) {
// TODO: Debt, add event handling
const { costInputs, carbonInputs } =
await this.excelParser.parseUserExcels(fileBuffers);
const mappedCostInputs = userDataCostInputsMapJsonToEntity(
costInputs,
userId,
);
const mappedRestorationInputs = userDataRestorationInputMapJsonToEntity(
carbonInputs.restoration,
userId,
);
const mappedConservationInputs = userDataConservationInputMapJsonToEntity(
carbonInputs.conservation,
userId,
);
await this.dataSource.transaction(async (manager) => {
const userCostInputsRepo = manager.getRepository(UserUploadCostInputs);
const userRestorationInputsRepo = manager.getRepository(
UserUploadRestorationInputs,
this.logger.warn('importDataProvidedByPartner started...');
this.registerImportEvent(userId, this.eventMap.STARTED);

try {
const { costInputs, carbonInputs } =
await this.excelParser.parseUserExcels(fileBuffers);
const mappedCostInputs = userDataCostInputsMapJsonToEntity(
costInputs,
userId,
);
const userConservationInputsRepo = manager.getRepository(
UserUploadConservationInputs,
const mappedRestorationInputs = userDataRestorationInputMapJsonToEntity(
carbonInputs.restoration,
userId,
);
await userCostInputsRepo.save(mappedCostInputs);
await userRestorationInputsRepo.save(mappedRestorationInputs);
await userConservationInputsRepo.save(mappedConservationInputs);
});
const mappedConservationInputs = userDataConservationInputMapJsonToEntity(
carbonInputs.conservation,
userId,
);
await this.dataSource.transaction(async (manager) => {
const userCostInputsRepo = manager.getRepository(UserUploadCostInputs);
const userRestorationInputsRepo = manager.getRepository(
UserUploadRestorationInputs,
);
const userConservationInputsRepo = manager.getRepository(
UserUploadConservationInputs,
);
await userCostInputsRepo.save(mappedCostInputs);
await userRestorationInputsRepo.save(mappedRestorationInputs);
await userConservationInputsRepo.save(mappedConservationInputs);
});

return carbonInputs;
this.logger.warn('importDataProvidedByPartner completed successfully');
this.registerImportEvent(userId, this.eventMap.SUCCESS);
return carbonInputs;
} catch (e) {
this.logger.error('importDataProvidedByPartner failed', e);
this.registerImportEvent(userId, this.eventMap.FAILED, {
error: { type: e.constructor.name, message: e.message },
});
throw new ConflictException(e);
}
}
}

0 comments on commit f0bcfe2

Please sign in to comment.