Refactor preview services to return Billing::PreviewResult objects
The following discussion from !14704 should be addressed:
-
@vitallium started a discussion:
suggestion: I think we can use the existing
PreviewResultclass here to work with an object instead of plain hash. The naming is not that good, IMHO,Billing::PreviewResult, so I'd check if we can rename it to something more meaningful.
Problem
The Zuora::Orders::PreviewService and related services currently return raw hash objects when previewing orders and subscriptions. This creates inconsistent data access patterns across the codebase, where some parts of the application expect hash access (e.g., [:success]) while others may expect object methods (e.g., .success).
This inconsistency makes the code harder to maintain and more prone to errors when accessing preview results. Additionally, it violates the principle of returning structured domain objects instead of primitive data structures.
Proposal
Standardize all preview service responses to return Billing::PreviewResult objects instead of raw hashes. This involves:
-
PreviewService: Return
Billing::PreviewResultobject -
Seat Addition Resolver: Update to use
.successinstead of[:success], handlemessageattribute -
Reconciliation Service: Convert hash to
PreviewResultobject before accessing -
Send Reconciliation Email Job: Convert hash to
PreviewResultobject before accessing -
Order Preview Resolver: Convert hash to
PreviewResultobject before accessing -
PreviewResult Model (Optional): Add
messageanderrorsattributes if needed for error handling -
Interactions (Review): Check if
NewRatePlan.previewneeds similar updates
Key Implementation Principle:
Wherever amendment_preview or similar methods return a hash from SubscriptionUpdateService.execute(...).payload, wrap it with Billing::PreviewResult.build_from_preview_service_data() before accessing its properties.