# Marketing App - Project Context

## Tech Stack
- **Frontend:** React (Vite), Tailwind CSS, Lucide icons
- **Backend:** Express.js, NocoDB (database via REST API), SQLite (auth)
- **Key patterns:** `api.js` normalizes NocoDB responses (snake_case + camelCase aliases, Id/id/_id). SlidePanel uses createPortal. AppContext provides brands/teamMembers. AuthContext provides user/permissions/canEditResource/canDeleteResource.

## Architecture Notes
- NocoDB uses `Id` (capital I) as primary key, `CreatedAt`/`UpdatedAt` timestamps
- `ensureRequiredTables()` only creates NEW tables — skips existing ones
- `ensureFKColumns()` adds Number columns to existing tables (FK_COLUMNS map)
- `ensureTextColumns()` adds text/other columns to existing tables (TEXT_COLUMNS map)
- Column migrations run on every server restart in `startServer()`

## Current State / Known Issues

### CRITICAL: Approvers on Artefacts — NOT YET WORKING
The `approver_ids` column (SingleLineText, comma-separated user IDs) may not exist in the NocoDB Artefacts table. Symptoms: approvers selected in the UI don't persist when panel is closed and reopened.

**Diagnosis steps added:**
1. Server startup now lists ALL Artefacts table columns and auto-creates missing ones (`approver_ids`, `project_id`, `campaign_id`)
2. `POST /artefacts` logs the approver_ids being sent and what NocoDB returns
3. `PATCH /artefacts/:id` logs the update data and the re-read value
4. `ensureTextColumns()` now logs success/failure for each column creation

**What to check:** Restart the server and look at terminal output:
- If `approver_ids` is listed in columns → column exists, issue is elsewhere
- If `⚠ MISSING column` appears → column was missing, should be auto-created now
- PATCH logs show if the value persists after write (`After re-read: approver_ids=...`)

**Root cause:** `ensureRequiredTables()` (line ~310) skips existing tables entirely (`if (existingTables.has(tableName)) continue`). The `approver_ids` column was added to REQUIRED_TABLES schema but the Artefacts table already existed, so the column was never created. Fixed by adding to TEXT_COLUMNS and adding a belt-and-suspenders check at startup.

### Recently Completed Features

#### Artefacts Page
- Grid + List view toggle with sorting (recently updated, newest, oldest, title A-Z)
- Filters: brand, status, type, creator, project, campaign
- Project and Campaign assignment (dropdowns in create modal + detail panel)
- Approver multi-select (ApproverMultiSelect component at bottom of Artefacts.jsx)
- **Save Draft** button in detail panel header (saves title + description)
- **Delete** button in detail panel header (with permission check via canDeleteResource)
- Editable title (inline input in header) and description (textarea) in detail panel
- Language picker for copy artefacts uses predefined list: Arabic (AR), English (EN), French (FR), Bahasa Indonesia (ID)
- Auto-opens detail panel after creating an artefact

#### Issues Page
- Board (kanban) + List view toggle with drag-and-drop
- 5 status columns: New, Acknowledged, In Progress, Resolved, Declined
- IssueCard component (`client/src/components/IssueCard.jsx`)
- Inline filters (always visible, no toggle button)
- Status count cards work as quick-filter buttons

#### AuthContext
- Added `artefact` type to `canEditResource` and `canDeleteResource` (uses `canEditAnyPost`/`canDeleteAnyPost` permissions + isOwner)

#### Server
- Artefacts schema has: project_id, campaign_id, approver_ids columns
- GET/POST/PATCH /artefacts routes handle project_id, campaign_id, approver_ids with name enrichment
- Issues PATCH supports brand_id (bug fix)
- FK_COLUMNS includes `Artefacts: ['project_id', 'campaign_id']`
- TEXT_COLUMNS includes `Artefacts: [{ name: 'approver_ids', uidt: 'SingleLineText' }]`

#### i18n
- Added 12+ keys to both en.json and ar.json for issues board/list and artefacts grid/list/filters/sorting

## Key Files
- `server/server.js` — all API routes, schema definitions, migrations
- `server/nocodb.js` — NocoDB client wrapper
- `client/src/pages/Artefacts.jsx` — artefacts page + ArtefactDetailPanel + ApproverMultiSelect
- `client/src/pages/Issues.jsx` — issues page with board + list views
- `client/src/components/IssueCard.jsx` — card component for issues kanban
- `client/src/contexts/AuthContext.jsx` — auth, permissions, ownership checks
- `client/src/utils/api.js` — API client with normalize function
- `client/src/i18n/en.json` / `ar.json` — translations

## Debugging Tips
- Console logs prefixed with `[POST /artefacts]` and `[PATCH /artefacts/:id]` show approver data flow
- Startup logs show all Artefacts table columns and missing column auto-creation
- NocoDB silently ignores writes to non-existent columns (no error thrown) — this is why approvers appear to save but don't persist
