# Afak 22 - Conversion Mapping (C++ Builder 6 → Node.js)

This document maps the original C++ Builder 6 server components to the new Node.js implementation.

---

## Architecture Comparison

| Original (C++ Builder 6) | New (Node.js) |
|-------------------------|----------------|
| COM/DCOM Remote Data Module | REST API (JSON over HTTP) |
| DataSnap / Midas | NestJS + Prisma |
| TUniConnection (UniDAC) | Prisma Client |
| TCRemoteDataModule | NestJS Controllers + Services |
| DataSetProvider | REST endpoints returning JSON |

---

## Module Mapping

### PublicDataImpl (Connection & Auth)

| Original COM Method | New REST Endpoint | Notes |
|--------------------|-------------------|-------|
| `Connect()` | `POST /api/auth/login` | Body: dbName, userName, password |
| `Disconnect()` | (Token expiry / logout) | JWT expires automatically |
| `SetDatabaseName(BSTR)` | `POST /api/auth/select-database` | Or include in login |
| `SetProviders()` | (Automatic) | Prisma handles connections |
| `StartTransaction()` | (Per-request or explicit) | Use `$transaction` in services |
| `Commit()` | (Automatic) | |
| `Rollback()` | (On error) | |

### TAmzServerImpl (Business Logic)

| Original COM Method | New REST Endpoint |
|--------------------|-------------------|
| `LastACDaily(kind, ref)` | `GET /api/daily/last/:kind/:ref` |
| `HoldACDaily(kind, ref)` | `GET /api/daily/hold/:kind/:ref` |
| `LastInvoice(dft, st, ref)` | `GET /api/invoices/sale/last?dft=&st=&ref=` |
| `HoldSInvoice(dft, st, ref)` | `GET /api/invoices/sale/hold?dft=&ref=` |
| `LastBuyInvoice(dft, st, ref)` | `GET /api/invoices/buy/last?dft=&st=&ref=` |
| `HoldBuyInvoice(dft, st, ref)` | `GET /api/invoices/buy/hold?dft=&ref=` |
| `SetDatabaseName(BSTR)` | Handled in login |
| `ExecuteCommand(BSTR sql)` | (Use with caution) `POST /data/query` - not yet implemented |

### Data Providers → REST

| Original Provider | New Endpoint |
|------------------|--------------|
| LedgerProvider | `GET /api/ledger` |
| ItemsProvider | `GET /api/items` |
| SInvoiceProvider | (Part of invoices module) |
| BInvoiceProvider | (Part of invoices module) |

---

## Table Mapping (OracleDM.dfm → Prisma)

| Original Table | Prisma Model | File |
|----------------|--------------|------|
| GNLEDGER | Gnledger | prisma/schema.prisma |
| DAILY_TR | DailyTr | prisma/schema.prisma |
| ITEMS | Item | prisma/schema.prisma |
| fat_sale | FatSale | prisma/schema.prisma |
| fat_buy | FatBuy | prisma/schema.prisma |
| Users | User | prisma/schema.prisma |

---

## Database Type Mapping

| Original dbType | Database | Prisma |
|-----------------|----------|--------|
| 1 | Oracle | Change provider in schema |
| 2 | MySQL | `mysql` provider |
| 3 | SQLite | `sqlite` provider (default) |
| 4 | SQL Server | `sqlserver` provider |

---

## RAD Studio Client Migration

### Before (DataSnap)

```pascal
// C++ Builder / Delphi
SocketConnection1.Connected := True;
ClientDataSet1.ProviderName := 'LedgerProvider';
ClientDataSet1.Open;
```

### After (REST)

```pascal
// Delphi - Using TRESTClient
RESTClient1.BaseURL := 'http://localhost:3000/api';
RESTRequest1.Resource := 'auth/login';
RESTRequest1.AddBody('{"dbName":"afak22","userName":"admin","password":"1"}');
RESTRequest1.Execute;
// Parse JSON for accessToken

RESTRequest1.Resource := 'ledger';
RESTRequest1.Params.AddItem('Authorization', 'Bearer ' + accessToken, pkHTTPHEADER);
RESTRequest1.Execute;
// Parse RESTResponse.Content as JSON
```

---

## Newly Converted (Phase 2)

| Original | New Endpoint |
|----------|--------------|
| `ExecuteCommand(sql)` | `POST /api/data/execute` |
| `GetRDataSQL(sql, fname, flag)` | `POST /api/data/query` |
| `LastMGDaily`, `HoldMGDaily` | `GET /api/daily/mg/last/:kind/:ref`, `hold/:kind/:ref` |
| `LastBBack`, `HoldBBackInvoice` | `GET /api/invoices/bback/last`, `bback/hold` |
| `LastSBack`, `HoldSBackInvoice` | `GET /api/invoices/sback/last`, `sback/hold` |
| `LastPosl`, `HoldPosl` | `GET /api/invoices/posl/last`, `posl/hold` |
| `LastExInvoice`, `HoldExInvoice` | `GET /api/invoices/ex/last`, `ex/hold` |
| `UsersProvider` | `GET /api/users` |

## Newly Converted (Phase 3 – Posting)

| Original | New Endpoint |
|----------|--------------|
| `DoAcount(N, kind, ref)` | `POST /api/accounting/do-acount` Body: `{ n, kind, ref }` |
| `UnDoAcount(N, kind, ref)` | `POST /api/accounting/undo-acount` Body: `{ n, kind, ref }` |
| `DoMG(N, kind, ref)` | `POST /api/inventory/do-mg` Body: `{ n, kind, ref }` |
| `UnDoMG(N, kind, ref)` | `POST /api/inventory/undo-mg` Body: `{ n, kind, ref }` |
| `DoInvoice(dft, N, ref)` | `POST /api/invoices/sale/do` Body: `{ dft, n, ref }` |
| `UnDoInvoice(dft, N, ref)` | `POST /api/invoices/sale/undo` Body: `{ dft, n, ref }` |
| `DoBuyInvoice(dft, N, ref)` | `POST /api/invoices/buy/do` Body: `{ dft, n, ref }` |
| `UnDoBuyInvoice(dft, N, ref)` | `POST /api/invoices/buy/undo` Body: `{ dft, n, ref }` |

## Not Yet Converted

The following from the original server are not yet implemented:

- `DoPoinOfSale`, `UnDoPoinOfSale`, `DoExInvoice`, `UnDoExInvoice`
- `DoBBackInvoice`, `UnDoBBackInvoice`, `DoSBackInvoice`, `UnDoSBackInvoice`
- `PutTableData` (import file to table)
- `ReCreateDatabase`, `ReDoAcount`, `ReDoItems`
- `CreateDynamic`, `DeleteDynamic`
- Many other providers (Archif, Talab, etc.)

These can be added incrementally following the same pattern: Controller → Service → Prisma.
