-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contracts commands cache implementation (#82)
* working on added basic boilerplate * formatting changes * working on progress milestone command * working on progress milestone command * worked on implementing remaining contracts commands, added caching * format message * small fix to use send and receive instead of just send * fixed small bug * adding testing * formating * replaced every sys out with the logger template --------- Co-authored-by: HusseinYasser <husseinyasser388@gmail.com>
- Loading branch information
1 parent
3e534ed
commit fda9c13
Showing
26 changed files
with
613 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
services/contracts/src/main/java/com/workup/contracts/commands/EvaluateMilestoneCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
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.requests.EvaluateMilestoneRequest; | ||
import com.workup.shared.commands.contracts.responses.EvaluateMilestoneResponse; | ||
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; | ||
|
||
public class EvaluateMilestoneCommand | ||
extends ContractCommand<EvaluateMilestoneRequest, EvaluateMilestoneResponse> { | ||
private EvaluateMilestoneResponse isValid(Optional<ContractMilestone> milestoneOptional) { | ||
if (milestoneOptional.isEmpty()) | ||
return EvaluateMilestoneResponse.builder() | ||
.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(); | ||
return null; | ||
} | ||
|
||
@Override | ||
public EvaluateMilestoneResponse Run(EvaluateMilestoneRequest request) { | ||
Optional<ContractMilestone> milestoneOptional = | ||
contractMilestoneRepository.findById(UUID.fromString(request.getMilestoneId())); | ||
|
||
EvaluateMilestoneResponse checkerResponse = isValid(milestoneOptional); | ||
if (checkerResponse != null) return checkerResponse; | ||
|
||
ContractMilestone updatedMilestone = milestoneOptional.get(); | ||
|
||
updatedMilestone.setStatus(request.getEvaluatedState()); | ||
|
||
try { | ||
contractMilestoneRepository.save(updatedMilestone); | ||
ContractsLogger.print(" [x] Milestone evaluated " + updatedMilestone); | ||
|
||
if (request.getEvaluatedState() == MilestoneState.ACCEPTED) { | ||
ContractsLogger.print(" [x] Sending payment 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()) | ||
throw new Exception("Contract Optional was empty, therefore unable to fetch data"); | ||
|
||
Contract milestoneContract = contractOptional.get(); | ||
|
||
CreatePaymentRequestRequest externalRequest = | ||
CreatePaymentRequestRequest.builder() | ||
.withAmount(updatedMilestone.getAmount()) | ||
.withClientId(milestoneContract.getClientId()) | ||
.withFreelancerId(milestoneContract.getFreelancerId()) | ||
.withDescription(updatedMilestone.getMilestoneId().toString()) | ||
.build(); | ||
rabbitTemplate.convertSendAndReceive(ServiceQueueNames.PAYMENTS, externalRequest); | ||
ContractsLogger.print(" [x] Payment request sent "); | ||
} | ||
|
||
return EvaluateMilestoneResponse.builder() | ||
.withStatusCode(HttpStatusCode.OK) | ||
.withErrorMessage("") | ||
.build(); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return EvaluateMilestoneResponse.builder() | ||
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR) | ||
.withErrorMessage(e.getMessage()) | ||
.build(); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
services/contracts/src/main/java/com/workup/contracts/commands/GetContractCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.workup.contracts.commands; | ||
|
||
import com.workup.contracts.logger.ContractsLogger; | ||
import com.workup.contracts.models.Contract; | ||
import com.workup.shared.commands.contracts.requests.GetContractRequest; | ||
import com.workup.shared.commands.contracts.responses.GetContractResponse; | ||
import com.workup.shared.enums.HttpStatusCode; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
public class GetContractCommand extends ContractCommand<GetContractRequest, GetContractResponse> { | ||
|
||
@Override | ||
public GetContractResponse Run(GetContractRequest request) { | ||
String cachingKey = request.getContractId(); | ||
GetContractResponse cachedResponse = | ||
(GetContractResponse) redisService.getValue(cachingKey, GetContractResponse.class); | ||
if (cachedResponse != null) { | ||
ContractsLogger.print( | ||
"[x] Contract request response fetched from cache: " + cachedResponse.toString()); | ||
|
||
return cachedResponse; | ||
} | ||
|
||
Optional<Contract> contractOptional = | ||
contractRepository.findById(UUID.fromString(request.getContractId())); | ||
|
||
if (contractOptional.isEmpty()) { | ||
return GetContractResponse.builder() | ||
.withStatusCode(HttpStatusCode.NOT_FOUND) | ||
.withErrorMessage("Requested contract does not exist") | ||
.build(); | ||
} | ||
|
||
Contract contract = contractOptional.get(); | ||
|
||
GetContractResponse response = | ||
GetContractResponse.builder() | ||
.withContractId(contract.getContractId().toString()) | ||
.withProposalId(contract.getProposalId()) | ||
.withJobId(contract.getJobId()) | ||
.withJobTitle(contract.getJobTitle()) | ||
.withClientId(contract.getClientId()) | ||
.withFreelancerId(contract.getFreelancerId()) | ||
.withMilestonesIds(contract.getMilestonesIds()) | ||
.withStatus(contract.getStatus()) | ||
.withStatusCode(HttpStatusCode.OK) | ||
.withErrorMessage("") | ||
.build(); | ||
|
||
redisService.setValue(cachingKey, response); | ||
return response; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
services/contracts/src/main/java/com/workup/contracts/commands/GetMilestoneCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.workup.contracts.commands; | ||
|
||
import com.workup.contracts.logger.ContractsLogger; | ||
import com.workup.contracts.models.ContractMilestone; | ||
import com.workup.shared.commands.contracts.requests.GetMilestoneRequest; | ||
import com.workup.shared.commands.contracts.responses.GetMilestoneResponse; | ||
import com.workup.shared.enums.HttpStatusCode; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
public class GetMilestoneCommand | ||
extends ContractCommand<GetMilestoneRequest, GetMilestoneResponse> { | ||
|
||
@Override | ||
public GetMilestoneResponse Run(GetMilestoneRequest request) { | ||
String cachingKey = request.getMilestoneId(); | ||
|
||
GetMilestoneResponse cachedResponse = | ||
(GetMilestoneResponse) redisService.getValue(cachingKey, GetMilestoneResponse.class); | ||
if (cachedResponse != null) { | ||
ContractsLogger.print( | ||
"[x] Milestone request response fetched from cache: " + cachedResponse.toString()); | ||
|
||
return cachedResponse; | ||
} | ||
|
||
Optional<ContractMilestone> milestoneOptional = | ||
contractMilestoneRepository.findById(UUID.fromString(request.getMilestoneId())); | ||
|
||
if (milestoneOptional.isEmpty()) { | ||
return GetMilestoneResponse.builder() | ||
.withStatusCode(HttpStatusCode.NOT_FOUND) | ||
.withErrorMessage("Requested milestone does not exist") | ||
.build(); | ||
} | ||
|
||
ContractMilestone milestone = milestoneOptional.get(); | ||
|
||
GetMilestoneResponse response = | ||
GetMilestoneResponse.builder() | ||
.withContractId(milestone.getContractId()) | ||
.withMilestoneId(milestone.getMilestoneId().toString()) | ||
.withAmount(milestone.getAmount()) | ||
.withDescription(milestone.getDescription()) | ||
.withDueDate(milestone.getDueDate()) | ||
.withAmount(milestone.getAmount()) | ||
.withStatusCode(HttpStatusCode.OK) | ||
.withErrorMessage("") | ||
.build(); | ||
|
||
redisService.setValue(cachingKey, response); | ||
return response; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
.../contracts/src/main/java/com/workup/contracts/commands/GetPendingTerminationsCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.workup.contracts.commands; | ||
|
||
import com.workup.contracts.logger.ContractsLogger; | ||
import com.workup.contracts.models.TerminationRequest; | ||
import com.workup.shared.commands.contracts.requests.GetPendingTerminationsRequest; | ||
import com.workup.shared.commands.contracts.responses.GetPendingTerminationsResponse; | ||
import com.workup.shared.enums.HttpStatusCode; | ||
import java.util.List; | ||
|
||
public class GetPendingTerminationsCommand | ||
extends ContractCommand<GetPendingTerminationsRequest, GetPendingTerminationsResponse> { | ||
|
||
@Override | ||
public GetPendingTerminationsResponse Run(GetPendingTerminationsRequest request) { | ||
String cachingKey = request.getContractId() + "/pending_terminations"; | ||
|
||
GetPendingTerminationsResponse cachedResponse = | ||
(GetPendingTerminationsResponse) | ||
redisService.getValue(cachingKey, GetPendingTerminationsResponse.class); | ||
if (cachedResponse != null) { | ||
ContractsLogger.print( | ||
"[x] Contract terminations response fetched from cache: " + cachedResponse.toString()); | ||
|
||
return cachedResponse; | ||
} | ||
|
||
List<TerminationRequest> terminationsList = | ||
terminationRequestRepository.findByContractId(request.getContractId()); | ||
|
||
if (terminationsList.isEmpty()) { | ||
return GetPendingTerminationsResponse.builder() | ||
.withStatusCode(HttpStatusCode.NOT_FOUND) | ||
.withErrorMessage("No pending terminations exist") | ||
.build(); | ||
} | ||
|
||
TerminationRequest terminationRequest = terminationsList.getFirst(); | ||
|
||
GetPendingTerminationsResponse response = | ||
GetPendingTerminationsResponse.builder() | ||
.withRequestId(terminationRequest.getRequestId().toString()) | ||
.withRequesterId(terminationRequest.getRequesterId()) | ||
.withContractId(terminationRequest.getContractId()) | ||
.withReason(terminationRequest.getReason()) | ||
.withStatus(terminationRequest.getStatus()) | ||
.withStatusCode(HttpStatusCode.OK) | ||
.withErrorMessage("") | ||
.build(); | ||
|
||
redisService.setValue(cachingKey, response); | ||
return response; | ||
} | ||
} |
Oops, something went wrong.