Case study
Shop
What it is
Shop is an online store for electronics: phones, laptops, tablets, watches and audio. I built all of it on my own: an API in NestJS on MySQL, a storefront and an admin panel, both in React.
A shop looks simple until two people buy at once, a payment gateway calls back twice, or a token leaks. Most of the work went into those moments, in the API, where they are decided.
- Role
- Solo: API, storefront and admin
- Type
- Online store, three apps
- Stack
- NestJS, TypeORM, MySQL, React
- Tests
- Unit and end-to-end, in CI on MySQL
One phone, two customers
Two checkouts that both read "1 in stock" and both succeed sell the same phone twice. So a
checkout locks the rows it is about to change (SELECT … FOR UPDATE), and every
path in the API takes its locks in the same order (category, product, variant, user) so two
checkouts can wait for each other but never deadlock.
The lock alone was not enough. I first locked the rows with a query that read only their ids,
then read the stock with a plain SELECT. In MySQL that plain read returns the
snapshot the transaction took at its first read, from before the other checkout committed, so
the last phone could still be sold many times. The stock now comes from the locking read
itself, which always sees the latest row. The losing checkout gets INSUFFICIENT_STOCK,
naming the line and how many are left.
The last phone
Two customers press "Pay" at the same moment.
Stock of the white 512 GB1
Customer A
Customer B
Paid exactly once
A checkout carries an Idempotency-Key. Sending it twice, by a double click or a
retry on a bad connection, gets back the same order instead of a second one, and reusing the
key for a different basket is refused.
ZarinPal tells the API about a payment by calling it back, and a callback can arrive twice. The order is locked and checked first, so the second call finds it paid and changes nothing. A payment can also arrive after its order expired and gave its stock back. The money did leave the customer, so it goes to their wallet rather than nowhere. Order messages are sent only after the payment is committed, so an SMS outage can never undo a sale.
What the API answers
Three things the network does to a checkout.
A stolen token ends the session
Signing up is confirmed by an SMS code. The access token lives in the page's memory for 15 minutes; the refresh token never reaches JavaScript at all: it is an httpOnly cookie, and the database keeps only its hash.
Every refresh uses the token up and issues a new one, in one conditional update, so two refreshes racing each other cannot both succeed. A token that comes back after it was used means someone else has a copy, so the whole family of tokens from that sign-in is revoked.
- Sign inToken
R1is set as an httpOnly cookie; only its hash is stored. - Refresh
R1is marked used andR2issued, in one atomic step. - Someone replays R1It was already used: it has leaked.
- The family is revoked
R2stops working too. Whoever holds either token signs in again.
Filters that tell the truth
Each colour and storage is its own variant, with its own price and stock, built from attributes the shop defines with fixed lists of values. A filter's values of one attribute widen the search, different attributes narrow it, and the chosen values have to meet on one variant in stock: a phone that comes in white, and in 1 TB, but not as a white 1 TB, is not a match.
An example phone
Pick colours and storage, as a shopper would.
Not shownIt comes in white and in 1 TB, but not as a white 1 TB.
Also in the API
- Returns are approved, received, then refunded, with the money and the restocking in one transaction, and never more than was paid for that order.
- Discount codes have a ceiling, a minimum order, a window, total and per-customer limits, and a scope: a code for one category only comes off those lines.
- Invoice numbers come from one atomic counter per Persian year, so two checkouts can never share one.
- Changes to money and permissions go to an audit log that no endpoint can edit or delete.
- Every error carries a stable code, so the storefront writes its own Persian message instead of showing an English one.
- In production the API refuses to start with an open CORS policy, a sandbox gateway or a callback address that is not https.
- On every push, CI type-checks, lints and runs the unit tests and the end-to-end suites (sign-in, a whole checkout, a return) against MySQL.
- The end-to-end suite refuses to run against any database whose name does not end in
_test: it writes real rows.
Built with
Screens