Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contracts commands cache implementation #82

Merged
merged 14 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import com.workup.shared.commands.CommandRequest;
import com.workup.shared.commands.CommandResponse;
import lombok.Setter;
import org.springframework.amqp.core.AmqpTemplate;

public abstract class ContractCommand<T extends CommandRequest, Q extends CommandResponse>
implements Command<T, Q> {

@Setter AmqpTemplate rabbitTemplate;

@Setter ContractRepository contractRepository;

@Setter ContractMilestoneRepository contractMilestoneRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import com.workup.shared.commands.CommandMap;
import com.workup.shared.commands.CommandRequest;
import com.workup.shared.commands.CommandResponse;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ContractCommandMap
extends CommandMap<ContractCommand<? extends CommandRequest, ? extends CommandResponse>> {

@Autowired AmqpTemplate rabbitTemplate;
@Autowired ContractRepository contractRepository;

@Autowired ContractMilestoneRepository contractMilestoneRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,66 @@
package com.workup.contracts.commands;


import com.workup.contracts.models.ContractMilestone;
import com.workup.shared.commands.contracts.requests.ProgressMilestoneRequest;
import com.workup.shared.commands.contracts.responses.ProgressMilestoneResponse;
import com.workup.shared.enums.HttpStatusCode;
import com.workup.shared.enums.contracts.MilestoneState;
import java.util.Optional;
import java.util.UUID;

public class ProgressMilestoneCommand
extends ContractCommand<ProgressMilestoneRequest, ProgressMilestoneResponse> {

private ProgressMilestoneResponse isValid(ProgressMilestoneRequest request) {
Optional<ContractMilestone> milestone =
contractMilestoneRepository.findById(UUID.fromString(request.getMilestoneId()));
if (milestone.isEmpty())
return ProgressMilestoneResponse.builder()
.withStatusCode(HttpStatusCode.BAD_REQUEST)
.withErrorMessage("Milestone is not found")
.build();
if (milestone.get().getStatus() != MilestoneState.OPEN
&& milestone.get().getStatus() != MilestoneState.IN_PROGRESS)
return ProgressMilestoneResponse.builder()
.withStatusCode(HttpStatusCode.BAD_REQUEST)
.withErrorMessage("Milestone cannot be progressed through this command")
.build();
return null;
}

@Override
public ProgressMilestoneResponse Run(ProgressMilestoneRequest request) {
// First we will get the milestones and add them to the database first,
// This will allow us to have their IDs for when we insert the contract.
ProgressMilestoneResponse checkerResponse = isValid(request);
if (checkerResponse != null) return checkerResponse;

return null;
Optional<ContractMilestone> milestone =
contractMilestoneRepository.findById(UUID.fromString(request.getMilestoneId()));
ContractMilestone updatedMilestone = milestone.get();
if (updatedMilestone.getStatus() == MilestoneState.OPEN) {
updatedMilestone.setStatus(MilestoneState.IN_PROGRESS);
} else if (updatedMilestone.getStatus() == MilestoneState.IN_PROGRESS) {
updatedMilestone.setStatus(MilestoneState.IN_REVIEW);
// Send to payments here
Kemosalamy marked this conversation as resolved.
Show resolved Hide resolved

// get required data from milestone
milestoneContract =
contractRepository.findById(UUID.fromString(updatedMilestone.getContractId()));
}

try {
contractMilestoneRepository.save(updatedMilestone);
System.out.println(" [x] Milestone Progressed " + updatedMilestone);
return ProgressMilestoneResponse.builder()
.withStatusCode(HttpStatusCode.OK)
.withErrorMessage("")
.build();

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