This repository contains a full-stack application designed to handle insurance claim process. It allows a user to manage their repair case by selecting a service option and completing a mock payment. The application is built using a client-server architecture with a standalone Node.js backend and a Next.js frontend.
- Dynamic Case Handling: The application is accessed via a unique
/en/{guid}route that fetches and displays the case data. - Service Option Selection: User can choose between available service options. The "Swap" option is conditionally enabled based on a realtime SOAP API stock check.
- State Persistence: The user's progress is automatically saved to
localStorage, allowing them to close the tab and seamlessly resume their claim. - Mock Payment Flow: A complete payment flow with price calculation, multiple payment method options (Card & Swish), and simulated success/failure responses.
- Internationalization (i18n): Built with
next-intlto be fully multilingual. - Robust Error Handling: Handles API errors like "Case Not Found" and external service failures.
The project is structured as a pnpm monorepo containing two applications: a server and a client for the sake of client-server separation.
The backend is a lightweight Node.js/Express application that acts as a secure API Gateway. Its sole responsibility is to be an intermediary between the client and the external, third-party services. This separation is secure, as it ensures that sensitive credentials (like the accessToken for the case API and the credentials for the SOAP API) are never exposed to frontend/browser.
The frontend is a Next.js application. It is a robust application that prioritizes Client-Side Rendered (CSR) feel.
The core architectural pattern is quite simple:
- The Page (
/app/[locale]/[guid]/page.tsx): This component owns the application state (useState) and contains all the critical logic for fetching data, initializing the state (either fresh or fromlocalStorage), and persisting changes. - The Provider (
/context/CaseContext.tsx): The React Context provider receives the state from theCasePageand pass it down to any component that needs it.
State is managed by a combination of useState and useContext.
- Ownership: The state is owned by the main
CasePagecomponent. - Initialization: When the
CasePageloads, a singleuseEffecthook runs. It fetches the case data and then checkslocalStoragefor any saved progress that matches the current case'sguid. If valid data is found, the state is initialized from storage. If not found, a clean state is created based on the case'sserviceTypeId. - Persistence: A separate, simple
useEffecthook inCasePagewatches for any changes to thestateobject. Whenever the state changes, this effect saves the entire state object tolocalStorageunder a key unique to the currentguid.
- Credential Isolation: The most important security feature is the client-server architecture. The frontend never has access to the
accessTokenor SOAP credentials. - Input Validation: The backend uses the
zodlibrary to validate incoming parameters. This prevents errors and potential injection attacks.
The application is built to be multilingual using next-intl.
- Routing: The
middleware.tsfile enforces routing (e.g.,/en/...). - Messages: All user-facing text is stored in
src/messages/en.jsonand accessed usinguseTranslationshook, making it easy to add new languages in the future. - Configuration: The
src/i18n.tsfile sets global defaults for locales and thetimeZone.
- Runtime: Node.js
- Framework: Express.js
- HTTP Client:
axios(for calling external APIs) - XML Parsing:
fast-xml-parser(for handling the SOAP response) - Validation:
zod - Environment:
dotenv,cors
- Framework: Next.js
- Styling: Tailwind CSS with
postcssandautoprefixer - UI Components: Headless UI (for accessible
RadioGroupcomponents) - Internationalization:
next-intl - Client-Side Data Fetching:
swr(for the background stock check) - HTTP Client:
axios(for calling our own backend)
- Node.js
- pnpm
Clone the repository and install all dependencies for both the client and server from the root directory.
git clone https://github.com/farhanmaba/elcare-coding-assignment.git
cd elcare-coding-assignment
pnpm install
Ask the .env files in both the /server and /client directories from the repository owner to set up environment variables.
Run the following command from the root directory to start both the backend and the frontend.
pnpm dev
- The backend will be available at
http://localhost:3001. - The frontend will be available at
http://localhost:3000.
Open your browser and navigate to a case URL:
Theft/Lost: http://localhost:3000/en/a78f77b8-ea3c-4c3b-80e7-428a6a705a99
Drop-Off: http://localhost:3000/en/ea1d5e29-9e0d-4ff5-ae4c-197920a7def9
Swap: http://localhost:3000/en/cdd33a81-3b3f-4025-8009-327663b1e93c
Server health check can be done at: http://localhost:3001/api/health