# CV Digital API — `GET /cvdigital/employee/:id`

Returns one employee's digital CV, looked up by NRP (`:id`). It combines HR data from three MySQL databases with certifications from the LMS API.

Code: `app/routes/cvdigital.route.ts` → `app/controllers/cvDigital.controller.ts` → `app/services/cvDigital.service.ts` → `app/resources/cvDigital.resource.ts` (TypeScript, run by `tsx`; the rest of the app is JavaScript).

PIC access management (`/cvdigital/pic`): see [cvdigital-pic.md](cvdigital-pic.md).

## Response

```json
{ "code": 200, "status": "Ok", "message": "Successfully", "data": { ... } }
```

| Key | What it holds |
| --- | --- |
| `manpower` | Profile: name, position, grade, contract status, hire date, contacts, photo URL |
| `educations` | Education history, latest graduation first |
| `work_experiencies` | Previous jobs, latest end date first |
| `certificate_compentencies` | Certificates from the HR database, newest first (placeholder row of `"-"` when empty) |
| `lms_certification` | External + internal certifications from the LMS API, one flat array |
| `trainings` | Training list built from the same HR certificate rows |
| `versality` | SIMPER data; only present when the position starts with `operator` and a SIMPER row exists |

## Data sources

All databases are on the same MySQL host (`DB_HOST`, `DB_USERNAME`, `DB_PASSWORD`).

| Response key | Source | Database | Filter |
| --- | --- | --- | --- |
| `manpower` (profile) | `tbl_m_karyawan` (`posisi` = `IFNULL(posisi_sap, posisi)`) | `DB_DATABASE` | `nrp` |
| `manpower.statusPkwt` | `vw_pkwt.current`: 1–4 → `PKWT`, 5 → `PKWTT`, no row → `null` | `DB_DATABASE` | `nrp` |
| `manpower.perubahan_grade` / `perubahan_pos` | `perbuahanPosisi`, latest `tgl_efektif`, `status = 1`; falls back to hire date | `db_hrga` | `nrp` + `ket_grade` / `ket_posisi = 'PERUBAHAN'` |
| `educations` | `tbl_t_pra_pendidikan_calon_karyawan` | `DB_DATABASE` | `noKtp` from the employee row |
| `work_experiencies` | `tbl_t_pra_riwayat_kerja` | `DB_DATABASE` | `noKtp` from the employee row |
| `certificate_compentencies`, `trainings` | `tbl_m_certificate` | `DB_DATABASE` | `nrp` |
| `versality` | `tbl_t_simper` + `vw_simper_response` | `db_she` | `nrp`, then `id_simper` |
| `lms_certification` | LMS API `GET /certification/{nrp}` | — | `nrp` in the path |

Photo URL: `https://storage.{DOMAIN}/foto_user/{photo}`, default `user.jpg`.

## LMS certification API

| Item | Value |
| --- | --- |
| Request | `GET {LMS_CERT_URL}/certification/{nrp}` (NRP URL-encoded) |
| Auth header | `ss6-api-key: {LMS_CERT_API_ACCESS}` |
| Timeout | 10 s |
| Code | `getLmsCertification()` in `app/services/cvDigital.service.ts` (axios) |

LMS response: `{ statusCode, status, message, data: { idpTna, externalCertification, internalCertification } }`.

The service concatenates `externalCertification` then `internalCertification` and keeps 9 fields per item. `idpTna` is dropped, and items do not say whether they were external or internal.

```json
"lms_certification": [
  {
    "dept": "HCGA & EXTERNAL",
    "nrp": "23001071",
    "name": "AGUNG HERI SETYAWAN",
    "eventName": "PRODUCTIVITY IMPROVEMENT",
    "status": "PASS",
    "site": "BIB",
    "updatedAt": 1755255831,
    "year": 2024,
    "cost": 587000
  }
]
```

`updatedAt` is a Unix timestamp in seconds.

## Fetch mechanism

1. Load the employee from `tbl_m_karyawan`. No row → the request fails with a 500 error (`Employee {nrp} not found`).
2. Run the other 8 lookups in parallel with `Promise.all`: contract, grade history, position history, education, work experience, certificates, SIMPER, LMS. Education and work experience use the employee's `noKtp`, so they must wait for step 1.
3. `getAllCvDigital()` formats dates as `DD MMMM YYYY`, sorts the lists and builds the response.

| Failure | Result |
| --- | --- |
| LMS down, slower than 10 s, or `status: false` | `lms_certification: []`; the rest of the CV still loads; error logged |
| No SIMPER row or SIMPER query error | `versality` key left out |
| No contract row | `statusPkwt: null` |
| Any other database error | whole request fails with a 500 error |

## Allowed company

There is no company filter: any NRP found in `tbl_m_karyawan` returns a CV, whatever its `perusahaan_id`.

- `COMP_ID` / `COMP_ID_ALL` in `.env` restrict other endpoints (e.g. `hcga.controller.js`), but `/cvdigital` does not read them.
- Every `/cvdigital` route requires `personelAccess` (`app/middlewares/auth.js`): a Bearer access token from new-auth-dashboard, and the token's NRP must be allowed to open the Personel page in dashboard-site. See [Auth in cvdigital-pic.md](cvdigital-pic.md#auth) for the rules and error responses. CORS allows every origin (`origin: '*'` in `app.js`).
- Which employees have LMS data is decided by the LMS.

## Environment variables

| Variable | Used for |
| --- | --- |
| `DB_HOST`, `DB_PORT`, `DB_USERNAME`, `DB_PASSWORD`, `DB_DATABASE` | MySQL connection (`db_hrga` and `db_she` reuse the same host and credentials) |
| `DOMAIN` | Photo URL |
| `LMS_CERT_URL` | LMS base URL |
| `LMS_CERT_API_ACCESS` | Value of the `ss6-api-key` header |

## Run and deploy

| Task | Command |
| --- | --- |
| Start | `npm start` (`node --import tsx app.js`) |
| Develop | `npm run serve` (`node --watch --import tsx app.js`) |
| Type check | `npm run typecheck` (`tsc -p .`, no output files) |
| pm2 | `pm2 start ecosystem.config.js` (name `3015 HCGA-API`, port 3015, fork, 1 instance) |
| Deploy | `git pull && npm ci && pm2 restart "3015 HCGA-API"` |

- Always run `npm ci` on the server. TypeScript 7 and esbuild (used by tsx) install a platform-specific binary, so a `node_modules` copied from macOS breaks on Linux.
- `xlsx` 0.20.3 is installed from `cdn.sheetjs.com` (the npm copy has unfixed vulnerabilities); the server must be able to reach that site.
- Changes to `ecosystem.config.js` need `pm2 delete "3015 HCGA-API" && pm2 start ecosystem.config.js && pm2 save`; `pm2 restart` keeps the old settings.
