diff --git "a/ref doc/admin_openapi.yaml" "b/ref doc/admin_openapi.yaml" new file mode 100644--- /dev/null +++ "b/ref doc/admin_openapi.yaml" @@ -0,0 +1,3035 @@ +openapi: "3.0.3" + +info: + title: EarWax CRM Admin API + version: "1" + description: | + Session-authenticated REST API for the browser-based admin UI. + All endpoints require an active PHP session (established by logging in through the frontend). + + **URL mapping:** All endpoints share one PHP entry point. The semantic paths in this spec + map to the `?action=` query parameter on the real server URL. + + **Example:** `GET https://yourcrm.com/crm/backend/admin_api.php?action=leads` + + **Auth:** No `Authorization` header. The browser session cookie (`PHPSESSID`) is sent + automatically. The server reads `$_SESSION['crm_user_id']` to identify the caller. + + **Same-origin only:** No CORS headers are set. This API must be called from the same domain. + + **Admin-only endpoints:** Some endpoints are restricted to users with `role = admin`. + Calling them as a `staff` user returns `403 ERR_FORBIDDEN`. + + **Error codes** returned in `error_code` on every failure response: + + | Code | Meaning | + |------|---------| + | `ERR_AUTH` | Session missing or invalid | + | `ERR_FORBIDDEN` | Authenticated but role is insufficient (admin required) | + | `ERR_NOT_FOUND` | Resource does not exist | + | `ERR_VALIDATION` | Missing or invalid input field | + | `ERR_CONFLICT` | Uniqueness constraint violated | + | `ERR_METHOD` | Wrong HTTP method for this action | + | `ERR_DB` | Database error | + | `ERR_SERVER` | Unexpected server error | + +servers: + - url: https://staging.excelhearing.co.uk/crm-dev/backend/admin_api.php + description: "Staging — every path maps to ?action=" + - url: http://localhost/crm-dev/backend/admin_api.php + description: "Local Development" + +security: + - sessionCookie: [] + +tags: + - { name: Auth, description: Profile and password management } + - { name: Leads, description: Lead CRUD, notes, email/SMS triggers } + - { name: FormLeads, description: Elementor form submission leads } + - { name: Appointments,description: Appointment CRUD and email/SMS triggers } + - { name: Shipping, description: Shipping order management } + - { name: Courses, description: Course catalogue CRUD } + - { name: Users, description: CRM user management (admin only) } + - { name: Stats, description: Dashboard, reports, search } + - { name: EmailQueue, description: Email queue management } + - { name: SmsQueue, description: SMS queue management } + - { name: EmailTemplates, description: Email template CRUD } + - { name: SmsTemplates, description: SMS template CRUD } + - { name: Settings, description: General settings (admin only) } + - { name: EmailConfig, description: SMTP / email configuration (admin only) } + - { name: SmsConfig, description: Brevo SMS configuration (admin only) } + - { name: Export, description: CSV export and PDF generation } + - { name: WooCommerce, description: WooCommerce product sync } + +# ═══════════════════════════════════════════════════════════════════════════════ +# Reusable components +# ═══════════════════════════════════════════════════════════════════════════════ + +components: + + securitySchemes: + sessionCookie: + type: apiKey + in: cookie + name: PHPSESSID + description: "PHP session cookie set after logging in through the admin UI." + + parameters: + + PageParam: + name: page + in: query + schema: { type: integer, minimum: 1, default: 1 } + + IdQuery: + name: id + in: query + required: true + schema: { type: integer } + + schemas: + + # ── Enums ──────────────────────────────────────────────────────────────── + + LeadStatus: + type: string + enum: [new, enrolled, not_interested, planned_later, no_answer, refunded, closed] + + AppointmentStatus: + type: string + enum: [pending, paid, confirmed, attended, cancelled, refunded, no-show] + + ShippingStatus: + type: string + enum: [pending, processing, shipped, delivered, returned] + + UserRole: + type: string + enum: [admin, staff] + + # ── Response envelopes ─────────────────────────────────────────────────── + + SuccessEnvelope: + type: object + properties: + success: { type: boolean, example: true } + message: { type: string } + data: { type: object } + + ErrorEnvelope: + type: object + properties: + success: { type: boolean, example: false } + message: { type: string } + error_code: + type: string + enum: [ERR_AUTH, ERR_FORBIDDEN, ERR_NOT_FOUND, ERR_VALIDATION, ERR_CONFLICT, ERR_METHOD, ERR_DB, ERR_SERVER] + data: { type: object } + + # ── Domain entities ────────────────────────────────────────────────────── + + User: + type: object + properties: + id: { type: integer, readOnly: true } + username: { type: string } + full_name: { type: string } + email: { type: string, format: email, nullable: true } + role: { $ref: '#/components/schemas/UserRole' } + is_active: { type: integer, enum: [0, 1] } + last_login: { type: string, format: date-time, readOnly: true, nullable: true } + + LeadListItem: + type: object + description: "Lightweight lead row returned in list responses" + properties: + id: { type: integer } + first_name: { type: string } + last_name: { type: string } + email: { type: string } + phone: { type: string } + status: { $ref: '#/components/schemas/LeadStatus' } + source: { type: string } + gender: { type: string } + qualification: { type: string } + city: { type: string } + postcode: { type: string } + created_at: { type: string, format: date-time } + lead_date: { type: string, format: date } + woo_order_id: { type: string, nullable: true } + woo_order_status: { type: string, nullable: true } + woo_product_name: { type: string, nullable: true } + appointment_count: { type: integer } + + ActivityEntry: + type: object + properties: + action: { type: string } + description: { type: string } + created_by: { type: string } + created_at: { type: string, format: date-time } + + EmailHistoryItem: + type: object + properties: + id: { type: integer } + rule_name: { type: string } + template_title: { type: string } + subject: { type: string } + status: { type: string, enum: [pending, sent, failed, cancelled] } + scheduled_at: { type: string, format: date-time } + sent_at: { type: string, format: date-time, nullable: true } + triggered_status: { type: string } + + SmsHistoryItem: + type: object + properties: + id: { type: integer } + rule_name: { type: string } + template_name: { type: string } + rendered_message: { type: string } + status: { type: string, enum: [pending, sent, failed, cancelled] } + scheduled_at: { type: string, format: date-time } + sent_at: { type: string, format: date-time, nullable: true } + + AppointmentListItem: + type: object + properties: + id: { type: integer } + appointment_ref: { type: string } + appointment_date: { type: string, format: date } + appointment_time: { type: string } + status: { $ref: '#/components/schemas/AppointmentStatus' } + amount_paid: { type: number } + transaction_fee: { type: number } + payment_method: { type: string } + payment_date: { type: string, format: date, nullable: true } + training_location: { type: string, nullable: true } + course_type: { type: string, nullable: true } + duration: { type: string } + archived: { type: integer, enum: [0, 1] } + course_name: { type: string } + lead_id: { type: integer } + student_name: { type: string } + student_email: { type: string } + student_phone: { type: string } + + AppointmentStats: + type: object + properties: + total: { type: integer } + upcoming: { type: integer } + attended: { type: integer } + no_show: { type: integer } + revenue: { type: string, example: "1250.00" } + + Course: + type: object + properties: + id: { type: integer } + name: { type: string } + location: { type: string } + course_type: { type: string } + price: { type: number } + duration: { type: string } + max_capacity: { type: integer } + is_active: { type: integer, enum: [0, 1] } + description: { type: string } + + EmailTemplate: + type: object + properties: + id: { type: integer } + title: { type: string } + subject: { type: string } + body_html: { type: string } + is_active: { type: integer, enum: [0, 1] } + category: { type: string, enum: [forms, orders, schedule, shipping] } + condition_count: { type: integer } + sent_count: { type: integer } + pending_count: { type: integer } + failed_count: { type: integer } + + EmailCondition: + type: object + properties: + id: { type: integer } + template_id: { type: integer } + attendance_status: { type: string, nullable: true } + course_type: { type: string, nullable: true } + days_after: { type: integer } + send_time: { type: string, example: "09:00:00" } + direction: { type: string, enum: [after, before, today, immediate, on_date] } + send_once_per_lead: { type: integer, enum: [0, 1] } + is_active: { type: integer, enum: [0, 1] } + specific_date: { type: string, format: date, nullable: true } + + SmsTemplate: + type: object + properties: + id: { type: integer } + name: { type: string } + body_text: { type: string } + is_active: { type: integer, enum: [0, 1] } + category: { type: string, enum: [forms, orders, schedule] } + condition_count: { type: integer } + sent_count: { type: integer } + pending_count: { type: integer } + failed_count: { type: integer } + + TriggerCheck: + type: object + description: "Result of checking/firing email or SMS triggers after a status change" + properties: + queued: { type: integer } + + Pagination: + type: object + properties: + total: { type: integer } + page: { type: integer } + per_page: { type: integer } + total_pages: { type: integer } + + # ── Reusable responses ───────────────────────────────────────────────────── + + responses: + + Unauthorised: + description: Session missing or invalid + content: + application/json: + schema: { $ref: '#/components/schemas/ErrorEnvelope' } + example: { success: false, message: "Session expired. Please log in.", error_code: ERR_AUTH, data: {} } + + Forbidden: + description: Authenticated but role is insufficient (admin required) + content: + application/json: + schema: { $ref: '#/components/schemas/ErrorEnvelope' } + example: { success: false, message: "Admin role required.", error_code: ERR_FORBIDDEN, data: {} } + + NotFound: + description: Resource does not exist + content: + application/json: + schema: { $ref: '#/components/schemas/ErrorEnvelope' } + + ValidationError: + description: Missing or invalid input + content: + application/json: + schema: { $ref: '#/components/schemas/ErrorEnvelope' } + + Conflict: + description: Uniqueness constraint violated + content: + application/json: + schema: { $ref: '#/components/schemas/ErrorEnvelope' } + + SimpleOk: + description: Action completed successfully + content: + application/json: + schema: { $ref: '#/components/schemas/SuccessEnvelope' } + + PdfDownload: + description: PDF file download + headers: + Content-Disposition: + schema: { type: string, example: 'attachment; filename="invoice-APT-A3B2.pdf"' } + content: + application/pdf: + schema: { type: string, format: binary } + +# ═══════════════════════════════════════════════════════════════════════════════ +# Paths +# ═══════════════════════════════════════════════════════════════════════════════ + +paths: + + # ── Auth / Profile ───────────────────────────────────────────────────────── + + /auth/me: + get: + operationId: adminMe + summary: Get current session user's profile + description: "**Real URL:** `GET ?action=me`" + tags: [Auth] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [me] } + responses: + "200": + description: Current user + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + user: { $ref: '#/components/schemas/User' } + "401": { $ref: '#/components/responses/Unauthorised' } + + /auth/update-profile: + post: + operationId: adminUpdateProfile + summary: Update own profile (patch-style) + description: "**Real URL:** `POST ?action=update_profile`" + tags: [Auth] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [update_profile] } + requestBody: + required: true + content: + application/json: + schema: + type: object + minProperties: 1 + properties: + full_name: { type: string } + email: { type: string, format: email } + username: { type: string } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "409": { $ref: '#/components/responses/Conflict' } + + /auth/change-password: + post: + operationId: adminChangePassword + summary: Change own password + description: "**Real URL:** `POST ?action=change_password`" + tags: [Auth] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [change_password] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [current_password, new_password] + properties: + current_password: { type: string, format: password } + new_password: { type: string, format: password, minLength: 8 } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + # ── Leads ────────────────────────────────────────────────────────────────── + + /leads: + get: + operationId: adminListLeads + summary: Paginated lead list with duplicate detection + description: "**Real URL:** `GET ?action=leads`" + tags: [Leads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [leads] } + - $ref: '#/components/parameters/PageParam' + - name: status + in: query + schema: { $ref: '#/components/schemas/LeadStatus' } + - name: search + in: query + schema: { type: string, description: "Matches first_name, last_name, email, phone, or full name" } + - name: sort + in: query + schema: { type: string, enum: [created_at, lead_date, first_name, status, woo_order_id, lead_type, order_status], default: created_at } + - name: dir + in: query + schema: { type: string, enum: [asc, desc], default: desc } + - name: archived + in: query + schema: { type: integer, enum: [0, 1], default: 0 } + - name: dupes + in: query + schema: { type: integer, enum: [0, 1], description: "Set to 1 to show only leads with duplicate email/phone/name" } + responses: + "200": + description: Paginated leads + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + allOf: + - $ref: '#/components/schemas/Pagination' + - type: object + properties: + leads: { type: array, items: { $ref: '#/components/schemas/LeadListItem' } } + dup_emails: { type: object, description: "email → count map for duplicates" } + dup_phones: { type: object, description: "phone → count map for duplicates" } + dup_names: { type: object, description: "full_name → count map for duplicates" } + "401": { $ref: '#/components/responses/Unauthorised' } + + /leads/detail: + get: + operationId: adminGetLead + summary: Single lead with appointments, activity log, email and SMS history + description: "**Real URL:** `GET ?action=lead&id=`" + tags: [Leads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [lead] } + - $ref: '#/components/parameters/IdQuery' + responses: + "200": + description: Lead detail + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + lead: { type: object, description: "Full leads row (all columns)" } + appointments: { type: array, items: { $ref: '#/components/schemas/AppointmentListItem' } } + activity: { type: array, items: { $ref: '#/components/schemas/ActivityEntry' } } + email_history: { type: array, items: { $ref: '#/components/schemas/EmailHistoryItem' } } + sms_history: { type: array, items: { $ref: '#/components/schemas/SmsHistoryItem' } } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /leads/create: + post: + operationId: adminCreateLead + summary: Create a new lead + description: | + Generates a `submission_id` with prefix `ADM-`. Fires a push notification to admin devices. + **Real URL:** `POST ?action=lead_create` + tags: [Leads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [lead_create] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [first_name, email] + properties: + first_name: { type: string } + last_name: { type: string } + email: { type: string, format: email } + phone: { type: string } + gender: { type: string } + qualification: { type: string } + experience: { type: string } + address_line1: { type: string } + address_line2: { type: string } + city: { type: string } + county: { type: string } + postcode: { type: string } + country: { type: string, default: "United Kingdom" } + preferred_name:{ type: string } + device_type: { type: string } + status: { $ref: '#/components/schemas/LeadStatus' } + source: { type: string, default: "Admin" } + notes: { type: string } + lead_date: { type: string, format: date } + responses: + "201": + description: Lead created + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + id: { type: integer } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + + /leads/update: + post: + operationId: adminUpdateLead + summary: Update lead fields (patch-style) + description: | + Only send the fields you want to change. If `status` changes, email/SMS triggers fire. + **Real URL:** `POST ?action=lead_update` + tags: [Leads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [lead_update] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + first_name: { type: string } + last_name: { type: string } + email: { type: string, format: email } + phone: { type: string } + gender: { type: string } + preferred_name: { type: string } + qualification: { type: string } + experience: { type: string } + device_type: { type: string } + source: { type: string } + address_line1: { type: string } + address_line2: { type: string } + city: { type: string } + county: { type: string } + postcode: { type: string } + country: { type: string } + shipping_address_line1: { type: string } + shipping_address_line2: { type: string } + shipping_city: { type: string } + shipping_county: { type: string } + shipping_postcode: { type: string } + shipping_country: { type: string } + shipping_phone: { type: string } + notes: { type: string } + lead_date: { type: string, format: date } + woo_order_id: { type: string } + woo_order_status: { type: string } + woo_product_name: { type: string } + status: { $ref: '#/components/schemas/LeadStatus' } + responses: + "200": + description: Lead updated + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + lead: { type: object } + email_check: { $ref: '#/components/schemas/TriggerCheck', nullable: true } + sms_check: { $ref: '#/components/schemas/TriggerCheck', nullable: true } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /leads/update-status: + post: + operationId: adminUpdateLeadStatus + summary: Update lead status and check email/SMS triggers + description: "**Real URL:** `POST ?action=lead_update_status`" + tags: [Leads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [lead_update_status] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id, status] + properties: + id: { type: integer } + status: { $ref: '#/components/schemas/LeadStatus' } + responses: + "200": + description: Status updated + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + email_check: { $ref: '#/components/schemas/TriggerCheck' } + sms_check: { $ref: '#/components/schemas/TriggerCheck' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /leads/add-note: + post: + operationId: adminAddLeadNote + summary: Append a timestamped note to a lead + description: "**Real URL:** `POST ?action=lead_add_note`" + tags: [Leads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [lead_add_note] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id, note] + properties: + id: { type: integer } + note: { type: string } + responses: + "200": + description: Note appended + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + note: { type: string, description: "Full updated notes field value" } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /leads/archive: + post: + operationId: adminArchiveLead + summary: Archive or unarchive a lead (admin only) + description: | + Cascades to all linked appointments. Cancels pending emails when archiving. + **Real URL:** `POST ?action=lead_archive` + tags: [Leads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [lead_archive] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + archive: { type: boolean, default: true } + responses: + "200": + description: Archived/unarchived + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + id: { type: integer } + archived: { type: boolean } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "404": { $ref: '#/components/responses/NotFound' } + + /leads/delete: + post: + operationId: adminDeleteLead + summary: Permanently delete a lead and its appointments (admin only) + description: "**Real URL:** `POST ?action=lead_delete`" + tags: [Leads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [lead_delete] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + responses: + "200": + description: Deleted + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + deleted: { type: boolean, example: true } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "404": { $ref: '#/components/responses/NotFound' } + + /leads/cancel-emails: + post: + operationId: adminLeadCancelEmails + summary: Cancel pending emails for a lead by queue IDs + description: "**Real URL:** `POST ?action=lead_cancel_emails`" + tags: [Leads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [lead_cancel_emails] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [queue_ids] + properties: + queue_ids: { type: array, items: { type: integer } } + responses: + "200": + description: Cancelled + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + cancelled: { type: integer } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + + /leads/cancel-sms: + post: + operationId: adminLeadCancelSms + summary: Cancel pending SMS messages for a lead by queue IDs + description: "**Real URL:** `POST ?action=lead_cancel_sms`" + tags: [Leads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [lead_cancel_sms] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [queue_ids] + properties: + queue_ids: { type: array, items: { type: integer } } + responses: + "200": + description: Cancelled + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + cancelled: { type: integer } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + + /leads/trigger-email: + post: + operationId: adminLeadTriggerEmail + summary: Force-fire email templates for a lead status + description: "**Real URL:** `POST ?action=lead_trigger_email`" + tags: [Leads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [lead_trigger_email] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [lead_id, status] + properties: + lead_id: { type: integer } + status: { $ref: '#/components/schemas/LeadStatus' } + responses: + "200": + description: Triggered + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + queued: { type: integer } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + + /leads/trigger-sms: + post: + operationId: adminLeadTriggerSms + summary: Force-fire SMS templates for a lead status + description: "**Real URL:** `POST ?action=lead_trigger_sms`" + tags: [Leads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [lead_trigger_sms] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [lead_id, status] + properties: + lead_id: { type: integer } + status: { $ref: '#/components/schemas/LeadStatus' } + responses: + "200": + description: Triggered + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + queued: { type: integer } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + + # ── Form Leads ───────────────────────────────────────────────────────────── + + /form-leads: + get: + operationId: adminListFormLeads + summary: Paginated Elementor form submission leads + description: "**Real URL:** `GET ?action=form_leads`" + tags: [FormLeads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [form_leads] } + - $ref: '#/components/parameters/PageParam' + - name: search + in: query + schema: { type: string } + - name: status + in: query + schema: { $ref: '#/components/schemas/LeadStatus' } + responses: + "200": + description: Form leads list + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + allOf: + - $ref: '#/components/schemas/Pagination' + - type: object + properties: + leads: { type: array, items: { $ref: '#/components/schemas/LeadListItem' } } + "401": { $ref: '#/components/responses/Unauthorised' } + + /form-leads/archive: + post: + operationId: adminFormLeadArchive + summary: Archive or unarchive a form lead (admin only) + description: "**Real URL:** `POST ?action=form_lead_archive`" + tags: [FormLeads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [form_lead_archive] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + archive: { type: boolean, default: true } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /form-leads/delete: + post: + operationId: adminFormLeadDelete + summary: Permanently delete a form lead (admin only) + description: "**Real URL:** `POST ?action=form_lead_delete`" + tags: [FormLeads] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [form_lead_delete] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + # ── Appointments ─────────────────────────────────────────────────────────── + + /appointments: + get: + operationId: adminListAppointments + summary: Paginated appointment list with stats and filter options + description: "**Real URL:** `GET ?action=appointments`" + tags: [Appointments] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [appointments] } + - $ref: '#/components/parameters/PageParam' + - name: view + in: query + schema: { type: string, enum: [upcoming, today, past, all] } + - name: status + in: query + schema: { $ref: '#/components/schemas/AppointmentStatus' } + - name: location + in: query + schema: { type: string } + - name: course + in: query + schema: { type: integer, description: "course_id filter" } + - name: date_from + in: query + schema: { type: string, format: date } + - name: date_to + in: query + schema: { type: string, format: date } + - name: search + in: query + schema: { type: string, description: "Matches name, email, phone, appointment_ref" } + responses: + "200": + description: Appointment list + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + allOf: + - $ref: '#/components/schemas/Pagination' + - type: object + properties: + appointments: { type: array, items: { $ref: '#/components/schemas/AppointmentListItem' } } + stats: { $ref: '#/components/schemas/AppointmentStats' } + locations: { type: array, items: { type: string } } + courses: { type: array, items: { $ref: '#/components/schemas/Course' } } + "401": { $ref: '#/components/responses/Unauthorised' } + + /appointments/detail: + get: + operationId: adminGetAppointment + summary: Single appointment with activity log, email and SMS history + description: "**Real URL:** `GET ?action=appointment&id=`" + tags: [Appointments] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [appointment] } + - $ref: '#/components/parameters/IdQuery' + responses: + "200": + description: Appointment detail + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + appointment: { type: object, description: "Full row with joined lead and course fields" } + activity: { type: array, items: { $ref: '#/components/schemas/ActivityEntry' } } + email_history: { type: array, items: { $ref: '#/components/schemas/EmailHistoryItem' } } + sms_history: { type: array, items: { $ref: '#/components/schemas/SmsHistoryItem' } } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /appointments/create: + post: + operationId: adminCreateAppointment + summary: Create an appointment + description: | + If `course_id` is provided, `training_location` and `training_address` default to the course's values. + Fires email/SMS triggers for the initial status. Sends push notification to admin devices. + **Real URL:** `POST ?action=appointment_create` + tags: [Appointments] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [appointment_create] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [lead_id, appointment_date] + properties: + lead_id: { type: integer } + course_id: { type: integer } + appointment_date: { type: string, format: date } + appointment_time: { type: string, default: "09:00", example: "10:30" } + duration: { type: string, default: "1 day" } + amount_paid: { type: number, default: 0 } + transaction_fee: { type: number, default: 0 } + payment_method: { type: string, default: "Card" } + payment_ref: { type: string } + payment_date: { type: string, format: date } + status: { $ref: '#/components/schemas/AppointmentStatus' } + notes: { type: string } + training_location: { type: string } + training_address: { type: string } + responses: + "201": + description: Appointment created + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + id: { type: integer } + appointment_ref: { type: string, example: "APT-A3B2C1" } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /appointments/update: + post: + operationId: adminUpdateAppointment + summary: Update appointment fields (patch-style) + description: | + If `status` changes, email/SMS triggers fire automatically. + **Real URL:** `POST ?action=appointment_update` + tags: [Appointments] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [appointment_update] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + appointment_date: { type: string, format: date } + appointment_time: { type: string } + duration: { type: string } + training_location: { type: string } + training_address: { type: string } + amount_paid: { type: number } + transaction_fee: { type: number } + payment_method: { type: string } + payment_ref: { type: string } + payment_date: { type: string, format: date } + notes: { type: string } + course_id: { type: integer } + status: { $ref: '#/components/schemas/AppointmentStatus' } + responses: + "200": + description: Updated + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + appointment: { type: object } + email_check: { $ref: '#/components/schemas/TriggerCheck', nullable: true } + sms_check: { $ref: '#/components/schemas/TriggerCheck', nullable: true } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /appointments/update-status: + post: + operationId: adminUpdateAppointmentStatus + summary: Update appointment status and check triggers + description: "**Real URL:** `POST ?action=appointment_update_status`" + tags: [Appointments] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [appointment_update_status] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id, status] + properties: + id: { type: integer } + status: { $ref: '#/components/schemas/AppointmentStatus' } + responses: + "200": + description: Status updated + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + email_check: { $ref: '#/components/schemas/TriggerCheck' } + sms_check: { $ref: '#/components/schemas/TriggerCheck' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /appointments/archive: + post: + operationId: adminArchiveAppointment + summary: Archive or unarchive an appointment (admin only) + description: "**Real URL:** `POST ?action=appointment_archive`" + tags: [Appointments] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [appointment_archive] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + archive: { type: boolean, default: true } + responses: + "200": + description: Archived/unarchived + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + id: { type: integer } + archived: { type: boolean } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "404": { $ref: '#/components/responses/NotFound' } + + /appointments/delete: + post: + operationId: adminDeleteAppointment + summary: Permanently delete an appointment (admin only) + description: "**Real URL:** `POST ?action=appointment_delete`" + tags: [Appointments] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [appointment_delete] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "404": { $ref: '#/components/responses/NotFound' } + + /appointments/cancel-emails: + post: + operationId: adminAppointmentCancelEmails + summary: Cancel pending emails for an appointment by queue IDs + description: "**Real URL:** `POST ?action=appointment_cancel_emails`" + tags: [Appointments] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [appointment_cancel_emails] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [queue_ids] + properties: + queue_ids: { type: array, items: { type: integer } } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + + /appointments/cancel-sms: + post: + operationId: adminAppointmentCancelSms + summary: Cancel pending SMS messages for an appointment by queue IDs + description: "**Real URL:** `POST ?action=appointment_cancel_sms`" + tags: [Appointments] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [appointment_cancel_sms] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [queue_ids] + properties: + queue_ids: { type: array, items: { type: integer } } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + + /appointments/trigger-email: + post: + operationId: adminAppointmentTriggerEmail + summary: Force-fire email templates for an appointment status + description: "**Real URL:** `POST ?action=appointment_trigger_email`" + tags: [Appointments] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [appointment_trigger_email] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [appt_id, status] + properties: + appt_id: { type: integer } + status: { $ref: '#/components/schemas/AppointmentStatus' } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + + /appointments/trigger-sms: + post: + operationId: adminAppointmentTriggerSms + summary: Force-fire SMS templates for an appointment status + description: "**Real URL:** `POST ?action=appointment_trigger_sms`" + tags: [Appointments] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [appointment_trigger_sms] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [appt_id, status] + properties: + appt_id: { type: integer } + status: { $ref: '#/components/schemas/AppointmentStatus' } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + + # ── Shipping ─────────────────────────────────────────────────────────────── + + /shipping: + get: + operationId: adminListShipping + summary: Paginated shipping orders (Training + Equipment appointments) + description: "**Real URL:** `GET ?action=shipping`" + tags: [Shipping] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [shipping] } + - $ref: '#/components/parameters/PageParam' + - name: status + in: query + schema: { $ref: '#/components/schemas/ShippingStatus' } + - name: search + in: query + schema: { type: string } + responses: + "200": + description: Shipping orders + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + allOf: + - $ref: '#/components/schemas/Pagination' + - type: object + properties: + orders: { type: array, items: { type: object } } + "401": { $ref: '#/components/responses/Unauthorised' } + + /shipping/update-status: + post: + operationId: adminShippingUpdateStatus + summary: Update shipping status for an appointment + description: "**Real URL:** `POST ?action=shipping_update_status`" + tags: [Shipping] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [shipping_update_status] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [appt_id, status] + properties: + appt_id: { type: integer } + status: { $ref: '#/components/schemas/ShippingStatus' } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /shipping/update-address: + post: + operationId: adminShippingUpdateAddress + summary: Update shipping address for an appointment + description: "**Real URL:** `POST ?action=shipping_update_address`" + tags: [Shipping] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [shipping_update_address] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [appt_id] + properties: + appt_id: { type: integer } + shipping_first_name: { type: string } + shipping_last_name: { type: string } + shipping_address_line1: { type: string } + shipping_address_line2: { type: string } + shipping_city: { type: string } + shipping_county: { type: string } + shipping_postcode: { type: string } + shipping_country: { type: string } + shipping_phone: { type: string } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /shipping/archive: + post: + operationId: adminShippingArchive + summary: Archive or unarchive a shipping appointment (admin only) + description: "**Real URL:** `POST ?action=shipping_archive`" + tags: [Shipping] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [shipping_archive] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [appt_id] + properties: + appt_id: { type: integer } + archive: { type: boolean, default: true } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /shipping/delete: + post: + operationId: adminShippingDelete + summary: Delete a shipping appointment (admin only) + description: "**Real URL:** `POST ?action=shipping_delete`" + tags: [Shipping] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [shipping_delete] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [appt_id] + properties: + appt_id: { type: integer } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + # ── Courses ──────────────────────────────────────────────────────────────── + + /courses: + get: + operationId: adminListCourses + summary: All courses including inactive + description: "**Real URL:** `GET ?action=courses`" + tags: [Courses] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [courses] } + responses: + "200": + description: Course list + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + courses: { type: array, items: { $ref: '#/components/schemas/Course' } } + total: { type: integer } + "401": { $ref: '#/components/responses/Unauthorised' } + + /courses/create: + post: + operationId: adminCreateCourse + summary: Create a course (admin only) + description: "**Real URL:** `POST ?action=course_create`" + tags: [Courses] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [course_create] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [name] + properties: + name: { type: string } + location: { type: string } + course_type: { type: string } + price: { type: number } + duration: { type: string } + max_capacity: { type: integer } + description: { type: string } + is_active: { type: integer, enum: [0, 1], default: 1 } + responses: + "201": + description: Course created + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + id: { type: integer } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /courses/update: + post: + operationId: adminUpdateCourse + summary: Update course fields (patch-style, admin only) + description: "**Real URL:** `POST ?action=course_update`" + tags: [Courses] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [course_update] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + name: { type: string } + location: { type: string } + course_type: { type: string } + price: { type: number } + duration: { type: string } + max_capacity: { type: integer } + description: { type: string } + is_active: { type: integer, enum: [0, 1] } + responses: + "200": + description: Updated + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + course: { $ref: '#/components/schemas/Course' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "404": { $ref: '#/components/responses/NotFound' } + + /courses/delete: + post: + operationId: adminDeleteCourse + summary: Delete a course (admin only) — blocked if appointments are linked + description: "**Real URL:** `POST ?action=course_delete`" + tags: [Courses] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [course_delete] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "404": { $ref: '#/components/responses/NotFound' } + "409": { $ref: '#/components/responses/Conflict' } + + /courses/toggle: + post: + operationId: adminToggleCourse + summary: Toggle course is_active flag (admin only) + description: "**Real URL:** `POST ?action=course_toggle`" + tags: [Courses] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [course_toggle] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + responses: + "200": + description: Toggled + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + id: { type: integer } + is_active: { type: boolean } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "404": { $ref: '#/components/responses/NotFound' } + + # ── Users ────────────────────────────────────────────────────────────────── + + /users: + get: + operationId: adminListUsers + summary: List all CRM users (admin only) + description: "**Real URL:** `GET ?action=users`" + tags: [Users] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [users] } + responses: + "200": + description: All users + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + users: { type: array, items: { $ref: '#/components/schemas/User' } } + total: { type: integer } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /users/create: + post: + operationId: adminCreateUser + summary: Create a CRM user (admin only) + description: "**Real URL:** `POST ?action=user_create`" + tags: [Users] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [user_create] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [username, password, full_name] + properties: + username: { type: string } + password: { type: string, format: password, minLength: 8 } + full_name: { type: string } + email: { type: string, format: email } + role: { $ref: '#/components/schemas/UserRole' } + responses: + "201": + description: User created + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + user_id: { type: integer } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "409": { $ref: '#/components/responses/Conflict' } + + /users/update: + post: + operationId: adminUpdateUser + summary: Update a CRM user (patch-style, admin only) + description: "**Real URL:** `POST ?action=user_update`" + tags: [Users] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [user_update] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + username: { type: string } + full_name: { type: string } + email: { type: string, format: email } + role: { $ref: '#/components/schemas/UserRole' } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "404": { $ref: '#/components/responses/NotFound' } + "409": { $ref: '#/components/responses/Conflict' } + + /users/deactivate: + post: + operationId: adminDeactivateUser + summary: Deactivate or reactivate a user (admin only) + description: | + Cannot target own account. Send `active: true` to reactivate. + Deactivating invalidates all JWT tokens for that user. + **Real URL:** `POST ?action=user_deactivate` + tags: [Users] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [user_deactivate] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + active: { type: boolean, description: "true to reactivate; omit or false to deactivate" } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "404": { $ref: '#/components/responses/NotFound' } + + # ── Stats ────────────────────────────────────────────────────────────────── + + /stats/dashboard: + get: + operationId: adminDashboard + summary: KPI stats, upcoming appointments, recent leads, automation counts + description: "**Real URL:** `GET ?action=dashboard`" + tags: [Stats] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [dashboard] } + responses: + "200": + description: Dashboard data + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + stats: + type: object + properties: + total_leads: { type: integer } + enrolled: { type: integer } + new_today: { type: integer } + upcoming_appointments: { type: integer } + today_appointments: { type: integer } + total_revenue: { type: number } + month_revenue: { type: number } + email_pending: { type: integer } + sms_pending: { type: integer } + upcoming: { type: array, items: { type: object } } + recent_leads: { type: array, items: { type: object } } + "401": { $ref: '#/components/responses/Unauthorised' } + + /stats/reports: + get: + operationId: adminReports + summary: Full report data with optional date range (admin only) + description: "**Real URL:** `GET ?action=reports`" + tags: [Stats] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [reports] } + - name: date_from + in: query + schema: { type: string, format: date } + - name: date_to + in: query + schema: { type: string, format: date } + responses: + "200": + description: Report data + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + summary: + type: object + properties: + total_bookings: { type: integer } + gross_revenue: { type: string } + net_revenue: { type: string } + avg_booking_value: { type: string } + monthly: { type: array, items: { type: object, properties: { ym: { type: string }, label: { type: string }, bookings: { type: integer }, gross: { type: string }, net: { type: string } } } } + by_location: { type: array, items: { type: object, properties: { location: { type: string }, bookings: { type: integer }, revenue: { type: string } } } } + by_status: { type: array, items: { type: object, properties: { status: { type: string }, count: { type: integer } } } } + by_source: { type: array, items: { type: object, properties: { source: { type: string }, count: { type: integer } } } } + by_qualification: { type: array, items: { type: object, properties: { qualification: { type: string }, count: { type: integer } } } } + by_course: { type: array, items: { type: object, properties: { course_name: { type: string }, bookings: { type: integer }, revenue: { type: string } } } } + filters: { type: object, properties: { date_from: { type: string, nullable: true }, date_to: { type: string, nullable: true } } } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /search: + get: + operationId: adminSearch + summary: Cross-entity search across leads and appointments + description: | + Returns up to 20 matching leads and 10 matching appointments. + **Real URL:** `GET ?action=search&q=` + tags: [Stats] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [search] } + - name: q + in: query + required: true + schema: { type: string, minLength: 2 } + responses: + "200": + description: Search results + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + results: { type: array, items: { type: object } } + appointments: { type: array, items: { type: object } } + count: { type: integer } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + + # ── Email Queue ──────────────────────────────────────────────────────────── + + /email/logs: + get: + operationId: adminEmailLogs + summary: Email logs — Brevo history or pending queue + description: | + `tab=brevo` fetches from Brevo API (last 7 days by default, up to 100 rows). + `tab=pending` fetches from local `email_queue` table. + **Real URL:** `GET ?action=email_logs` + tags: [EmailQueue] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [email_logs] } + - name: tab + in: query + schema: { type: string, enum: [brevo, pending], default: brevo } + - name: bpage + in: query + schema: { type: integer, description: "Page offset for Brevo results (0-based)", default: 0 } + - name: email + in: query + schema: { type: string, description: "Filter by recipient email" } + - name: event + in: query + schema: { type: string, description: "Brevo event type filter (e.g. delivered, opened, bounced)" } + - name: date_from + in: query + schema: { type: string, format: date } + - name: date_to + in: query + schema: { type: string, format: date } + - $ref: '#/components/parameters/PageParam' + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "401": { $ref: '#/components/responses/Unauthorised' } + + /email/preview: + get: + operationId: adminEmailPreview + summary: Preview a queued email's rendered body + description: "**Real URL:** `GET ?action=email_preview&id=`" + tags: [EmailQueue] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [email_preview] } + - $ref: '#/components/parameters/IdQuery' + responses: + "200": + description: Email content + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + body_html: { type: string } + subject: { type: string } + recipient_name: { type: string } + recipient_email: { type: string } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /email/queue-cancel: + post: + operationId: adminEmailQueueCancel + summary: Cancel a single pending email (admin only) + description: "**Real URL:** `POST ?action=email_queue_cancel`" + tags: [EmailQueue] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [email_queue_cancel] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [queue_id] + properties: + queue_id: { type: integer } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /email/queue-clear: + post: + operationId: adminEmailQueueClear + summary: Bulk-clear sent or failed emails from queue (admin only) + description: "**Real URL:** `POST ?action=email_queue_clear`" + tags: [EmailQueue] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [email_queue_clear] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [type] + properties: + type: { type: string, enum: [sent, failed] } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + # ── SMS Queue ────────────────────────────────────────────────────────────── + + /sms/logs: + get: + operationId: adminSmsLogs + summary: SMS queue history + description: "**Real URL:** `GET ?action=sms_logs`" + tags: [SmsQueue] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [sms_logs] } + - $ref: '#/components/parameters/PageParam' + - name: status + in: query + schema: { type: string, enum: [pending, sent, failed, cancelled] } + - name: search + in: query + schema: { type: string } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "401": { $ref: '#/components/responses/Unauthorised' } + + /sms/queue-cancel: + post: + operationId: adminSmsQueueCancel + summary: Cancel a single pending SMS (admin only) + description: "**Real URL:** `POST ?action=sms_queue_cancel`" + tags: [SmsQueue] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [sms_queue_cancel] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [queue_id] + properties: + queue_id: { type: integer } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /sms/queue-clear: + post: + operationId: adminSmsQueueClear + summary: Bulk-clear sent or failed SMS from queue (admin only) + description: "**Real URL:** `POST ?action=sms_queue_clear`" + tags: [SmsQueue] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [sms_queue_clear] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [type] + properties: + type: { type: string, enum: [sent, failed] } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + # ── Email Templates ──────────────────────────────────────────────────────── + + /email-templates: + get: + operationId: adminListEmailTemplates + summary: All email templates with usage counts + description: "**Real URL:** `GET ?action=email_templates`" + tags: [EmailTemplates] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [email_templates] } + responses: + "200": + description: Template list + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + templates: { type: array, items: { $ref: '#/components/schemas/EmailTemplate' } } + total: { type: integer } + "401": { $ref: '#/components/responses/Unauthorised' } + + /email-templates/detail: + get: + operationId: adminGetEmailTemplate + summary: Single email template with all conditions + description: "**Real URL:** `GET ?action=email_template&id=`" + tags: [EmailTemplates] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [email_template] } + - $ref: '#/components/parameters/IdQuery' + responses: + "200": + description: Template detail + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + template: { $ref: '#/components/schemas/EmailTemplate' } + conditions: { type: array, items: { $ref: '#/components/schemas/EmailCondition' } } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /email-templates/save: + post: + operationId: adminSaveEmailTemplate + summary: Create or update an email template (admin only) + description: | + Include `id` to update; omit to create. Conditions array replaces all existing conditions. + **Real URL:** `POST ?action=email_template_save` + tags: [EmailTemplates] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [email_template_save] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [name, subject] + properties: + id: { type: integer, description: "Omit to create; include to update" } + name: { type: string, description: "Also accepted as 'title'" } + subject: { type: string } + body_html: { type: string } + is_active: { type: integer, enum: [0, 1], default: 1 } + category: { type: string, enum: [forms, orders, schedule, shipping], default: orders } + conditions: + type: array + items: + type: object + properties: + attendance_status: { type: string, nullable: true } + course_type: { type: string, nullable: true } + days_after: { type: integer, default: 0 } + send_time: { type: string, example: "09:00" } + direction: { type: string, enum: [after, before, today, immediate, on_date] } + is_active: { type: integer, enum: [0, 1], default: 1 } + specific_date: { type: string, format: date, nullable: true } + responses: + "200": + description: Saved + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + id: { type: integer } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /email-templates/delete: + post: + operationId: adminDeleteEmailTemplate + summary: Delete an email template and its conditions (admin only) + description: "**Real URL:** `POST ?action=email_template_delete`" + tags: [EmailTemplates] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [email_template_delete] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /email-templates/toggle: + post: + operationId: adminToggleEmailTemplate + summary: Toggle email template is_active flag (admin only) + description: "**Real URL:** `POST ?action=email_template_toggle`" + tags: [EmailTemplates] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [email_template_toggle] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + responses: + "200": + description: Toggled + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + id: { type: integer } + is_active: { type: boolean } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "404": { $ref: '#/components/responses/NotFound' } + + /email-templates/import: + post: + operationId: adminImportEmailTemplate + summary: Import an email template from JSON string (admin only) + description: | + JSON must contain at minimum a `title` key. + **Real URL:** `POST ?action=email_template_import` + tags: [EmailTemplates] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [email_template_import] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [json] + properties: + json: { type: string, description: "JSON-encoded template object with title, subject, body_html, conditions" } + responses: + "200": + description: Imported + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + id: { type: integer } + imported: { type: integer, description: "Number of conditions imported" } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + # ── SMS Templates ────────────────────────────────────────────────────────── + + /sms-templates: + get: + operationId: adminListSmsTemplates + summary: All SMS templates with usage counts + description: "**Real URL:** `GET ?action=sms_templates`" + tags: [SmsTemplates] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [sms_templates] } + responses: + "200": + description: SMS template list + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + templates: { type: array, items: { $ref: '#/components/schemas/SmsTemplate' } } + total: { type: integer } + "401": { $ref: '#/components/responses/Unauthorised' } + + /sms-templates/detail: + get: + operationId: adminGetSmsTemplate + summary: Single SMS template with all conditions + description: "**Real URL:** `GET ?action=sms_template&id=`" + tags: [SmsTemplates] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [sms_template] } + - $ref: '#/components/parameters/IdQuery' + responses: + "200": + description: SMS template detail + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + template: { $ref: '#/components/schemas/SmsTemplate' } + conditions: { type: array, items: { type: object } } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /sms-templates/save: + post: + operationId: adminSaveSmsTemplate + summary: Create or update an SMS template (admin only) + description: | + Include `id` to update; omit to create. Conditions array replaces all existing conditions. + **Real URL:** `POST ?action=sms_template_save` + tags: [SmsTemplates] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [sms_template_save] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [name, message] + properties: + id: { type: integer } + name: { type: string } + message: { type: string, description: "Also accepted as 'body_text'" } + is_active: { type: integer, enum: [0, 1], default: 1 } + category: { type: string, enum: [forms, orders, schedule], default: orders } + conditions: + type: array + items: + type: object + properties: + attendance_status: { type: string, nullable: true } + course_type: { type: string, nullable: true } + days_after: { type: integer } + send_time: { type: string, example: "09:00" } + direction: { type: string, enum: [after, before, today, immediate, on_date] } + is_active: { type: integer, enum: [0, 1] } + specific_date: { type: string, format: date, nullable: true } + responses: + "200": + description: Saved + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + id: { type: integer } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /sms-templates/delete: + post: + operationId: adminDeleteSmsTemplate + summary: Delete an SMS template and its conditions (admin only) + description: "**Real URL:** `POST ?action=sms_template_delete`" + tags: [SmsTemplates] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [sms_template_delete] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /sms-templates/toggle: + post: + operationId: adminToggleSmsTemplate + summary: Toggle SMS template is_active flag (admin only) + description: "**Real URL:** `POST ?action=sms_template_toggle`" + tags: [SmsTemplates] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [sms_template_toggle] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [id] + properties: + id: { type: integer } + responses: + "200": + description: Toggled + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + id: { type: integer } + is_active: { type: boolean } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "404": { $ref: '#/components/responses/NotFound' } + + # ── Settings ─────────────────────────────────────────────────────────────── + + /settings: + get: + operationId: adminGetSettings + summary: Read all general settings (admin only) + description: "**Real URL:** `GET ?action=settings`" + tags: [Settings] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [settings] } + responses: + "200": + description: Settings key-value map + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + settings: { type: object, description: "setting_key → setting_value map" } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /settings/save: + post: + operationId: adminSaveSettings + summary: Save general settings (admin only) + description: | + Send any key-value pairs. Uses INSERT … ON DUPLICATE KEY UPDATE. + **Real URL:** `POST ?action=settings_save` + tags: [Settings] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [settings_save] } + requestBody: + required: true + content: + application/json: + schema: + type: object + description: "Any key-value pairs to upsert into crm_settings" + additionalProperties: { type: string } + responses: + "200": + description: Saved + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + saved: { type: integer, description: "Number of rows upserted" } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + # ── Email Config ─────────────────────────────────────────────────────────── + + /email-config: + get: + operationId: adminGetEmailConfig + summary: Read SMTP / email configuration (admin only) + description: | + `smtp_pass` is masked with asterisks in the response. + **Real URL:** `GET ?action=email_config` + tags: [EmailConfig] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [email_config] } + responses: + "200": + description: Email config + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + config: + type: object + properties: + smtp_host: { type: string } + smtp_port: { type: string } + smtp_user: { type: string } + smtp_pass: { type: string, description: "Masked with asterisks" } + smtp_secure: { type: string } + smtp_from: { type: string } + smtp_from_name: { type: string } + sender_email: { type: string } + sender_name: { type: string } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /email-config/save: + post: + operationId: adminSaveEmailConfig + summary: Save SMTP / email configuration (admin only) + description: | + If `smtp_pass` is all asterisks it is not overwritten (masked value from GET). + **Real URL:** `POST ?action=email_config_save` + tags: [EmailConfig] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [email_config_save] } + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + smtp_host: { type: string } + smtp_port: { type: string } + smtp_user: { type: string } + smtp_pass: { type: string } + smtp_secure: { type: string } + smtp_from: { type: string } + smtp_from_name: { type: string } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /email-config/test: + post: + operationId: adminEmailTestSend + summary: Send a test email (admin only) + description: "**Real URL:** `POST ?action=email_test_send`" + tags: [EmailConfig] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [email_test_send] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [to_email] + properties: + to_email: { type: string, format: email } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "500": + description: SMTP failure + content: + application/json: + schema: { $ref: '#/components/schemas/ErrorEnvelope' } + + # ── SMS Config ───────────────────────────────────────────────────────────── + + /sms-config: + get: + operationId: adminGetSmsConfig + summary: Read Brevo SMS configuration (admin only) + description: | + `brevo_api_key` is masked — only last 6 characters are visible. + **Real URL:** `GET ?action=sms_config` + tags: [SmsConfig] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [sms_config] } + responses: + "200": + description: SMS config + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + config: + type: object + properties: + brevo_api_key: { type: string, description: "Masked — last 6 chars visible" } + sms_from: { type: string } + sms_sender: { type: string } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /sms-config/save: + post: + operationId: adminSaveSmsConfig + summary: Save Brevo SMS configuration (admin only) + description: | + If `brevo_api_key` starts with asterisks it is not overwritten. + **Real URL:** `POST ?action=sms_config_save` + tags: [SmsConfig] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [sms_config_save] } + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + brevo_api_key: { type: string } + sms_from: { type: string } + sms_sender: { type: string } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /sms-config/test: + post: + operationId: adminSmsTestSend + summary: Send a test SMS (admin only) + description: "**Real URL:** `POST ?action=sms_test_send`" + tags: [SmsConfig] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [sms_test_send] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [to_phone] + properties: + to_phone: { type: string, example: "+447700900001" } + responses: + "200": { $ref: '#/components/responses/SimpleOk' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "500": + description: Brevo SMS failure or not configured + content: + application/json: + schema: { $ref: '#/components/schemas/ErrorEnvelope' } + + # ── Export ───────────────────────────────────────────────────────────────── + + /export: + post: + operationId: adminExport + summary: Download CSV export of leads, appointments, or form leads (admin only) + description: | + Response Content-Type is `text/csv`, not JSON. Includes UTF-8 BOM for Excel compatibility. + **Real URL:** `POST ?action=export` + tags: [Export] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [export] } + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [type] + properties: + type: { type: string, enum: [leads, appointments, form_leads] } + responses: + "200": + description: CSV file download + headers: + Content-Disposition: + schema: { type: string, example: 'attachment; filename="leads_export_2026-05-10.csv"' } + content: + text/csv: + schema: { type: string, format: binary } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + + /print-labels: + get: + operationId: adminPrintLabels + summary: Download PDF shipping labels for one or more appointments + description: | + Response Content-Type is `application/pdf`. Uses dompdf. + **Real URL:** `GET ?action=print_labels&appointment_ids=1,2,3` + tags: [Export] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [print_labels] } + - name: appointment_ids + in: query + required: true + schema: { type: string, description: "Comma-separated appointment IDs", example: "1,2,3" } + responses: + "200": { $ref: '#/components/responses/PdfDownload' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + /invoice-pdf: + get: + operationId: adminInvoicePdf + summary: Download appointment invoice as PDF + description: | + Response Content-Type is `application/pdf`. Uses dompdf. Filename includes appointment_ref. + **Real URL:** `GET ?action=invoice_pdf&id=` + tags: [Export] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [invoice_pdf] } + - $ref: '#/components/parameters/IdQuery' + responses: + "200": { $ref: '#/components/responses/PdfDownload' } + "400": { $ref: '#/components/responses/ValidationError' } + "401": { $ref: '#/components/responses/Unauthorised' } + "404": { $ref: '#/components/responses/NotFound' } + + # ── WooCommerce ──────────────────────────────────────────────────────────── + + /woo-products: + get: + operationId: adminWooProducts + summary: List all synced WooCommerce products with attributes + description: "**Real URL:** `GET ?action=woo_products`" + tags: [WooCommerce] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [woo_products] } + responses: + "200": + description: WooCommerce products + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + products: { type: array, items: { type: object } } + total: { type: integer } + last_sync: { type: string, format: date-time, nullable: true } + attr_map: { type: object, description: "woo_product_id → attributes array" } + "401": { $ref: '#/components/responses/Unauthorised' } + + /woo-sync: + post: + operationId: adminWooSync + summary: Trigger WooCommerce product sync (admin only) + description: | + The actual sync is performed by the WordPress plugin on its next cron run. + This endpoint acknowledges the request and returns the current product list. + **Real URL:** `POST ?action=woo_sync` + tags: [WooCommerce] + parameters: + - name: action + in: query + required: true + schema: { type: string, enum: [woo_sync] } + requestBody: + required: false + content: + application/json: + schema: { type: object } + responses: + "200": + description: Sync acknowledged + content: + application/json: + schema: + type: object + properties: + success: { type: boolean } + data: + type: object + properties: + synced: { type: integer } + products: { type: array, items: { type: object } } + "401": { $ref: '#/components/responses/Unauthorised' } + "403": { $ref: '#/components/responses/Forbidden' } + "500": + description: WooCommerce API key not configured + content: + application/json: + schema: { $ref: '#/components/schemas/ErrorEnvelope' }