Skip to content

Commit c26c2c2

Browse files
Merge pull request #65 from Bug-Hunter-X/feature/implement-vault-repository-balance-snapshot
Feature/implement vault repository balance snapshot
2 parents d9da930 + fb65610 commit c26c2c2

File tree

7 files changed

+4336
-7049
lines changed

7 files changed

+4336
-7049
lines changed

README.md

Lines changed: 23 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -12,69 +12,29 @@ API gateway, usage metering, and billing services for the Callora API marketplac
1212

1313
- Health check: `GET /api/health`
1414
- Placeholder routes: `GET /api/apis`, `GET /api/usage`
15-
- Developer analytics route: `GET /api/developers/analytics`
15+
- JSON body parsing; ready to add auth, metering, and contract calls
16+
- In-memory `VaultRepository` with:
17+
- `create(userId, contractId, network)`
18+
- `findByUserId(userId, network)`
19+
- `updateBalanceSnapshot(id, balance, lastSyncedAt)`
1620

17-
## Developer analytics route
21+
## Vault repository behavior
1822

19-
Endpoint:
20-
21-
`GET /api/developers/analytics`
22-
23-
Authentication:
24-
25-
- Requires `x-user-id` header (developer identity for now).
26-
27-
Query params:
28-
29-
- `from` (required): ISO date/time
30-
- `to` (required): ISO date/time
31-
- `groupBy` (optional): `day | week | month` (default: `day`)
32-
- `apiId` (optional): filters to one API (must belong to authenticated developer)
33-
- `includeTop` (optional): set to `true` to include `topEndpoints` and anonymized `topUsers`
34-
35-
Response:
36-
37-
```json
38-
{
39-
"data": [
40-
{ "period": "2026-02-01", "calls": 12, "revenue": "3400" }
41-
],
42-
"topEndpoints": [
43-
{ "endpoint": "/v1/search", "calls": 8 }
44-
],
45-
"topUsers": [
46-
{ "userId": "user_1a2b", "calls": 4 }
47-
]
48-
}
49-
```
50-
51-
Notes:
52-
53-
- Returns analytics only for the authenticated developer's events.
54-
- If `apiId` does not belong to the developer, the endpoint returns `403`.
55-
- Revenue is returned as stringified integer units.
23+
- Enforces one vault per user per network.
24+
- `balanceSnapshot` is stored in smallest units using non-negative integer `bigint` values.
25+
- `findByUserId` is network-aware and returns the vault for a specific user/network pair.
5626

5727
## Local setup
5828

59-
1. Prerequisites: Node.js 18+
60-
2. Install and run:
29+
1. **Prerequisites:** Node.js 18+
30+
2. **Install and run (dev):**
6131

6232
```bash
33+
cd callora-backend
6334
npm install
6435
npm run dev
6536
```
6637

67-
3. API base: `http://localhost:3000`
68-
### Docker Setup
69-
70-
You can run the entire stack (API and PostgreSQL) locally using Docker Compose:
71-
72-
```bash
73-
docker compose up --build
74-
```
75-
The API will be available at http://localhost:3000, and the PostgreSQL database will be mapped to local port 5432.
76-
77-
7838
3. API base: [http://localhost:3000](http://localhost:3000). Example: [http://localhost:3000/api/health](http://localhost:3000/api/health).
7939

8040
## Scripts
@@ -84,50 +44,24 @@ The API will be available at http://localhost:3000, and the PostgreSQL database
8444
| `npm run dev` | Run with tsx watch (no build) |
8545
| `npm run build` | Compile TypeScript to `dist/` |
8646
| `npm start` | Run compiled `dist/index.js` |
87-
| `npm test` | Run unit/integration tests |
88-
89-
## Database migrations
90-
91-
This repository includes SQL migrations for `api_keys` and `vaults` in `migrations/`.
92-
93-
- `api_keys` stores only `key_hash` (never the raw API key).
94-
- `api_keys` enforces unique `(user_id, api_id)` and has an index on `(user_id, prefix)` for key lookup.
95-
- `vaults` stores per-user per-network snapshots with unique `(user_id, network)`.
96-
97-
Run migrations with PostgreSQL:
98-
99-
```bash
100-
psql "$DATABASE_URL" -f migrations/0001_create_api_keys_and_vaults.up.sql
101-
```
102-
103-
Rollback:
104-
105-
```bash
106-
psql "$DATABASE_URL" -f migrations/0001_create_api_keys_and_vaults.down.sql
107-
```
108-
109-
Validate issue #9 requirements locally:
110-
111-
```bash
112-
npm run validate:issue-9
113-
```
47+
| `npm test` | Run unit tests |
48+
| `npm run test:coverage` | Run unit tests with coverage |
11449

11550
## Project layout
11651

11752
```text
11853
callora-backend/
11954
|-- src/
120-
| |-- app.ts
121-
| |-- app.test.ts
122-
| |-- index.ts
123-
| |-- middleware/
124-
| | |-- requireAuth.ts
55+
| |-- index.ts # Express app and routes
12556
| |-- repositories/
126-
| | |-- usageEventsRepository.ts
127-
| |-- services/
128-
| | |-- developerAnalytics.ts
129-
| |-- types/
130-
| |-- auth.ts
57+
| |-- vaultRepository.ts # Vault repository implementation
58+
| |-- vaultRepository.test.ts # Unit tests
13159
|-- package.json
13260
|-- tsconfig.json
13361
```
62+
63+
## Environment
64+
65+
- `PORT` - HTTP port (default: 3000). Optional for local dev.
66+
67+
This repo is part of [Callora](https://github.com/your-org/callora). Frontend: `callora-frontend`. Contracts: `callora-contracts`.

0 commit comments

Comments
 (0)