# Coursepedia LMS

Coursepedia LMS is an original, full-stack learning management platform with a Coursepedia-branded course marketplace, student dashboard, instructor studio, and admin control centre. It uses built-in Node.js/browser APIs plus `bcryptjs` so migrated WordPress password hashes can continue to work.

## Features

- Role-based accounts for students, instructors, organization managers, and admins.
- Course catalog with search, category filters, level filters, pricing, ratings, bundles, coupons, and enrollments.
- Frontend course builder for instructors with draft and published states.
- Rich course structure: modules, lessons, previews, video/text/live/slide lesson types, attachments, drip timing, quizzes, and assignments.
- Lesson players normalize YouTube watch/short links, Vimeo URLs, Google Drive share/download links, Loom links, iframe snippets, and direct video files.
- Lesson authors can switch between a compact visual editor and sanitized HTML source mode for headings, links, lists, tables, images, videos, and supported embeds.
- Course pages generate concise curriculum-based summaries automatically and use a payment-confirmed checkout gate before learner access is granted.
- Paystack and Flutterwave use backend initialization, strict backend verification, signed webhooks,
  replay protection, auditable payment states, and idempotent enrollment.
- Student learning area with progress tracking, lesson completion, quiz attempts, assignment submissions, points, and certificates.
- Instructor workspace with course metrics, gradebook, submissions, announcements, and messaging.
- Admin console with revenue, enrollments, user counts, course moderation, coupons, groups, bundles, and audit events.
- Certificate issuing with verification codes.
- PostgreSQL production persistence with a JSON migration/fallback driver.
- Automated API smoke tests using Node's built-in test runner.

## Run

From this folder:

```powershell
.\start.ps1
```

Then open:

```text
http://localhost:4173
```

If you have Node.js on PATH, this also works:

```powershell
npm install
Copy-Item .env.example .env
npm start
```

Set private runtime values in `.env`. Never commit that file.

### PostgreSQL production setup

Create a PostgreSQL database and set `DATABASE_URL` in `.env`, then run:

```powershell
pnpm db:schema
pnpm db:migrate -- --dry-run
pnpm db:migrate
pnpm db:reconcile
```

After reconciliation reports every entity as balanced, set `DATABASE_DRIVER=postgres` and restart
the application. The importer uses deterministic IDs and upserts, so it can be run again immediately
before launch. Use `--replace` only for an approved pre-launch rebuild and only with
`ALLOW_DESTRUCTIVE_MIGRATION=true`.

To run against a private imported WordPress database without replacing the active database:

```powershell
.\start.ps1 -DataFile "server/data/coursepedia-imported.db.json"
```

Imported and backup databases should be stored outside the production application directory. Use
`MIGRATION_SOURCE_FILE` and `LEGACY_ARCHIVE_FILE` in `.env` when migration tools need those files.

## Accounts

Use an administrator account already present in the migrated database. Development passwords belong
in local test fixtures only and must never be exposed in browser code or production documentation.

## Verify

With Node.js on PATH:

```powershell
npm run check
```

With the bundled Codex runtime:

```powershell
& "C:\Users\Admin\.cache\codex-runtimes\codex-primary-runtime\dependencies\node\bin\node.exe" --check server/server.js
& "C:\Users\Admin\.cache\codex-runtimes\codex-primary-runtime\dependencies\node\bin\node.exe" --test
```

## WordPress Migration

The migration tools read the Coursepedia WordPress/MasterStudy SQL dump from `C:\Users\Admin\Downloads\mbkqdpn9_wp388.sql` by default.

```powershell
npm run audit:wordpress
npm run import:wordpress
```

Generated files:

- `migration-report.json`: source audit and table/content counts.
- `migration-import-summary.json`: final import counts and migration notes.
- `server/data/coursepedia-imported.db.json`: preview database for the new LMS.
- `server/data/coursepedia-legacy-meta.ndjson`: raw WordPress post/user/comment metadata archive for reconciliation.

The importer preserves WordPress password hashes and the app verifies both portable `$P$` hashes and newer `$wp$` bcrypt hashes. Media URLs continue to point at `https://coursepedia.ng/wp-content/uploads/...` until the `wp-content/uploads` folder is copied into the new system.

MasterStudy lesson media fields such as `lesson_embed_ctx`, `lesson_youtube_url`, `lesson_vimeo_url`, `lesson_stream_url`, and `lesson_video_url` are converted into the new lesson player. To repair an existing local database created by an older importer, run:

```powershell
npm run repair:lesson-media
npm run repair:lesson-content
```

The repair creates `server/data/coursepedia.db.before-lesson-media-repair.json` before changing the active database.

## Data Model

The production schema is in `server/db/schema.sql`. It normalizes users, roles, courses, categories,
curriculum, learning records, commerce, certificates, reviews, communications, and audit events.
`legacy_mappings` preserves WordPress/current identifiers, while `lms_documents` preserves a lossless
copy of every imported source record for compatibility and migration auditing.

## Production Notes

- Run `pnpm production:validate` with the production environment before deployment.
- Run `pnpm production:build` to create the clean release under `dist/coursepedia-lms`.
- Register production Paystack and Flutterwave webhooks and complete low-value live acceptance tests.
- Put the server behind HTTPS, configure `TRUST_PROXY` correctly, and use a new `SESSION_SECRET`.
- Use persistent local storage or S3-compatible storage and scan uploaded media.
- Configure SMTP, verify the sender domain, and test all critical transactional templates.
- Provision PostgreSQL, run final reconciliation, and schedule encrypted database and media backups.
- Complete [COURSEPEDIA_FINAL_TEST_CHECKLIST.md](COURSEPEDIA_FINAL_TEST_CHECKLIST.md) before launch.
