Repository navigation
Support partial refunds #4
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| import type { OrderRecord, Store } from '../data/store'; | ||
| import { badInput, notFound } from './errors'; | ||
| import { computeTotals } from './pricing'; | ||
| import { computeTotals, roundCurrency } from './pricing'; | ||
|
|
||
| const REFUNDABLE = new Set(['PAID', 'FULFILLED']); | ||
| const REFUNDABLE = new Set(['PAID', 'FULFILLED', 'PARTIALLY_REFUNDED']); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make explicit partial refunds safe to retry. If a client repeats a successful
|
||
|
|
||
| /** | ||
| * Refunds an order in full. Partial refunds are not supported yet; callers | ||
| * passing an amount lower than the order total receive BAD_USER_INPUT. | ||
| * Refunds part or all of an order. Omitting the amount refunds the remaining | ||
| * balance. The order becomes PARTIALLY_REFUNDED until the full total has been | ||
| * returned, then REFUNDED. | ||
| */ | ||
| export function refundOrder(store: Store, orderId: string, amount?: number | null): OrderRecord { | ||
| const order = store.orders.get(orderId); | ||
|
|
@@ -16,14 +17,21 @@ export function refundOrder(store: Store, orderId: string, amount?: number | nul | |
| } | ||
|
|
||
| const { total } = computeTotals(order.items); | ||
| if (amount != null && amount < total) { | ||
| throw badInput('Partial refunds are not supported'); | ||
| } | ||
| const alreadyRefunded = order.refundedAmount ?? 0; | ||
| const remaining = roundCurrency(total - alreadyRefunded); | ||
| const refund = amount ?? remaining; | ||
| if (refund <= 0) throw badInput('Refund amount must be greater than zero'); | ||
| if (refund > remaining) throw badInput(`Refund exceeds remaining balance of ${remaining}`); | ||
|
Comment on lines
+23
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reject refund amounts that are not whole cents. A request for As per path instructions, changes to the “precision, rounding, or meaning of a returned value” are behavioral contract changes.
|
||
|
|
||
| const payment = store.payments.get(order.paymentId); | ||
| if (!payment) throw notFound('Payment', order.paymentId); | ||
|
|
||
| order.status = 'REFUNDED'; | ||
| payment.status = 'REFUNDED'; | ||
| order.refundedAmount = roundCurrency(alreadyRefunded + refund); | ||
| if (order.refundedAmount >= total) { | ||
| order.status = 'REFUNDED'; | ||
| payment.status = 'REFUNDED'; | ||
| } else { | ||
| order.status = 'PARTIALLY_REFUNDED'; | ||
| } | ||
| return order; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,6 +78,7 @@ export const resolvers = { | |
| tax: (order: OrderRecord) => computeTotals(order.items).tax, | ||
| total: (order: OrderRecord) => computeTotals(order.items).total, | ||
| currency: () => 'USD', | ||
| refundedAmount: (order: OrderRecord) => order.refundedAmount ?? 0, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Populate historical refunds before treating missing amounts as zero. Seeded order As per path instructions, “Check that values are returned in the units documented by the field’s description.”
|
||
| payment: (order: OrderRecord, _: unknown, { store }: Context) => { | ||
| const payment = store.payments.get(order.paymentId); | ||
| if (!payment) throw notFound('Payment', order.paymentId); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Coordinate the new status with both clients before release.
This enum addition is dangerous.
thicket-webselectsstatusthroughOrderSummaryinOrderDetail, but itsorderStatusLabelandorderStatusTonehandlers throw forPARTIALLY_REFUNDED.thicket-mobileselectsstatusthroughOrderCardinOrderReceipt, but its missing theme makesStatusPilldereferenceundefined. Update both clients’ status handling before the API returns the new value. (raw.githubusercontent.com)As per coding guidelines, “Enum additions are coordinated.” As per path instructions, “Adding an enum value is dangerous.”
Sources: Coding guidelines, Path instructions, Linked repositories
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.
@coderabbitai What’s the safest rollout order for this change across thicket-api, thicket-web, and thicket-mobile?
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.
@mtyates The safest order is API contract only → compatible clients → client adoption check → enable partial refunds. Split schema deployment from behavior activation.
Deploy a compatibility-only release of
thicket-api.Order.refundedAmountand the enum declaration.PARTIALLY_REFUNDEDyet.refundedAmountreturns a number for existing orders.refundedAmountis additive. The enum addition is dangerous because existing clients can fail when they receive the new value. The changedrefundOrderdescription also documents a behavior change: an omitted amount refunds the remaining balance.Release compatible
thicket-mobileandthicket-webclients.orderStatusLabelandorderStatusTone.PARTIALLY_REFUNDEDtheme forStatusPill.OrderDetail/OrderSummaryand mobile’sOrderReceipt/OrderCardwith partial-refund responses.Both clients can now select
refundedAmountbecause the API field exists. Start the mobile release early because installation takes longer. The relative order of the two client releases does not otherwise matter.Check adoption before enabling the behavior.
A published mobile release does not protect users who still run an older version. Require a supported version, or provide a tested compatibility mechanism for older clients. Check cached web versions and long-lived sessions too.
Enable partial refunds gradually.
Verify a partial refund, a second refund, and an omitted-amount refund. Confirm that both clients render each result. Monitor client errors and refund failures.
Rollback caution: Disabling new partial refunds does not remove existing
PARTIALLY_REFUNDEDrecords. Keep compatible readers deployed after activation. Do not roll back to API or client versions that cannot handle those records.This PR currently couples the contract and behavior changes. The sequence above requires a separate compatibility release or an activation gate; I have not verified that either exists.
You are interacting with an AI system.
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.
@coderabbitai Good catch. For future reviews: thicket-web deploys together with the API, so web only needs its change merged first. thicket-mobile is the real gate. A new enum value or a field removal can only ship once a mobile release that handles it has been live for at least 30 days. Flag API PRs that don’t reference that mobile change.
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.
@mtyates Understood. This corrects my earlier rollout order:
thicket-webchange first. Web deploys together with the API.thicket-mobilerelease must have been live for at least 30 days.For
#4, include the mobile change reference and its release date so the 30-day requirement can be verified. The compatibility concern remains open; this clarification does not confirm a fix.✏️ Learnings added
You are interacting with an AI system.