From 74f0d0dbede2aa888f76d1f838ffc4a003390492 Mon Sep 17 00:00:00 2001 From: echo Date: Thu, 19 Mar 2026 03:37:15 +0100 Subject: [PATCH] user report gtound work --- apps/admin/package-lock.json | 536 +++++++++++- apps/admin/package.json | 3 + .../app/api/fitness-profile/history/route.ts | 52 ++ apps/admin/src/app/api/hydration/route.ts | 138 +++ .../src/app/api/nutrition/meals/route.ts | 126 +++ apps/admin/src/app/api/nutrition/route.ts | 138 +++ .../__tests__/report-generation.test.ts | 373 ++++++++ .../app/api/reports/user/[userId]/route.ts | 425 +++++++++ .../src/app/api/trainer-client/[id]/route.ts | 100 +++ .../admin/src/app/api/trainer-client/route.ts | 160 ++++ apps/admin/src/app/api/users/me/route.ts | 32 + apps/admin/src/app/reports/page.tsx | 52 ++ apps/admin/src/app/trainer-clients/page.tsx | 304 +++++++ .../components/charts/chart-components.tsx | 132 +++ .../components/reports/GoalsSummaryCard.tsx | 108 +++ .../reports/HydrationSummaryCard.tsx | 119 +++ .../reports/NutritionSummaryCard.tsx | 121 +++ .../reports/RecommendationsCard.tsx | 130 +++ .../src/components/reports/ReportFilters.tsx | 195 +++++ .../src/components/reports/UserReport.tsx | 136 +++ .../components/reports/WeeklyCheckInsCard.tsx | 110 +++ apps/admin/src/components/ui/label.tsx | 22 + apps/admin/src/components/ui/select.tsx | 160 ++++ apps/admin/src/lib/database/drizzle.ts | 657 ++++++++++++++ apps/admin/src/lib/database/types.ts | 104 +++ .../lib/pdf/__tests__/test-pdf-generation.ts | 236 +++++ apps/admin/src/lib/pdf/chart-generator.ts | 316 +++++++ apps/admin/src/lib/pdf/index.ts | 15 + apps/admin/src/lib/pdf/pdf-generator.ts | 821 ++++++++++++++++++ apps/admin/src/lib/pdf/report-helpers.ts | 35 + apps/admin/src/lib/utils/iso-week.ts | 153 ++++ apps/mobile/src/api/hydration.ts | 79 ++ apps/mobile/src/api/index.ts | 2 + apps/mobile/src/api/nutrition.ts | 146 ++++ apps/mobile/src/config/api.ts | 13 + apps/mobile/src/contexts/HydrationContext.tsx | 173 ++++ apps/mobile/src/contexts/NutritionContext.tsx | 190 ++++ docs/IMPLEMENTATION_SUMMARY.md | 443 ++++++++++ docs/QUICK_REFERENCE.md | 193 ++++ docs/TESTING_GUIDE.md | 528 +++++++++++ packages/database/src/schema.ts | 201 +++++ packages/shared/src/types/index.ts | 155 ++++ 42 files changed, 8127 insertions(+), 5 deletions(-) create mode 100644 apps/admin/src/app/api/fitness-profile/history/route.ts create mode 100644 apps/admin/src/app/api/hydration/route.ts create mode 100644 apps/admin/src/app/api/nutrition/meals/route.ts create mode 100644 apps/admin/src/app/api/nutrition/route.ts create mode 100644 apps/admin/src/app/api/reports/__tests__/report-generation.test.ts create mode 100644 apps/admin/src/app/api/reports/user/[userId]/route.ts create mode 100644 apps/admin/src/app/api/trainer-client/[id]/route.ts create mode 100644 apps/admin/src/app/api/trainer-client/route.ts create mode 100644 apps/admin/src/app/api/users/me/route.ts create mode 100644 apps/admin/src/app/reports/page.tsx create mode 100644 apps/admin/src/app/trainer-clients/page.tsx create mode 100644 apps/admin/src/components/charts/chart-components.tsx create mode 100644 apps/admin/src/components/reports/GoalsSummaryCard.tsx create mode 100644 apps/admin/src/components/reports/HydrationSummaryCard.tsx create mode 100644 apps/admin/src/components/reports/NutritionSummaryCard.tsx create mode 100644 apps/admin/src/components/reports/RecommendationsCard.tsx create mode 100644 apps/admin/src/components/reports/ReportFilters.tsx create mode 100644 apps/admin/src/components/reports/UserReport.tsx create mode 100644 apps/admin/src/components/reports/WeeklyCheckInsCard.tsx create mode 100644 apps/admin/src/components/ui/label.tsx create mode 100644 apps/admin/src/components/ui/select.tsx create mode 100644 apps/admin/src/lib/pdf/__tests__/test-pdf-generation.ts create mode 100644 apps/admin/src/lib/pdf/chart-generator.ts create mode 100644 apps/admin/src/lib/pdf/index.ts create mode 100644 apps/admin/src/lib/pdf/pdf-generator.ts create mode 100644 apps/admin/src/lib/pdf/report-helpers.ts create mode 100644 apps/admin/src/lib/utils/iso-week.ts create mode 100644 apps/mobile/src/api/hydration.ts create mode 100644 apps/mobile/src/api/nutrition.ts create mode 100644 apps/mobile/src/contexts/HydrationContext.tsx create mode 100644 apps/mobile/src/contexts/NutritionContext.tsx create mode 100644 docs/IMPLEMENTATION_SUMMARY.md create mode 100644 docs/QUICK_REFERENCE.md create mode 100644 docs/TESTING_GUIDE.md diff --git a/apps/admin/package-lock.json b/apps/admin/package-lock.json index a564f04..5c52997 100644 --- a/apps/admin/package-lock.json +++ b/apps/admin/package-lock.json @@ -13,6 +13,7 @@ "@fitai/shared": "file:../../packages/shared", "@hookform/resolvers": "^5.2.2", "@radix-ui/react-dialog": "^1.1.15", + "@radix-ui/react-select": "^2.2.6", "@radix-ui/react-slot": "^1.2.4", "@react-email/components": "^1.0.8", "@react-email/render": "^2.0.4", @@ -32,6 +33,8 @@ "date-fns": "^4.1.0", "dotenv": "^17.2.3", "drizzle-orm": "^0.44.7", + "jspdf": "^4.2.1", + "jspdf-autotable": "^5.0.7", "lucide-react": "^0.553.0", "next": "^16.0.1", "pino": "^10.3.1", @@ -574,10 +577,9 @@ } }, "node_modules/@babel/runtime": { - "version": "7.28.4", - "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.28.4.tgz", - "integrity": "sha512-Q/N6JNWvIvPnLDvjlE1OUBLPQHH6l3CltCEsHIujp45zQUSSh8K+gHnaEX45yAT1nyngnINhvWtzN+Nb9D8RAQ==", - "dev": true, + "version": "7.29.2", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.29.2.tgz", + "integrity": "sha512-JiDShH45zKHWyGe4ZNVRrCjBz8Nh9TMmZG1kh4QTK8hCBTWBi8Da+i7s1fJw7/lYpM4ccepSNfqzZ/QvABBi5g==", "license": "MIT", "engines": { "node": ">=6.9.0" @@ -1027,6 +1029,44 @@ "resolved": "../../packages/shared", "link": true }, + "node_modules/@floating-ui/core": { + "version": "1.7.5", + "resolved": "https://registry.npmjs.org/@floating-ui/core/-/core-1.7.5.tgz", + "integrity": "sha512-1Ih4WTWyw0+lKyFMcBHGbb5U5FtuHJuujoyyr5zTaWS5EYMeT6Jb2AuDeftsCsEuchO+mM2ij5+q9crhydzLhQ==", + "license": "MIT", + "dependencies": { + "@floating-ui/utils": "^0.2.11" + } + }, + "node_modules/@floating-ui/dom": { + "version": "1.7.6", + "resolved": "https://registry.npmjs.org/@floating-ui/dom/-/dom-1.7.6.tgz", + "integrity": "sha512-9gZSAI5XM36880PPMm//9dfiEngYoC6Am2izES1FF406YFsjvyBMmeJ2g4SAju3xWwtuynNRFL2s9hgxpLI5SQ==", + "license": "MIT", + "dependencies": { + "@floating-ui/core": "^1.7.5", + "@floating-ui/utils": "^0.2.11" + } + }, + "node_modules/@floating-ui/react-dom": { + "version": "2.1.8", + "resolved": "https://registry.npmjs.org/@floating-ui/react-dom/-/react-dom-2.1.8.tgz", + "integrity": "sha512-cC52bHwM/n/CxS87FH0yWdngEZrjdtLW/qVruo68qg+prK7ZQ4YGdut2GyDVpoGeAYe/h899rVeOVm6Oi40k2A==", + "license": "MIT", + "dependencies": { + "@floating-ui/dom": "^1.7.6" + }, + "peerDependencies": { + "react": ">=16.8.0", + "react-dom": ">=16.8.0" + } + }, + "node_modules/@floating-ui/utils": { + "version": "0.2.11", + "resolved": "https://registry.npmjs.org/@floating-ui/utils/-/utils-0.2.11.tgz", + "integrity": "sha512-RiB/yIh78pcIxl6lLMG0CgBXAZ2Y0eVHqMPYugu+9U0AeT6YBeiJpf7lbdJNIugFP5SIjwNRgo4DhR1Qxi26Gg==", + "license": "MIT" + }, "node_modules/@gar/promisify": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/@gar/promisify/-/promisify-1.1.3.tgz", @@ -2417,12 +2457,85 @@ "url": "https://opencollective.com/pkgr" } }, + "node_modules/@radix-ui/number": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@radix-ui/number/-/number-1.1.1.tgz", + "integrity": "sha512-MkKCwxlXTgz6CFoJx3pCwn07GKp36+aZyu/u2Ln2VrA5DcdyCZkASEDBTd8x5whTQQL5CiYf4prXKLcgQdv29g==", + "license": "MIT" + }, "node_modules/@radix-ui/primitive": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/@radix-ui/primitive/-/primitive-1.1.3.tgz", "integrity": "sha512-JTF99U/6XIjCBo0wqkU5sK10glYe27MRRsfwoiq5zzOEZLHU3A3KCMa5X/azekYRCJ0HlwI0crAXS/5dEHTzDg==", "license": "MIT" }, + "node_modules/@radix-ui/react-arrow": { + "version": "1.1.7", + "resolved": "https://registry.npmjs.org/@radix-ui/react-arrow/-/react-arrow-1.1.7.tgz", + "integrity": "sha512-F+M1tLhO+mlQaOWspE8Wstg+z6PwxwRd8oQ8IXceWz92kfAmalTRf0EjrouQeo7QssEPfCn05B4Ihs1K9WQ/7w==", + "license": "MIT", + "dependencies": { + "@radix-ui/react-primitive": "2.1.3" + }, + "peerDependencies": { + "@types/react": "*", + "@types/react-dom": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc", + "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + }, + "@types/react-dom": { + "optional": true + } + } + }, + "node_modules/@radix-ui/react-collection": { + "version": "1.1.7", + "resolved": "https://registry.npmjs.org/@radix-ui/react-collection/-/react-collection-1.1.7.tgz", + "integrity": "sha512-Fh9rGN0MoI4ZFUNyfFVNU4y9LUz93u9/0K+yLgA2bwRojxM8JU1DyvvMBabnZPBgMWREAJvU2jjVzq+LrFUglw==", + "license": "MIT", + "dependencies": { + "@radix-ui/react-compose-refs": "1.1.2", + "@radix-ui/react-context": "1.1.2", + "@radix-ui/react-primitive": "2.1.3", + "@radix-ui/react-slot": "1.2.3" + }, + "peerDependencies": { + "@types/react": "*", + "@types/react-dom": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc", + "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + }, + "@types/react-dom": { + "optional": true + } + } + }, + "node_modules/@radix-ui/react-collection/node_modules/@radix-ui/react-slot": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.2.3.tgz", + "integrity": "sha512-aeNmHnBxbi2St0au6VBVC7JXFlhLlOnvIIlePNniyUNAClzmtAUEY8/pBiK3iHjufOlwA+c20/8jngo7xcrg8A==", + "license": "MIT", + "dependencies": { + "@radix-ui/react-compose-refs": "1.1.2" + }, + "peerDependencies": { + "@types/react": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + } + } + }, "node_modules/@radix-ui/react-compose-refs": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@radix-ui/react-compose-refs/-/react-compose-refs-1.1.2.tgz", @@ -2507,6 +2620,21 @@ } } }, + "node_modules/@radix-ui/react-direction": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@radix-ui/react-direction/-/react-direction-1.1.1.tgz", + "integrity": "sha512-1UEWRX6jnOA2y4H5WczZ44gOOjTEmlqv1uNW4GAJEO5+bauCBhv8snY65Iw5/VOS/ghKN9gr2KjnLKxrsvoMVw==", + "license": "MIT", + "peerDependencies": { + "@types/react": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + } + } + }, "node_modules/@radix-ui/react-dismissable-layer": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/@radix-ui/react-dismissable-layer/-/react-dismissable-layer-1.1.11.tgz", @@ -2592,6 +2720,38 @@ } } }, + "node_modules/@radix-ui/react-popper": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@radix-ui/react-popper/-/react-popper-1.2.8.tgz", + "integrity": "sha512-0NJQ4LFFUuWkE7Oxf0htBKS6zLkkjBH+hM1uk7Ng705ReR8m/uelduy1DBo0PyBXPKVnBA6YBlU94MBGXrSBCw==", + "license": "MIT", + "dependencies": { + "@floating-ui/react-dom": "^2.0.0", + "@radix-ui/react-arrow": "1.1.7", + "@radix-ui/react-compose-refs": "1.1.2", + "@radix-ui/react-context": "1.1.2", + "@radix-ui/react-primitive": "2.1.3", + "@radix-ui/react-use-callback-ref": "1.1.1", + "@radix-ui/react-use-layout-effect": "1.1.1", + "@radix-ui/react-use-rect": "1.1.1", + "@radix-ui/react-use-size": "1.1.1", + "@radix-ui/rect": "1.1.1" + }, + "peerDependencies": { + "@types/react": "*", + "@types/react-dom": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc", + "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + }, + "@types/react-dom": { + "optional": true + } + } + }, "node_modules/@radix-ui/react-portal": { "version": "1.1.9", "resolved": "https://registry.npmjs.org/@radix-ui/react-portal/-/react-portal-1.1.9.tgz", @@ -2681,6 +2841,67 @@ } } }, + "node_modules/@radix-ui/react-select": { + "version": "2.2.6", + "resolved": "https://registry.npmjs.org/@radix-ui/react-select/-/react-select-2.2.6.tgz", + "integrity": "sha512-I30RydO+bnn2PQztvo25tswPH+wFBjehVGtmagkU78yMdwTwVf12wnAOF+AeP8S2N8xD+5UPbGhkUfPyvT+mwQ==", + "license": "MIT", + "dependencies": { + "@radix-ui/number": "1.1.1", + "@radix-ui/primitive": "1.1.3", + "@radix-ui/react-collection": "1.1.7", + "@radix-ui/react-compose-refs": "1.1.2", + "@radix-ui/react-context": "1.1.2", + "@radix-ui/react-direction": "1.1.1", + "@radix-ui/react-dismissable-layer": "1.1.11", + "@radix-ui/react-focus-guards": "1.1.3", + "@radix-ui/react-focus-scope": "1.1.7", + "@radix-ui/react-id": "1.1.1", + "@radix-ui/react-popper": "1.2.8", + "@radix-ui/react-portal": "1.1.9", + "@radix-ui/react-primitive": "2.1.3", + "@radix-ui/react-slot": "1.2.3", + "@radix-ui/react-use-callback-ref": "1.1.1", + "@radix-ui/react-use-controllable-state": "1.2.2", + "@radix-ui/react-use-layout-effect": "1.1.1", + "@radix-ui/react-use-previous": "1.1.1", + "@radix-ui/react-visually-hidden": "1.2.3", + "aria-hidden": "^1.2.4", + "react-remove-scroll": "^2.6.3" + }, + "peerDependencies": { + "@types/react": "*", + "@types/react-dom": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc", + "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + }, + "@types/react-dom": { + "optional": true + } + } + }, + "node_modules/@radix-ui/react-select/node_modules/@radix-ui/react-slot": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.2.3.tgz", + "integrity": "sha512-aeNmHnBxbi2St0au6VBVC7JXFlhLlOnvIIlePNniyUNAClzmtAUEY8/pBiK3iHjufOlwA+c20/8jngo7xcrg8A==", + "license": "MIT", + "dependencies": { + "@radix-ui/react-compose-refs": "1.1.2" + }, + "peerDependencies": { + "@types/react": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + } + } + }, "node_modules/@radix-ui/react-slot": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/@radix-ui/react-slot/-/react-slot-1.2.4.tgz", @@ -2784,6 +3005,86 @@ } } }, + "node_modules/@radix-ui/react-use-previous": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@radix-ui/react-use-previous/-/react-use-previous-1.1.1.tgz", + "integrity": "sha512-2dHfToCj/pzca2Ck724OZ5L0EVrr3eHRNsG/b3xQJLA2hZpVCS99bLAX+hm1IHXDEnzU6by5z/5MIY794/a8NQ==", + "license": "MIT", + "peerDependencies": { + "@types/react": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + } + } + }, + "node_modules/@radix-ui/react-use-rect": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@radix-ui/react-use-rect/-/react-use-rect-1.1.1.tgz", + "integrity": "sha512-QTYuDesS0VtuHNNvMh+CjlKJ4LJickCMUAqjlE3+j8w+RlRpwyX3apEQKGFzbZGdo7XNG1tXa+bQqIE7HIXT2w==", + "license": "MIT", + "dependencies": { + "@radix-ui/rect": "1.1.1" + }, + "peerDependencies": { + "@types/react": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + } + } + }, + "node_modules/@radix-ui/react-use-size": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@radix-ui/react-use-size/-/react-use-size-1.1.1.tgz", + "integrity": "sha512-ewrXRDTAqAXlkl6t/fkXWNAhFX9I+CkKlw6zjEwk86RSPKwZr3xpBRso655aqYafwtnbpHLj6toFzmd6xdVptQ==", + "license": "MIT", + "dependencies": { + "@radix-ui/react-use-layout-effect": "1.1.1" + }, + "peerDependencies": { + "@types/react": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + } + } + }, + "node_modules/@radix-ui/react-visually-hidden": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@radix-ui/react-visually-hidden/-/react-visually-hidden-1.2.3.tgz", + "integrity": "sha512-pzJq12tEaaIhqjbzpCuv/OypJY/BPavOofm+dbab+MHLajy277+1lLm6JFcGgF5eskJ6mquGirhXY2GD/8u8Ug==", + "license": "MIT", + "dependencies": { + "@radix-ui/react-primitive": "2.1.3" + }, + "peerDependencies": { + "@types/react": "*", + "@types/react-dom": "*", + "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc", + "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + }, + "@types/react-dom": { + "optional": true + } + } + }, + "node_modules/@radix-ui/rect": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@radix-ui/rect/-/rect-1.1.1.tgz", + "integrity": "sha512-HPwpGIzkl28mWyZqG52jiqDJ12waP11Pa1lGoiyUkIEuMLBP0oeK/C89esbXrxsky5we7dfd8U58nm0SgAWpVw==", + "license": "MIT" + }, "node_modules/@react-email/body": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/@react-email/body/-/body-0.2.1.tgz", @@ -3019,7 +3320,6 @@ "resolved": "https://registry.npmjs.org/@react-email/render/-/render-2.0.4.tgz", "integrity": "sha512-kht2oTFQ1SwrLpd882ahTvUtNa9s53CERHstiTbzhm6aR2Hbykp/mQ4tpPvsBGkKAEvKRlDEoooh60Uk6nHK1g==", "license": "MIT", - "peer": true, "dependencies": { "html-to-text": "^9.0.5", "prettier": "^3.5.3" @@ -3595,6 +3895,19 @@ "undici-types": "~7.16.0" } }, + "node_modules/@types/pako": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/@types/pako/-/pako-2.0.4.tgz", + "integrity": "sha512-VWDCbrLeVXJM9fihYodcLiIv0ku+AlOa/TQ1SvYOaBuyrSKgEcro95LJyIsJ4vSo6BXIxOKxiJAat04CmST9Fw==", + "license": "MIT" + }, + "node_modules/@types/raf": { + "version": "3.4.3", + "resolved": "https://registry.npmjs.org/@types/raf/-/raf-3.4.3.tgz", + "integrity": "sha512-c4YAvMedbPZ5tEyxzQdMoOhhJ4RD3rngZIdwC2/qDN3d7JpEhB6fiBRKVY1lg5B7Wk+uPBjn5f39j1/2MY1oOw==", + "license": "MIT", + "optional": true + }, "node_modules/@types/react": { "version": "19.2.2", "resolved": "https://registry.npmjs.org/@types/react/-/react-19.2.2.tgz", @@ -3641,6 +3954,13 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/trusted-types": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/@types/trusted-types/-/trusted-types-2.0.7.tgz", + "integrity": "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==", + "license": "MIT", + "optional": true + }, "node_modules/@types/use-sync-external-store": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/@types/use-sync-external-store/-/use-sync-external-store-0.0.6.tgz", @@ -4891,6 +5211,16 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "license": "MIT" }, + "node_modules/base64-arraybuffer": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-1.0.2.tgz", + "integrity": "sha512-I3yl4r9QB5ZRY3XuJVEPfc2XhZO6YweFPI+UovAzn+8/hb3oJ6lnysaFcjVpkCPfVWFUDvoZ8kmVDP7WyRtYtQ==", + "license": "MIT", + "optional": true, + "engines": { + "node": ">= 0.6.0" + } + }, "node_modules/base64-js": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", @@ -5268,6 +5598,26 @@ ], "license": "CC-BY-4.0" }, + "node_modules/canvg": { + "version": "3.0.11", + "resolved": "https://registry.npmjs.org/canvg/-/canvg-3.0.11.tgz", + "integrity": "sha512-5ON+q7jCTgMp9cjpu4Jo6XbvfYwSB2Ow3kzHKfIyJfaCAOHLbdKPQqGKgfED/R5B+3TFFfe8pegYA+b423SRyA==", + "license": "MIT", + "optional": true, + "dependencies": { + "@babel/runtime": "^7.12.5", + "@types/raf": "^3.4.0", + "core-js": "^3.8.3", + "raf": "^3.4.1", + "regenerator-runtime": "^0.13.7", + "rgbcolor": "^1.0.1", + "stackblur-canvas": "^2.0.0", + "svg-pathdata": "^6.0.3" + }, + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", @@ -5569,6 +5919,18 @@ "node": ">=18" } }, + "node_modules/core-js": { + "version": "3.49.0", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.49.0.tgz", + "integrity": "sha512-es1U2+YTtzpwkxVLwAFdSpaIMyQaq0PBgm3YD1W3Qpsn1NAmO3KSgZfu+oGSWVu6NvLHoHCV/aYcsE5wiB7ALg==", + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/core-js" + } + }, "node_modules/cross-spawn": { "version": "7.0.6", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", @@ -5583,6 +5945,16 @@ "node": ">= 8" } }, + "node_modules/css-line-break": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/css-line-break/-/css-line-break-2.1.0.tgz", + "integrity": "sha512-FHcKFCZcAha3LwfVBhCQbW2nCNbkZXn7KVUJcsT5/P8YmfsVja0FMPJr0B903j/E69HUphKiV9iQArX8SDYA4w==", + "license": "MIT", + "optional": true, + "dependencies": { + "utrie": "^1.0.2" + } + }, "node_modules/css.escape": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/css.escape/-/css.escape-1.5.1.tgz", @@ -6092,6 +6464,16 @@ "url": "https://github.com/fb55/domhandler?sponsor=1" } }, + "node_modules/dompurify": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/dompurify/-/dompurify-3.3.3.tgz", + "integrity": "sha512-Oj6pzI2+RqBfFG+qOaOLbFXLQ90ARpcGG6UePL82bJLtdsa6CYJD7nmiU8MW9nQNOtCHV3lZ/Bzq1X0QYbBZCA==", + "license": "(MPL-2.0 OR Apache-2.0)", + "optional": true, + "optionalDependencies": { + "@types/trusted-types": "^2.0.7" + } + }, "node_modules/domutils": { "version": "3.2.2", "resolved": "https://registry.npmjs.org/domutils/-/domutils-3.2.2.tgz", @@ -7135,6 +7517,17 @@ "integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==", "license": "MIT" }, + "node_modules/fast-png": { + "version": "6.4.0", + "resolved": "https://registry.npmjs.org/fast-png/-/fast-png-6.4.0.tgz", + "integrity": "sha512-kAqZq1TlgBjZcLr5mcN6NP5Rv4V2f22z00c3g8vRrwkcqjerx7BEhPbOnWCPqaHUl2XWQBJQvOT/FQhdMT7X/Q==", + "license": "MIT", + "dependencies": { + "@types/pako": "^2.0.3", + "iobuffer": "^5.3.2", + "pako": "^2.1.0" + } + }, "node_modules/fast-safe-stringify": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz", @@ -7166,6 +7559,12 @@ "bser": "2.1.1" } }, + "node_modules/fflate": { + "version": "0.8.2", + "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.2.tgz", + "integrity": "sha512-cPJU47OaAoCbg0pBvzsgpTPhmhqI5eJjh/JIu8tPj5q+T7iLvW/JAYUqmE7KOB4R1ZyEhzBaIQpQpardBF5z8A==", + "license": "MIT" + }, "node_modules/file-entry-cache": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-8.0.0.tgz", @@ -7907,6 +8306,20 @@ "node": ">=14" } }, + "node_modules/html2canvas": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/html2canvas/-/html2canvas-1.4.1.tgz", + "integrity": "sha512-fPU6BHNpsyIhr8yyMpTLLxAbkaK8ArIBcmZIRiBLiDhjeqvXolaEmDGmELFuX9I4xDcaKKcJl+TKZLqruBbmWA==", + "license": "MIT", + "optional": true, + "dependencies": { + "css-line-break": "^2.1.0", + "text-segmentation": "^1.0.3" + }, + "engines": { + "node": ">=8.0.0" + } + }, "node_modules/htmlparser2": { "version": "8.0.2", "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-8.0.2.tgz", @@ -8156,6 +8569,12 @@ "node": ">=12" } }, + "node_modules/iobuffer": { + "version": "5.4.0", + "resolved": "https://registry.npmjs.org/iobuffer/-/iobuffer-5.4.0.tgz", + "integrity": "sha512-DRebOWuqDvxunfkNJAlc3IzWIPD5xVxwUNbHr7xKB8E6aLJxIPfNX3CoMJghcFjpv6RWQsrcJbghtEwSPoJqMA==", + "license": "MIT" + }, "node_modules/ip-address": { "version": "10.1.0", "resolved": "https://registry.npmjs.org/ip-address/-/ip-address-10.1.0.tgz", @@ -9897,6 +10316,33 @@ "node": ">=6" } }, + "node_modules/jspdf": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/jspdf/-/jspdf-4.2.1.tgz", + "integrity": "sha512-YyAXyvnmjTbR4bHQRLzex3CuINCDlQnBqoSYyjJwTP2x9jDLuKDzy7aKUl0hgx3uhcl7xzg32agn5vlie6HIlQ==", + "license": "MIT", + "peer": true, + "dependencies": { + "@babel/runtime": "^7.28.6", + "fast-png": "^6.2.0", + "fflate": "^0.8.1" + }, + "optionalDependencies": { + "canvg": "^3.0.11", + "core-js": "^3.6.0", + "dompurify": "^3.3.1", + "html2canvas": "^1.0.0-rc.5" + } + }, + "node_modules/jspdf-autotable": { + "version": "5.0.7", + "resolved": "https://registry.npmjs.org/jspdf-autotable/-/jspdf-autotable-5.0.7.tgz", + "integrity": "sha512-2wr7H6liNDBYNwt25hMQwXkEWFOEopgKIvR1Eukuw6Zmprm/ZcnmLTQEjW7Xx3FCbD3v7pflLcnMAv/h1jFDQw==", + "license": "MIT", + "peerDependencies": { + "jspdf": "^2 || ^3 || ^4" + } + }, "node_modules/jsx-ast-utils": { "version": "3.3.5", "resolved": "https://registry.npmjs.org/jsx-ast-utils/-/jsx-ast-utils-3.3.5.tgz", @@ -11119,6 +11565,12 @@ "dev": true, "license": "BlueOak-1.0.0" }, + "node_modules/pako": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/pako/-/pako-2.1.0.tgz", + "integrity": "sha512-w+eufiZ1WuJYgPXbV/PO3NCMEc3xqylkKHzp8bxp1uW4qaSNQUkwmLLEc3kKsfz8lpV1F8Ht3U1Cm+9Srog2ug==", + "license": "(MIT AND Zlib)" + }, "node_modules/parent-module": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz", @@ -11243,6 +11695,13 @@ "url": "https://ko-fi.com/killymxi" } }, + "node_modules/performance-now": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", + "integrity": "sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow==", + "license": "MIT", + "optional": true + }, "node_modules/picocolors": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", @@ -11810,6 +12269,16 @@ "integrity": "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg==", "license": "MIT" }, + "node_modules/raf": { + "version": "3.4.1", + "resolved": "https://registry.npmjs.org/raf/-/raf-3.4.1.tgz", + "integrity": "sha512-Sq4CW4QhwOHE8ucn6J34MqtZCeWFP2aQSmrlroYgqAV1PjStIhJXxYuTgUIfkEk7zTLjmIjLmU5q+fbD1NnOJA==", + "license": "MIT", + "optional": true, + "dependencies": { + "performance-now": "^2.1.0" + } + }, "node_modules/rc": { "version": "1.2.8", "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz", @@ -12102,6 +12571,13 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/regenerator-runtime": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz", + "integrity": "sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==", + "license": "MIT", + "optional": true + }, "node_modules/regexp.prototype.flags": { "version": "1.5.4", "resolved": "https://registry.npmjs.org/regexp.prototype.flags/-/regexp.prototype.flags-1.5.4.tgz", @@ -12242,6 +12718,16 @@ "node": ">=0.10.0" } }, + "node_modules/rgbcolor": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/rgbcolor/-/rgbcolor-1.0.1.tgz", + "integrity": "sha512-9aZLIrhRaD97sgVhtJOW6ckOEh6/GnvQtdVNfdZ6s67+3/XwLS9lBcQYzEEhYVeUowN7pRzMLsyGhK2i/xvWbw==", + "license": "MIT OR SEE LICENSE IN FEEL-FREE.md", + "optional": true, + "engines": { + "node": ">= 0.8.15" + } + }, "node_modules/rimraf": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", @@ -12943,6 +13429,16 @@ "node": ">=8" } }, + "node_modules/stackblur-canvas": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/stackblur-canvas/-/stackblur-canvas-2.7.0.tgz", + "integrity": "sha512-yf7OENo23AGJhBriGx0QivY5JP6Y1HbrrDI6WLt6C5auYZXlQrheoY8hD4ibekFKz1HOfE48Ww8kMWMnJD/zcQ==", + "license": "MIT", + "optional": true, + "engines": { + "node": ">=0.1.14" + } + }, "node_modules/standardwebhooks": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/standardwebhooks/-/standardwebhooks-1.0.0.tgz", @@ -13333,6 +13829,16 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/svg-pathdata": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/svg-pathdata/-/svg-pathdata-6.0.3.tgz", + "integrity": "sha512-qsjeeq5YjBZ5eMdFuUa4ZosMLxgr5RZ+F+Y1OrDhuOCEInRMA3x74XdBtggJcj9kOeInz0WE+LgCPDkZFlBYJw==", + "license": "MIT", + "optional": true, + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/svix": { "version": "1.84.1", "resolved": "https://registry.npmjs.org/svix/-/svix-1.84.1.tgz", @@ -13566,6 +14072,16 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/text-segmentation": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/text-segmentation/-/text-segmentation-1.0.3.tgz", + "integrity": "sha512-iOiPUo/BGnZ6+54OsWxZidGCsdU8YbE4PSpdPinp7DeMtUJNJBoJ/ouUSTJjHkh1KntHaltHl/gDs2FC4i5+Nw==", + "license": "MIT", + "optional": true, + "dependencies": { + "utrie": "^1.0.2" + } + }, "node_modules/thenify": { "version": "3.3.1", "resolved": "https://registry.npmjs.org/thenify/-/thenify-3.3.1.tgz", @@ -14212,6 +14728,16 @@ "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", "license": "MIT" }, + "node_modules/utrie": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/utrie/-/utrie-1.0.2.tgz", + "integrity": "sha512-1MLa5ouZiOmQzUbjbu9VmjLzn1QLXBhwpUa7kdLUQK+KQ5KA9I1vk5U4YHe/X2Ch7PYnJfWuWT+VbuxbGwljhw==", + "license": "MIT", + "optional": true, + "dependencies": { + "base64-arraybuffer": "^1.0.2" + } + }, "node_modules/uuid": { "version": "10.0.0", "resolved": "https://registry.npmjs.org/uuid/-/uuid-10.0.0.tgz", diff --git a/apps/admin/package.json b/apps/admin/package.json index 43205f8..9b5d374 100644 --- a/apps/admin/package.json +++ b/apps/admin/package.json @@ -19,6 +19,7 @@ "@fitai/shared": "file:../../packages/shared", "@hookform/resolvers": "^5.2.2", "@radix-ui/react-dialog": "^1.1.15", + "@radix-ui/react-select": "^2.2.6", "@radix-ui/react-slot": "^1.2.4", "@react-email/components": "^1.0.8", "@react-email/render": "^2.0.4", @@ -38,6 +39,8 @@ "date-fns": "^4.1.0", "dotenv": "^17.2.3", "drizzle-orm": "^0.44.7", + "jspdf": "^4.2.1", + "jspdf-autotable": "^5.0.7", "lucide-react": "^0.553.0", "next": "^16.0.1", "pino": "^10.3.1", diff --git a/apps/admin/src/app/api/fitness-profile/history/route.ts b/apps/admin/src/app/api/fitness-profile/history/route.ts new file mode 100644 index 0000000..5a61ac0 --- /dev/null +++ b/apps/admin/src/app/api/fitness-profile/history/route.ts @@ -0,0 +1,52 @@ +import { NextRequest, NextResponse } from "next/server"; +import { auth } from "@clerk/nextjs/server"; +import { getDatabase } from "@/lib/database"; + +/** + * GET /api/fitness-profile/history + * Get fitness profile history for a user + * + * Query params: + * - userId: string (required) + * - startDate: YYYY-MM-DD (optional) + * - endDate: YYYY-MM-DD (optional) + */ +export async function GET(request: NextRequest) { + try { + const { userId: authUserId } = await auth(); + if (!authUserId) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + const { searchParams } = new URL(request.url); + const userId = searchParams.get("userId"); + const startDateStr = searchParams.get("startDate"); + const endDateStr = searchParams.get("endDate"); + + if (!userId) { + return NextResponse.json( + { error: "userId is required" }, + { status: 400 }, + ); + } + + // Convert date strings to Date objects if provided + const startDate = startDateStr ? new Date(startDateStr) : undefined; + const endDate = endDateStr ? new Date(endDateStr) : undefined; + + const db = await getDatabase(); + const history = await db.getFitnessProfileHistory( + userId, + startDate, + endDate, + ); + + return NextResponse.json({ history }); + } catch (error) { + console.error("Get fitness profile history error:", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} diff --git a/apps/admin/src/app/api/hydration/route.ts b/apps/admin/src/app/api/hydration/route.ts new file mode 100644 index 0000000..f22ba44 --- /dev/null +++ b/apps/admin/src/app/api/hydration/route.ts @@ -0,0 +1,138 @@ +import { auth } from "@clerk/nextjs/server"; +import { NextRequest, NextResponse } from "next/server"; +import { getDatabase } from "@/lib/database"; +import { ensureUserSynced } from "@/lib/sync-user"; +import log from "@/lib/logger"; + +export async function POST(req: NextRequest) { + try { + const { userId } = await auth(); + if (!userId) return new NextResponse("Unauthorized", { status: 401 }); + + const db = await getDatabase(); + await ensureUserSynced(userId, db); + + const body = await req.json(); + const { date, entries, totalWater, waterGoal } = body; + + if (!date) { + return NextResponse.json({ error: "Date is required" }, { status: 400 }); + } + + // Check if entry already exists for this date + const existing = await db.getDailyHydration(userId, date); + + let result; + if (existing) { + // Update existing entry + result = await db.updateDailyHydration(existing.id, { + entries, + totalWater: totalWater ?? existing.totalWater, + waterGoal: waterGoal ?? existing.waterGoal, + }); + } else { + // Create new entry + result = await db.createDailyHydration({ + userId, + date, + entries: entries || [], + totalWater: totalWater || 0, + waterGoal: waterGoal || 2000, + }); + } + + return NextResponse.json(result); + } catch (error) { + log.error("Failed to save hydration data", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} + +export async function GET(req: NextRequest) { + try { + const { userId } = await auth(); + if (!userId) return new NextResponse("Unauthorized", { status: 401 }); + + const db = await getDatabase(); + await ensureUserSynced(userId, db); + + const url = new URL(req.url); + const date = url.searchParams.get("date"); + const startDate = url.searchParams.get("startDate"); + const endDate = url.searchParams.get("endDate"); + + // Single date query + if (date) { + const result = await db.getDailyHydration(userId, date); + return NextResponse.json(result); + } + + // Date range query + if (startDate && endDate) { + const results = await db.getDailyHydrationRange( + userId, + startDate, + endDate, + ); + return NextResponse.json(results); + } + + return NextResponse.json( + { error: "Either 'date' or 'startDate' and 'endDate' are required" }, + { status: 400 }, + ); + } catch (error) { + log.error("Failed to fetch hydration data", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} + +export async function DELETE(req: NextRequest) { + try { + const { userId } = await auth(); + if (!userId) return new NextResponse("Unauthorized", { status: 401 }); + + const db = await getDatabase(); + await ensureUserSynced(userId, db); + + const url = new URL(req.url); + const id = url.searchParams.get("id"); + + if (!id) { + return NextResponse.json({ error: "ID is required" }, { status: 400 }); + } + + // Verify ownership before deletion + const existing = await db.getDailyHydrationById(id); + if (!existing) { + return NextResponse.json({ error: "Not found" }, { status: 404 }); + } + + if (existing.userId !== userId) { + return NextResponse.json( + { error: "Forbidden: You can only delete your own hydration data" }, + { status: 403 }, + ); + } + + const success = await db.deleteDailyHydration(id); + + if (success) { + return NextResponse.json({ success: true }); + } else { + return NextResponse.json({ error: "Not found" }, { status: 404 }); + } + } catch (error) { + log.error("Failed to delete hydration data", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} diff --git a/apps/admin/src/app/api/nutrition/meals/route.ts b/apps/admin/src/app/api/nutrition/meals/route.ts new file mode 100644 index 0000000..dd1f8b2 --- /dev/null +++ b/apps/admin/src/app/api/nutrition/meals/route.ts @@ -0,0 +1,126 @@ +import { auth } from "@clerk/nextjs/server"; +import { NextRequest, NextResponse } from "next/server"; +import { getDatabase } from "@/lib/database"; +import { ensureUserSynced } from "@/lib/sync-user"; +import log from "@/lib/logger"; + +export async function POST(req: NextRequest) { + try { + const { userId } = await auth(); + if (!userId) return new NextResponse("Unauthorized", { status: 401 }); + + const db = await getDatabase(); + await ensureUserSynced(userId, db); + + const body = await req.json(); + const { + dailyNutritionId, + mealType, + foodName, + calories, + protein, + carbs, + fats, + } = body; + + if (!mealType || !foodName || calories === undefined) { + return NextResponse.json( + { error: "mealType, foodName, and calories are required" }, + { status: 400 }, + ); + } + + const result = await db.createMealEntry({ + userId, + dailyNutritionId, + mealType, + foodName, + calories, + protein, + carbs, + fats, + timestamp: new Date(), + }); + + return NextResponse.json(result); + } catch (error) { + log.error("Failed to create meal entry", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} + +export async function GET(req: NextRequest) { + try { + const { userId } = await auth(); + if (!userId) return new NextResponse("Unauthorized", { status: 401 }); + + const db = await getDatabase(); + await ensureUserSynced(userId, db); + + const url = new URL(req.url); + const date = url.searchParams.get("date"); + + if (!date) { + return NextResponse.json( + { error: "date is required (YYYY-MM-DD format)" }, + { status: 400 }, + ); + } + + const results = await db.getMealEntriesByDate(userId, date); + return NextResponse.json(results); + } catch (error) { + log.error("Failed to fetch meal entries", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} + +export async function DELETE(req: NextRequest) { + try { + const { userId } = await auth(); + if (!userId) return new NextResponse("Unauthorized", { status: 401 }); + + const db = await getDatabase(); + await ensureUserSynced(userId, db); + + const url = new URL(req.url); + const id = url.searchParams.get("id"); + + if (!id) { + return NextResponse.json({ error: "ID is required" }, { status: 400 }); + } + + // Verify ownership before deletion + const existing = await db.getMealEntryById(id); + if (!existing) { + return NextResponse.json({ error: "Not found" }, { status: 404 }); + } + + if (existing.userId !== userId) { + return NextResponse.json( + { error: "Forbidden: You can only delete your own meal entries" }, + { status: 403 }, + ); + } + + const success = await db.deleteMealEntry(id); + + if (success) { + return NextResponse.json({ success: true }); + } else { + return NextResponse.json({ error: "Not found" }, { status: 404 }); + } + } catch (error) { + log.error("Failed to delete meal entry", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} diff --git a/apps/admin/src/app/api/nutrition/route.ts b/apps/admin/src/app/api/nutrition/route.ts new file mode 100644 index 0000000..88206f8 --- /dev/null +++ b/apps/admin/src/app/api/nutrition/route.ts @@ -0,0 +1,138 @@ +import { auth } from "@clerk/nextjs/server"; +import { NextRequest, NextResponse } from "next/server"; +import { getDatabase } from "@/lib/database"; +import { ensureUserSynced } from "@/lib/sync-user"; +import log from "@/lib/logger"; + +export async function POST(req: NextRequest) { + try { + const { userId } = await auth(); + if (!userId) return new NextResponse("Unauthorized", { status: 401 }); + + const db = await getDatabase(); + await ensureUserSynced(userId, db); + + const body = await req.json(); + const { date, meals, totalCalories, calorieGoal } = body; + + if (!date) { + return NextResponse.json({ error: "Date is required" }, { status: 400 }); + } + + // Check if entry already exists for this date + const existing = await db.getDailyNutrition(userId, date); + + let result; + if (existing) { + // Update existing entry + result = await db.updateDailyNutrition(existing.id, { + meals, + totalCalories: totalCalories ?? existing.totalCalories, + calorieGoal: calorieGoal ?? existing.calorieGoal, + }); + } else { + // Create new entry + result = await db.createDailyNutrition({ + userId, + date, + meals: meals || [], + totalCalories: totalCalories || 0, + calorieGoal: calorieGoal || 2000, + }); + } + + return NextResponse.json(result); + } catch (error) { + log.error("Failed to save nutrition data", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} + +export async function GET(req: NextRequest) { + try { + const { userId } = await auth(); + if (!userId) return new NextResponse("Unauthorized", { status: 401 }); + + const db = await getDatabase(); + await ensureUserSynced(userId, db); + + const url = new URL(req.url); + const date = url.searchParams.get("date"); + const startDate = url.searchParams.get("startDate"); + const endDate = url.searchParams.get("endDate"); + + // Single date query + if (date) { + const result = await db.getDailyNutrition(userId, date); + return NextResponse.json(result); + } + + // Date range query + if (startDate && endDate) { + const results = await db.getDailyNutritionRange( + userId, + startDate, + endDate, + ); + return NextResponse.json(results); + } + + return NextResponse.json( + { error: "Either 'date' or 'startDate' and 'endDate' are required" }, + { status: 400 }, + ); + } catch (error) { + log.error("Failed to fetch nutrition data", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} + +export async function DELETE(req: NextRequest) { + try { + const { userId } = await auth(); + if (!userId) return new NextResponse("Unauthorized", { status: 401 }); + + const db = await getDatabase(); + await ensureUserSynced(userId, db); + + const url = new URL(req.url); + const id = url.searchParams.get("id"); + + if (!id) { + return NextResponse.json({ error: "ID is required" }, { status: 400 }); + } + + // Verify ownership before deletion + const existing = await db.getDailyNutritionById(id); + if (!existing) { + return NextResponse.json({ error: "Not found" }, { status: 404 }); + } + + if (existing.userId !== userId) { + return NextResponse.json( + { error: "Forbidden: You can only delete your own nutrition data" }, + { status: 403 }, + ); + } + + const success = await db.deleteDailyNutrition(id); + + if (success) { + return NextResponse.json({ success: true }); + } else { + return NextResponse.json({ error: "Not found" }, { status: 404 }); + } + } catch (error) { + log.error("Failed to delete nutrition data", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} diff --git a/apps/admin/src/app/api/reports/__tests__/report-generation.test.ts b/apps/admin/src/app/api/reports/__tests__/report-generation.test.ts new file mode 100644 index 0000000..93638a2 --- /dev/null +++ b/apps/admin/src/app/api/reports/__tests__/report-generation.test.ts @@ -0,0 +1,373 @@ +/** + * Report Generation API Tests + * + * Tests for /api/reports/user/[userId] endpoint + * + * Test Coverage: + * 1. Access Control (client, trainer, admin, superAdmin) + * 2. Data Aggregation (attendance, nutrition, hydration, goals) + * 3. PDF Generation + * 4. Date Range Validation + */ + +import { describe, it, expect, beforeAll, afterAll } from "@jest/globals"; + +// Mock database +const mockDatabase = { + getUserById: jest.fn(), + getClientByUserId: jest.fn(), + getFitnessProfileByUserId: jest.fn(), + getAttendanceByWeek: jest.fn(), + getDailyNutritionRange: jest.fn(), + getDailyHydrationRange: jest.fn(), + getFitnessGoalsByUserId: jest.fn(), + getFitnessProfileHistory: jest.fn(), + getRecommendationsByUserId: jest.fn(), + getTrainerClientAssignments: jest.fn(), +}; + +// Mock user data +const mockUsers = { + client: { + id: "user_client_123", + email: "client@example.com", + firstName: "John", + lastName: "Client", + role: "client", + gymId: "gym_123", + }, + trainer: { + id: "user_trainer_456", + email: "trainer@example.com", + firstName: "Jane", + lastName: "Trainer", + role: "trainer", + gymId: "gym_123", + }, + admin: { + id: "user_admin_789", + email: "admin@example.com", + firstName: "Admin", + lastName: "User", + role: "admin", + gymId: "gym_123", + }, + superAdmin: { + id: "user_superadmin_000", + email: "superadmin@example.com", + firstName: "Super", + lastName: "Admin", + role: "superAdmin", + gymId: undefined, + }, +}; + +describe("Report Generation API", () => { + describe("Access Control", () => { + it("should allow clients to view their own reports", async () => { + const currentUser = mockUsers.client; + const targetUserId = mockUsers.client.id; + + // Users can always view their own reports + const hasAccess = currentUser.id === targetUserId; + expect(hasAccess).toBe(true); + }); + + it("should allow trainers to view assigned client reports", async () => { + const currentUser = mockUsers.trainer; + const targetUserId = mockUsers.client.id; + + // Mock assignment exists + mockDatabase.getTrainerClientAssignments.mockResolvedValue([ + { + trainerId: currentUser.id, + clientId: targetUserId, + isActive: true, + }, + ]); + + const assignments = await mockDatabase.getTrainerClientAssignments( + currentUser.id, + ); + const assignment = assignments.find( + (a: any) => a.clientId === targetUserId && a.isActive, + ); + const hasAccess = !!assignment; + + expect(hasAccess).toBe(true); + }); + + it("should deny trainers access to non-assigned client reports", async () => { + const currentUser = mockUsers.trainer; + const targetUserId = "user_other_client_999"; + + mockDatabase.getTrainerClientAssignments.mockResolvedValue([]); + + const assignments = await mockDatabase.getTrainerClientAssignments( + currentUser.id, + ); + const assignment = assignments.find( + (a: any) => a.clientId === targetUserId && a.isActive, + ); + const hasAccess = !!assignment; + + expect(hasAccess).toBe(false); + }); + + it("should allow admins to view clients in their gym", async () => { + const currentUser = mockUsers.admin; + const targetUser = { ...mockUsers.client, gymId: "gym_123" }; + + mockDatabase.getUserById.mockResolvedValue(targetUser); + + const user = await mockDatabase.getUserById(targetUser.id); + const hasAccess = user?.gymId === currentUser.gymId; + + expect(hasAccess).toBe(true); + }); + + it("should deny admins access to clients outside their gym", async () => { + const currentUser = mockUsers.admin; + const targetUser = { ...mockUsers.client, gymId: "gym_other_999" }; + + mockDatabase.getUserById.mockResolvedValue(targetUser); + + const user = await mockDatabase.getUserById(targetUser.id); + const hasAccess = user?.gymId === currentUser.gymId; + + expect(hasAccess).toBe(false); + }); + + it("should allow superAdmins to view any user", async () => { + const currentUser = mockUsers.superAdmin; + + // SuperAdmins bypass gym checks + const hasAccess = currentUser.role === "superAdmin"; + + expect(hasAccess).toBe(true); + }); + + it("should deny clients access to other users reports", async () => { + const currentUser = mockUsers.client; + const targetUserId = "user_other_123"; + + // Only own ID or specific permissions + const hasAccess = currentUser.id === targetUserId; + + expect(hasAccess).toBe(false); + }); + }); + + describe("Date Range Validation", () => { + it("should accept valid date format YYYY-MM-DD", () => { + const validDates = ["2024-01-01", "2024-12-31", "2024-06-15"]; + + validDates.forEach((date) => { + const parsed = Date.parse(date); + expect(isNaN(parsed)).toBe(false); + }); + }); + + it("should reject invalid date formats", () => { + const invalidDates = ["01-01-2024", "2024/01/01", "2024-1-1", "invalid"]; + + invalidDates.forEach((date) => { + const parsed = Date.parse(date); + expect(isNaN(parsed)).toBe(true); + }); + }); + + it("should default to last 30 days if no dates provided", () => { + const endDate = new Date(); + const startDate = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000); + + const daysDiff = Math.ceil( + (endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24), + ); + + expect(daysDiff).toBe(30); + }); + }); + + describe("Data Aggregation", () => { + it("should aggregate weekly check-ins correctly", () => { + const attendanceRecords = [ + { + checkInTime: new Date("2024-01-02T10:00:00"), + checkOutTime: new Date("2024-01-02T11:00:00"), + type: "gym", + }, + { + checkInTime: new Date("2024-01-03T14:00:00"), + checkOutTime: new Date("2024-01-03T15:30:00"), + type: "gym", + }, + { + checkInTime: new Date("2024-01-04T09:00:00"), + checkOutTime: null, // Still checked in + type: "class", + }, + ]; + + const totalCheckIns = attendanceRecords.length; + const completedCheckIns = attendanceRecords.filter( + (r) => r.checkOutTime, + ).length; + + expect(totalCheckIns).toBe(3); + expect(completedCheckIns).toBe(2); + }); + + it("should calculate total time spent correctly", () => { + const attendanceRecords = [ + { + checkInTime: new Date("2024-01-02T10:00:00"), + checkOutTime: new Date("2024-01-02T11:30:00"), // 90 minutes + }, + { + checkInTime: new Date("2024-01-03T14:00:00"), + checkOutTime: new Date("2024-01-03T15:00:00"), // 60 minutes + }, + ]; + + const totalMinutes = attendanceRecords.reduce((sum, record) => { + if (record.checkOutTime) { + const duration = + record.checkOutTime.getTime() - record.checkInTime.getTime(); + return sum + Math.floor(duration / 60000); + } + return sum; + }, 0); + + expect(totalMinutes).toBe(150); + }); + + it("should handle nutrition goal achievement correctly", () => { + const nutritionRecords = [ + { date: "2024-01-01", totalCalories: 2100, calorieGoal: 2000 }, + { date: "2024-01-02", totalCalories: 1900, calorieGoal: 2000 }, + { date: "2024-01-03", totalCalories: 2200, calorieGoal: 2000 }, + ]; + + const daysMetGoal = nutritionRecords.filter((record) => { + const lowerBound = record.calorieGoal * 0.9; + const upperBound = record.calorieGoal * 1.1; + return ( + record.totalCalories >= lowerBound && + record.totalCalories <= upperBound + ); + }).length; + + expect(daysMetGoal).toBe(3); // All within ±10% + }); + + it("should handle hydration goal achievement correctly", () => { + const hydrationRecords = [ + { date: "2024-01-01", totalWater: 2500, waterGoal: 2000 }, // 125% + { date: "2024-01-02", totalWater: 1800, waterGoal: 2000 }, // 90% + { date: "2024-01-03", totalWater: 2000, waterGoal: 2000 }, // 100% + ]; + + const daysMetGoal = hydrationRecords.filter( + (record) => record.totalWater / record.waterGoal >= 1, + ).length; + + expect(daysMetGoal).toBe(2); // Days 1 and 3 meet goal + }); + }); + + describe("Report Structure", () => { + it("should include all required sections", () => { + const requiredSections = [ + "userId", + "user", + "client", + "fitnessProfile", + "reportPeriod", + "weeklyCheckIns", + "nutrition", + "hydration", + "goals", + "profileHistory", + "recommendations", + "generatedAt", + ]; + + // Mock report object + const report = { + userId: "user_123", + user: {}, + client: null, + fitnessProfile: null, + reportPeriod: { startDate: "", endDate: "" }, + weeklyCheckIns: [], + nutrition: { + dailySummaries: [], + averageDailyCalories: 0, + totalDays: 0, + daysMetGoal: 0, + }, + hydration: { + dailySummaries: [], + averageDailyWater: 0, + totalDays: 0, + daysMetGoal: 0, + }, + goals: { + active: [], + completed: [], + totalActive: 0, + totalCompleted: 0, + averageProgress: 0, + }, + profileHistory: [], + recommendations: { + accepted: [], + rejected: [], + pending: [], + totalAccepted: 0, + totalRejected: 0, + totalPending: 0, + }, + generatedAt: new Date(), + }; + + requiredSections.forEach((section) => { + expect(report).toHaveProperty(section); + }); + }); + }); +}); + +describe("ISO Week Calculations", () => { + it("should start week on Monday", () => { + // Test dates + const dates = [ + { date: "2024-01-01", expectedMonday: "2024-01-01" }, // Monday + { date: "2024-01-02", expectedMonday: "2024-01-01" }, // Tuesday + { date: "2024-01-03", expectedMonday: "2024-01-01" }, // Wednesday + { date: "2024-01-07", expectedMonday: "2024-01-01" }, // Sunday + { date: "2024-01-08", expectedMonday: "2024-01-08" }, // Monday (next week) + ]; + + dates.forEach(({ date, expectedMonday }) => { + const current = new Date(date); + const day = current.getDay(); + const diff = current.getDate() - day + (day === 0 ? -6 : 1); + const monday = new Date(current); + monday.setDate(diff); + + const mondayStr = monday.toISOString().split("T")[0]; + expect(mondayStr).toBe(expectedMonday); + }); + }); + + it("should end week on Sunday", () => { + const monday = new Date("2024-01-01"); + const sunday = new Date(monday); + sunday.setDate(sunday.getDate() + 6); + + const sundayStr = sunday.toISOString().split("T")[0]; + expect(sundayStr).toBe("2024-01-07"); + }); +}); diff --git a/apps/admin/src/app/api/reports/user/[userId]/route.ts b/apps/admin/src/app/api/reports/user/[userId]/route.ts new file mode 100644 index 0000000..2ca7e1e --- /dev/null +++ b/apps/admin/src/app/api/reports/user/[userId]/route.ts @@ -0,0 +1,425 @@ +import { NextRequest, NextResponse } from "next/server"; +import { auth } from "@clerk/nextjs/server"; +import { getDatabase } from "@/lib/database"; +import { ensureUserSynced } from "@/lib/sync-user"; +import log from "@/lib/logger"; +import type { + UserReport, + WeeklyCheckInStats, + NutritionSummary, + HydrationSummary, + GoalSummary, + ProfileChangeSummary, + RecommendationSummary, + AttendanceType, +} from "@fitai/shared"; +import { ATTENDANCE_TYPES } from "@fitai/shared"; +import { generateReportPDFBase64 } from "@/lib/pdf/report-helpers"; + +/** + * GET /api/reports/user/[userId] + * Generate a comprehensive user report + * + * Query params: + * - userId: string (required) - Target user ID + * - startDate: YYYY-MM-DD (optional, default: 30 days ago) + * - endDate: YYYY-MM-DD (optional, default: today) + * + * Access Control: + * - Admins: Can generate reports for any user in their gym + * - Trainers: Can generate reports for their assigned clients only + * - Users: Can only generate reports for themselves + */ +export async function GET( + request: NextRequest, + { params }: { params: Promise<{ userId: string }> }, +) { + try { + const { userId: clerkUserId } = await auth(); + if (!clerkUserId) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + const db = await getDatabase(); + const currentUser = await ensureUserSynced(clerkUserId, db); + + if (!currentUser) { + return NextResponse.json({ error: "Forbidden" }, { status: 403 }); + } + + const { userId: targetUserId } = await params; + + // Access Control Check + const hasAccess = await checkReportAccess(currentUser, targetUserId, db); + if (!hasAccess) { + return NextResponse.json( + { error: "You don't have permission to view this report" }, + { status: 403 }, + ); + } + + // Parse date range from query params + const { searchParams } = new URL(request.url); + const endDate = + searchParams.get("endDate") || new Date().toISOString().split("T")[0]; + const startDate = + searchParams.get("startDate") || + new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) + .toISOString() + .split("T")[0]; + const format = searchParams.get("format") || "json"; + + // Validate dates + if (isNaN(Date.parse(startDate)) || isNaN(Date.parse(endDate))) { + return NextResponse.json( + { error: "Invalid date format. Use YYYY-MM-DD" }, + { status: 400 }, + ); + } + + // Fetch all user data + const [user, client, fitnessProfile] = await Promise.all([ + db.getUserById(targetUserId), + db.getClientByUserId(targetUserId), + db.getFitnessProfileByUserId(targetUserId), + ]); + + if (!user) { + return NextResponse.json({ error: "User not found" }, { status: 404 }); + } + + // Aggregate data in parallel + const [ + nutritionRecords, + hydrationRecords, + fitnessGoals, + profileHistory, + recommendations, + ] = await Promise.all([ + db.getDailyNutritionRange(targetUserId, startDate, endDate), + db.getDailyHydrationRange(targetUserId, startDate, endDate), + db.getFitnessGoalsByUserId(targetUserId), + db.getFitnessProfileHistory( + targetUserId, + new Date(startDate), + new Date(endDate), + ), + db.getRecommendationsByUserId(targetUserId), + ]); + + // Process and aggregate data + const weeklyCheckIns = await processWeeklyCheckIns( + db, + targetUserId, + startDate, + endDate, + ); + const nutrition = processNutrition(nutritionRecords); + const hydration = processHydration(hydrationRecords); + const goals = processGoals(fitnessGoals); + const profileChanges = processProfileHistory(profileHistory); + const recs = processRecommendations(recommendations); + + const report: UserReport = { + userId: targetUserId, + user, + client, + fitnessProfile, + reportPeriod: { + startDate, + endDate, + }, + weeklyCheckIns, + nutrition, + hydration, + goals, + profileHistory: profileChanges, + recommendations: recs, + generatedAt: new Date(), + }; + + // Return PDF if requested + if (format === "pdf") { + try { + const pdfBase64 = generateReportPDFBase64(report); + const pdfBuffer = Buffer.from(pdfBase64, "base64"); + + return new NextResponse(pdfBuffer, { + status: 200, + headers: { + "Content-Type": "application/pdf", + "Content-Disposition": `attachment; filename="FitAI_Report_${report.user.firstName}_${report.user.lastName}_${startDate}_${endDate}.pdf"`, + "Content-Length": pdfBuffer.length.toString(), + }, + }); + } catch (pdfError) { + log.error("Failed to generate PDF", pdfError); + return NextResponse.json( + { error: "Failed to generate PDF" }, + { status: 500 }, + ); + } + } + + return NextResponse.json(report); + } catch (error) { + log.error("Failed to generate user report", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} + +/** + * Check if the current user has access to view the target user's report + */ +async function checkReportAccess( + currentUser: { id: string; role: string; gymId?: string }, + targetUserId: string, + db: Awaited>, +): Promise { + // Users can always view their own reports + if (currentUser.id === targetUserId) { + return true; + } + + // Admins can view any user's report in their gym + if (currentUser.role === "admin" || currentUser.role === "superAdmin") { + const targetUser = await db.getUserById(targetUserId); + if (!targetUser) return false; + + // SuperAdmins can view all users + if (currentUser.role === "superAdmin") return true; + + // Admins can only view users in their gym + if (currentUser.gymId && targetUser.gymId === currentUser.gymId) { + return true; + } + return false; + } + + // Trainers can only view reports for their assigned clients + if (currentUser.role === "trainer") { + const assignments = await db.getTrainerClientAssignments(currentUser.id); + const assignment = assignments.find( + (a) => a.clientId === targetUserId && a.isActive, + ); + return !!assignment; + } + + // Regular users cannot view other users' reports + return false; +} + +/** + * Process attendance records into weekly check-in stats + */ +async function processWeeklyCheckIns( + db: Awaited>, + userId: string, + startDate: string, + endDate: string, +): Promise { + const weeks: WeeklyCheckInStats[] = []; + + // Generate all weeks in the range + const current = new Date(startDate); + const end = new Date(endDate); + + while (current <= end) { + // Get Monday of current week (ISO week) + const monday = new Date(current); + const day = monday.getDay(); + const diff = monday.getDate() - day + (day === 0 ? -6 : 1); + monday.setDate(diff); + + const weekStart = monday.toISOString().split("T")[0]; + const sunday = new Date(monday); + sunday.setDate(sunday.getDate() + 6); + const weekEnd = sunday.toISOString().split("T")[0]; + + weeks.push({ + weekStart, + weekEnd, + totalCheckIns: 0, + totalTimeMinutes: 0, + averageDurationMinutes: 0, + checkInsByType: ATTENDANCE_TYPES.map((type) => ({ type, count: 0 })), + }); + + current.setDate(current.getDate() + 7); + } + + // Fetch attendance for each week and aggregate + for (let i = 0; i < weeks.length; i++) { + const week = weeks[i]; + const attendanceRecords = await db.getAttendanceByWeek( + userId, + new Date(week.weekStart), + ); + + // Filter records to only include those within the week + const weekStartDate = new Date(week.weekStart); + const weekEndDate = new Date(week.weekEnd); + weekEndDate.setHours(23, 59, 59, 999); + + const filteredRecords = attendanceRecords.filter((record) => { + const checkInTime = new Date(record.checkInTime); + return checkInTime >= weekStartDate && checkInTime <= weekEndDate; + }); + + week.totalCheckIns = filteredRecords.length; + + // Calculate time spent + for (const record of filteredRecords) { + if (record.checkOutTime) { + const checkIn = new Date(record.checkInTime); + const checkOut = new Date(record.checkOutTime); + const durationMs = checkOut.getTime() - checkIn.getTime(); + const durationMinutes = Math.floor(durationMs / 60000); + week.totalTimeMinutes += durationMinutes; + } + + // Count by type + const typeIndex = week.checkInsByType.findIndex( + (t) => t.type === record.type, + ); + if (typeIndex !== -1) { + week.checkInsByType[typeIndex].count++; + } + } + + // Calculate average duration + week.averageDurationMinutes = + week.totalCheckIns > 0 + ? Math.round(week.totalTimeMinutes / week.totalCheckIns) + : 0; + } + + return weeks.sort((a, b) => a.weekStart.localeCompare(b.weekStart)); +} + +/** + * Process nutrition records into summaries + */ +function processNutrition(records: any[]): UserReport["nutrition"] { + const dailySummaries: NutritionSummary[] = records.map((record) => ({ + date: record.date, + totalCalories: record.totalCalories, + calorieGoal: record.calorieGoal, + caloriesDelta: record.totalCalories - record.calorieGoal, + mealsCount: record.meals?.length || 0, + })); + + const totalDays = dailySummaries.length; + const totalCalories = dailySummaries.reduce( + (sum, day) => sum + day.totalCalories, + 0, + ); + const averageDailyCalories = + totalDays > 0 ? Math.round(totalCalories / totalDays) : 0; + + // Count days where calories were within ±10% of goal + const daysMetGoal = dailySummaries.filter((day) => { + const lowerBound = day.calorieGoal * 0.9; + const upperBound = day.calorieGoal * 1.1; + return day.totalCalories >= lowerBound && day.totalCalories <= upperBound; + }).length; + + return { + dailySummaries, + averageDailyCalories, + totalDays, + daysMetGoal, + }; +} + +/** + * Process hydration records into summaries + */ +function processHydration(records: any[]): UserReport["hydration"] { + const dailySummaries: HydrationSummary[] = records.map((record) => ({ + date: record.date, + totalWater: record.totalWater, + waterGoal: record.waterGoal, + hydrationPercentage: + record.waterGoal > 0 + ? Math.round((record.totalWater / record.waterGoal) * 100) + : 0, + })); + + const totalDays = dailySummaries.length; + const totalWater = dailySummaries.reduce( + (sum, day) => sum + day.totalWater, + 0, + ); + const averageDailyWater = + totalDays > 0 ? Math.round(totalWater / totalDays) : 0; + + // Count days where water goal was met (>= 100%) + const daysMetGoal = dailySummaries.filter( + (day) => day.hydrationPercentage >= 100, + ).length; + + return { + dailySummaries, + averageDailyWater, + totalDays, + daysMetGoal, + }; +} + +/** + * Process fitness goals into summaries + */ +function processGoals(goals: any[]): GoalSummary { + const active = goals.filter((g) => g.status === "active"); + const completed = goals.filter((g) => g.status === "completed"); + + const averageProgress = + active.length > 0 + ? Math.round( + active.reduce((sum, g) => sum + g.progress, 0) / active.length, + ) + : 0; + + return { + active, + completed, + totalActive: active.length, + totalCompleted: completed.length, + averageProgress, + }; +} + +/** + * Process fitness profile history into summaries + */ +function processProfileHistory(history: any[]): ProfileChangeSummary[] { + return history.map((record) => ({ + changeType: record.changeType, + fieldName: record.fieldName, + previousValue: record.previousValue, + newValue: record.newValue, + changedAt: record.changedAt, + })); +} + +/** + * Process recommendations into summaries + */ +function processRecommendations(recommendations: any[]): RecommendationSummary { + const accepted = recommendations.filter((r) => r.status === "accepted"); + const rejected = recommendations.filter((r) => r.status === "rejected"); + const pending = recommendations.filter((r) => r.status === "pending"); + + return { + accepted, + rejected, + pending, + totalAccepted: accepted.length, + totalRejected: rejected.length, + totalPending: pending.length, + }; +} diff --git a/apps/admin/src/app/api/trainer-client/[id]/route.ts b/apps/admin/src/app/api/trainer-client/[id]/route.ts new file mode 100644 index 0000000..3c35ff3 --- /dev/null +++ b/apps/admin/src/app/api/trainer-client/[id]/route.ts @@ -0,0 +1,100 @@ +import { NextRequest, NextResponse } from "next/server"; +import { auth } from "@clerk/nextjs/server"; +import { getDatabase } from "@/lib/database"; +import { ensureUserSynced } from "@/lib/sync-user"; +import log from "@/lib/logger"; + +/** + * DELETE /api/trainer-client/[id] + * Delete (deactivate) a trainer-client assignment + */ +export async function DELETE( + request: NextRequest, + { params }: { params: Promise<{ id: string }> }, +) { + try { + const { userId: authUserId } = await auth(); + if (!authUserId) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + const db = await getDatabase(); + const currentUser = await ensureUserSynced(authUserId, db); + + if (!currentUser) { + return NextResponse.json({ error: "Forbidden" }, { status: 403 }); + } + + // Only admins can delete assignments + if (currentUser.role !== "admin" && currentUser.role !== "superAdmin") { + return NextResponse.json( + { error: "Only admins can remove trainer-client assignments" }, + { status: 403 }, + ); + } + + const { id } = await params; + + // Get all assignments for the trainer to find the one with this ID + // Note: We need to find the assignment ID, not the trainer or client ID + // For now, we'll deactivate by finding the assignment through the trainer + // This is a simplified approach - in production you'd want a direct getById method + + const trainerAssignments = await db.getTrainerClientAssignments( + currentUser.id, + ); + const assignment = trainerAssignments.find((a) => a.id === id); + + if (!assignment) { + // Check if any trainer has this assignment + const allAssignments = await db.getTrainerClientAssignments(""); + const foundAssignment = allAssignments.find((a) => a.id === id); + + if (!foundAssignment) { + return NextResponse.json( + { error: "Assignment not found" }, + { status: 404 }, + ); + } + + // Deactivate the assignment + await db.deactivateTrainerClientAssignment(id); + + log.info("Trainer-client assignment deactivated", { + assignmentId: id, + deactivatedBy: currentUser.id, + }); + + return NextResponse.json({ + success: true, + message: "Assignment deactivated successfully", + }); + } + + // Deactivate the assignment + const result = await db.deactivateTrainerClientAssignment(id); + + if (!result) { + return NextResponse.json( + { error: "Failed to deactivate assignment" }, + { status: 500 }, + ); + } + + log.info("Trainer-client assignment deactivated", { + assignmentId: id, + deactivatedBy: currentUser.id, + }); + + return NextResponse.json({ + success: true, + message: "Assignment deactivated successfully", + }); + } catch (error) { + log.error("Failed to delete trainer-client assignment", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} diff --git a/apps/admin/src/app/api/trainer-client/route.ts b/apps/admin/src/app/api/trainer-client/route.ts new file mode 100644 index 0000000..0a81dbe --- /dev/null +++ b/apps/admin/src/app/api/trainer-client/route.ts @@ -0,0 +1,160 @@ +import { NextRequest, NextResponse } from "next/server"; +import { auth } from "@clerk/nextjs/server"; +import { getDatabase } from "@/lib/database"; +import { ensureUserSynced } from "@/lib/sync-user"; +import log from "@/lib/logger"; + +/** + * GET /api/trainer-client + * Get trainer-client assignments + * + * Query params: + * - trainerId: string (optional) - Filter by trainer + * - clientId: string (optional) - Filter by client + */ +export async function GET(request: NextRequest) { + try { + const { userId: authUserId } = await auth(); + if (!authUserId) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + const db = await getDatabase(); + const currentUser = await ensureUserSynced(authUserId, db); + + if (!currentUser) { + return NextResponse.json({ error: "Forbidden" }, { status: 403 }); + } + + // Only admins and superadmins can view all assignments + if (currentUser.role !== "admin" && currentUser.role !== "superAdmin") { + return NextResponse.json( + { error: "Only admins can view trainer-client assignments" }, + { status: 403 }, + ); + } + + const { searchParams } = new URL(request.url); + const trainerId = searchParams.get("trainerId"); + const clientId = searchParams.get("clientId"); + + let assignments; + + if (trainerId) { + assignments = await db.getTrainerClientAssignments(trainerId); + // Filter by clientId if provided + if (clientId) { + assignments = assignments.filter((a) => a.clientId === clientId); + } + } else { + // Get all assignments (for admins, filtered by gym) + assignments = await db.getTrainerClientAssignments(""); + // TODO: Filter by gym once gymId is properly set + } + + return NextResponse.json({ assignments }); + } catch (error) { + log.error("Failed to get trainer-client assignments", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} + +/** + * POST /api/trainer-client + * Create a new trainer-client assignment + */ +export async function POST(request: NextRequest) { + try { + const { userId: authUserId } = await auth(); + if (!authUserId) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + const db = await getDatabase(); + const currentUser = await ensureUserSynced(authUserId, db); + + if (!currentUser) { + return NextResponse.json({ error: "Forbidden" }, { status: 403 }); + } + + // Only admins can create assignments + if (currentUser.role !== "admin" && currentUser.role !== "superAdmin") { + return NextResponse.json( + { error: "Only admins can assign trainers to clients" }, + { status: 403 }, + ); + } + + const body = await request.json(); + const { trainerId, clientId } = body; + + if (!trainerId || !clientId) { + return NextResponse.json( + { error: "trainerId and clientId are required" }, + { status: 400 }, + ); + } + + // Verify trainer exists and has trainer role + const trainer = await db.getUserById(trainerId); + if (!trainer) { + return NextResponse.json({ error: "Trainer not found" }, { status: 404 }); + } + if (trainer.role !== "trainer" && trainer.role !== "admin") { + return NextResponse.json( + { error: "User must be a trainer or admin" }, + { status: 400 }, + ); + } + + // Verify client exists + const client = await db.getUserById(clientId); + if (!client) { + return NextResponse.json({ error: "Client not found" }, { status: 404 }); + } + if (client.role !== "client") { + return NextResponse.json( + { error: "User must be a client" }, + { status: 400 }, + ); + } + + // Check if assignment already exists + const existingAssignments = await db.getTrainerClientAssignments(trainerId); + const existingAssignment = existingAssignments.find( + (a) => a.clientId === clientId && a.isActive, + ); + if (existingAssignment) { + return NextResponse.json( + { error: "Trainer is already assigned to this client" }, + { status: 409 }, + ); + } + + // Create the assignment + const assignment = await db.createTrainerClientAssignment({ + trainerId, + clientId, + assignedAt: new Date(), + assignedBy: currentUser.id, + isActive: true, + }); + + log.info("Trainer-client assignment created", { + trainerId, + clientId, + assignedBy: currentUser.id, + }); + + return NextResponse.json({ assignment }, { status: 201 }); + } catch (error) { + log.error("Failed to create trainer-client assignment", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} diff --git a/apps/admin/src/app/api/users/me/route.ts b/apps/admin/src/app/api/users/me/route.ts new file mode 100644 index 0000000..9a876c9 --- /dev/null +++ b/apps/admin/src/app/api/users/me/route.ts @@ -0,0 +1,32 @@ +import { NextResponse } from "next/server"; +import { auth } from "@clerk/nextjs/server"; +import { getDatabase } from "@/lib/database"; +import { ensureUserSynced } from "@/lib/sync-user"; + +/** + * GET /api/users/me + * Get current authenticated user's information + */ +export async function GET() { + try { + const { userId: clerkUserId } = await auth(); + if (!clerkUserId) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + const db = await getDatabase(); + const user = await ensureUserSynced(clerkUserId, db); + + if (!user) { + return NextResponse.json({ error: "User not found" }, { status: 404 }); + } + + return NextResponse.json({ user }); + } catch (error) { + console.error("Failed to get current user:", error); + return NextResponse.json( + { error: "Internal server error" }, + { status: 500 }, + ); + } +} diff --git a/apps/admin/src/app/reports/page.tsx b/apps/admin/src/app/reports/page.tsx new file mode 100644 index 0000000..6f54df9 --- /dev/null +++ b/apps/admin/src/app/reports/page.tsx @@ -0,0 +1,52 @@ +"use client"; + +import { useState } from "react"; +import { UserReport } from "@/components/reports/UserReport"; +import { ReportFilters } from "@/components/reports/ReportFilters"; +import { Card, CardContent } from "@/components/ui/card"; + +export default function ReportsPage() { + const [selectedUserId, setSelectedUserId] = useState(""); + const [dateRange, setDateRange] = useState<{ + startDate: string; + endDate: string; + }>({ + startDate: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) + .toISOString() + .split("T")[0], + endDate: new Date().toISOString().split("T")[0], + }); + + return ( +
+
+
+

User Reports

+
+ + + + {selectedUserId ? ( + + ) : ( + + +

+ Select a user to view their report +

+
+
+ )} +
+
+ ); +} diff --git a/apps/admin/src/app/trainer-clients/page.tsx b/apps/admin/src/app/trainer-clients/page.tsx new file mode 100644 index 0000000..1782171 --- /dev/null +++ b/apps/admin/src/app/trainer-clients/page.tsx @@ -0,0 +1,304 @@ +"use client"; + +import { useState, useEffect } from "react"; +import { User, TrainerClientAssignment } from "@fitai/shared"; +import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; +import { Button } from "@/components/ui/button"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { Badge } from "@/components/ui/badge"; +import { Plus, Trash2, UserCheck, UserX } from "lucide-react"; + +export default function TrainerClientsPage() { + const [trainers, setTrainers] = useState([]); + const [clients, setClients] = useState([]); + const [assignments, setAssignments] = useState< + (TrainerClientAssignment & { trainer?: User; client?: User })[] + >([]); + const [loading, setLoading] = useState(true); + const [selectedTrainer, setSelectedTrainer] = useState(""); + const [selectedClient, setSelectedClient] = useState(""); + + useEffect(() => { + fetchData(); + }, []); + + const fetchData = async () => { + try { + setLoading(true); + + // Fetch trainers + const trainersRes = await fetch("/api/users?role=trainer"); + if (trainersRes.ok) { + const trainersData = await trainersRes.json(); + setTrainers(trainersData.users || []); + } + + // Fetch clients + const clientsRes = await fetch("/api/users?role=client"); + if (clientsRes.ok) { + const clientsData = await clientsRes.json(); + setClients(clientsData.users || []); + } + + // Fetch all assignments + const assignmentsRes = await fetch("/api/trainer-client"); + if (assignmentsRes.ok) { + const assignmentsData = await assignmentsRes.json(); + // Enrich assignments with user data + const enrichedAssignments = await Promise.all( + (assignmentsData.assignments || []).map( + async (assignment: TrainerClientAssignment) => { + const trainer = trainers.find( + (t: User) => t.id === assignment.trainerId, + ); + const client = clients.find( + (c: User) => c.id === assignment.clientId, + ); + return { ...assignment, trainer, client }; + }, + ), + ); + setAssignments(enrichedAssignments); + } + } catch (error) { + console.error("Failed to fetch data:", error); + } finally { + setLoading(false); + } + }; + + const handleAssign = async () => { + if (!selectedTrainer || !selectedClient) { + alert("Please select both a trainer and a client"); + return; + } + + try { + const response = await fetch("/api/trainer-client", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + trainerId: selectedTrainer, + clientId: selectedClient, + }), + }); + + if (response.ok) { + alert("Assignment created successfully!"); + setSelectedTrainer(""); + setSelectedClient(""); + fetchData(); // Refresh + } else { + const error = await response.json(); + alert(error.error || "Failed to create assignment"); + } + } catch (error) { + console.error("Failed to create assignment:", error); + alert("Failed to create assignment"); + } + }; + + const handleRemoveAssignment = async (id: string) => { + if (!confirm("Are you sure you want to remove this assignment?")) { + return; + } + + try { + const response = await fetch(`/api/trainer-client/${id}`, { + method: "DELETE", + }); + + if (response.ok) { + alert("Assignment removed successfully!"); + fetchData(); // Refresh + } else { + const error = await response.json(); + alert(error.error || "Failed to remove assignment"); + } + } catch (error) { + console.error("Failed to remove assignment:", error); + alert("Failed to remove assignment"); + } + }; + + const getActiveAssignments = () => assignments.filter((a) => a.isActive); + const getInactiveAssignments = () => assignments.filter((a) => !a.isActive); + + if (loading) { + return ( +
+
+
+
Loading...
+
+
+
+ ); + } + + return ( +
+
+
+

+ Trainer-Client Assignments +

+
+ + {/* Create Assignment Form */} + + + Assign Trainer to Client + + +
+
+ + +
+ +
+ + +
+ +
+ +
+
+
+
+ + {/* Active Assignments */} + + +
+ Active Assignments + {getActiveAssignments().length} +
+
+ + {getActiveAssignments().length === 0 ? ( +

+ No active assignments +

+ ) : ( +
+ {getActiveAssignments().map((assignment) => ( +
+
+
+ + + {assignment.trainer?.firstName}{" "} + {assignment.trainer?.lastName} + +
+ + + {assignment.client?.firstName}{" "} + {assignment.client?.lastName} + +
+
+ + Assigned{" "} + {new Date(assignment.assignedAt).toLocaleDateString()} + + +
+
+ ))} +
+ )} +
+
+ + {/* Inactive Assignments */} + {getInactiveAssignments().length > 0 && ( + + +
+ + Inactive Assignments + + + {getInactiveAssignments().length} + +
+
+ +
+ {getInactiveAssignments().map((assignment) => ( +
+
+ + {assignment.trainer?.firstName}{" "} + {assignment.trainer?.lastName} + + + + {assignment.client?.firstName}{" "} + {assignment.client?.lastName} + +
+ Inactive +
+ ))} +
+
+
+ )} +
+
+ ); +} diff --git a/apps/admin/src/components/charts/chart-components.tsx b/apps/admin/src/components/charts/chart-components.tsx new file mode 100644 index 0000000..66f2aa2 --- /dev/null +++ b/apps/admin/src/components/charts/chart-components.tsx @@ -0,0 +1,132 @@ +"use client"; + +import React from "react"; +import { AgCharts } from "ag-charts-react"; +import { + AgBarSeriesOptions, + AgChartOptions, + AgLineSeriesOptions, + AgPieSeriesOptions, +} from "ag-charts-community"; + +export function BarChart({ + data, + xKey, + yKey, + color = "#3c82e6", + height = 300, +}: { + data: any[]; + xKey: string; + yKey: string; + color?: string; + height?: number; +}) { + const options: AgChartOptions = { + data, + series: [ + { + type: "bar", + xKey, + yKey, + fill: color, + } as AgBarSeriesOptions, + ], + height, + }; + + return ; +} + +export function LineChart({ + data, + xKey, + yKey, + color = "#3b82f6", + height = 300, +}: { + data: any[]; + xKey: string; + yKey: string; + color?: string; + height?: number; +}) { + const options: AgChartOptions = { + data, + series: [ + { + type: "line", + xKey, + yKey, + stroke: color, + strokeWidth: 3, + marker: { + size: 6, + fill: color, + stroke: "#ffffff", + strokeWidth: 2, + }, + } as AgLineSeriesOptions, + ], + height, + }; + + return ; +} + +export function PieChart({ + data, + labelKey, + valueKey, + height = 300, +}: { + data: any[]; + labelKey: string; + valueKey: string; + height?: number; +}) { + const options: AgChartOptions = { + data, + series: [ + { + type: "pie", + labelKey, + angleKey: valueKey, + } as AgPieSeriesOptions, + ], + height, + }; + + return ; +} + +export function AreaChart({ + data, + xKey, + yKey, + color = "#3b82f6", + height = 300, +}: { + data: any[]; + xKey: string; + yKey: string; + color?: string; + height?: number; +}) { + const options: AgChartOptions = { + data, + series: [ + { + type: "area", + xKey, + yKey, + fill: color, + stroke: color, + strokeWidth: 2, + }, + ], + height, + }; + + return ; +} diff --git a/apps/admin/src/components/reports/GoalsSummaryCard.tsx b/apps/admin/src/components/reports/GoalsSummaryCard.tsx new file mode 100644 index 0000000..83e8580 --- /dev/null +++ b/apps/admin/src/components/reports/GoalsSummaryCard.tsx @@ -0,0 +1,108 @@ +"use client"; + +import { GoalSummary } from "@fitai/shared"; +import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; +import { Badge } from "@/components/ui/badge"; + +interface GoalsSummaryCardProps { + goals: GoalSummary; +} + +export function GoalsSummaryCard({ goals }: GoalsSummaryCardProps) { + return ( + + + Fitness Goals + + + {/* Summary Stats */} +
+
+
+ {goals.totalActive} +
+
Active Goals
+
+
+
+ {goals.totalCompleted} +
+
Completed
+
+
+
+ {goals.averageProgress}% +
+
Avg Progress
+
+
+ + {/* Active Goals */} + {goals.active.length > 0 && ( +
+

Active Goals

+
+ {goals.active.map((goal) => ( +
+
+
{goal.title}
+ + {goal.priority} + +
+
+ {goal.goalType.replace("_", " ")} +
+ {/* Progress Bar */} +
+
+
+
+ {goal.progress}% complete +
+
+ ))} +
+
+ )} + + {/* Completed Goals */} + {goals.completed.length > 0 && ( +
+

Completed Goals

+
+ {goals.completed.map((goal) => ( +
+ {goal.title} + + ✓{" "} + {goal.completedDate + ? new Date(goal.completedDate).toLocaleDateString() + : "N/A"} + +
+ ))} +
+
+ )} + + {/* Empty State */} + {goals.active.length === 0 && goals.completed.length === 0 && ( +
+ No goals found for this period +
+ )} + + + ); +} diff --git a/apps/admin/src/components/reports/HydrationSummaryCard.tsx b/apps/admin/src/components/reports/HydrationSummaryCard.tsx new file mode 100644 index 0000000..6fe4cce --- /dev/null +++ b/apps/admin/src/components/reports/HydrationSummaryCard.tsx @@ -0,0 +1,119 @@ +"use client"; + +import { HydrationSummary } from "@fitai/shared"; +import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; +import { + PieChart, + Pie, + Cell, + ResponsiveContainer, + Legend, + Tooltip, +} from "recharts"; + +interface HydrationSummaryCardProps { + hydration: { + dailySummaries: HydrationSummary[]; + averageDailyWater: number; + totalDays: number; + daysMetGoal: number; + }; +} + +export function HydrationSummaryCard({ hydration }: HydrationSummaryCardProps) { + const goalMetPercentage = + hydration.totalDays > 0 + ? Math.round((hydration.daysMetGoal / hydration.totalDays) * 100) + : 0; + + const chartData = [ + { name: "Days Met Goal", value: hydration.daysMetGoal, color: "#06b6d4" }, + { + name: "Days Under Goal", + value: hydration.totalDays - hydration.daysMetGoal, + color: "#f59e0b", + }, + ]; + + return ( + + + Hydration Summary + + + {/* Summary Stats */} +
+
+
+ {(hydration.averageDailyWater / 1000).toFixed(1)}L +
+
Avg Daily Water
+
+
+
+ {hydration.totalDays} +
+
Days Tracked
+
+
+ + {/* Goal Achievement */} +
+
+ {goalMetPercentage}% +
+
Days Met Hydration Goal
+
+ + {/* Chart */} + {hydration.totalDays > 0 && ( +
+ + + + {chartData.map((entry, index) => ( + + ))} + + + + + +
+ )} + + {/* Recent Days */} + {hydration.dailySummaries.length > 0 && ( +
+

Recent Days

+
+ {hydration.dailySummaries + .slice(-5) + .reverse() + .map((day) => ( +
+ {day.date} + + {day.totalWater}ml / {day.waterGoal}ml ( + {day.hydrationPercentage}%) + +
+ ))} +
+
+ )} +
+
+ ); +} diff --git a/apps/admin/src/components/reports/NutritionSummaryCard.tsx b/apps/admin/src/components/reports/NutritionSummaryCard.tsx new file mode 100644 index 0000000..86fb0d1 --- /dev/null +++ b/apps/admin/src/components/reports/NutritionSummaryCard.tsx @@ -0,0 +1,121 @@ +"use client"; + +import { NutritionSummary } from "@fitai/shared"; +import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; +import { + PieChart, + Pie, + Cell, + ResponsiveContainer, + Legend, + Tooltip, +} from "recharts"; + +interface NutritionSummaryCardProps { + nutrition: { + dailySummaries: NutritionSummary[]; + averageDailyCalories: number; + totalDays: number; + daysMetGoal: number; + }; +} + +export function NutritionSummaryCard({ nutrition }: NutritionSummaryCardProps) { + const goalMetPercentage = + nutrition.totalDays > 0 + ? Math.round((nutrition.daysMetGoal / nutrition.totalDays) * 100) + : 0; + + const chartData = [ + { name: "Days Met Goal", value: nutrition.daysMetGoal, color: "#10b981" }, + { + name: "Days Over/Under", + value: nutrition.totalDays - nutrition.daysMetGoal, + color: "#f59e0b", + }, + ]; + + return ( + + + Nutrition Summary + + + {/* Summary Stats */} +
+
+
+ {nutrition.averageDailyCalories.toLocaleString()} +
+
Avg Daily Calories
+
+
+
+ {nutrition.totalDays} +
+
Days Tracked
+
+
+ + {/* Goal Achievement */} +
+
+ {goalMetPercentage}% +
+
+ Days Met Calorie Goal (±10%) +
+
+ + {/* Chart */} + {nutrition.totalDays > 0 && ( +
+ + + + {chartData.map((entry, index) => ( + + ))} + + + + + +
+ )} + + {/* Recent Days */} + {nutrition.dailySummaries.length > 0 && ( +
+

Recent Days

+
+ {nutrition.dailySummaries + .slice(-5) + .reverse() + .map((day) => ( +
+ {day.date} + + {day.totalCalories.toLocaleString()} /{" "} + {day.calorieGoal.toLocaleString()} cal + +
+ ))} +
+
+ )} +
+
+ ); +} diff --git a/apps/admin/src/components/reports/RecommendationsCard.tsx b/apps/admin/src/components/reports/RecommendationsCard.tsx new file mode 100644 index 0000000..77202f4 --- /dev/null +++ b/apps/admin/src/components/reports/RecommendationsCard.tsx @@ -0,0 +1,130 @@ +"use client"; + +import { RecommendationSummary } from "@fitai/shared"; +import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; +import { Badge } from "@/components/ui/badge"; + +interface RecommendationsCardProps { + recommendations: RecommendationSummary; +} + +export function RecommendationsCard({ + recommendations, +}: RecommendationsCardProps) { + return ( + + + AI Recommendations + + + {/* Summary Stats */} +
+
+
+ {recommendations.totalAccepted} +
+
Accepted
+
+
+
+ {recommendations.totalPending} +
+
Pending
+
+
+
+ {recommendations.totalRejected} +
+
Rejected
+
+
+ + {/* Accepted Recommendations */} + {recommendations.accepted.length > 0 && ( +
+

+ Accepted Recommendations +

+
+ {recommendations.accepted.map((rec) => ( +
+
+ + Accepted + + + {new Date(rec.generatedAt).toLocaleDateString()} + +
+
{rec.recommendationText}
+
+ ))} +
+
+ )} + + {/* Pending Recommendations */} + {recommendations.pending.length > 0 && ( +
+

+ Pending Recommendations +

+
+ {recommendations.pending.map((rec) => ( +
+
+ Pending + + {new Date(rec.generatedAt).toLocaleDateString()} + +
+
{rec.recommendationText}
+
+ ))} +
+
+ )} + + {/* Rejected Recommendations */} + {recommendations.rejected.length > 0 && ( +
+

+ Rejected Recommendations +

+
+ {recommendations.rejected.map((rec) => ( +
+
+ Rejected + + {new Date(rec.generatedAt).toLocaleDateString()} + +
+
{rec.recommendationText}
+
+ ))} +
+
+ )} + + {/* Empty State */} + {recommendations.totalAccepted === 0 && + recommendations.totalPending === 0 && + recommendations.totalRejected === 0 && ( +
+ No recommendations found for this period +
+ )} +
+
+ ); +} diff --git a/apps/admin/src/components/reports/ReportFilters.tsx b/apps/admin/src/components/reports/ReportFilters.tsx new file mode 100644 index 0000000..96f1c37 --- /dev/null +++ b/apps/admin/src/components/reports/ReportFilters.tsx @@ -0,0 +1,195 @@ +"use client"; + +import { useState, useEffect } from "react"; +import { User, Client } from "@fitai/shared"; +import { Button } from "@/components/ui/button"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { Label } from "@/components/ui/label"; +import { Card, CardContent } from "@/components/ui/card"; +import { useUser } from "@clerk/nextjs"; +import log from "@/lib/logger"; + +interface ReportFiltersProps { + selectedUserId: string; + onUserChange: (userId: string) => void; + dateRange: { + startDate: string; + endDate: string; + }; + onDateRangeChange: (range: { startDate: string; endDate: string }) => void; +} + +export function ReportFilters({ + selectedUserId, + onUserChange, + dateRange, + onDateRangeChange, +}: ReportFiltersProps) { + const { user: clerkUser } = useUser(); + const [users, setUsers] = useState<(User & { client?: Client | null })[]>([]); + const [loading, setLoading] = useState(true); + const [currentUser, setCurrentUser] = useState(null); + + useEffect(() => { + fetchCurrentUserAndClients(); + }, []); + + const fetchCurrentUserAndClients = async () => { + try { + setLoading(true); + + // Fetch current user info + const userResponse = await fetch("/api/users/me"); + if (userResponse.ok) { + const userData = await userResponse.json(); + setCurrentUser(userData.user); + + // Determine which clients to show based on role + if (userData.user.role === "client") { + // Regular users can only view their own report + setUsers([userData.user]); + onUserChange(userData.user.id); + } else if (userData.user.role === "trainer") { + // Trainers can only view their assigned clients + const assignmentsRes = await fetch("/api/trainer-client"); + if (assignmentsRes.ok) { + const assignmentsData = await assignmentsRes.json(); + const assignedClientIds = (assignmentsData.assignments || []) + .filter((a: any) => a.isActive) + .map((a: any) => a.clientId); + + if (assignedClientIds.length > 0) { + // Fetch assigned clients + const clientsRes = await fetch( + `/api/users?role=client&ids=${assignedClientIds.join(",")}`, + ); + if (clientsRes.ok) { + const clientsData = await clientsRes.json(); + setUsers(clientsData.users || []); + } + } else { + setUsers([]); + } + } + } else { + // Admins and superadmins can view all clients + const clientsRes = await fetch("/api/users?role=client"); + if (clientsRes.ok) { + const clientsData = await clientsRes.json(); + setUsers(clientsData.users || []); + } + } + } + } catch (error) { + log.error("Failed to fetch users:", error); + } finally { + setLoading(false); + } + }; + + const handlePresetRange = (days: number) => { + const end = new Date(); + const start = new Date(Date.now() - days * 24 * 60 * 60 * 1000); + onDateRangeChange({ + startDate: start.toISOString().split("T")[0], + endDate: end.toISOString().split("T")[0], + }); + }; + + return ( + + +
+ {/* User Selector */} +
+ + +
+ + {/* Start Date */} +
+ + + onDateRangeChange({ + ...dateRange, + startDate: e.target.value, + }) + } + /> +
+ + {/* End Date */} +
+ + + onDateRangeChange({ + ...dateRange, + endDate: e.target.value, + }) + } + /> +
+ + {/* Preset Ranges */} +
+ +
+ + + +
+
+
+
+
+ ); +} diff --git a/apps/admin/src/components/reports/UserReport.tsx b/apps/admin/src/components/reports/UserReport.tsx new file mode 100644 index 0000000..dfde033 --- /dev/null +++ b/apps/admin/src/components/reports/UserReport.tsx @@ -0,0 +1,136 @@ +"use client"; + +import { useState, useEffect } from "react"; +import { UserReport as UserReportType } from "@fitai/shared"; +import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; +import { WeeklyCheckInsCard } from "./WeeklyCheckInsCard"; +import { NutritionSummaryCard } from "./NutritionSummaryCard"; +import { HydrationSummaryCard } from "./HydrationSummaryCard"; +import { GoalsSummaryCard } from "./GoalsSummaryCard"; +import { RecommendationsCard } from "./RecommendationsCard"; +import { Button } from "@/components/ui/button"; +import { Download } from "lucide-react"; + +interface UserReportProps { + userId: string; + startDate: string; + endDate: string; +} + +export function UserReport({ userId, startDate, endDate }: UserReportProps) { + const [report, setReport] = useState(null); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + + useEffect(() => { + fetchReport(); + }, [userId, startDate, endDate]); + + const fetchReport = async () => { + try { + setLoading(true); + setError(null); + + const response = await fetch( + `/api/reports/user/${userId}?startDate=${startDate}&endDate=${endDate}`, + ); + + if (!response.ok) { + const errorData = await response.json(); + throw new Error(errorData.error || "Failed to fetch report"); + } + + const data = await response.json(); + setReport(data); + } catch (err) { + setError(err instanceof Error ? err.message : "Failed to load report"); + console.error("Report fetch error:", err); + } finally { + setLoading(false); + } + }; + + const handleExportPDF = () => { + if (!report) return; + + const filename = `FitAI_Report_${report.user.firstName}_${report.user.lastName}_${startDate}_${endDate}.pdf`; + const url = `/api/reports/user/${userId}?startDate=${startDate}&endDate=${endDate}&format=pdf`; + + // Open in new tab to trigger download + window.open(url, "_blank"); + }; + + if (loading) { + return ( + + +
Loading report...
+
+
+ ); + } + + if (error) { + return ( + + +
{error}
+
+
+ ); + } + + if (!report) { + return ( + + +
No report data available
+
+
+ ); + } + + return ( +
+ {/* Report Header */} + + +
+
+ + {report.user.firstName} {report.user.lastName} + +

+ {report.user.email} • {report.client?.membershipType || "N/A"}{" "} + Member +

+

+ Report Period: {report.reportPeriod.startDate} to{" "} + {report.reportPeriod.endDate} +

+
+ +
+
+
+ + {/* Report Sections */} +
+ + +
+ +
+ + +
+ +
+ +
+
+ ); +} diff --git a/apps/admin/src/components/reports/WeeklyCheckInsCard.tsx b/apps/admin/src/components/reports/WeeklyCheckInsCard.tsx new file mode 100644 index 0000000..91c5ba6 --- /dev/null +++ b/apps/admin/src/components/reports/WeeklyCheckInsCard.tsx @@ -0,0 +1,110 @@ +"use client"; + +import { WeeklyCheckInStats } from "@fitai/shared"; +import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; +import { + BarChart, + Bar, + XAxis, + YAxis, + CartesianGrid, + Tooltip, + Legend, + ResponsiveContainer, +} from "recharts"; + +interface WeeklyCheckInsCardProps { + weeklyCheckIns: WeeklyCheckInStats[]; +} + +export function WeeklyCheckInsCard({ + weeklyCheckIns, +}: WeeklyCheckInsCardProps) { + const totalCheckIns = weeklyCheckIns.reduce( + (sum, week) => sum + week.totalCheckIns, + 0, + ); + const totalTimeMinutes = weeklyCheckIns.reduce( + (sum, week) => sum + week.totalTimeMinutes, + 0, + ); + const avgTimeMinutes = + totalCheckIns > 0 ? Math.round(totalTimeMinutes / totalCheckIns) : 0; + + const chartData = weeklyCheckIns.map((week) => ({ + week: week.weekStart, + checkIns: week.totalCheckIns, + timeMinutes: week.totalTimeMinutes, + })); + + return ( + + + Weekly Check-ins + + +
+
+
+ {totalCheckIns} +
+
Total Check-ins
+
+
+
+ {Math.floor(totalTimeMinutes / 60)}h {totalTimeMinutes % 60}m +
+
Total Time
+
+
+
+ {avgTimeMinutes}m +
+
Avg Duration
+
+
+ + {chartData.length > 0 ? ( +
+ + + + + + + + + + + +
+ ) : ( +
+ No check-in data available +
+ )} + + {weeklyCheckIns.length > 0 && ( +
+

Weekly Breakdown

+
+ {weeklyCheckIns.slice(-4).map((week) => ( +
+ + {week.weekStart} - {week.weekEnd} + + + {week.totalCheckIns} check-ins • {week.totalTimeMinutes} min + +
+ ))} +
+
+ )} +
+
+ ); +} diff --git a/apps/admin/src/components/ui/label.tsx b/apps/admin/src/components/ui/label.tsx new file mode 100644 index 0000000..46653cf --- /dev/null +++ b/apps/admin/src/components/ui/label.tsx @@ -0,0 +1,22 @@ +import * as React from "react"; + +import { cn } from "@/lib/utils"; + +export interface LabelProps + extends React.LabelHTMLAttributes {} + +const Label = React.forwardRef( + ({ className, ...props }, ref) => ( +