From 845086d9a5762235d8188156223e26c9e0748177 Mon Sep 17 00:00:00 2001 From: fretuks Date: Mon, 6 Jul 2026 18:13:06 +0200 Subject: [PATCH] Implement basic API structure --- .gitignore | 4 + README.md | 204 +++++- package-lock.json | 1282 +++++++++++++++++++++++++++++++++++ package.json | 15 + src/app.js | 45 ++ src/db.js | 111 +++ src/middleware/adminAuth.js | 12 + src/routes/milestones.js | 112 +++ src/routes/platforms.js | 23 + src/routes/registrations.js | 54 ++ src/routes/rewards.js | 128 ++++ src/routes/users.js | 140 ++++ src/utils/errors.js | 20 + src/utils/validation.js | 35 + 14 files changed, 2184 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/app.js create mode 100644 src/db.js create mode 100644 src/middleware/adminAuth.js create mode 100644 src/routes/milestones.js create mode 100644 src/routes/platforms.js create mode 100644 src/routes/registrations.js create mode 100644 src/routes/rewards.js create mode 100644 src/routes/users.js create mode 100644 src/utils/errors.js create mode 100644 src/utils/validation.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9873d6e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +database.sqlite +.env +npm-debug.log* diff --git a/README.md b/README.md index 63f4c36..0948767 100644 --- a/README.md +++ b/README.md @@ -1 +1,203 @@ -# linux_distro_roguelite_promotion \ No newline at end of file +# Linux Distro Roguelite Promotion API + +REST-API fuer eine Pre-Register-Page eines kostenlosen Games. Spieler registrieren sich mit Benutzername und E-Mail, waehlen Plattformen aus und schalten ueber Community-Meilensteine Belohnungen frei. + +## Installation + +```bash +npm install +``` + +## Start + +```bash +ADMIN_KEY=secret npm start +``` + +Unter PowerShell: + +```powershell +$env:ADMIN_KEY = "secret" +npm start +``` + +Der Server laeuft standardmaessig auf Port `3000`. Mit `PORT` kann der Port ueberschrieben werden. + +```powershell +$env:PORT = "4000" +$env:ADMIN_KEY = "secret" +npm start +``` + +Beim ersten Start wird `database.sqlite` erstellt und automatisch mit diesen Daten befuellt: + +- Plattformen: PC, PlayStation, Xbox, Nintendo Switch, Mobile +- Meilensteine: 1,000 und 5,000 Community-Registrierungen +- Je eine Beispiel-Belohnung pro Meilenstein + +Alle Fehler haben das Format: + +```json +{ "error": "beschreibende Meldung" } +``` + +## Endpunkte + +### Health Check + +```bash +curl http://localhost:3000/ +``` + +### Benutzer + +`POST /users` erstellt einen neuen vorregistrierten Nutzer. + +```bash +curl -X POST http://localhost:3000/users \ + -H "Content-Type: application/json" \ + -d "{\"username\":\"tuxrunner\",\"email\":\"tux@example.com\",\"newsletter_optin\":true}" +``` + +`GET /users/{id}` ruft ein Nutzerprofil ab. + +```bash +curl http://localhost:3000/users/1 +``` + +`PUT /users/{id}` aktualisiert beliebige Teilfelder. + +```bash +curl -X PUT http://localhost:3000/users/1 \ + -H "Content-Type: application/json" \ + -d "{\"username\":\"tuxrunner2\",\"newsletter_optin\":false}" +``` + +`DELETE /users/{id}` loescht den Nutzer und dessen Plattform-Registrierungen. + +```bash +curl -X DELETE http://localhost:3000/users/1 +``` + +### Plattformen + +`GET /platforms` ruft alle Plattformen ab. + +```bash +curl http://localhost:3000/platforms +``` + +`GET /platforms/{id}` ruft eine einzelne Plattform ab. + +```bash +curl http://localhost:3000/platforms/1 +``` + +### Registrierungen + +`POST /registrations` registriert einen Nutzer fuer eine Plattform. Nach jedem erfolgreichen Request werden erreichte Meilensteine automatisch aktualisiert. + +```bash +curl -X POST http://localhost:3000/registrations \ + -H "Content-Type: application/json" \ + -d "{\"user_id\":1,\"platform_id\":1}" +``` + +`GET /registrations/count` liefert die Gesamtzahl aller Plattform-Registrierungen. + +```bash +curl http://localhost:3000/registrations/count +``` + +`GET /users/{id}/registrations` ruft alle Plattform-Registrierungen eines Nutzers inklusive Plattformname ab. + +```bash +curl http://localhost:3000/users/1/registrations +``` + +`DELETE /registrations/{id}` loescht eine einzelne Plattform-Registrierung. + +```bash +curl -X DELETE http://localhost:3000/registrations/1 +``` + +### Meilensteine + +`GET /milestones` ruft alle Meilensteine ab. Jeder Eintrag enthaelt `current_count` und `progress`, wobei `progress` bei 100 gedeckelt ist. + +```bash +curl http://localhost:3000/milestones +``` + +`POST /milestones` erstellt einen Meilenstein. Admin-Key erforderlich. + +```bash +curl -X POST http://localhost:3000/milestones \ + -H "Content-Type: application/json" \ + -H "X-Admin-Key: secret" \ + -d "{\"title\":\"10,000 community registrations\",\"target_count\":10000}" +``` + +`PUT /milestones/{id}` aktualisiert beliebige Teilfelder. Admin-Key erforderlich. + +```bash +curl -X PUT http://localhost:3000/milestones/1 \ + -H "Content-Type: application/json" \ + -H "X-Admin-Key: secret" \ + -d "{\"title\":\"1,000 pre-registrations\",\"reached\":false}" +``` + +`DELETE /milestones/{id}` loescht einen Meilenstein und dessen Belohnungen. Admin-Key erforderlich. + +```bash +curl -X DELETE http://localhost:3000/milestones/1 \ + -H "X-Admin-Key: secret" +``` + +### Belohnungen + +`GET /rewards` ruft alle Belohnungen ab. + +```bash +curl http://localhost:3000/rewards +``` + +`GET /milestones/{id}/rewards` ruft Belohnungen eines Meilensteins ab. + +```bash +curl http://localhost:3000/milestones/1/rewards +``` + +`POST /rewards` erstellt eine Belohnung. Admin-Key erforderlich. + +```bash +curl -X POST http://localhost:3000/rewards \ + -H "Content-Type: application/json" \ + -H "X-Admin-Key: secret" \ + -d "{\"milestone_id\":1,\"name\":\"Beta Hat\",\"description\":\"Cosmetic launch hat\",\"image_url\":\"https://example.com/rewards/beta-hat.png\"}" +``` + +`PUT /rewards/{id}` aktualisiert beliebige Teilfelder. Admin-Key erforderlich. + +```bash +curl -X PUT http://localhost:3000/rewards/1 \ + -H "Content-Type: application/json" \ + -H "X-Admin-Key: secret" \ + -d "{\"description\":\"Updated reward description\"}" +``` + +`DELETE /rewards/{id}` loescht eine Belohnung. Admin-Key erforderlich. + +```bash +curl -X DELETE http://localhost:3000/rewards/1 \ + -H "X-Admin-Key: secret" +``` + +## Statuscodes + +- `200`: Erfolgreiche Abfrage, Aktualisierung oder Loeschung +- `201`: Ressource erstellt +- `400`: Ungueltige Eingaben oder nicht existierende Fremdschluessel +- `401`: Fehlender oder falscher Admin-Key +- `404`: Ressource oder Route nicht gefunden +- `409`: Konflikt, z. B. doppelte E-Mail, doppelter Benutzername oder doppelte Nutzer-Plattform-Kombination diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..13a8f86 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,1282 @@ +{ + "name": "linux-distro-roguelite-promotion", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "linux-distro-roguelite-promotion", + "version": "1.0.0", + "dependencies": { + "better-sqlite3": "^12.11.1", + "cors": "^2.8.5", + "express": "^4.21.2" + } + }, + "node_modules/accepts": { + "version": "1.3.8", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", + "integrity": "sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==", + "license": "MIT", + "dependencies": { + "mime-types": "~2.1.34", + "negotiator": "0.6.3" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/array-flatten": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", + "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==", + "license": "MIT" + }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/better-sqlite3": { + "version": "12.11.1", + "resolved": "https://registry.npmjs.org/better-sqlite3/-/better-sqlite3-12.11.1.tgz", + "integrity": "sha512-dq9AtApgg5PGFtBzPFSBl3HZQjHok5gaQCM6zh2Yk0aSmDCs1CbnVI8/HgASQkNKsWFpseIO9beg5xxpYhbIfA==", + "hasInstallScript": true, + "license": "MIT", + "dependencies": { + "bindings": "^1.5.0", + "prebuild-install": "^7.1.1" + }, + "engines": { + "node": "20.x || 22.x || 23.x || 24.x || 25.x || 26.x" + } + }, + "node_modules/bindings": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", + "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", + "license": "MIT", + "dependencies": { + "file-uri-to-path": "1.0.0" + } + }, + "node_modules/bl": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", + "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", + "license": "MIT", + "dependencies": { + "buffer": "^5.5.0", + "inherits": "^2.0.4", + "readable-stream": "^3.4.0" + } + }, + "node_modules/body-parser": { + "version": "1.20.5", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.5.tgz", + "integrity": "sha512-3grm+/2tUOvu2cjJkvsIxrv/wVpfXQW4PsQHYm7yk4vfpu7Ekl6nEsYBoJUL6qDwZUx8wUhQ8tR2qz+ad9c9OA==", + "license": "MIT", + "dependencies": { + "bytes": "~3.1.2", + "content-type": "~1.0.5", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "~1.2.0", + "http-errors": "~2.0.1", + "iconv-lite": "~0.4.24", + "on-finished": "~2.4.1", + "qs": "~6.15.1", + "raw-body": "~2.5.3", + "type-is": "~1.6.18", + "unpipe": "~1.0.0" + }, + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, + "node_modules/buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT", + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, + "node_modules/bytes": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", + "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/call-bind-apply-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/call-bound": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/call-bound/-/call-bound-1.0.4.tgz", + "integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "get-intrinsic": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/chownr": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz", + "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==", + "license": "ISC" + }, + "node_modules/content-disposition": { + "version": "0.5.4", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", + "integrity": "sha512-FveZTNuGw04cxlAiWbzi6zTAL/lhehaWbTtgluJh4/E95DqMwTmha3KZN1aAWA8cFIhHzMZUvLevkw5Rqk+tSQ==", + "license": "MIT", + "dependencies": { + "safe-buffer": "5.2.1" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/content-type": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", + "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.2.tgz", + "integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie-signature": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.7.tgz", + "integrity": "sha512-NXdYc3dLr47pBkpUCHtKSwIOQXLVn8dZEuywboCOJY/osA0wFSLlSawr3KN8qXJEyX66FcONTH8EIlVuK0yyFA==", + "license": "MIT" + }, + "node_modules/cors": { + "version": "2.8.6", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.6.tgz", + "integrity": "sha512-tJtZBBHA6vjIAaF6EnIaq6laBBP9aq/Y3ouVJjEfoHbRBcHBAHYcMh/w8LDrk2PvIMMq8gmopa5D4V8RmbrxGw==", + "license": "MIT", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "license": "MIT", + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/decompress-response": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-6.0.0.tgz", + "integrity": "sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ==", + "license": "MIT", + "dependencies": { + "mimic-response": "^3.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/deep-extend": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", + "integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==", + "license": "MIT", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/depd": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/destroy": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", + "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==", + "license": "MIT", + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, + "node_modules/detect-libc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", + "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "license": "Apache-2.0", + "engines": { + "node": ">=8" + } + }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==", + "license": "MIT" + }, + "node_modules/encodeurl": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz", + "integrity": "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/end-of-stream": { + "version": "1.4.5", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.5.tgz", + "integrity": "sha512-ooEGc6HP26xXq/N+GCGOT0JKCLDGrq2bQUZrQ7gyrJiZANJ/8YDTxTpQBXGMn+WbIQXNVpyWymm7KYVICQnyOg==", + "license": "MIT", + "dependencies": { + "once": "^1.4.0" + } + }, + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-object-atoms": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.2.tgz", + "integrity": "sha512-HWcBoN6NileqtSydK2FqHbS/LoDd2pqrnQHLyJzBj4kOp/ky2MWMN694xOfkK8/SnUsW2DH7EfyVlydKCsm1Zw==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==", + "license": "MIT" + }, + "node_modules/etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/expand-template": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz", + "integrity": "sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==", + "license": "(MIT OR WTFPL)", + "engines": { + "node": ">=6" + } + }, + "node_modules/express": { + "version": "4.22.2", + "resolved": "https://registry.npmjs.org/express/-/express-4.22.2.tgz", + "integrity": "sha512-IuL+Elrou2ZvCFHs18/CIzy2Nzvo25nZ1/D2eIZlz7c+QUayAcYoiM2BthCjs+EBHVpjYjcuLDAiCWgeIX3X1Q==", + "license": "MIT", + "dependencies": { + "accepts": "~1.3.8", + "array-flatten": "1.1.1", + "body-parser": "~1.20.5", + "content-disposition": "~0.5.4", + "content-type": "~1.0.4", + "cookie": "~0.7.1", + "cookie-signature": "~1.0.6", + "debug": "2.6.9", + "depd": "2.0.0", + "encodeurl": "~2.0.0", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "finalhandler": "~1.3.1", + "fresh": "~0.5.2", + "http-errors": "~2.0.0", + "merge-descriptors": "1.0.3", + "methods": "~1.1.2", + "on-finished": "~2.4.1", + "parseurl": "~1.3.3", + "path-to-regexp": "~0.1.12", + "proxy-addr": "~2.0.7", + "qs": "~6.15.1", + "range-parser": "~1.2.1", + "safe-buffer": "5.2.1", + "send": "~0.19.0", + "serve-static": "~1.16.2", + "setprototypeof": "1.2.0", + "statuses": "~2.0.1", + "type-is": "~1.6.18", + "utils-merge": "1.0.1", + "vary": "~1.1.2" + }, + "engines": { + "node": ">= 0.10.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/file-uri-to-path": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", + "license": "MIT" + }, + "node_modules/finalhandler": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.3.2.tgz", + "integrity": "sha512-aA4RyPcd3badbdABGDuTXCMTtOneUCAYH/gxoYRTZlIJdF0YPWuGqiAsIrhNnnqdXGswYk6dGujem4w80UJFhg==", + "license": "MIT", + "dependencies": { + "debug": "2.6.9", + "encodeurl": "~2.0.0", + "escape-html": "~1.0.3", + "on-finished": "~2.4.1", + "parseurl": "~1.3.3", + "statuses": "~2.0.2", + "unpipe": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/forwarded": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", + "integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/fresh": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", + "integrity": "sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/fs-constants": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", + "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==", + "license": "MIT" + }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-intrinsic": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", + "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", + "function-bind": "^1.1.2", + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "license": "MIT", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/github-from-package": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/github-from-package/-/github-from-package-0.0.0.tgz", + "integrity": "sha512-SyHy3T1v2NUXn29OsWdxmK6RwHD+vkj3v8en8AOBZ1wBQ/hCAQ5bAQTD02kW4W9tUp/3Qh6J8r9EvntiyCmOOw==", + "license": "MIT" + }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-symbols": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/hasown": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.4.tgz", + "integrity": "sha512-T2UbfbBEF32wiepXIsMlTW9+dDYC6wMh/t/vYA4tuOMKqWz/n3vr1NFSxQiyP+zk2mXsoMA/i/7qV6LKut1t1A==", + "license": "MIT", + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/http-errors": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz", + "integrity": "sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ==", + "license": "MIT", + "dependencies": { + "depd": "~2.0.0", + "inherits": "~2.0.4", + "setprototypeof": "~1.2.0", + "statuses": "~2.0.2", + "toidentifier": "~1.0.1" + }, + "engines": { + "node": ">= 0.8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "license": "MIT", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "BSD-3-Clause" + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "license": "ISC" + }, + "node_modules/ini": { + "version": "1.3.8", + "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz", + "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==", + "license": "ISC" + }, + "node_modules/ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==", + "license": "MIT", + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/media-typer": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "integrity": "sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/merge-descriptors": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.3.tgz", + "integrity": "sha512-gaNvAS7TZ897/rVaZ0nMtAyxNyi/pdbjbAwUpFQpN70GqnVfOiXpeUUMKRBmzXaSQ8DdTX4/0ms62r2K+hE6mQ==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz", + "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==", + "license": "MIT", + "bin": { + "mime": "cli.js" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "license": "MIT", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mimic-response": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-3.1.0.tgz", + "integrity": "sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ==", + "license": "MIT", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/minimist": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/mkdirp-classic": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz", + "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==", + "license": "MIT" + }, + "node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", + "license": "MIT" + }, + "node_modules/napi-build-utils": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/napi-build-utils/-/napi-build-utils-2.0.0.tgz", + "integrity": "sha512-GEbrYkbfF7MoNaoh2iGG84Mnf/WZfB0GdGEsM8wz7Expx/LlWf5U8t9nvJKXSp3qr5IsEbK04cBGhol/KwOsWA==", + "license": "MIT" + }, + "node_modules/negotiator": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz", + "integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/node-abi": { + "version": "3.94.0", + "resolved": "https://registry.npmjs.org/node-abi/-/node-abi-3.94.0.tgz", + "integrity": "sha512-W5ZNO5KRPB5TkYmGVD9F6YqhsglXJzE6etpbmT+f6EQElhiX/UTG551cnsRGvLG3fyZEg9HwaDmNmj5nwJ4z9g==", + "license": "MIT", + "dependencies": { + "semver": "^7.3.5" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/object-inspect": { + "version": "1.13.4", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz", + "integrity": "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/on-finished": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz", + "integrity": "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==", + "license": "MIT", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "license": "ISC", + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/path-to-regexp": { + "version": "0.1.13", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.13.tgz", + "integrity": "sha512-A/AGNMFN3c8bOlvV9RreMdrv7jsmF9XIfDeCd87+I8RNg6s78BhJxMu69NEMHBSJFxKidViTEdruRwEk/WIKqA==", + "license": "MIT" + }, + "node_modules/prebuild-install": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.1.3.tgz", + "integrity": "sha512-8Mf2cbV7x1cXPUILADGI3wuhfqWvtiLA1iclTDbFRZkgRQS0NqsPZphna9V+HyTEadheuPmjaJMsbzKQFOzLug==", + "deprecated": "No longer maintained. Please contact the author of the relevant native addon; alternatives are available.", + "license": "MIT", + "dependencies": { + "detect-libc": "^2.0.0", + "expand-template": "^2.0.3", + "github-from-package": "0.0.0", + "minimist": "^1.2.3", + "mkdirp-classic": "^0.5.3", + "napi-build-utils": "^2.0.0", + "node-abi": "^3.3.0", + "pump": "^3.0.0", + "rc": "^1.2.7", + "simple-get": "^4.0.0", + "tar-fs": "^2.0.0", + "tunnel-agent": "^0.6.0" + }, + "bin": { + "prebuild-install": "bin.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/proxy-addr": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", + "integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==", + "license": "MIT", + "dependencies": { + "forwarded": "0.2.0", + "ipaddr.js": "1.9.1" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/pump": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.4.tgz", + "integrity": "sha512-VS7sjc6KR7e1ukRFhQSY5LM2uBWAUPiOPa/A3mkKmiMwSmRFUITt0xuj+/lesgnCv+dPIEYlkzrcyXgquIHMcA==", + "license": "MIT", + "dependencies": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + }, + "node_modules/qs": { + "version": "6.15.3", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.15.3.tgz", + "integrity": "sha512-O9gl3zCl5h5blw1KGUzQKhA5oUXSl8rwUIM5o0S3nCXMliSvy5Dzx7/DJcI+SwgICv+IneSZwhBh1oSyEHA71A==", + "license": "BSD-3-Clause", + "dependencies": { + "es-define-property": "^1.0.1", + "side-channel": "^1.1.1" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/raw-body": { + "version": "2.5.3", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.3.tgz", + "integrity": "sha512-s4VSOf6yN0rvbRZGxs8Om5CWj6seneMwK3oDb4lWDH0UPhWcxwOWw5+qk24bxq87szX1ydrwylIOp2uG1ojUpA==", + "license": "MIT", + "dependencies": { + "bytes": "~3.1.2", + "http-errors": "~2.0.1", + "iconv-lite": "~0.4.24", + "unpipe": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/rc": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz", + "integrity": "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==", + "license": "(BSD-2-Clause OR MIT OR Apache-2.0)", + "dependencies": { + "deep-extend": "^0.6.0", + "ini": "~1.3.0", + "minimist": "^1.2.0", + "strip-json-comments": "~2.0.1" + }, + "bin": { + "rc": "cli.js" + } + }, + "node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "license": "MIT", + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", + "license": "MIT" + }, + "node_modules/semver": { + "version": "7.8.5", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz", + "integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==", + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/send": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/send/-/send-0.19.2.tgz", + "integrity": "sha512-VMbMxbDeehAxpOtWJXlcUS5E8iXh6QmN+BkRX1GARS3wRaXEEgzCcB10gTQazO42tpNIya8xIyNx8fll1OFPrg==", + "license": "MIT", + "dependencies": { + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "encodeurl": "~2.0.0", + "escape-html": "~1.0.3", + "etag": "~1.8.1", + "fresh": "~0.5.2", + "http-errors": "~2.0.1", + "mime": "1.6.0", + "ms": "2.1.3", + "on-finished": "~2.4.1", + "range-parser": "~1.2.1", + "statuses": "~2.0.2" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/send/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, + "node_modules/serve-static": { + "version": "1.16.3", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.16.3.tgz", + "integrity": "sha512-x0RTqQel6g5SY7Lg6ZreMmsOzncHFU7nhnRWkKgWuMTu5NN0DR5oruckMqRvacAN9d5w6ARnRBXl9xhDCgfMeA==", + "license": "MIT", + "dependencies": { + "encodeurl": "~2.0.0", + "escape-html": "~1.0.3", + "parseurl": "~1.3.3", + "send": "~0.19.1" + }, + "engines": { + "node": ">= 0.8.0" + } + }, + "node_modules/setprototypeof": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==", + "license": "ISC" + }, + "node_modules/side-channel": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.1.tgz", + "integrity": "sha512-6x6dK6zJdpTzF4sQeNYxwtvBzf6Eg4GtlesS94HOvTudUeyK2WXAaIfmDgsyslYrRBeFIlsi54AYsFGUuhmvrQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.4", + "side-channel-list": "^1.0.1", + "side-channel-map": "^1.0.1", + "side-channel-weakmap": "^1.0.2" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-list": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/side-channel-list/-/side-channel-list-1.0.1.tgz", + "integrity": "sha512-mjn/0bi/oUURjc5Xl7IaWi/OJJJumuoJFQJfDDyO46+hBWsfaVM65TBHq2eoZBhzl9EchxOijpkbRC8SVBQU0w==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.4" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-map": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/side-channel-map/-/side-channel-map-1.0.1.tgz", + "integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-weakmap": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz", + "integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3", + "side-channel-map": "^1.0.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/simple-concat": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/simple-concat/-/simple-concat-1.0.1.tgz", + "integrity": "sha512-cSFtAPtRhljv69IK0hTVZQ+OfE9nePi/rtJmw5UjHeVyVroEqJXP1sFztKUy1qU+xvz3u/sfYJLa947b7nAN2Q==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/simple-get": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/simple-get/-/simple-get-4.0.1.tgz", + "integrity": "sha512-brv7p5WgH0jmQJr1ZDDfKDOSeWWg+OVypG99A/5vYGPqJ6pxiaHLy8nxtFjBA7oMa01ebA9gfh1uMCFqOuXxvA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT", + "dependencies": { + "decompress-response": "^6.0.0", + "once": "^1.3.1", + "simple-concat": "^1.0.0" + } + }, + "node_modules/statuses": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz", + "integrity": "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, + "node_modules/strip-json-comments": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", + "integrity": "sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/tar-fs": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.5.tgz", + "integrity": "sha512-OboTd8mmMhZDNPV+UjQcK9yKAatXu2aJ+r1w4im1Otd4M4fl2hwvdoXUxIYHFTHWK/3y3FarBP70v3vwmGlOxw==", + "license": "MIT", + "dependencies": { + "chownr": "^1.1.1", + "mkdirp-classic": "^0.5.2", + "pump": "^3.0.0", + "tar-stream": "^2.1.4" + } + }, + "node_modules/tar-stream": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.2.0.tgz", + "integrity": "sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==", + "license": "MIT", + "dependencies": { + "bl": "^4.0.3", + "end-of-stream": "^1.4.1", + "fs-constants": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^3.1.1" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/toidentifier": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", + "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==", + "license": "MIT", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/tunnel-agent": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", + "integrity": "sha512-McnNiV1l8RYeY8tBgEpuodCC1mLUdbSN+CYBL7kJsJNInOP8UjDDEwdk6Mw60vdLLrr5NHKZhMAOSrR2NZuQ+w==", + "license": "Apache-2.0", + "dependencies": { + "safe-buffer": "^5.0.1" + }, + "engines": { + "node": "*" + } + }, + "node_modules/type-is": { + "version": "1.6.18", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", + "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", + "license": "MIT", + "dependencies": { + "media-typer": "0.3.0", + "mime-types": "~2.1.24" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", + "license": "MIT" + }, + "node_modules/utils-merge": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", + "integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==", + "license": "MIT", + "engines": { + "node": ">= 0.4.0" + } + }, + "node_modules/vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "license": "ISC" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..6bdae19 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "linux-distro-roguelite-promotion", + "version": "1.0.0", + "description": "REST API for a free game pre-register page.", + "main": "src/app.js", + "scripts": { + "start": "node src/app.js", + "dev": "node --watch src/app.js" + }, + "dependencies": { + "better-sqlite3": "^12.11.1", + "cors": "^2.8.5", + "express": "^4.21.2" + } +} diff --git a/src/app.js b/src/app.js new file mode 100644 index 0000000..7672440 --- /dev/null +++ b/src/app.js @@ -0,0 +1,45 @@ +const express = require("express"); +const cors = require("cors"); +const { initializeDatabase } = require("./db"); + +const usersRouter = require("./routes/users"); +const platformsRouter = require("./routes/platforms"); +const registrationsRouter = require("./routes/registrations"); +const milestonesRouter = require("./routes/milestones"); +const rewardsRouter = require("./routes/rewards"); + +initializeDatabase(); + +const app = express(); + +app.use(cors()); +app.use(express.json()); + +app.get("/", (req, res) => { + res.json({ message: "Pre-registration API is running." }); +}); + +app.use("/users", usersRouter); +app.use("/platforms", platformsRouter); +app.use("/registrations", registrationsRouter); +app.use("/milestones", milestonesRouter); +app.use("/rewards", rewardsRouter); + +app.use((req, res) => { + res.status(404).json({ error: "Route not found." }); +}); + +app.use((err, req, res, next) => { + if (err instanceof SyntaxError && err.status === 400 && "body" in err) { + return res.status(400).json({ error: "Invalid JSON body." }); + } + + console.error(err); + res.status(500).json({ error: "Internal server error." }); +}); + +const port = process.env.PORT || 3000; + +app.listen(port, () => { + console.log(`Pre-registration API listening on port ${port}`); +}); diff --git a/src/db.js b/src/db.js new file mode 100644 index 0000000..1211992 --- /dev/null +++ b/src/db.js @@ -0,0 +1,111 @@ +const path = require("path"); +const Database = require("better-sqlite3"); + +const dbPath = path.join(__dirname, "..", "database.sqlite"); +const db = new Database(dbPath); + +db.pragma("foreign_keys = ON"); + +function boolToInt(value) { + return value ? 1 : 0; +} + +function createSchema() { + db.exec(` + CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT UNIQUE NOT NULL, + email TEXT UNIQUE NOT NULL, + newsletter_optin INTEGER NOT NULL DEFAULT 0, + created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP + ); + + CREATE TABLE IF NOT EXISTS platforms ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT UNIQUE NOT NULL + ); + + CREATE TABLE IF NOT EXISTS registrations ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER NOT NULL, + platform_id INTEGER NOT NULL, + created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, + FOREIGN KEY (platform_id) REFERENCES platforms(id), + UNIQUE (user_id, platform_id) + ); + + CREATE TABLE IF NOT EXISTS milestones ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT NOT NULL, + target_count INTEGER NOT NULL, + reached INTEGER NOT NULL DEFAULT 0 + ); + + CREATE TABLE IF NOT EXISTS rewards ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + milestone_id INTEGER NOT NULL, + name TEXT NOT NULL, + description TEXT, + image_url TEXT, + FOREIGN KEY (milestone_id) REFERENCES milestones(id) ON DELETE CASCADE + ); + `); +} + +function seedDatabase() { + const platforms = ["PC", "PlayStation", "Xbox", "Nintendo Switch", "Mobile"]; + const insertPlatform = db.prepare("INSERT OR IGNORE INTO platforms (name) VALUES (?)"); + platforms.forEach((platform) => insertPlatform.run(platform)); + + // Seed example milestones only when the table is empty to avoid duplicate demo data. + const milestoneCount = db.prepare("SELECT COUNT(*) AS count FROM milestones").get().count; + if (milestoneCount === 0) { + const insertMilestone = db.prepare( + "INSERT INTO milestones (title, target_count, reached) VALUES (?, ?, 0)" + ); + const insertReward = db.prepare( + "INSERT INTO rewards (milestone_id, name, description, image_url) VALUES (?, ?, ?, ?)" + ); + + const first = insertMilestone.run("1,000 community registrations", 1000); + insertReward.run( + first.lastInsertRowid, + "Founder Banner", + "Exclusive in-game profile banner for early supporters.", + "https://example.com/rewards/founder-banner.png" + ); + + const second = insertMilestone.run("5,000 community registrations", 5000); + insertReward.run( + second.lastInsertRowid, + "Launch Loot Crate", + "Free cosmetic crate unlocked for all players at launch.", + "https://example.com/rewards/launch-loot-crate.png" + ); + } +} + +function initializeDatabase() { + createSchema(); + seedDatabase(); +} + +function getRegistrationCount() { + return db.prepare("SELECT COUNT(*) AS count FROM registrations").get().count; +} + +function updateReachedMilestones() { + const currentCount = getRegistrationCount(); + db.prepare( + "UPDATE milestones SET reached = 1 WHERE reached = 0 AND target_count <= ?" + ).run(currentCount); +} + +module.exports = { + db, + initializeDatabase, + getRegistrationCount, + updateReachedMilestones, + boolToInt +}; diff --git a/src/middleware/adminAuth.js b/src/middleware/adminAuth.js new file mode 100644 index 0000000..438b8b0 --- /dev/null +++ b/src/middleware/adminAuth.js @@ -0,0 +1,12 @@ +function requireAdminKey(req, res, next) { + const expectedKey = process.env.ADMIN_KEY; + const providedKey = req.get("X-Admin-Key"); + + if (!expectedKey || providedKey !== expectedKey) { + return res.status(401).json({ error: "Missing or invalid admin key." }); + } + + next(); +} + +module.exports = requireAdminKey; diff --git a/src/routes/milestones.js b/src/routes/milestones.js new file mode 100644 index 0000000..41d357a --- /dev/null +++ b/src/routes/milestones.js @@ -0,0 +1,112 @@ +const express = require("express"); +const { db, boolToInt, getRegistrationCount } = require("../db"); +const requireAdminKey = require("../middleware/adminAuth"); +const { sendError } = require("../utils/errors"); +const { + isBoolean, + isNonEmptyString, + isPositiveInteger, + normalizeString, + parseId +} = require("../utils/validation"); + +const router = express.Router(); + +function serializeMilestone(milestone, currentCount) { + return { + ...milestone, + reached: Boolean(milestone.reached), + current_count: currentCount, + progress: Math.min(100, Math.round((currentCount / milestone.target_count) * 100)) + }; +} + +router.get("/", (req, res) => { + const currentCount = getRegistrationCount(); + const milestones = db.prepare("SELECT * FROM milestones ORDER BY target_count, id").all(); + return res.json(milestones.map((milestone) => serializeMilestone(milestone, currentCount))); +}); + +router.get("/:id/rewards", (req, res) => { + const id = parseId(req.params.id); + if (!id) return sendError(res, 400, "Invalid milestone id."); + + const milestone = db.prepare("SELECT id FROM milestones WHERE id = ?").get(id); + if (!milestone) return sendError(res, 404, "Milestone not found."); + + const rewards = db.prepare("SELECT * FROM rewards WHERE milestone_id = ? ORDER BY id").all(id); + return res.json(rewards); +}); + +router.post("/", requireAdminKey, (req, res) => { + const title = normalizeString(req.body.title); + const targetCount = req.body.target_count; + + if (!isNonEmptyString(title)) return sendError(res, 400, "title is required."); + if (!isPositiveInteger(targetCount)) { + return sendError(res, 400, "target_count must be a positive integer."); + } + + const currentCount = getRegistrationCount(); + const result = db + .prepare("INSERT INTO milestones (title, target_count, reached) VALUES (?, ?, ?)") + .run(title, targetCount, boolToInt(currentCount >= targetCount)); + const milestone = db.prepare("SELECT * FROM milestones WHERE id = ?").get(result.lastInsertRowid); + + return res.status(201).json(serializeMilestone(milestone, currentCount)); +}); + +router.put("/:id", requireAdminKey, (req, res) => { + const id = parseId(req.params.id); + if (!id) return sendError(res, 400, "Invalid milestone id."); + + const existing = db.prepare("SELECT * FROM milestones WHERE id = ?").get(id); + if (!existing) return sendError(res, 404, "Milestone not found."); + + const updates = []; + const values = []; + + if (Object.prototype.hasOwnProperty.call(req.body, "title")) { + const title = normalizeString(req.body.title); + if (!isNonEmptyString(title)) return sendError(res, 400, "title must not be empty."); + updates.push("title = ?"); + values.push(title); + } + + if (Object.prototype.hasOwnProperty.call(req.body, "target_count")) { + if (!isPositiveInteger(req.body.target_count)) { + return sendError(res, 400, "target_count must be a positive integer."); + } + updates.push("target_count = ?"); + values.push(req.body.target_count); + } + + if (Object.prototype.hasOwnProperty.call(req.body, "reached")) { + if (!isBoolean(req.body.reached)) return sendError(res, 400, "reached must be a boolean."); + updates.push("reached = ?"); + values.push(boolToInt(req.body.reached)); + } + + if (updates.length === 0) { + return sendError(res, 400, "At least one updatable field is required."); + } + + values.push(id); + db.prepare(`UPDATE milestones SET ${updates.join(", ")} WHERE id = ?`).run(...values); + + const currentCount = getRegistrationCount(); + const milestone = db.prepare("SELECT * FROM milestones WHERE id = ?").get(id); + return res.json(serializeMilestone(milestone, currentCount)); +}); + +router.delete("/:id", requireAdminKey, (req, res) => { + const id = parseId(req.params.id); + if (!id) return sendError(res, 400, "Invalid milestone id."); + + const result = db.prepare("DELETE FROM milestones WHERE id = ?").run(id); + if (result.changes === 0) return sendError(res, 404, "Milestone not found."); + + return res.json({ message: "Milestone deleted." }); +}); + +module.exports = router; diff --git a/src/routes/platforms.js b/src/routes/platforms.js new file mode 100644 index 0000000..3cdaba5 --- /dev/null +++ b/src/routes/platforms.js @@ -0,0 +1,23 @@ +const express = require("express"); +const { db } = require("../db"); +const { sendError } = require("../utils/errors"); +const { parseId } = require("../utils/validation"); + +const router = express.Router(); + +router.get("/", (req, res) => { + const platforms = db.prepare("SELECT * FROM platforms ORDER BY id").all(); + return res.json(platforms); +}); + +router.get("/:id", (req, res) => { + const id = parseId(req.params.id); + if (!id) return sendError(res, 400, "Invalid platform id."); + + const platform = db.prepare("SELECT * FROM platforms WHERE id = ?").get(id); + if (!platform) return sendError(res, 404, "Platform not found."); + + return res.json(platform); +}); + +module.exports = router; diff --git a/src/routes/registrations.js b/src/routes/registrations.js new file mode 100644 index 0000000..2b1e543 --- /dev/null +++ b/src/routes/registrations.js @@ -0,0 +1,54 @@ +const express = require("express"); +const { db, getRegistrationCount, updateReachedMilestones } = require("../db"); +const { sendError, mapSqliteError } = require("../utils/errors"); +const { isPositiveInteger, parseId } = require("../utils/validation"); + +const router = express.Router(); + +router.post("/", (req, res) => { + const userId = req.body.user_id; + const platformId = req.body.platform_id; + + if (!isPositiveInteger(userId)) return sendError(res, 400, "user_id is required."); + if (!isPositiveInteger(platformId)) return sendError(res, 400, "platform_id is required."); + + const user = db.prepare("SELECT id FROM users WHERE id = ?").get(userId); + if (!user) return sendError(res, 400, "User does not exist."); + + const platform = db.prepare("SELECT id FROM platforms WHERE id = ?").get(platformId); + if (!platform) return sendError(res, 400, "Platform does not exist."); + + try { + const result = db + .prepare("INSERT INTO registrations (user_id, platform_id) VALUES (?, ?)") + .run(userId, platformId); + + // Every successful registration can unlock one or more community milestones. + updateReachedMilestones(); + + const registration = db + .prepare("SELECT * FROM registrations WHERE id = ?") + .get(result.lastInsertRowid); + return res.status(201).json(registration); + } catch (error) { + const mapped = mapSqliteError(error, "User is already registered for this platform."); + if (mapped) return sendError(res, mapped.status, mapped.message); + throw error; + } +}); + +router.get("/count", (req, res) => { + return res.json({ count: getRegistrationCount() }); +}); + +router.delete("/:id", (req, res) => { + const id = parseId(req.params.id); + if (!id) return sendError(res, 400, "Invalid registration id."); + + const result = db.prepare("DELETE FROM registrations WHERE id = ?").run(id); + if (result.changes === 0) return sendError(res, 404, "Registration not found."); + + return res.json({ message: "Registration deleted." }); +}); + +module.exports = router; diff --git a/src/routes/rewards.js b/src/routes/rewards.js new file mode 100644 index 0000000..8103980 --- /dev/null +++ b/src/routes/rewards.js @@ -0,0 +1,128 @@ +const express = require("express"); +const { db } = require("../db"); +const requireAdminKey = require("../middleware/adminAuth"); +const { sendError, mapSqliteError } = require("../utils/errors"); +const { isNonEmptyString, isPositiveInteger, normalizeString, parseId } = require("../utils/validation"); + +const router = express.Router(); + +function validateOptionalString(value, fieldName, res) { + if (value !== null && value !== undefined && typeof value !== "string") { + sendError(res, 400, `${fieldName} must be a string or null.`); + return false; + } + return true; +} + +router.get("/", (req, res) => { + const rewards = db.prepare("SELECT * FROM rewards ORDER BY id").all(); + return res.json(rewards); +}); + +router.get("/milestone/:milestoneId", (req, res) => { + const milestoneId = parseId(req.params.milestoneId); + if (!milestoneId) return sendError(res, 400, "Invalid milestone id."); + + const milestone = db.prepare("SELECT id FROM milestones WHERE id = ?").get(milestoneId); + if (!milestone) return sendError(res, 404, "Milestone not found."); + + const rewards = db + .prepare("SELECT * FROM rewards WHERE milestone_id = ? ORDER BY id") + .all(milestoneId); + return res.json(rewards); +}); + +router.post("/", requireAdminKey, (req, res) => { + const name = normalizeString(req.body.name); + const description = req.body.description == null ? null : normalizeString(req.body.description); + const imageUrl = req.body.image_url == null ? null : normalizeString(req.body.image_url); + const milestoneId = req.body.milestone_id; + + if (!isNonEmptyString(name)) return sendError(res, 400, "name is required."); + if (!validateOptionalString(description, "description", res)) return undefined; + if (!validateOptionalString(imageUrl, "image_url", res)) return undefined; + if (!isPositiveInteger(milestoneId)) { + return sendError(res, 400, "milestone_id must be a positive integer."); + } + + const milestone = db.prepare("SELECT id FROM milestones WHERE id = ?").get(milestoneId); + if (!milestone) return sendError(res, 400, "Milestone does not exist."); + + try { + const result = db + .prepare( + "INSERT INTO rewards (milestone_id, name, description, image_url) VALUES (?, ?, ?, ?)" + ) + .run(milestoneId, name, description, imageUrl); + const reward = db.prepare("SELECT * FROM rewards WHERE id = ?").get(result.lastInsertRowid); + return res.status(201).json(reward); + } catch (error) { + const mapped = mapSqliteError(error, "Reward conflict."); + if (mapped) return sendError(res, mapped.status, mapped.message); + throw error; + } +}); + +router.put("/:id", requireAdminKey, (req, res) => { + const id = parseId(req.params.id); + if (!id) return sendError(res, 400, "Invalid reward id."); + + const existing = db.prepare("SELECT * FROM rewards WHERE id = ?").get(id); + if (!existing) return sendError(res, 404, "Reward not found."); + + const updates = []; + const values = []; + + if (Object.prototype.hasOwnProperty.call(req.body, "name")) { + const name = normalizeString(req.body.name); + if (!isNonEmptyString(name)) return sendError(res, 400, "name must not be empty."); + updates.push("name = ?"); + values.push(name); + } + + if (Object.prototype.hasOwnProperty.call(req.body, "description")) { + const description = req.body.description == null ? null : normalizeString(req.body.description); + if (!validateOptionalString(description, "description", res)) return undefined; + updates.push("description = ?"); + values.push(description); + } + + if (Object.prototype.hasOwnProperty.call(req.body, "image_url")) { + const imageUrl = req.body.image_url == null ? null : normalizeString(req.body.image_url); + if (!validateOptionalString(imageUrl, "image_url", res)) return undefined; + updates.push("image_url = ?"); + values.push(imageUrl); + } + + if (Object.prototype.hasOwnProperty.call(req.body, "milestone_id")) { + if (!isPositiveInteger(req.body.milestone_id)) { + return sendError(res, 400, "milestone_id must be a positive integer."); + } + const milestone = db.prepare("SELECT id FROM milestones WHERE id = ?").get(req.body.milestone_id); + if (!milestone) return sendError(res, 400, "Milestone does not exist."); + updates.push("milestone_id = ?"); + values.push(req.body.milestone_id); + } + + if (updates.length === 0) { + return sendError(res, 400, "At least one updatable field is required."); + } + + values.push(id); + db.prepare(`UPDATE rewards SET ${updates.join(", ")} WHERE id = ?`).run(...values); + + const reward = db.prepare("SELECT * FROM rewards WHERE id = ?").get(id); + return res.json(reward); +}); + +router.delete("/:id", requireAdminKey, (req, res) => { + const id = parseId(req.params.id); + if (!id) return sendError(res, 400, "Invalid reward id."); + + const result = db.prepare("DELETE FROM rewards WHERE id = ?").run(id); + if (result.changes === 0) return sendError(res, 404, "Reward not found."); + + return res.json({ message: "Reward deleted." }); +}); + +module.exports = router; diff --git a/src/routes/users.js b/src/routes/users.js new file mode 100644 index 0000000..adb2869 --- /dev/null +++ b/src/routes/users.js @@ -0,0 +1,140 @@ +const express = require("express"); +const { db, boolToInt } = require("../db"); +const { sendError, mapSqliteError } = require("../utils/errors"); +const { + isBoolean, + isNonEmptyString, + isValidEmail, + normalizeString, + parseId +} = require("../utils/validation"); + +const router = express.Router(); + +function serializeUser(user) { + return { + ...user, + newsletter_optin: Boolean(user.newsletter_optin) + }; +} + +router.post("/", (req, res) => { + const username = normalizeString(req.body.username); + const email = normalizeString(req.body.email); + const newsletterOptin = req.body.newsletter_optin ?? false; + + if (!isNonEmptyString(username)) { + return sendError(res, 400, "username is required."); + } + + if (!isValidEmail(email)) { + return sendError(res, 400, "A valid email is required."); + } + + if (!isBoolean(newsletterOptin)) { + return sendError(res, 400, "newsletter_optin must be a boolean."); + } + + try { + const result = db + .prepare("INSERT INTO users (username, email, newsletter_optin) VALUES (?, ?, ?)") + .run(username, email, boolToInt(newsletterOptin)); + const user = db.prepare("SELECT * FROM users WHERE id = ?").get(result.lastInsertRowid); + return res.status(201).json(serializeUser(user)); + } catch (error) { + const mapped = mapSqliteError(error, "username or email already exists."); + if (mapped) return sendError(res, mapped.status, mapped.message); + throw error; + } +}); + +router.get("/:id", (req, res) => { + const id = parseId(req.params.id); + if (!id) return sendError(res, 400, "Invalid user id."); + + const user = db.prepare("SELECT * FROM users WHERE id = ?").get(id); + if (!user) return sendError(res, 404, "User not found."); + + return res.json(serializeUser(user)); +}); + +router.put("/:id", (req, res) => { + const id = parseId(req.params.id); + if (!id) return sendError(res, 400, "Invalid user id."); + + const existing = db.prepare("SELECT * FROM users WHERE id = ?").get(id); + if (!existing) return sendError(res, 404, "User not found."); + + const updates = []; + const values = []; + + if (Object.prototype.hasOwnProperty.call(req.body, "username")) { + const username = normalizeString(req.body.username); + if (!isNonEmptyString(username)) return sendError(res, 400, "username must not be empty."); + updates.push("username = ?"); + values.push(username); + } + + if (Object.prototype.hasOwnProperty.call(req.body, "email")) { + const email = normalizeString(req.body.email); + if (!isValidEmail(email)) return sendError(res, 400, "A valid email is required."); + updates.push("email = ?"); + values.push(email); + } + + if (Object.prototype.hasOwnProperty.call(req.body, "newsletter_optin")) { + if (!isBoolean(req.body.newsletter_optin)) { + return sendError(res, 400, "newsletter_optin must be a boolean."); + } + updates.push("newsletter_optin = ?"); + values.push(boolToInt(req.body.newsletter_optin)); + } + + if (updates.length === 0) { + return sendError(res, 400, "At least one updatable field is required."); + } + + try { + values.push(id); + db.prepare(`UPDATE users SET ${updates.join(", ")} WHERE id = ?`).run(...values); + const user = db.prepare("SELECT * FROM users WHERE id = ?").get(id); + return res.json(serializeUser(user)); + } catch (error) { + const mapped = mapSqliteError(error, "username or email already exists."); + if (mapped) return sendError(res, mapped.status, mapped.message); + throw error; + } +}); + +router.delete("/:id", (req, res) => { + const id = parseId(req.params.id); + if (!id) return sendError(res, 400, "Invalid user id."); + + const result = db.prepare("DELETE FROM users WHERE id = ?").run(id); + if (result.changes === 0) return sendError(res, 404, "User not found."); + + return res.json({ message: "User deleted." }); +}); + +router.get("/:id/registrations", (req, res) => { + const id = parseId(req.params.id); + if (!id) return sendError(res, 400, "Invalid user id."); + + const user = db.prepare("SELECT id FROM users WHERE id = ?").get(id); + if (!user) return sendError(res, 404, "User not found."); + + const registrations = db + .prepare( + `SELECT registrations.id, registrations.user_id, registrations.platform_id, + platforms.name AS platform_name, registrations.created_at + FROM registrations + JOIN platforms ON platforms.id = registrations.platform_id + WHERE registrations.user_id = ? + ORDER BY registrations.id` + ) + .all(id); + + return res.json(registrations); +}); + +module.exports = router; diff --git a/src/utils/errors.js b/src/utils/errors.js new file mode 100644 index 0000000..f85861b --- /dev/null +++ b/src/utils/errors.js @@ -0,0 +1,20 @@ +function sendError(res, status, message) { + return res.status(status).json({ error: message }); +} + +function mapSqliteError(error, conflictMessage) { + if (error && error.code === "SQLITE_CONSTRAINT_UNIQUE") { + return { status: 409, message: conflictMessage }; + } + + if (error && error.code === "SQLITE_CONSTRAINT_FOREIGNKEY") { + return { status: 400, message: "Referenced resource does not exist." }; + } + + return null; +} + +module.exports = { + sendError, + mapSqliteError +}; diff --git a/src/utils/validation.js b/src/utils/validation.js new file mode 100644 index 0000000..44d9d71 --- /dev/null +++ b/src/utils/validation.js @@ -0,0 +1,35 @@ +const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + +function isNonEmptyString(value) { + return typeof value === "string" && value.trim().length > 0; +} + +function isValidEmail(value) { + return isNonEmptyString(value) && EMAIL_PATTERN.test(value.trim()); +} + +function isBoolean(value) { + return typeof value === "boolean"; +} + +function isPositiveInteger(value) { + return Number.isInteger(value) && value > 0; +} + +function parseId(value) { + const id = Number(value); + return Number.isInteger(id) && id > 0 ? id : null; +} + +function normalizeString(value) { + return typeof value === "string" ? value.trim() : value; +} + +module.exports = { + isNonEmptyString, + isValidEmail, + isBoolean, + isPositiveInteger, + parseId, + normalizeString +};