From aae3b2f7bc17efd67575c7c27c298d2980f0cd7b Mon Sep 17 00:00:00 2001 From: JishnuGoyal Date: Sun, 1 Sep 2024 23:41:32 +0530 Subject: [PATCH] break down large function into chunks for readability and maintainability. --- .../quash/service/IntegrationService.java | 99 +++++++++++-------- 1 file changed, 59 insertions(+), 40 deletions(-) diff --git a/backend/src/main/java/com/quashbugs/quash/service/IntegrationService.java b/backend/src/main/java/com/quashbugs/quash/service/IntegrationService.java index 4553657..94d1cc2 100644 --- a/backend/src/main/java/com/quashbugs/quash/service/IntegrationService.java +++ b/backend/src/main/java/com/quashbugs/quash/service/IntegrationService.java @@ -56,51 +56,70 @@ public IntegrationService( } public Integration createOrUpdateIntegration(String type, User user, String accessToken, String refreshToken) throws Exception { - try { - var integrations = integrationRepository.findAllByOrganisation(organisationRepository.findByCreatedBy(user)); - Boolean integrationExists = !integrations.stream() - .filter(integration -> integration.getIntegrationType().equals(type)) - .collect(Collectors.toList()).isEmpty(); - if (integrationExists) { - Optional integration = integrations.stream() - .filter(integration1 -> integration1.getIntegrationType().equals(type)) - .findFirst(); - - var settings = integration.get().getSettings(); - if (type.equals(JIRA)) { - settings.put("integrationRefreshToken", refreshToken); - settings.put("expiryTime", getExpiryTime()); - } - settings.put("integrationAccessToken", accessToken); - - integration.get().setSettings(settings); - integrationRepository.save(integration.get()); - return integration.get(); - } + var organisation = getOrganisationByUser(user); + var existingIntegration = findExistingIntegration(type, organisation); - var organisation = teamMemberRepository.findByUser(user).getOrganisation(); - if (organisation == null) { - throw new Exception("Organisation not found"); - } - LinkedHashMap settings = new LinkedHashMap<>(); - if (type.equals(JIRA)) { - settings.put("integrationRefreshToken", refreshToken); - settings.put("expiryTime", getExpiryTime()); + if (existingIntegration.isPresent()) { + return updateExistingIntegration(existingIntegration.get(), type, accessToken, refreshToken); + } else { + return createNewIntegration(type, organisation, accessToken, refreshToken); } - settings.put("integrationAccessToken", accessToken); - var integration = Integration.builder() - .integrationType(type) - .settings(settings) - .createdAt(new Date()) - .updatedAt(new Date()) - .isActive(true) - .organisation(organisation).build(); - integrationRepository.save(integration); - return integration; } catch (Exception e) { - throw new Exception(e.getMessage()); + throw new Exception("Error creating or updating integration: " + e.getMessage()); + } + } + + private Organisation getOrganisationByUser(User user) throws Exception { + var organisation = teamMemberRepository.findByUser(user).getOrganisation(); + if (organisation == null) { + throw new Exception("Organisation not found"); + } + return organisation; + } + + private Optional findExistingIntegration(String type, Organisation organisation) { + var integrations = integrationRepository.findAllByOrganisation(organisation); + return integrations.stream() + .filter(integration -> integration.getIntegrationType().equals(type)) + .findFirst(); + } + + private Integration updateExistingIntegration(Integration integration, String type, String accessToken, String refreshToken) { + var settings = integration.getSettings(); + + if (type.equals(JIRA)) { + settings.put("integrationRefreshToken", refreshToken); + settings.put("expiryTime", getExpiryTime()); + } + settings.put("integrationAccessToken", accessToken); + + integration.setSettings(settings); + integration.setUpdatedAt(new Date()); + integrationRepository.save(integration); + return integration; + } + + private Integration createNewIntegration(String type, Organisation organisation, String accessToken, String refreshToken) { + LinkedHashMap settings = new LinkedHashMap<>(); + + if (type.equals(JIRA)) { + settings.put("integrationRefreshToken", refreshToken); + settings.put("expiryTime", getExpiryTime()); } + settings.put("integrationAccessToken", accessToken); + + var integration = Integration.builder() + .integrationType(type) + .settings(settings) + .createdAt(new Date()) + .updatedAt(new Date()) + .isActive(true) + .organisation(organisation) + .build(); + + integrationRepository.save(integration); + return integration; } private Date getExpiryTime() {