# Staff New Ticket (WhatsApp) Implementation Plan

> **For agentic workers:** Implement task-by-task. Steps use checkbox syntax.

**Goal:** Staff can open New ticket, pick connected WhatsApp channel + contact/phone, send first outbound message, land on ticket chat.

**Architecture:** Single `POST /api/v1/tickets` creates contact (if needed), ticket (always new, assigned to actor), outbound message, and WA send. Shared modal wired from Layout + Tickets.

**Tech Stack:** Fastify API, existing `ticketService` / `contactService` / `channelService`, React + Dialog.

## Global Constraints

- Staff only (`admin` | `user`)
- Connected WhatsApp channels only
- Always new ticket
- Required text content; no media on create
- Match existing outbound reply delivery-error behaviour

---

### Task 1: API create ticket + first outbound

**Files:**
- Modify: `apps/api/src/routes/tickets.ts`
- Modify: `apps/api/src/services/ticketService.ts` (optional `assigned_to` on create)

**Interfaces:**
- Produces: `POST /api/v1/tickets` → `{ ticket, message }`

- [ ] Validate body; resolve contact; assert WA channel connected
- [ ] `createTicket` with assignment + done intake
- [ ] Append message + `channelService.sendMessage`; emit events
- [ ] Rebuild/restart API; smoke with curl/login token

### Task 2: Frontend modal + wire buttons

**Files:**
- Create: `apps/frontend/src/components/StaffNewTicketModal.tsx`
- Modify: `apps/frontend/src/components/Layout.tsx`
- Modify: `apps/frontend/src/pages/Tickets.tsx`

- [ ] Modal: channel, contact search, phone, content
- [ ] Enable New ticket in Layout; Add Start on Tickets for staff
- [ ] Rebuild frontend; manual smoke

### Task 3: Docs / PRD

**Files:**
- Modify: `CHANGELOG.md`, `README.md`, `docs/NewKnowledgeBase.md`, `docs/TestSprite_PRD_WA_Ticketing.md`

- [ ] Document endpoint + UI flow
