# BAGIAN 4: FLOWCHART BACKEND (BE)
## Detailed Backend Application Flowcharts

---

## 4.1 BE General Service Architecture

Arsitektur umum Backend menggunakan pattern **Repository → Service → Route** (Layered Architecture).

![](./images/4.1.png)

---

## 4.2 BE Auth & RBAC Flow (Detailed)

Alur lengkap autentikasi dan authorization di Backend.

![](./images/4.2.png)

---

## 4.3 BE Conversation & Message Flow (Chat Engine)

Alur lengkap mesin chat Backend, baik inbound maupun outbound.

![](./images/4.3.png)

---

## 4.4 BE Conversation Status Flow

Alur perubahan status conversation. Status mengalir **satu arah** (one-way pipeline), tidak bisa mundur.

> **Catatan Penting (Kondisi Aktual):**
> - **4 status** didefinisikan: `UNASSIGNED`, `ASSIGNED`, `RESOLVED`, `CLOSED`
> - **CLOSED** didefinisikan di enum tapi **tidak ada implementasi** — tidak ada endpoint/code path yang mengarah ke status ini
> - **UNASSIGNED ← ASSIGNED (BLOCKED)** — kode secara eksplisit throw error: *"Status cannot be changed to unassigned, you must resolve or reassign to another member."*
> - **RESOLVED tidak bisa di-reopen** — customer reply setelah RESOLVED akan membuat **conversation BARU** dengan status UNASSIGNED, bukan membuka kembali yang lama
> - **Chain Mechanism**: Conversation baru di-link ke conversation lama via `previousConversationId` (linked list)
> - **RESOLVED tidak bisa di-resolve ulang** — kode throw `ConversationAlreadyResolvedError`
> - **Hanya agent yang di-assign** yang bisa resolve conversation-nya sendiri
> - **Pesan ke conversation RESOLVED diblok** — kode throw `ConversationAlreadyResolvedError`

### State Diagram

![](./images/4.4a.png)

### Detail Per Transisi

![](./images/4.4b.png)

### Chain Mechanism (Customer Reply Setelah RESOLVED)

![](./images/4.4c.png)


### Tabel Perubahan Field Per Transisi

| Transisi | `conversationStatus` | `assignedTo` | `assignedAt` | `assignedBy` | `resolvedAt` | `resolvedBy` | `resolvedDescription` | `unreadCounts` | `readBy` |
|---|---|---|---|---|---|---|---|---|---|
| **Create** | `UNASSIGNED` | `null` | `null` | `null` | `null` | `null` | `null` | `0` atau `1` | `null` |
| **→ ASSIGNED** | `ASSIGNED` | `userId` | `now()` | `callerId` | — | — | — | — | — |
| **Reassign** | `ASSIGNED` | `newUserId` | `now()` | `callerId` | — | — | — | — | — |
| **→ RESOLVED** | `RESOLVED` | *(tidak di-clear)* | *(tidak di-clear)* | *(tidak di-clear)* | `now()` | `callerId` | optional | — | — |
| **→ UNASSIGNED** | **BLOCKED** | — | — | — | — | — | — | — | — |
| **Mark Read** | — | — | — | — | — | — | — | `0` | `userId` |
| **Pesan Masuk** | — | — | — | — | — | — | — | `+1` | `null` |

---

## 4.5 BE Campaign Execution Flow (Detailed)

Alur lengkap eksekusi campaign di Backend.

![](./images/4.5.png)

---

## 4.6 BE Ticket Lifecycle Flow (Detailed)

Alur lengkap lifecycle ticket di Backend.

![](./images/4.6.png)

---

## 4.7 BE Webhook Processing Architecture

Arsitektur pemrosesan webhook dari external channels.

![](./images/4.7.png)

---

## 4.8 BE Service Module Pattern

Pattern yang digunakan di setiap service module Backend.

![](./images/4.8.png)

---

## 4.9 BE Error Handling Flow

![](./images/4.9.png)

