Problem statement
Complete the loyalty points engine of a purchase system: every committed purchase earns exactly one award, a rolled-back purchase earns nothing, and a replayed event changes nothing.
Requirements
PointsAwardListener
- React to every PurchaseCompletedEvent and store one PointsAward for it, but only after the purchase transaction that published the event has committed successfully. The award must actually be persisted: any read of the PointsAward store performed after the purchase request completes must find it.
- Never award anything for a purchase whose transaction rolls back. When a request carries simulateFailure set to true, the purchase row is absent afterwards and no award may ever appear for it. A rollback must not disturb awards already stored for earlier purchases, and the next successful purchase must still earn its award normally.
- Compute points as one point per full 100 cents of amountCents, discarding any remainder: 250 cents earn 2 points, 199 cents earn 1 point, exactly 100 cents earn 1 point, and 99 cents earn 0 points, stored as an award row with 0 points.
- Store exactly one award per purchase, carrying the customerId of the event, the computed points, and the purchaseId of the event. Every committed purchase earns its own award tied to its purchaseId, including several purchases by the same customer, whose awards accumulate as separate rows.
- Treat a redelivered event as already handled when an award with its purchaseId exists: store nothing more, however many times the event is replayed through POST /test-support/republish, and never change the stored award, whatever values the replayed event carries.
- Stored awards are served by the provided GET /customers/{customerId}/awards endpoint: once stored, an award appears there with its id, customerId, points, and purchaseId, and each customer sees exactly their own awards, an empty array when they have none.
- Keep all points logic out of PurchaseService: it must not declare a dependency on PointsAwardRepository or any award class. PointsAwardListener itself declares the PointsAwardRepository dependency it writes awards through.
Provided purchase flow
- Recording a purchase runs in a single transaction: it stores the Purchase row, publishes a PurchaseCompletedEvent carrying the purchaseId, customerId, and amountCents, and then deliberately fails when the request asks it to, rolling the whole transaction back
- POST /purchases accepts a JSON body with customerId, amountCents, and simulateFailure; on success it returns 201 Created with the stored purchase, and when simulateFailure is true it returns 500 Internal Server Error with error message: "Purchase processing failed" and stores nothing
- POST /test-support/republish accepts purchaseId, customerId, and amountCents and delivers a PurchaseCompletedEvent with those values again, returning 202 Accepted
Sample Requests & Responses
Expand to view details on sample requests and responses.
POST /purchases
Request:
{
"customerId": 42,
"amountCents": 2500,
"simulateFailure": false
}
Response (201 Created):
{
"id": 1,
"customerId": 42,
"amountCents": 2500
}
GET /customers/42/awards
Response (200 OK):
[
{
"id": 1,
"customerId": 42,
"points": 25,
"purchaseId": 1
}
]
POST /purchases
Request:
{
"customerId": 42,
"amountCents": 500,
"simulateFailure": true
}
Response (500 Internal Server Error):
{
"error": "Purchase processing failed",
"timestamp": "2026-08-05T23:40:02.034031",
"path": "/purchases"
}
GET /customers/42/awards
Response (200 OK), unchanged by the rolled-back purchase:
[
{
"id": 1,
"customerId": 42,
"points": 25,
"purchaseId": 1
}
]
POST /test-support/republish
Request:
{
"purchaseId": 1,
"customerId": 42,
"amountCents": 2500
}
Response (202 Accepted):
{
"status": "REPUBLISHED",
"purchaseId": 1
}
GET /customers/42/awards
Response (200 OK), unchanged by the replay:
[
{
"id": 1,
"customerId": 42,
"points": 25,
"purchaseId": 1
}
]
POST /purchases
Request:
{
"customerId": 7,
"amountCents": 99
}
Response (201 Created):
{
"id": 3,
"customerId": 7,
"amountCents": 99
}
GET /customers/7/awards
Response (200 OK):
[
{
"id": 2,
"customerId": 7,
"points": 0,
"purchaseId": 3
}
]
