# PEOPLEFLOW — Integration Architecture

## 1. Adapter Pattern & Decoupling Principle

External systems (**Microsoft Entra ID / M365**, **Freshservice**, **Ramco ERP**, **SMTP / Graph Mail**) are strictly isolated behind TypeScript adapter interfaces under `src/integrations/`.

- Core domain services (`onboarding.service.ts`, `offboarding.service.ts`, `workflow-engine.service.ts`) never call external REST SDKs directly.
- Every integration supports two explicit runtime modes stored in the `Integration` table and gated by environment flags:
  - **`MOCK` (Default in Dev/Staging)**: Deterministic simulated responses with configurable latency and failure injection for testing retry & error handling.
  - **`LIVE` (Production Only)**: Requires explicit Admin enablement, valid server-side credentials, passing health check, and formal IT security sign-off.
- Every outbound invocation creates an immutable `IntegrationLog` entry capturing `operation`, `externalRefId`, redacted request/response payloads, `status`, `retryCount`, and `executedAt`.

---

## 2. Microsoft 365 / Entra ID (Microsoft Graph Adapter)

### Planned Operations
- `createUser(payload)` — Provision Entra ID user with company-specific UPN/domain rules.
- `assignLicenses(userId, skuIds)` — Assign approved Microsoft 365 licenses.
- `updateUserProfile(userId, attributes)` — Sync department, title, manager, company on transfers.
- `manageGroupMemberships(userId, addGroups, removeGroups)` — Teams/SharePoint/Security groups.
- `disableAccount(userId)` — Set `accountEnabled: false`.
- `revokeSignInSessions(userId)` — Invoke `revokeSignInSessions` + invalidate refresh tokens.
- `verifyAccountStatus(userId)` — Read-back query verifying `accountEnabled === false` before offboarding clearance can close.

### Security Controls
- Application permissions granted strictly via least-privilege Graph scopes approved by SMC Cement Group M365 Global Admins.
- Secrets (`AZURE_AD_CLIENT_SECRET` / certificate) stored strictly server-side; never serialized to frontend responses.

---

## 3. Freshservice ITSM Adapter

### Planned Operations
- `createServiceRequest(catalogItem, payload)` — Spawn tickets for:
  - New Employee Onboarding
  - Employee Offboarding
  - Employee Transfer
  - IT Account Provisioning / Deactivation
  - Asset Issuance / Return
- `assignTicketToGroup(ticketId, groupId)` — Route to ECC/NCC/SCII IT support queues.
- `getTicketStatus(ticketId)` — Synchronize external ticket progress back into PEOPLEFLOW tasks.
- `addTicketNote(ticketId, note)` — Post audit updates & clearance references.

**Boundary**: PEOPLEFLOW remains the system of record for lifecycle approvals, employee history, and governance; Freshservice executes IT operational fulfillment tickets linked by `externalRefId`.

---

## 4. Ramco ERP Adapter

### Pragmatic Staged Approach (Rule 9 — Never Invent Undocumented Endpoints)
Because actual Ramco ERP API capabilities, authentication scheme, and permitted endpoints must be confirmed with the SMC Cement Group Ramco technical team:
- **Stage 1 (Initial Production Release)**: `RamcoManualAssistedAdapter` — Generates structured IT provisioning/deactivation tasks in PEOPLEFLOW with required verification checklist fields (Ramco User ID, Role/Module profile, Verification Timestamp, Screenshot/Evidence upload).
- **Stage 2 (Post-Ramco API Spec Confirmation)**: Implement `RamcoApiAdapter` behind the exact same `IRamcoAdapter` interface (`lookupEmployee`, `requestProvisioning`, `requestDeactivation`, `verifyAccessStatus`) without changing core lifecycle services.

---

## 5. Resilience, Retries & Failure Handling

- Transient errors (`429`, `502`, `503`, `504`, network timeout) automatically enqueue retries in `BackgroundJob` with exponential backoff (`1m → 5m → 15m → 60m`, max 4 attempts).
- Permanent failures (`400`, `401`, `403`, or exhausted retries) transition the associated task to `BLOCKED`, mark `IntegrationLog.status = FAILED`, and dispatch a high-priority alert to **IT Administrator** and **System Administrator** with one-click manual retry once resolved.
