Skip to main content

Overview

PC Fix features a sophisticated shopping cart system that provides a seamless user experience with client-side state management using Zustand, server-side persistence, and automated abandoned cart recovery to maximize conversion rates.

Client-Side Cart Management

Zustand State Store

The shopping cart uses Zustand for reactive state management with session storage persistence:
packages/web/src/stores/cartStore.ts

Cart Operations

Add to Cart

Automatically increments quantity if item already exists in cart

Stock Validation

Prevents adding more items than available in stock

Quantity Controls

Increase/decrease quantity with minimum of 1 and maximum of stock level

Remove Items

Instantly remove items from cart with one click

Server-Side Cart Persistence

Database Schema

Carts are persisted in the database with support for abandoned cart tracking:
packages/api/prisma/schema.prisma

Cart Synchronization

When users log in or update their cart, the client syncs with the server to ensure data consistency:
packages/api/src/modules/cart/cart.service.ts
Cart synchronization uses database transactions to ensure data consistency. All cart items are replaced atomically during sync.

Abandoned Cart Recovery

Automated Email Notifications

PC Fix automatically detects abandoned carts and sends recovery emails to encourage customers to complete their purchase:
packages/api/src/shared/services/cron.service.ts

Recovery Timeline

1

User adds items to cart

Items are saved both in sessionStorage and synced to the database if user is logged in
2

User leaves without purchasing

Cart remains active in the database with timestamp tracking
3

30 minutes pass

System identifies cart as potentially abandoned
4

Email sent

If cart hasn’t been modified in 30 minutes (but less than 24 hours), recovery email is sent
5

Flag updated

Cart is marked with abandonedEmailSent: true to prevent duplicate emails

Cron Schedule

The abandoned cart check runs every 30 minutes:
packages/api/src/shared/services/cron.service.ts
Abandoned cart emails are only sent once per cart to avoid spam. The abandonedEmailSent flag is reset when the cart is updated.

Cart Validation

Product Validation

The system validates that:
  • Products exist and haven’t been deleted
  • Products have sufficient stock
  • Prices are current from the database

Stock Management

Quantity increases are capped at available stock:

Cart Persistence Strategy

  • Cart stored only in browser sessionStorage
  • Lost when browser session ends
  • Synced to database upon login

Cart-to-Order Flow

When users proceed to checkout:
  1. Cart items are retrieved from the store
  2. Stock is validated against current inventory
  3. Prices are recalculated from the database
  4. Order is created with a transaction
  5. Cart is cleared after successful order
  6. Stock is decremented atomically
packages/api/src/modules/sales/sales.service.ts

Performance Optimizations

Session Storage

Cart state stored in sessionStorage for instant access without API calls

Batch Sync

Cart synced to server in single operation, not per-item

Database Indexes

Indexes on cartId and productoId for fast lookups

Transaction Safety

Cart operations use database transactions for consistency

API Endpoints

The cart automatically syncs when users log in, ensuring their cart follows them across devices.