# TAF Node.js Services

This directory contains two Node.js services for the TAF system:
1. **WebSocket Server** - Real-time synchronization via Redis/PostgreSQL
2. **TaxCore Service** - FRCS TaxCore API integration

## Services Overview

### 1. WebSocket Server (server.js)
Forwards update events from Redis to connected WebSocket clients for real-time data synchronization.

## Environment Variables

- `WS_PORT` - Port to run the WebSocket server (default: 8080)
- `REDIS_URL` - Connection string for Redis (default: `redis://localhost:6379`)
- `REDIS_CHANNEL` - Channel to subscribe for updates (default: `taf.sync`)
- `WS_DEBUG` - When set to `true`, prints verbose console output and writes logs to `logs/websocket_debug.log`, `logs/ws-server.log` and `logs/pgListener.log`

## Usage

Install dependencies and start the server:

```bash
cd websocket-server
npm install
node server.js
```

Run `pgListener.js` alongside the server to forward PostgreSQL notifications to
Redis:

```bash
node pgListener.js
```

When `WS_DEBUG` is `true` the server records incoming Redis messages and the matched subscriptions in `logs/websocket_debug.log`. General log output is written to `logs/ws-server.log` and `logs/pgListener.log`.

Clients must send a subscription object as their first message after connecting.

```json
{
  "subscribe": {
    "tenantId": 1,
    "groupIds": [4,5],
    "branchIds": [2],
    "userId": 10,
    "all": false
  }
}
```

Use `all: true` to receive every message (super admin access).

From the browser you can call `LocalSyncManager.startWebSocketListener(url, subscription)`
to establish the connection and automatically send the subscription payload.

Redis messages should include routing fields like `tenantId`, `groupId`,
`branchId`, `userId` or `broadcast: true`. The server forwards the payload only
to subscribed clients whose filters match or when `broadcast` is true.

Example Redis payload:

```json
{
  "tenantId": 1,
  "groupId": 4,
  "updates": { "demo": [] }
}
```

Whenever a JSON message containing `updates`, `workflowEvent` or a `command`
object and matching subscription data is published to `REDIS_CHANNEL`, the
server forwards it to the appropriate WebSocket clients.

## Command Payloads

Commands instruct the browser to perform a specific action. A message may
contain a `command` object with at least an `action` field. Clients currently
support the `open_modal` action which loads the modal identified by `name`.

Example:

```json
{
  "broadcast": true,
  "command": {
    "action": "open_modal",
    "name": "Workplace Invoice Preview"
  }
}
```

### 2. TaxCore Service (taxcore-service.js)
Handles communication with the Fiji Revenue and Customs Service (FRCS) TaxCore API.

**Port**: 3001 (configurable via `TAXCORE_SERVICE_PORT`)

**Endpoints**:
- `GET /health` - Health check
- `GET /tax-rates` - Fetch tax rates
- `POST /sale` - Create sales (Normal, Advance, Proforma, Training, Copy)

See [TAXCORE_SERVICE.md](./TAXCORE_SERVICE.md) for detailed documentation.

## Quick Start

### Install Dependencies
```bash
npm install
```

### Start Both Services
```bash
# Development mode
npm run dev

# Or start individually
npm start          # WebSocket server
npm run taxcore    # TaxCore service
```

### Using Quick Start Script
From the project root:
```bash
./start-taxcore-service.sh
```

## Push Messages

When a connected client sends a JSON message with a `push` object, the server
forwards that payload to the PHP endpoint defined by `PUSH_ENDPOINT`. The
session cookies from the WebSocket request are included so the PHP API can
authenticate the user. After the request resolves, the server parses the JSON
response body and sends a `pushResult` message back to the originating
WebSocket client containing that data.
