Skip to content

Commit

Permalink
format message
Browse files Browse the repository at this point in the history
  • Loading branch information
Kemosalamy committed May 9, 2024
1 parent d05b30e commit 2b81ea6
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
public class ContractCommandMap
extends CommandMap<ContractCommand<? extends CommandRequest, ? extends CommandResponse>> {


@Autowired ContractRepository contractRepository;

@Autowired ContractMilestoneRepository contractMilestoneRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import com.workup.contracts.models.Contract;
import com.workup.contracts.models.ContractMilestone;
import com.workup.shared.commands.contracts.requests.EvaluateMilestoneRequest;
import com.workup.shared.commands.contracts.requests.ProgressMilestoneRequest;
import com.workup.shared.commands.contracts.responses.EvaluateMilestoneResponse;
import com.workup.shared.commands.contracts.responses.ProgressMilestoneResponse;
import com.workup.shared.commands.payments.paymentrequest.requests.CreatePaymentRequestRequest;
import com.workup.shared.enums.HttpStatusCode;
import com.workup.shared.enums.ServiceQueueNames;
import com.workup.shared.enums.contracts.MilestoneState;

import java.util.Optional;
import java.util.UUID;

Expand All @@ -19,20 +16,21 @@ public class EvaluateMilestoneCommand
private EvaluateMilestoneResponse isValid(Optional<ContractMilestone> milestoneOptional) {
if (milestoneOptional.isEmpty())
return EvaluateMilestoneResponse.builder()
.withStatusCode(HttpStatusCode.BAD_REQUEST)
.withErrorMessage("Milestone is not found")
.build();
.withStatusCode(HttpStatusCode.BAD_REQUEST)
.withErrorMessage("Milestone is not found")
.build();
if (milestoneOptional.get().getStatus() != MilestoneState.IN_REVIEW)
return EvaluateMilestoneResponse.builder()
.withStatusCode(HttpStatusCode.BAD_REQUEST)
.withErrorMessage("Milestone cannot be evaluated as it has not progressed enough")
.build();
.withStatusCode(HttpStatusCode.BAD_REQUEST)
.withErrorMessage("Milestone cannot be evaluated as it has not progressed enough")
.build();
return null;
}

@Override
public EvaluateMilestoneResponse Run(EvaluateMilestoneRequest request) {
Optional<ContractMilestone> milestoneOptional =
contractMilestoneRepository.findById(UUID.fromString(request.getMilestoneId()));
contractMilestoneRepository.findById(UUID.fromString(request.getMilestoneId()));

EvaluateMilestoneResponse checkerResponse = isValid(milestoneOptional);
if (checkerResponse != null) return checkerResponse;
Expand All @@ -49,37 +47,35 @@ public EvaluateMilestoneResponse Run(EvaluateMilestoneRequest request) {
{
// Getting the contract as we need to send the freelancer and client id since they are
// in the payment request parameters.
Optional<Contract> contractOptional = contractRepository.findById(UUID.fromString(updatedMilestone.getContractId()));
if(contractOptional.isEmpty())
Optional<Contract> contractOptional =
contractRepository.findById(UUID.fromString(updatedMilestone.getContractId()));
if (contractOptional.isEmpty())
throw new Exception("Contract Optional was empty, therefore unable to fetch data");

Contract milestoneContract = contractOptional.get();

CreatePaymentRequestRequest externalRequest = CreatePaymentRequestRequest
.builder()
CreatePaymentRequestRequest externalRequest =
CreatePaymentRequestRequest.builder()
.withAmount(updatedMilestone.getAmount())
.withClientId(milestoneContract.getClientId())
.withFreelancerId(milestoneContract.getFreelancerId())
.withDescription(updatedMilestone.getMilestoneId().toString())
.build();
rabbitTemplate.convertAndSend(ServiceQueueNames.PAYMENTS,externalRequest);
rabbitTemplate.convertAndSend(ServiceQueueNames.PAYMENTS, externalRequest);
}

System.out.println(" [x] Payment request sent ");
return EvaluateMilestoneResponse.builder()
.withStatusCode(HttpStatusCode.OK)
.withErrorMessage("")
.build();
.withStatusCode(HttpStatusCode.OK)
.withErrorMessage("")
.build();

} catch (Exception e) {
e.printStackTrace();
return EvaluateMilestoneResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage(e.getMessage())
.build();
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage(e.getMessage())
.build();
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import com.workup.shared.commands.contracts.requests.GetContractRequest;
import com.workup.shared.commands.contracts.responses.GetContractResponse;
import com.workup.shared.enums.HttpStatusCode;
import com.workup.shared.redis.RedisService;

import java.util.Optional;
import java.util.UUID;

Expand All @@ -16,28 +14,28 @@ public class GetContractCommand extends ContractCommand<GetContractRequest, GetC
public GetContractResponse Run(GetContractRequest request) {
String cachingKey = request.getContractId();
GetContractResponse cachedResponse =
(GetContractResponse)
redisService.getValue(cachingKey, GetContractResponse.class);
(GetContractResponse) redisService.getValue(cachingKey, GetContractResponse.class);
if (cachedResponse != null) {
ContractsLogger.print("[x] Contract request response fetched from cache: " + cachedResponse.toString());
ContractsLogger.print(
"[x] Contract request response fetched from cache: " + cachedResponse.toString());

return cachedResponse;
}

Optional<Contract> contractOptional =
contractRepository.findById(UUID.fromString(request.getContractId()));

Optional<Contract> contractOptional = contractRepository.findById(UUID.fromString(request.getContractId()));

if(contractOptional.isEmpty()){
if (contractOptional.isEmpty()) {
return GetContractResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("Requested contract does not exist")
.build();
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("Requested contract does not exist")
.build();
}

Contract contract = contractOptional.get();

GetContractResponse response = GetContractResponse
.builder()
GetContractResponse response =
GetContractResponse.builder()
.withContractId(contract.getContractId().toString())
.withProposalId(contract.getProposalId())
.withJobId(contract.getJobId())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package com.workup.contracts.commands;

import com.workup.contracts.logger.ContractsLogger;
import com.workup.contracts.models.Contract;
import com.workup.contracts.models.ContractMilestone;
import com.workup.shared.commands.contracts.Milestone;
import com.workup.shared.commands.contracts.requests.GetMilestoneRequest;
import com.workup.shared.commands.contracts.responses.GetContractResponse;
import com.workup.shared.commands.contracts.responses.GetMilestoneResponse;
import com.workup.shared.enums.HttpStatusCode;

import java.util.Optional;
import java.util.UUID;

Expand All @@ -20,27 +16,28 @@ public GetMilestoneResponse Run(GetMilestoneRequest request) {
String cachingKey = request.getMilestoneId();

GetMilestoneResponse cachedResponse =
(GetMilestoneResponse)
redisService.getValue(cachingKey, GetMilestoneResponse.class);
(GetMilestoneResponse) redisService.getValue(cachingKey, GetMilestoneResponse.class);
if (cachedResponse != null) {
ContractsLogger.print("[x] Milestone request response fetched from cache: " + cachedResponse.toString());
ContractsLogger.print(
"[x] Milestone request response fetched from cache: " + cachedResponse.toString());

return cachedResponse;
}

Optional<ContractMilestone> milestoneOptional = contractMilestoneRepository.findById(UUID.fromString(request.getMilestoneId()));
Optional<ContractMilestone> milestoneOptional =
contractMilestoneRepository.findById(UUID.fromString(request.getMilestoneId()));

if(milestoneOptional.isEmpty()){
if (milestoneOptional.isEmpty()) {
return GetMilestoneResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("Requested milestone does not exist")
.build();
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("Requested milestone does not exist")
.build();
}

ContractMilestone milestone = milestoneOptional.get();

GetMilestoneResponse response = GetMilestoneResponse
.builder()
GetMilestoneResponse response =
GetMilestoneResponse.builder()
.withContractId(milestone.getContractId())
.withMilestoneId(milestone.getMilestoneId().toString())
.withAmount(milestone.getAmount())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package com.workup.contracts.commands;

import com.workup.contracts.logger.ContractsLogger;
import com.workup.contracts.models.ContractMilestone;
import com.workup.contracts.models.TerminationRequest;
import com.workup.shared.commands.contracts.requests.GetPendingTerminationsRequest;
import com.workup.shared.commands.contracts.responses.GetMilestoneResponse;
import com.workup.shared.commands.contracts.responses.GetPendingTerminationsResponse;
import com.workup.shared.enums.HttpStatusCode;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

public class GetPendingTerminationsCommand
extends ContractCommand<GetPendingTerminationsRequest, GetPendingTerminationsResponse> {
Expand All @@ -20,27 +15,29 @@ public GetPendingTerminationsResponse Run(GetPendingTerminationsRequest request)
String cachingKey = request.getContractId() + "/pending_terminations";

GetPendingTerminationsResponse cachedResponse =
(GetPendingTerminationsResponse)
redisService.getValue(cachingKey, GetPendingTerminationsResponse.class);
(GetPendingTerminationsResponse)
redisService.getValue(cachingKey, GetPendingTerminationsResponse.class);
if (cachedResponse != null) {
ContractsLogger.print("[x] Contract terminations response fetched from cache: " + cachedResponse.toString());
ContractsLogger.print(
"[x] Contract terminations response fetched from cache: " + cachedResponse.toString());

return cachedResponse;
}

List<TerminationRequest> terminationsList = terminationRequestRepository.findByContractId(request.getContractId());
List<TerminationRequest> terminationsList =
terminationRequestRepository.findByContractId(request.getContractId());

if (terminationsList.isEmpty()) {
return GetPendingTerminationsResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("No pending terminations exist")
.build();
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("No pending terminations exist")
.build();
}

TerminationRequest terminationRequest = terminationsList.getFirst();

GetPendingTerminationsResponse response = GetPendingTerminationsResponse
.builder()
GetPendingTerminationsResponse response =
GetPendingTerminationsResponse.builder()
.withRequestId(terminationRequest.getRequestId().toString())
.withRequesterId(terminationRequest.getRequesterId())
.withContractId(terminationRequest.getContractId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ private ProgressMilestoneResponse isValid(Optional<ContractMilestone> milestoneO
.build();

MilestoneState milestoneState = milestoneOptional.get().getStatus();
if (milestoneState != MilestoneState.OPEN
&& milestoneState != MilestoneState.IN_PROGRESS)
if (milestoneState != MilestoneState.OPEN && milestoneState != MilestoneState.IN_PROGRESS)
return ProgressMilestoneResponse.builder()
.withStatusCode(HttpStatusCode.BAD_REQUEST)
.withErrorMessage("Milestone cannot be progressed through this command")
Expand All @@ -31,7 +30,7 @@ private ProgressMilestoneResponse isValid(Optional<ContractMilestone> milestoneO
@Override
public ProgressMilestoneResponse Run(ProgressMilestoneRequest request) {
Optional<ContractMilestone> milestoneOptional =
contractMilestoneRepository.findById(UUID.fromString(request.getMilestoneId()));
contractMilestoneRepository.findById(UUID.fromString(request.getMilestoneId()));

ProgressMilestoneResponse checkerResponse = isValid(milestoneOptional);
if (checkerResponse != null) return checkerResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

public class ContractsLogger {

public static void print(String logMessage) {
System.out.println(logMessage);
}

public static void print(String logMessage) {
System.out.println(logMessage);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.workup.contracts.repositories;

import com.workup.contracts.models.ContractMilestone;
import com.workup.contracts.models.TerminationRequest;
import com.workup.shared.enums.contracts.TerminationRequestStatus;
import java.util.List;
Expand All @@ -19,5 +18,4 @@ List<TerminationRequest> findByRequesterIdAndContractIdAndStatus(

@Query("SELECT * FROM contracts_data.termination_requests WHERE contractid = ?0")
List<TerminationRequest> findByContractId(String contractId);

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package com.workup.shared.commands.contracts.responses;

import com.workup.shared.commands.CommandResponse;
import com.workup.shared.commands.contracts.Milestone;
import com.workup.shared.enums.contracts.MilestoneState;
import java.util.Date;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;

import java.util.Date;
import java.util.UUID;

@Getter
@SuperBuilder(setterPrefix = "with")
@Jacksonized
Expand Down

0 comments on commit 2b81ea6

Please sign in to comment.