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

Rewrite RecalculatePriceHandler #609

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

MattSchur
Copy link
Collaborator

@MattSchur MattSchur commented Jun 13, 2023

Updates the total Travel price on Patch and Cancellation of Booking and BookingSupplement Drafts

bookingFee = (BigDecimal) bookingFeeRow.get().get("BookingFee");
private BigDecimal calculateTravelPrice(String travelUUID) {
BigDecimal bookingFee = run(Select.from(TRAVEL)
.columns(t -> t.BookingFee().as("sum"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

booking fee is not a sum

Comment on lines 95 to 97
private static interface Price {
BigDecimal sum();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this interface buys much

Travel travel = Struct.access(update.data()).as(Travel.class);
BigDecimal newFee = travel.getBookingFee();
if (newFee != null) {
Map<String, Object> travelKeys = CqnAnalyzer.create(context.getModel()).analyze(update).targetKeys();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CqnAnalyzer.create(context.getModel()) -> we should offer this directly on the "event context"

@On(event = { EVENT_DRAFT_PATCH }, entity = Travel_.CDS_NAME)
public void updateTravelPriceOnBookingFeeUpdate(DraftPatchEventContext context) {
CqnUpdate update = context.getCqn();
Travel travel = Struct.access(update.data()).as(Travel.class);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Travel travel = Struct.access(update.data()).as(Travel.class);
Travel travelData = Struct.access(update.data()).as(Travel.class);

Travel travel = Struct.access(update.data()).as(Travel.class);
BigDecimal newFee = travel.getBookingFee();
if (newFee != null) {
Map<String, Object> travelKeys = CqnAnalyzer.create(context.getModel()).analyze(update).targetKeys();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Access travelKeys as Travel ?

e.g.

Travel travelKeys = CqnAnalyzer.create(context.getModel()).analyze(update).targetKeys(Travel.class);
BigDecimal oldFee = selectTravelFee(travelKeys);
BigDecimal travelPrice = selectTravelPrice(travelKeys.getTravelUuid()).add(newFee).subtract(oldFee);

Comment on lines 180 to 182
CqnSelect query = Select.from(TRAVEL)
.matching(Map.of(Travel.TRAVEL_UUID, travelUUID, Travel.IS_ACTIVE_ENTITY, false))
.columns(t -> t.TotalPrice().as("sum"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe extract the query as a private static final and parameterize it for reuse

.first()
.ifPresent(row -> calculateAndPatchNewTotalPriceForDraft((String) row.get(Travel.TRAVEL_UUID)));
private BigDecimal selectTravelFee(Map<String, Object> travelKeys) {
CqnSelect query = Select.from(TRAVEL).matching(travelKeys).columns(b -> b.BookingFee());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe extract the query as a private static final and parameterize it for reuse

}

private BigDecimal calculateAndPatchNewTotalPriceForDraft(final String travelUUID) {
private Booking selectBooking(Map<String, Object> bookingKeys) {
CqnSelect query = Select.from(BOOKING).matching(bookingKeys).columns(b -> b.BookingUUID(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe extract the query as a private static final and parameterize it for reuse

}

private BookingSupplement selectSupplement(Map<String, Object> supplementKeys) {
return draftService.run(Select.from(BOOKING_SUPPLEMENT).matching(supplementKeys)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe extract the query as a private static final and parameterize it for reuse

private BigDecimal run(CqnSelect query) {
BigDecimal sum = persistenceService.run(query).first(Price.class).map(Price::sum).orElse(ZERO);

return nullToZero(sum);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants