# Section Management

| | |
|---|---|
| **Nav ID** | `section-management` |
| **Component** | `AdminSectionManagementPage.jsx` |
| **Services** | `formsApi.js` |

---

## Endpoints

See prior endpoint list for `/ptw_section_details` and `/ptw_section_question_map`.

---

## SQL queries

### List sections

```sql
SELECT * FROM `ptw_section_details`;
```

### Get / create / update section

```sql
SELECT * FROM `ptw_section_details` WHERE `section_id` = ? LIMIT 1;

INSERT INTO ptw_section_details (`title`, `created_by`, ...) VALUES (?, ?, ...);

-- update (with audit snapshot to ptw_section_logs first):
INSERT INTO ptw_section_logs (user_id, section_id, title, ..., action, created_at)
SELECT ?, s.section_id, s.title, ..., 'UPDATE', NOW()
FROM ptw_section_details s WHERE s.section_id = ? LIMIT 1;

UPDATE ptw_section_details SET `title` = ? WHERE section_id = ?;
```

### Delete section (transaction)

```sql
INSERT INTO ptw_section_logs (..., action) VALUES (..., 'DELETE');  -- snapshot first

DELETE FROM ptw_section_question_map WHERE section_id = ?;
DELETE FROM ptw_form_section_map WHERE section_id = ?;
DELETE FROM ptw_alternative_sections
WHERE skipable_section_id = ? OR other_section_id = ?;
DELETE FROM ptw_archive_section WHERE section_id = ?;
DELETE FROM ptw_section_details WHERE section_id = ?;
```

### Section–question map counts

```sql
SELECT * FROM `ptw_section_question_map`;

SELECT * FROM `ptw_form_section_map`;   -- form usage counts
```

### Section with questions (view/update sub-pages)

```sql
SELECT * FROM ptw_section_details WHERE section_id = ? LIMIT 1;

SELECT q.question_id, q.question, q.question_type_id, q.allows_media, qt.question_type
FROM ptw_section_question_map m
JOIN ptw_questions q ON q.question_id = m.question_id
LEFT JOIN ptw_question_type qt ON qt.question_type_id = q.question_type_id
WHERE m.section_id = ?
ORDER BY q.question_id;

SELECT question_options_id, question_id, _options
FROM ptw_question_options WHERE question_id IN (?) ORDER BY question_options_id;
```

### Map / unmap question

```sql
INSERT INTO ptw_section_question_map (section_id, question_id) VALUES (?, ?);

DELETE FROM ptw_section_question_map WHERE section_id = ? AND question_id = ?;
```

**Controllers:** `ptwSectionController.js`, `ptwSectionQuestionMapController.js`
