# Profil pengguna (`/me`) & aktivitas — implementasi BE

Spesifikasi asli: [`../FE/docs/BE_SPEC_PROFILE_USER_AND_ACTIVITY.md`](../FE/docs/BE_SPEC_PROFILE_USER_AND_ACTIVITY.md).

Semua rute di bawah **`authMiddleware`** (Bearer atau cookie access token).

---

## `GET /api/v1/me` dan `GET /api/v1/auth/me`

Handler sama. Respons `data`:

| Field | Tipe | Keterangan |
|-------|------|------------|
| `id` | number | User id |
| `email` | string | Dari DB |
| `name` | string | |
| `role` | string | `superadmin` \| `user` |
| `isActive` | boolean | Jika `false` → **403** `forbidden` (bukan 200). |
| `createdAt` | string | ISO8601 dari PostgreSQL |
| `avatarUrl` | string | Opsional jika terisi |

---

## `PATCH /api/v1/me`

Body JSON parsial:

- `name` (string, opsional)
- `avatarUrl` (string, opsional)

Minimal satu field. Email **tidak** dapat diubah di sini. User `is_active = false` → **403** `forbidden`.

Sukses: **200** + body sama seperti `GET /me` (memanggil ulang load profil).

---

## `GET /api/v1/me/summary`

Query: `period` = `month` (default) \| `week` \| `all`.

Agregasi **nyata** dari DB:

- `reportsDraft`, `reportsSubmitted`, `reportsTotal` — filter waktu menurut `period` pada kolom `reports.created_at`.
- `reportsExpected` — saat ini sama dengan `reportsTotal` (placeholder domain sampai ada aturan harian).
- `productivityScore` / `goalProgressPercent` — persen sederhana: `reportsSubmitted` / `reportsTotal` dalam periode (0 jika total 0).
- `calendarEventsUpcoming` — jumlah event user dengan `starts_at` antara **sekarang** dan **+30 hari** (tidak dibatasi `period`).
- `notificationsUnread` — notifikasi belum dibaca (global, bukan per periode).

Respons menyertakan `period` yang dipakai.

---

## `GET /api/v1/me/activity`

Query: `limit` (default 20, max 50), `offset` (default 0).

Menggabungkan:

- **report**: `title`, `description` = `status`, `at` = `updated_at`
- **notification**: `title`, `description` = `body` (teks), `at` = `created_at`

Field item: `type` (`report` \| `notification`), `id`, `title`, `description`, `at`.

Envelope `data`: `items`, `total` (jumlah baris report + notification user), `limit`, `offset`, `hasMore`.

User nonaktif → **403** `forbidden`.

---

## File kode

- `internal/api/me.go` — `getMe`, `patchMe`, `getMeSummary`, `getMeActivity`
- `internal/api/router.go` — registrasi rute `/me`, `/me/summary`, `/me/activity`

Setelah ubah kode: build + restart PM2 `workpulse-api`.
