-
Notifications
You must be signed in to change notification settings - Fork 133
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
base: main
Are you sure you want to change the base?
Conversation
bookingFee = (BigDecimal) bookingFeeRow.get().get("BookingFee"); | ||
private BigDecimal calculateTravelPrice(String travelUUID) { | ||
BigDecimal bookingFee = run(Select.from(TRAVEL) | ||
.columns(t -> t.BookingFee().as("sum")) |
There was a problem hiding this comment.
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
private static interface Price { | ||
BigDecimal sum(); | ||
} |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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(); |
There was a problem hiding this comment.
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);
CqnSelect query = Select.from(TRAVEL) | ||
.matching(Map.of(Travel.TRAVEL_UUID, travelUUID, Travel.IS_ACTIVE_ENTITY, false)) | ||
.columns(t -> t.TotalPrice().as("sum")); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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(), |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needed?
Updates the total
Travel
price on Patch and Cancellation ofBooking
andBookingSupplement
Drafts