# ICC Interac Manager — Scan Implementation
## What This Fixes
The "Scanner maintenant" button now actually **connects to Gmail**, **fetches Interac e-Transfer emails**, **parses them with AI** (Groq/Mistral), and **saves parsed transactions** to the database — all with real-time progress via WebSocket.
---
## File Map
```
icc-scan-impl/
├── .env.example ← Copy to .env and fill in API keys
│
├── shared/ ← Shared types & constants
│ ├── types/
│ │ ├── scan.ts ← ScanPreset, ScanDateRange, resolveScanDates()
│ │ ├── transaction.ts ← InteracTransaction schema
│ │ └── aiProvider.ts ← ProviderSlot, SwitcherEvent
│ └── constants/
│ └── branches.ts ← 48-branch email→city mapping
│
├── server/ ← Backend (Express + SQLite)
│ ├── index.ts ← Server entry point
│ ├── db/
│ │ └── database.ts ← SQLite schema + operations
│ ├── middleware/
│ │ └── auth.ts ← JWT middleware
│ ├── routes/
│ │ ├── auth.ts ← Google OAuth routes
│ │ ├── scan.ts ← POST /api/scan/start
│ │ └── transactions.ts ← GET /api/transactions
│ ├── services/
│ │ ├── gmailService.ts ← Gmail API: query builder + email fetcher
│ │ ├── aiService.ts ← AI parsing: Groq + Mistral + auto-switcher
│ │ └── scanService.ts ← Scan pipeline: Gmail → AI → DB
│ └── websocket/
│ └── setup.ts ← WebSocket server for progress events
│
└── web/ ← Frontend (React)
├── services/
│ ├── api.ts ← HTTP client with auth
│ └── websocket.ts ← WebSocket client
├── hooks/
│ └── useEmailScan.ts ← React hook for scan state + progress
├── components/scan/
│ └── ScanControls.tsx ← ★ THE scan button component
└── pages/
└── AuthCallback.tsx ← Handles OAuth redirect
```
---
## Setup Steps
### 1. Install Dependencies
In your `packages/server/` directory:
```bash
npm install express cors helmet dotenv googleapis jsonwebtoken better-sqlite3 ws uuid p-limit zod
npm install -D typescript @types/express @types/cors @types/jsonwebtoken @types/better-sqlite3 @types/ws @types/uuid tsx
```
In your `packages/web/` directory (most likely already installed):
```bash
npm install react-router-dom
```
### 2. Configure Google OAuth
1. Go to [Google Cloud Console](https://console.cloud.google.com/apis/credentials)
2. Create a project (or use existing)
3. Enable the **Gmail API**
4. Create **OAuth 2.0 Client ID** (Web application)
5. Add authorized redirect URI: `http://localhost:3001/api/auth/google/callback`
6. Copy the Client ID and Client Secret
### 3. Get AI API Keys (Free)
Pick at least one:
- **Groq** (recommended primary): [console.groq.com/keys](https://console.groq.com/keys) — 30 RPM free
- **Mistral** (recommended fallback): [console.mistral.ai/api-keys](https://console.mistral.ai/api-keys) — free tier
### 4. Create .env File
```bash
cp .env.example .env
# Edit .env with your actual keys
```
### 5. Copy Files Into Your Project
Copy each file from this package into the corresponding location in your monorepo.
The file paths in this package mirror the target paths in your project.
### 6. Add Vite Proxy (vite.config.ts)
Make sure your Vite dev server proxies API calls to the backend:
```typescript
// packages/web/vite.config.ts
export default defineConfig({
server: {
proxy: {
'/api': 'http://localhost:3001',
'/ws': {
target: 'ws://localhost:3001',
ws: true,
},
},
},
});
```
### 7. Add Route for Auth Callback
In your React Router config:
```tsx
import AuthCallback from './pages/AuthCallback';
// Add this route: