user report gtound work

This commit is contained in:
dimitar committed 2026-03-19 03:37:15 +01:00
1 parent 1be2de05fa
commit 74f0d0dbed
42 files changed
+8127 -5

No files matched your search

+531 -5
View File
@@ -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",
+3
View File
@@ -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",
@@ -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 },
);
}
}
+138
View File
@@ -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 },
);
}
}
@@ -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 },
);
}
}
+138
View File
@@ -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 },
);
}
}
@@ -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");
});
});
@@ -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<ReturnType<typeof getDatabase>>,
): Promise<boolean> {
// 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<ReturnType<typeof getDatabase>>,
userId: string,
startDate: string,
endDate: string,
): Promise<WeeklyCheckInStats[]> {
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,
};
}
@@ -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 },
);
}
}
@@ -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 },
);
}
}
+32
View File
@@ -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 },
);
}
}
+52
View File
@@ -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<string>("");
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 (
<div className="min-h-screen bg-gray-50">
<div className="container mx-auto px-4 py-8 space-y-6">
<div className="flex justify-between items-center">
<h1 className="text-3xl font-bold text-gray-900">User Reports</h1>
</div>
<ReportFilters
selectedUserId={selectedUserId}
onUserChange={setSelectedUserId}
dateRange={dateRange}
onDateRangeChange={setDateRange}
/>
{selectedUserId ? (
<UserReport
userId={selectedUserId}
startDate={dateRange.startDate}
endDate={dateRange.endDate}
/>
) : (
<Card>
<CardContent className="flex items-center justify-center h-64">
<p className="text-gray-500 text-lg">
Select a user to view their report
</p>
</CardContent>
</Card>
)}
</div>
</div>
);
}
+304
View File
@@ -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<User[]>([]);
const [clients, setClients] = useState<User[]>([]);
const [assignments, setAssignments] = useState<
(TrainerClientAssignment & { trainer?: User; client?: User })[]
>([]);
const [loading, setLoading] = useState(true);
const [selectedTrainer, setSelectedTrainer] = useState<string>("");
const [selectedClient, setSelectedClient] = useState<string>("");
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 (
<div className="min-h-screen bg-gray-50">
<div className="container mx-auto px-4 py-8">
<div className="flex items-center justify-center h-64">
<div className="text-lg">Loading...</div>
</div>
</div>
</div>
);
}
return (
<div className="min-h-screen bg-gray-50">
<div className="container mx-auto px-4 py-8 space-y-6">
<div className="flex justify-between items-center">
<h1 className="text-3xl font-bold text-gray-900">
Trainer-Client Assignments
</h1>
</div>
{/* Create Assignment Form */}
<Card>
<CardHeader>
<CardTitle>Assign Trainer to Client</CardTitle>
</CardHeader>
<CardContent>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<div className="space-y-2">
<label className="text-sm font-medium">Select Trainer</label>
<Select
value={selectedTrainer}
onValueChange={setSelectedTrainer}
>
<SelectTrigger>
<SelectValue placeholder="Choose a trainer..." />
</SelectTrigger>
<SelectContent>
{trainers.map((trainer) => (
<SelectItem key={trainer.id} value={trainer.id}>
{trainer.firstName} {trainer.lastName}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<label className="text-sm font-medium">Select Client</label>
<Select
value={selectedClient}
onValueChange={setSelectedClient}
>
<SelectTrigger>
<SelectValue placeholder="Choose a client..." />
</SelectTrigger>
<SelectContent>
{clients.map((client) => (
<SelectItem key={client.id} value={client.id}>
{client.firstName} {client.lastName}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="flex items-end">
<Button onClick={handleAssign} className="w-full">
<Plus className="w-4 h-4 mr-2" />
Assign
</Button>
</div>
</div>
</CardContent>
</Card>
{/* Active Assignments */}
<Card>
<CardHeader>
<div className="flex justify-between items-center">
<CardTitle>Active Assignments</CardTitle>
<Badge variant="default">{getActiveAssignments().length}</Badge>
</div>
</CardHeader>
<CardContent>
{getActiveAssignments().length === 0 ? (
<p className="text-gray-500 text-center py-8">
No active assignments
</p>
) : (
<div className="space-y-2">
{getActiveAssignments().map((assignment) => (
<div
key={assignment.id}
className="flex items-center justify-between p-4 border rounded-lg hover:bg-gray-50"
>
<div className="flex items-center space-x-4">
<div className="flex items-center space-x-2">
<UserCheck className="w-4 h-4 text-green-600" />
<span className="font-medium">
{assignment.trainer?.firstName}{" "}
{assignment.trainer?.lastName}
</span>
</div>
<span className="text-gray-400">→</span>
<span>
{assignment.client?.firstName}{" "}
{assignment.client?.lastName}
</span>
</div>
<div className="flex items-center space-x-2">
<span className="text-xs text-gray-500">
Assigned{" "}
{new Date(assignment.assignedAt).toLocaleDateString()}
</span>
<Button
variant="ghost"
size="sm"
onClick={() => handleRemoveAssignment(assignment.id)}
>
<UserX className="w-4 h-4 text-red-600" />
</Button>
</div>
</div>
))}
</div>
)}
</CardContent>
</Card>
{/* Inactive Assignments */}
{getInactiveAssignments().length > 0 && (
<Card>
<CardHeader>
<div className="flex justify-between items-center">
<CardTitle className="text-gray-500">
Inactive Assignments
</CardTitle>
<Badge variant="secondary">
{getInactiveAssignments().length}
</Badge>
</div>
</CardHeader>
<CardContent>
<div className="space-y-2">
{getInactiveAssignments().map((assignment) => (
<div
key={assignment.id}
className="flex items-center justify-between p-4 border rounded-lg bg-gray-50 opacity-60"
>
<div className="flex items-center space-x-4">
<span className="font-medium text-gray-600">
{assignment.trainer?.firstName}{" "}
{assignment.trainer?.lastName}
</span>
<span className="text-gray-400">→</span>
<span className="text-gray-600">
{assignment.client?.firstName}{" "}
{assignment.client?.lastName}
</span>
</div>
<Badge variant="secondary">Inactive</Badge>
</div>
))}
</div>
</CardContent>
</Card>
)}
</div>
</div>
);
}
@@ -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 <AgCharts options={options} />;
}
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 <AgCharts options={options} />;
}
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 <AgCharts options={options} />;
}
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 <AgCharts options={options} />;
}
@@ -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 (
<Card>
<CardHeader>
<CardTitle>Fitness Goals</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
{/* Summary Stats */}
<div className="grid grid-cols-3 gap-4">
<div className="text-center">
<div className="text-2xl font-bold text-blue-600">
{goals.totalActive}
</div>
<div className="text-xs text-gray-600">Active Goals</div>
</div>
<div className="text-center">
<div className="text-2xl font-bold text-green-600">
{goals.totalCompleted}
</div>
<div className="text-xs text-gray-600">Completed</div>
</div>
<div className="text-center">
<div className="text-2xl font-bold text-purple-600">
{goals.averageProgress}%
</div>
<div className="text-xs text-gray-600">Avg Progress</div>
</div>
</div>
{/* Active Goals */}
{goals.active.length > 0 && (
<div className="space-y-2">
<h4 className="text-sm font-semibold">Active Goals</h4>
<div className="space-y-2">
{goals.active.map((goal) => (
<div key={goal.id} className="border rounded-lg p-3">
<div className="flex justify-between items-start mb-2">
<div className="font-medium">{goal.title}</div>
<Badge
variant={
goal.priority === "high" ? "destructive" : "secondary"
}
>
{goal.priority}
</Badge>
</div>
<div className="text-xs text-gray-600 mb-2">
{goal.goalType.replace("_", " ")}
</div>
{/* Progress Bar */}
<div className="w-full bg-gray-200 rounded-full h-2">
<div
className="bg-blue-600 h-2 rounded-full transition-all"
style={{ width: `${goal.progress}%` }}
/>
</div>
<div className="text-xs text-gray-600 mt-1">
{goal.progress}% complete
</div>
</div>
))}
</div>
</div>
)}
{/* Completed Goals */}
{goals.completed.length > 0 && (
<div className="space-y-2">
<h4 className="text-sm font-semibold">Completed Goals</h4>
<div className="space-y-1">
{goals.completed.map((goal) => (
<div
key={goal.id}
className="flex justify-between items-center text-sm p-2 bg-green-50 rounded"
>
<span className="font-medium">{goal.title}</span>
<span className="text-xs text-green-700">
✓{" "}
{goal.completedDate
? new Date(goal.completedDate).toLocaleDateString()
: "N/A"}
</span>
</div>
))}
</div>
</div>
)}
{/* Empty State */}
{goals.active.length === 0 && goals.completed.length === 0 && (
<div className="text-center text-gray-500 py-8">
No goals found for this period
</div>
)}
</CardContent>
</Card>
);
}
@@ -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 (
<Card>
<CardHeader>
<CardTitle>Hydration Summary</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
{/* Summary Stats */}
<div className="grid grid-cols-2 gap-4">
<div className="text-center">
<div className="text-2xl font-bold text-blue-600">
{(hydration.averageDailyWater / 1000).toFixed(1)}L
</div>
<div className="text-xs text-gray-600">Avg Daily Water</div>
</div>
<div className="text-center">
<div className="text-2xl font-bold text-cyan-600">
{hydration.totalDays}
</div>
<div className="text-xs text-gray-600">Days Tracked</div>
</div>
</div>
{/* Goal Achievement */}
<div className="text-center">
<div className="text-3xl font-bold text-green-600">
{goalMetPercentage}%
</div>
<div className="text-xs text-gray-600">Days Met Hydration Goal</div>
</div>
{/* Chart */}
{hydration.totalDays > 0 && (
<div className="h-48">
<ResponsiveContainer width="100%" height="100%">
<PieChart>
<Pie
data={chartData}
cx="50%"
cy="50%"
innerRadius={40}
outerRadius={70}
paddingAngle={5}
dataKey="value"
>
{chartData.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.color} />
))}
</Pie>
<Tooltip />
<Legend />
</PieChart>
</ResponsiveContainer>
</div>
)}
{/* Recent Days */}
{hydration.dailySummaries.length > 0 && (
<div className="space-y-2">
<h4 className="text-sm font-semibold">Recent Days</h4>
<div className="max-h-32 overflow-y-auto space-y-1">
{hydration.dailySummaries
.slice(-5)
.reverse()
.map((day) => (
<div
key={day.date}
className="flex justify-between items-center text-sm"
>
<span className="text-gray-600">{day.date}</span>
<span className="font-medium">
{day.totalWater}ml / {day.waterGoal}ml (
{day.hydrationPercentage}%)
</span>
</div>
))}
</div>
</div>
)}
</CardContent>
</Card>
);
}
@@ -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 (
<Card>
<CardHeader>
<CardTitle>Nutrition Summary</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
{/* Summary Stats */}
<div className="grid grid-cols-2 gap-4">
<div className="text-center">
<div className="text-2xl font-bold text-blue-600">
{nutrition.averageDailyCalories.toLocaleString()}
</div>
<div className="text-xs text-gray-600">Avg Daily Calories</div>
</div>
<div className="text-center">
<div className="text-2xl font-bold text-green-600">
{nutrition.totalDays}
</div>
<div className="text-xs text-gray-600">Days Tracked</div>
</div>
</div>
{/* Goal Achievement */}
<div className="text-center">
<div className="text-3xl font-bold text-purple-600">
{goalMetPercentage}%
</div>
<div className="text-xs text-gray-600">
Days Met Calorie Goal (±10%)
</div>
</div>
{/* Chart */}
{nutrition.totalDays > 0 && (
<div className="h-48">
<ResponsiveContainer width="100%" height="100%">
<PieChart>
<Pie
data={chartData}
cx="50%"
cy="50%"
innerRadius={40}
outerRadius={70}
paddingAngle={5}
dataKey="value"
>
{chartData.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.color} />
))}
</Pie>
<Tooltip />
<Legend />
</PieChart>
</ResponsiveContainer>
</div>
)}
{/* Recent Days */}
{nutrition.dailySummaries.length > 0 && (
<div className="space-y-2">
<h4 className="text-sm font-semibold">Recent Days</h4>
<div className="max-h-32 overflow-y-auto space-y-1">
{nutrition.dailySummaries
.slice(-5)
.reverse()
.map((day) => (
<div
key={day.date}
className="flex justify-between items-center text-sm"
>
<span className="text-gray-600">{day.date}</span>
<span className="font-medium">
{day.totalCalories.toLocaleString()} /{" "}
{day.calorieGoal.toLocaleString()} cal
</span>
</div>
))}
</div>
</div>
)}
</CardContent>
</Card>
);
}
@@ -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 (
<Card>
<CardHeader>
<CardTitle>AI Recommendations</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
{/* Summary Stats */}
<div className="grid grid-cols-3 gap-4">
<div className="text-center">
<div className="text-2xl font-bold text-green-600">
{recommendations.totalAccepted}
</div>
<div className="text-xs text-gray-600">Accepted</div>
</div>
<div className="text-center">
<div className="text-2xl font-bold text-yellow-600">
{recommendations.totalPending}
</div>
<div className="text-xs text-gray-600">Pending</div>
</div>
<div className="text-center">
<div className="text-2xl font-bold text-red-600">
{recommendations.totalRejected}
</div>
<div className="text-xs text-gray-600">Rejected</div>
</div>
</div>
{/* Accepted Recommendations */}
{recommendations.accepted.length > 0 && (
<div className="space-y-2">
<h4 className="text-sm font-semibold text-green-700">
Accepted Recommendations
</h4>
<div className="space-y-2">
{recommendations.accepted.map((rec) => (
<div
key={rec.id}
className="border border-green-200 bg-green-50 rounded-lg p-3"
>
<div className="flex justify-between items-start mb-2">
<Badge variant="default" className="bg-green-600">
Accepted
</Badge>
<span className="text-xs text-gray-500">
{new Date(rec.generatedAt).toLocaleDateString()}
</span>
</div>
<div className="text-sm">{rec.recommendationText}</div>
</div>
))}
</div>
</div>
)}
{/* Pending Recommendations */}
{recommendations.pending.length > 0 && (
<div className="space-y-2">
<h4 className="text-sm font-semibold text-yellow-700">
Pending Recommendations
</h4>
<div className="space-y-2">
{recommendations.pending.map((rec) => (
<div
key={rec.id}
className="border border-yellow-200 bg-yellow-50 rounded-lg p-3"
>
<div className="flex justify-between items-start mb-2">
<Badge variant="secondary">Pending</Badge>
<span className="text-xs text-gray-500">
{new Date(rec.generatedAt).toLocaleDateString()}
</span>
</div>
<div className="text-sm">{rec.recommendationText}</div>
</div>
))}
</div>
</div>
)}
{/* Rejected Recommendations */}
{recommendations.rejected.length > 0 && (
<div className="space-y-2">
<h4 className="text-sm font-semibold text-red-700">
Rejected Recommendations
</h4>
<div className="space-y-2">
{recommendations.rejected.map((rec) => (
<div
key={rec.id}
className="border border-red-200 bg-red-50 rounded-lg p-3"
>
<div className="flex justify-between items-start mb-2">
<Badge variant="destructive">Rejected</Badge>
<span className="text-xs text-gray-500">
{new Date(rec.generatedAt).toLocaleDateString()}
</span>
</div>
<div className="text-sm">{rec.recommendationText}</div>
</div>
))}
</div>
</div>
)}
{/* Empty State */}
{recommendations.totalAccepted === 0 &&
recommendations.totalPending === 0 &&
recommendations.totalRejected === 0 && (
<div className="text-center text-gray-500 py-8">
No recommendations found for this period
</div>
)}
</CardContent>
</Card>
);
}
@@ -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<User | null>(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 (
<Card>
<CardContent className="p-6">
<div className="grid grid-cols-1 md:grid-cols-4 gap-4">
{/* User Selector */}
<div className="space-y-2">
<Label htmlFor="user-select">Select User</Label>
<Select
value={selectedUserId}
onValueChange={onUserChange}
disabled={loading}
>
<SelectTrigger id="user-select">
<SelectValue placeholder="Choose a user..." />
</SelectTrigger>
<SelectContent>
{users.map((user) => (
<SelectItem key={user.id} value={user.id}>
{user.firstName} {user.lastName}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* Start Date */}
<div className="space-y-2">
<Label htmlFor="start-date">Start Date</Label>
<input
id="start-date"
type="date"
className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
value={dateRange.startDate}
onChange={(e) =>
onDateRangeChange({
...dateRange,
startDate: e.target.value,
})
}
/>
</div>
{/* End Date */}
<div className="space-y-2">
<Label htmlFor="end-date">End Date</Label>
<input
id="end-date"
type="date"
className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
value={dateRange.endDate}
onChange={(e) =>
onDateRangeChange({
...dateRange,
endDate: e.target.value,
})
}
/>
</div>
{/* Preset Ranges */}
<div className="space-y-2">
<Label>Quick Select</Label>
<div className="flex gap-2">
<Button
variant="outline"
size="sm"
onClick={() => handlePresetRange(7)}
>
7 Days
</Button>
<Button
variant="outline"
size="sm"
onClick={() => handlePresetRange(30)}
>
30 Days
</Button>
<Button
variant="outline"
size="sm"
onClick={() => handlePresetRange(90)}
>
90 Days
</Button>
</div>
</div>
</div>
</CardContent>
</Card>
);
}
@@ -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<UserReportType | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(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 (
<Card>
<CardContent className="flex items-center justify-center h-64">
<div className="text-lg text-gray-600">Loading report...</div>
</CardContent>
</Card>
);
}
if (error) {
return (
<Card>
<CardContent className="flex items-center justify-center h-64">
<div className="text-lg text-red-600">{error}</div>
</CardContent>
</Card>
);
}
if (!report) {
return (
<Card>
<CardContent className="flex items-center justify-center h-64">
<div className="text-lg text-gray-600">No report data available</div>
</CardContent>
</Card>
);
}
return (
<div className="space-y-6">
{/* Report Header */}
<Card>
<CardHeader>
<div className="flex justify-between items-center">
<div>
<CardTitle className="text-2xl">
{report.user.firstName} {report.user.lastName}
</CardTitle>
<p className="text-sm text-gray-600 mt-1">
{report.user.email} • {report.client?.membershipType || "N/A"}{" "}
Member
</p>
<p className="text-xs text-gray-500 mt-1">
Report Period: {report.reportPeriod.startDate} to{" "}
{report.reportPeriod.endDate}
</p>
</div>
<Button onClick={handleExportPDF} variant="default">
<Download className="w-4 h-4 mr-2" />
Export PDF
</Button>
</div>
</CardHeader>
</Card>
{/* Report Sections */}
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
<WeeklyCheckInsCard weeklyCheckIns={report.weeklyCheckIns} />
<NutritionSummaryCard nutrition={report.nutrition} />
</div>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
<HydrationSummaryCard hydration={report.hydration} />
<GoalsSummaryCard goals={report.goals} />
</div>
<div className="grid grid-cols-1 gap-6">
<RecommendationsCard recommendations={report.recommendations} />
</div>
</div>
);
}
@@ -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 (
<Card>
<CardHeader>
<CardTitle>Weekly Check-ins</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
<div className="grid grid-cols-3 gap-4">
<div className="text-center">
<div className="text-2xl font-bold text-blue-600">
{totalCheckIns}
</div>
<div className="text-xs text-gray-600">Total Check-ins</div>
</div>
<div className="text-center">
<div className="text-2xl font-bold text-green-600">
{Math.floor(totalTimeMinutes / 60)}h {totalTimeMinutes % 60}m
</div>
<div className="text-xs text-gray-600">Total Time</div>
</div>
<div className="text-center">
<div className="text-2xl font-bold text-purple-600">
{avgTimeMinutes}m
</div>
<div className="text-xs text-gray-600">Avg Duration</div>
</div>
</div>
{chartData.length > 0 ? (
<div className="h-64">
<ResponsiveContainer width="100%" height="100%">
<BarChart data={chartData}>
<CartesianGrid strokeDasharray="3 3" />
<XAxis dataKey="week" />
<YAxis />
<Tooltip />
<Legend />
<Bar dataKey="checkIns" fill="#3c82e6" name="Check-ins" />
<Bar dataKey="timeMinutes" fill="#10b981" name="Minutes" />
</BarChart>
</ResponsiveContainer>
</div>
) : (
<div className="h-64 flex items-center justify-center text-gray-500">
No check-in data available
</div>
)}
{weeklyCheckIns.length > 0 && (
<div className="space-y-2">
<h4 className="text-sm font-semibold">Weekly Breakdown</h4>
<div className="space-y-1">
{weeklyCheckIns.slice(-4).map((week) => (
<div
key={week.weekStart}
className="flex justify-between items-center text-sm"
>
<span className="text-gray-600">
{week.weekStart} - {week.weekEnd}
</span>
<span className="font-medium">
{week.totalCheckIns} check-ins • {week.totalTimeMinutes} min
</span>
</div>
))}
</div>
</div>
)}
</CardContent>
</Card>
);
}
+22
View File
@@ -0,0 +1,22 @@
import * as React from "react";
import { cn } from "@/lib/utils";
export interface LabelProps
extends React.LabelHTMLAttributes<HTMLLabelElement> {}
const Label = React.forwardRef<HTMLLabelElement, LabelProps>(
({ className, ...props }, ref) => (
<label
ref={ref}
className={cn(
"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70",
className,
)}
{...props}
/>
),
);
Label.displayName = "Label";
export { Label };
+160
View File
@@ -0,0 +1,160 @@
"use client";
import * as React from "react";
import * as SelectPrimitive from "@radix-ui/react-select";
import { Check, ChevronDown, ChevronUp } from "lucide-react";
import { cn } from "@/lib/utils";
const Select = SelectPrimitive.Root;
const SelectGroup = SelectPrimitive.Group;
const SelectValue = SelectPrimitive.Value;
const SelectTrigger = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Trigger>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger>
>(({ className, children, ...props }, ref) => (
<SelectPrimitive.Trigger
ref={ref}
className={cn(
"flex h-10 w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",
className,
)}
{...props}
>
{children}
<SelectPrimitive.Icon asChild>
<ChevronDown className="h-4 w-4 opacity-50" />
</SelectPrimitive.Icon>
</SelectPrimitive.Trigger>
));
SelectTrigger.displayName = SelectPrimitive.Trigger.displayName;
const SelectScrollUpButton = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.ScrollUpButton>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollUpButton>
>(({ className, ...props }, ref) => (
<SelectPrimitive.ScrollUpButton
ref={ref}
className={cn(
"flex cursor-default items-center justify-center py-1",
className,
)}
{...props}
>
<ChevronUp className="h-4 w-4" />
</SelectPrimitive.ScrollUpButton>
));
SelectScrollUpButton.displayName = SelectPrimitive.ScrollUpButton.displayName;
const SelectScrollDownButton = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.ScrollDownButton>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollDownButton>
>(({ className, ...props }, ref) => (
<SelectPrimitive.ScrollDownButton
ref={ref}
className={cn(
"flex cursor-default items-center justify-center py-1",
className,
)}
{...props}
>
<ChevronDown className="h-4 w-4" />
</SelectPrimitive.ScrollDownButton>
));
SelectScrollDownButton.displayName =
SelectPrimitive.ScrollDownButton.displayName;
const SelectContent = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Content>
>(({ className, children, position = "popper", ...props }, ref) => (
<SelectPrimitive.Portal>
<SelectPrimitive.Content
ref={ref}
className={cn(
"relative z-50 max-h-96 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
position === "popper" &&
"data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
className,
)}
position={position}
{...props}
>
<SelectScrollUpButton />
<SelectPrimitive.Viewport
className={cn(
"p-1",
position === "popper" &&
"h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]",
)}
>
{children}
</SelectPrimitive.Viewport>
<SelectScrollDownButton />
</SelectPrimitive.Content>
</SelectPrimitive.Portal>
));
SelectContent.displayName = SelectPrimitive.Content.displayName;
const SelectLabel = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Label>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Label>
>(({ className, ...props }, ref) => (
<SelectPrimitive.Label
ref={ref}
className={cn("py-1.5 pl-8 pr-2 text-sm font-semibold", className)}
{...props}
/>
));
SelectLabel.displayName = SelectPrimitive.Label.displayName;
const SelectItem = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Item>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item>
>(({ className, children, ...props }, ref) => (
<SelectPrimitive.Item
ref={ref}
className={cn(
"relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
className,
)}
{...props}
>
<span className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<SelectPrimitive.ItemIndicator>
<Check className="h-4 w-4" />
</SelectPrimitive.ItemIndicator>
</span>
<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
</SelectPrimitive.Item>
));
SelectItem.displayName = SelectPrimitive.Item.displayName;
const SelectSeparator = React.forwardRef<
React.ElementRef<typeof SelectPrimitive.Separator>,
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Separator>
>(({ className, ...props }, ref) => (
<SelectPrimitive.Separator
ref={ref}
className={cn("-mx-1 my-1 h-px bg-muted", className)}
{...props}
/>
));
SelectSeparator.displayName = SelectPrimitive.Separator.displayName;
export {
Select,
SelectGroup,
SelectValue,
SelectTrigger,
SelectContent,
SelectLabel,
SelectItem,
SelectSeparator,
SelectScrollUpButton,
SelectScrollDownButton,
};
+657
View File
@@ -7,6 +7,11 @@ import {
Recommendation,
FitnessGoal,
Notification,
DailyNutrition,
DailyHydration,
MealEntry,
FitnessProfileHistory,
TrainerClientAssignment,
DatabaseConfig,
} from "./types";
import {
@@ -18,6 +23,11 @@ import {
recommendations,
fitnessGoals,
notifications,
dailyNutrition,
dailyHydration,
mealEntries,
fitnessProfileHistory,
trainerClientAssignments,
eq,
and,
desc,
@@ -1524,4 +1534,651 @@ export class DrizzleDatabase implements IDatabase {
createdAt,
};
}
// ==================== DAILY NUTRITION OPERATIONS ====================
async createDailyNutrition(
nutrition: Omit<DailyNutrition, "id" | "createdAt" | "updatedAt">,
): Promise<DailyNutrition> {
const id = `nutrition_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const now = new Date();
const newNutrition = {
id,
...nutrition,
createdAt: now,
updatedAt: now,
};
await this.db.insert(dailyNutrition).values(newNutrition as any);
return this.mapDailyNutrition(newNutrition);
}
async getDailyNutrition(
userId: string,
date: string,
): Promise<DailyNutrition | null> {
const results = await this.db
.select()
.from(dailyNutrition)
.where(
and(eq(dailyNutrition.userId, userId), eq(dailyNutrition.date, date)),
)
.limit(1)
.all();
return results.length > 0 ? this.mapDailyNutrition(results[0]) : null;
}
async getDailyNutritionById(id: string): Promise<DailyNutrition | null> {
const results = await this.db
.select()
.from(dailyNutrition)
.where(eq(dailyNutrition.id, id))
.limit(1)
.all();
return results.length > 0 ? this.mapDailyNutrition(results[0]) : null;
}
async getDailyNutritionRange(
userId: string,
startDate: string,
endDate: string,
): Promise<DailyNutrition[]> {
const results = await this.db
.select()
.from(dailyNutrition)
.where(
and(
eq(dailyNutrition.userId, userId),
gte(dailyNutrition.date, startDate),
lte(dailyNutrition.date, endDate),
),
)
.orderBy(dailyNutrition.date)
.all();
return results.map((row) => this.mapDailyNutrition(row));
}
async updateDailyNutrition(
id: string,
updates: Partial<DailyNutrition>,
): Promise<DailyNutrition | null> {
const { id: _, ...updateData } = updates as any;
if (Object.keys(updateData).length === 0) {
const existing = await this.db
.select()
.from(dailyNutrition)
.where(eq(dailyNutrition.id, id))
.limit(1)
.all();
return existing.length > 0 ? this.mapDailyNutrition(existing[0]) : null;
}
await this.db
.update(dailyNutrition)
.set({ ...updateData, updatedAt: new Date() })
.where(eq(dailyNutrition.id, id))
.run();
const results = await this.db
.select()
.from(dailyNutrition)
.where(eq(dailyNutrition.id, id))
.limit(1)
.all();
return results.length > 0 ? this.mapDailyNutrition(results[0]) : null;
}
async deleteDailyNutrition(id: string): Promise<boolean> {
const result = await this.db
.delete(dailyNutrition)
.where(eq(dailyNutrition.id, id))
.run();
return result.changes > 0;
}
private mapDailyNutrition(row: Record<string, unknown>): DailyNutrition {
const createdAtValue = row.createdAt as number | Date;
const updatedAtValue = row.updatedAt as number | Date;
return {
id: String(row.id),
userId: String(row.userId),
date: String(row.date),
totalCalories: Number(row.totalCalories || 0),
calorieGoal: Number(row.calorieGoal || 2000),
meals: row.meals ? (JSON.parse(String(row.meals)) as any) : undefined,
createdAt:
typeof createdAtValue === "number"
? new Date(createdAtValue * 1000)
: new Date(createdAtValue),
updatedAt:
typeof updatedAtValue === "number"
? new Date(updatedAtValue * 1000)
: new Date(updatedAtValue),
};
}
// ==================== MEAL ENTRY OPERATIONS ====================
async createMealEntry(
meal: Omit<MealEntry, "id" | "createdAt">,
): Promise<MealEntry> {
const id = `meal_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const now = new Date();
const newMeal = {
id,
...meal,
createdAt: now,
};
await this.db.insert(mealEntries).values(newMeal as any);
return this.mapMealEntry(newMeal);
}
async getMealEntriesByDate(
userId: string,
date: string,
): Promise<MealEntry[]> {
// Parse date string to get start and end of day in seconds
const startOfDay = new Date(date);
startOfDay.setHours(0, 0, 0, 0);
const endOfDay = new Date(date);
endOfDay.setHours(23, 59, 59, 999);
const startTimestamp = Math.floor(startOfDay.getTime() / 1000);
const endTimestamp = Math.floor(endOfDay.getTime() / 1000);
const results = await this.db
.select()
.from(mealEntries)
.where(
and(
eq(mealEntries.userId, userId),
sql`${mealEntries.timestamp} >= ${startTimestamp}`,
sql`${mealEntries.timestamp} <= ${endTimestamp}`,
),
)
.orderBy(mealEntries.timestamp)
.all();
return results.map((row) => this.mapMealEntry(row));
}
async getMealEntryById(id: string): Promise<MealEntry | null> {
const results = await this.db
.select()
.from(mealEntries)
.where(eq(mealEntries.id, id))
.limit(1)
.all();
return results.length > 0 ? this.mapMealEntry(results[0]) : null;
}
async deleteMealEntry(id: string): Promise<boolean> {
const result = await this.db
.delete(mealEntries)
.where(eq(mealEntries.id, id))
.run();
return result.changes > 0;
}
private mapMealEntry(row: Record<string, unknown>): MealEntry {
const timestampValue = row.timestamp as number | Date;
const createdAtValue = row.createdAt as number | Date;
return {
id: String(row.id),
userId: String(row.userId),
dailyNutritionId: row.dailyNutritionId
? String(row.dailyNutritionId)
: undefined,
mealType: String(row.mealType) as MealEntry["mealType"],
foodName: String(row.foodName),
calories: Number(row.calories),
protein: row.protein ? Number(row.protein) : undefined,
carbs: row.carbs ? Number(row.carbs) : undefined,
fats: row.fats ? Number(row.fats) : undefined,
timestamp:
typeof timestampValue === "number"
? new Date(timestampValue * 1000)
: new Date(timestampValue),
createdAt:
typeof createdAtValue === "number"
? new Date(createdAtValue * 1000)
: new Date(createdAtValue),
};
}
// ==================== DAILY HYDRATION OPERATIONS ====================
async createDailyHydration(
hydration: Omit<DailyHydration, "id" | "createdAt" | "updatedAt">,
): Promise<DailyHydration> {
const id = `hydration_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const now = new Date();
const newHydration = {
id,
...hydration,
createdAt: now,
updatedAt: now,
};
await this.db.insert(dailyHydration).values(newHydration as any);
return this.mapDailyHydration(newHydration);
}
async getDailyHydration(
userId: string,
date: string,
): Promise<DailyHydration | null> {
const results = await this.db
.select()
.from(dailyHydration)
.where(
and(eq(dailyHydration.userId, userId), eq(dailyHydration.date, date)),
)
.limit(1)
.all();
return results.length > 0 ? this.mapDailyHydration(results[0]) : null;
}
async getDailyHydrationById(id: string): Promise<DailyHydration | null> {
const results = await this.db
.select()
.from(dailyHydration)
.where(eq(dailyHydration.id, id))
.limit(1)
.all();
return results.length > 0 ? this.mapDailyHydration(results[0]) : null;
}
async getDailyHydrationRange(
userId: string,
startDate: string,
endDate: string,
): Promise<DailyHydration[]> {
const results = await this.db
.select()
.from(dailyHydration)
.where(
and(
eq(dailyHydration.userId, userId),
gte(dailyHydration.date, startDate),
lte(dailyHydration.date, endDate),
),
)
.orderBy(dailyHydration.date)
.all();
return results.map((row) => this.mapDailyHydration(row));
}
async updateDailyHydration(
id: string,
updates: Partial<DailyHydration>,
): Promise<DailyHydration | null> {
const { id: _, ...updateData } = updates as any;
if (Object.keys(updateData).length === 0) {
const existing = await this.db
.select()
.from(dailyHydration)
.where(eq(dailyHydration.id, id))
.limit(1)
.all();
return existing.length > 0 ? this.mapDailyHydration(existing[0]) : null;
}
await this.db
.update(dailyHydration)
.set({ ...updateData, updatedAt: new Date() })
.where(eq(dailyHydration.id, id))
.run();
const results = await this.db
.select()
.from(dailyHydration)
.where(eq(dailyHydration.id, id))
.limit(1)
.all();
return results.length > 0 ? this.mapDailyHydration(results[0]) : null;
}
async deleteDailyHydration(id: string): Promise<boolean> {
const result = await this.db
.delete(dailyHydration)
.where(eq(dailyHydration.id, id))
.run();
return result.changes > 0;
}
private mapDailyHydration(row: Record<string, unknown>): DailyHydration {
const createdAtValue = row.createdAt as number | Date;
const updatedAtValue = row.updatedAt as number | Date;
return {
id: String(row.id),
userId: String(row.userId),
date: String(row.date),
totalWater: Number(row.totalWater || 0),
waterGoal: Number(row.waterGoal || 2000),
entries: row.entries
? (JSON.parse(String(row.entries)) as any)
: undefined,
createdAt:
typeof createdAtValue === "number"
? new Date(createdAtValue * 1000)
: new Date(createdAtValue),
updatedAt:
typeof updatedAtValue === "number"
? new Date(updatedAtValue * 1000)
: new Date(updatedAtValue),
};
}
// ==================== FITNESS PROFILE HISTORY OPERATIONS ====================
async createFitnessProfileHistory(
history: Omit<FitnessProfileHistory, "id" | "createdAt">,
): Promise<FitnessProfileHistory> {
const id = `profile_history_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const now = new Date();
const newHistory = {
id,
...history,
createdAt: now,
};
await this.db.insert(fitnessProfileHistory).values(newHistory as any);
return this.mapFitnessProfileHistory(newHistory);
}
async getFitnessProfileHistory(
userId: string,
startDate?: Date,
endDate?: Date,
): Promise<FitnessProfileHistory[]> {
const conditions = [eq(fitnessProfileHistory.userId, userId)];
if (startDate) {
const startTimestamp = Math.floor(startDate.getTime() / 1000);
conditions.push(
sql`${fitnessProfileHistory.changedAt} >= ${startTimestamp}`,
);
}
if (endDate) {
const endTimestamp = Math.floor(endDate.getTime() / 1000);
conditions.push(
sql`${fitnessProfileHistory.changedAt} <= ${endTimestamp}`,
);
}
const results = await this.db
.select()
.from(fitnessProfileHistory)
.where(and(...conditions))
.orderBy(desc(fitnessProfileHistory.changedAt))
.all();
return results.map((row) => this.mapFitnessProfileHistory(row));
}
async getWeightHistory(
userId: string,
startDate: Date,
endDate: Date,
): Promise<FitnessProfileHistory[]> {
const startTimestamp = Math.floor(startDate.getTime() / 1000);
const endTimestamp = Math.floor(endDate.getTime() / 1000);
const results = await this.db
.select()
.from(fitnessProfileHistory)
.where(
and(
eq(fitnessProfileHistory.userId, userId),
eq(fitnessProfileHistory.changeType, "weight"),
sql`${fitnessProfileHistory.changedAt} >= ${startTimestamp}`,
sql`${fitnessProfileHistory.changedAt} <= ${endTimestamp}`,
),
)
.orderBy(fitnessProfileHistory.changedAt)
.all();
return results.map((row) => this.mapFitnessProfileHistory(row));
}
private mapFitnessProfileHistory(
row: Record<string, unknown>,
): FitnessProfileHistory {
const changedAtValue = row.changedAt as number | Date;
const createdAtValue = row.createdAt as number | Date;
return {
id: String(row.id),
userId: String(row.userId),
fitnessProfileId: String(row.fitnessProfileId),
changeType: String(row.changeType) as FitnessProfileHistory["changeType"],
fieldName: String(row.fieldName),
previousValue: row.previousValue ? String(row.previousValue) : undefined,
newValue: row.newValue ? String(row.newValue) : undefined,
changedAt:
typeof changedAtValue === "number"
? new Date(changedAtValue * 1000)
: new Date(changedAtValue),
createdAt:
typeof createdAtValue === "number"
? new Date(createdAtValue * 1000)
: new Date(createdAtValue),
};
}
// ==================== TRAINER-CLIENT ASSIGNMENT OPERATIONS ====================
async createTrainerClientAssignment(
assignment: Omit<TrainerClientAssignment, "id" | "createdAt" | "updatedAt">,
): Promise<TrainerClientAssignment> {
const id = `assignment_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const now = new Date();
const newAssignment = {
id,
...assignment,
createdAt: now,
updatedAt: now,
};
await this.db.insert(trainerClientAssignments).values(newAssignment as any);
return this.mapTrainerClientAssignment(newAssignment);
}
async getTrainerClientAssignments(
trainerId: string,
): Promise<TrainerClientAssignment[]> {
const results = await this.db
.select()
.from(trainerClientAssignments)
.where(
and(
eq(trainerClientAssignments.trainerId, trainerId),
eq(trainerClientAssignments.isActive, true),
),
)
.orderBy(desc(trainerClientAssignments.assignedAt))
.all();
return results.map((row) => this.mapTrainerClientAssignment(row));
}
async getClientTrainerAssignment(
clientId: string,
): Promise<TrainerClientAssignment | null> {
const results = await this.db
.select()
.from(trainerClientAssignments)
.where(
and(
eq(trainerClientAssignments.clientId, clientId),
eq(trainerClientAssignments.isActive, true),
),
)
.limit(1)
.all();
return results.length > 0
? this.mapTrainerClientAssignment(results[0])
: null;
}
async deleteTrainerClientAssignment(id: string): Promise<boolean> {
const result = await this.db
.delete(trainerClientAssignments)
.where(eq(trainerClientAssignments.id, id))
.run();
return result.changes > 0;
}
async deactivateTrainerClientAssignment(
id: string,
): Promise<TrainerClientAssignment | null> {
await this.db
.update(trainerClientAssignments)
.set({ isActive: false, updatedAt: new Date() })
.where(eq(trainerClientAssignments.id, id))
.run();
const results = await this.db
.select()
.from(trainerClientAssignments)
.where(eq(trainerClientAssignments.id, id))
.limit(1)
.all();
return results.length > 0
? this.mapTrainerClientAssignment(results[0])
: null;
}
private mapTrainerClientAssignment(
row: Record<string, unknown>,
): TrainerClientAssignment {
const assignedAtValue = row.assignedAt as number | Date;
const createdAtValue = row.createdAt as number | Date;
const updatedAtValue = row.updatedAt as number | Date;
return {
id: String(row.id),
trainerId: String(row.trainerId),
clientId: String(row.clientId),
assignedAt:
typeof assignedAtValue === "number"
? new Date(assignedAtValue * 1000)
: new Date(assignedAtValue),
assignedBy: row.assignedBy ? String(row.assignedBy) : undefined,
isActive: Boolean(row.isActive),
createdAt:
typeof createdAtValue === "number"
? new Date(createdAtValue * 1000)
: new Date(createdAtValue),
updatedAt:
typeof updatedAtValue === "number"
? new Date(updatedAtValue * 1000)
: new Date(updatedAtValue),
};
}
// ==================== ENHANCED ATTENDANCE OPERATIONS FOR REPORTS ====================
async getAttendanceByWeek(
userId: string,
weekStart: Date,
): Promise<Attendance[]> {
// Calculate week end (Sunday at 23:59:59)
const weekEnd = new Date(weekStart);
weekEnd.setDate(weekEnd.getDate() + 6);
weekEnd.setHours(23, 59, 59, 999);
const startTimestamp = Math.floor(weekStart.getTime() / 1000);
const endTimestamp = Math.floor(weekEnd.getTime() / 1000);
const results = await this.db
.select()
.from(attendance)
.where(
and(
eq(attendance.userId, userId),
sql`${attendance.checkInTime} >= ${startTimestamp}`,
sql`${attendance.checkInTime} <= ${endTimestamp}`,
),
)
.orderBy(attendance.checkInTime)
.all();
return results.map((row) => this.mapAttendance(row));
}
async calculateWeeklyCheckInStats(
userId: string,
weekStart: Date,
): Promise<{
totalCheckIns: number;
totalTimeSpent: number;
avgSessionDuration: number;
byType: { gym: number; class: number; personal_training: number };
}> {
const attendanceRecords = await this.getAttendanceByWeek(userId, weekStart);
const stats = {
totalCheckIns: attendanceRecords.length,
totalTimeSpent: 0,
avgSessionDuration: 0,
byType: {
gym: 0,
class: 0,
personal_training: 0,
},
};
let completedSessions = 0;
for (const record of attendanceRecords) {
// Count by type
if (record.type === "gym") stats.byType.gym++;
else if (record.type === "class") stats.byType.class++;
else if (record.type === "personal_training")
stats.byType.personal_training++;
// Calculate time spent (only for completed sessions with check-out)
if (record.checkOutTime) {
const duration =
(record.checkOutTime.getTime() - record.checkInTime.getTime()) /
(1000 * 60); // in minutes
stats.totalTimeSpent += duration;
completedSessions++;
}
}
// Calculate average session duration
if (completedSessions > 0) {
stats.avgSessionDuration = stats.totalTimeSpent / completedSessions;
}
return stats;
}
}
+104
View File
@@ -6,6 +6,11 @@ import {
Recommendation,
FitnessGoal,
Notification,
DailyNutrition,
DailyHydration,
MealEntry,
FitnessProfileHistory,
TrainerClientAssignment,
} from "@fitai/shared";
import type { SortConfig, FilterCondition } from "../filtering";
@@ -23,6 +28,11 @@ export type {
Recommendation,
FitnessGoal,
Notification,
DailyNutrition,
DailyHydration,
MealEntry,
FitnessProfileHistory,
TrainerClientAssignment,
};
// Database Interface - allows us to swap implementations
@@ -188,6 +198,100 @@ export interface IDatabase {
totalRevenue: number;
revenueGrowth: number; // Percentage vs last month
}>;
// Daily Nutrition operations
createDailyNutrition(
nutrition: Omit<DailyNutrition, "id" | "createdAt" | "updatedAt">,
): Promise<DailyNutrition>;
getDailyNutrition(
userId: string,
date: string,
): Promise<DailyNutrition | null>;
getDailyNutritionById(id: string): Promise<DailyNutrition | null>;
getDailyNutritionRange(
userId: string,
startDate: string,
endDate: string,
): Promise<DailyNutrition[]>;
updateDailyNutrition(
id: string,
updates: Partial<DailyNutrition>,
): Promise<DailyNutrition | null>;
deleteDailyNutrition(id: string): Promise<boolean>;
// Meal Entry operations
createMealEntry(
meal: Omit<MealEntry, "id" | "createdAt">,
): Promise<MealEntry>;
getMealEntriesByDate(userId: string, date: string): Promise<MealEntry[]>;
getMealEntryById(id: string): Promise<MealEntry | null>;
deleteMealEntry(id: string): Promise<boolean>;
// Daily Hydration operations
createDailyHydration(
hydration: Omit<DailyHydration, "id" | "createdAt" | "updatedAt">,
): Promise<DailyHydration>;
getDailyHydration(
userId: string,
date: string,
): Promise<DailyHydration | null>;
getDailyHydrationById(id: string): Promise<DailyHydration | null>;
getDailyHydrationRange(
userId: string,
startDate: string,
endDate: string,
): Promise<DailyHydration[]>;
updateDailyHydration(
id: string,
updates: Partial<DailyHydration>,
): Promise<DailyHydration | null>;
deleteDailyHydration(id: string): Promise<boolean>;
// Fitness Profile History operations
createFitnessProfileHistory(
history: Omit<FitnessProfileHistory, "id" | "createdAt">,
): Promise<FitnessProfileHistory>;
getFitnessProfileHistory(
userId: string,
startDate?: Date,
endDate?: Date,
): Promise<FitnessProfileHistory[]>;
getWeightHistory(
userId: string,
startDate: Date,
endDate: Date,
): Promise<FitnessProfileHistory[]>;
// Trainer-Client Assignment operations
createTrainerClientAssignment(
assignment: Omit<TrainerClientAssignment, "id" | "createdAt" | "updatedAt">,
): Promise<TrainerClientAssignment>;
getTrainerClientAssignments(
trainerId: string,
): Promise<TrainerClientAssignment[]>;
getClientTrainerAssignment(
clientId: string,
): Promise<TrainerClientAssignment | null>;
deleteTrainerClientAssignment(id: string): Promise<boolean>;
deactivateTrainerClientAssignment(
id: string,
): Promise<TrainerClientAssignment | null>;
// Enhanced Attendance operations for reports
getAttendanceByWeek(userId: string, weekStart: Date): Promise<Attendance[]>;
calculateWeeklyCheckInStats(
userId: string,
weekStart: Date,
): Promise<{
totalCheckIns: number;
totalTimeSpent: number; // in minutes
avgSessionDuration: number; // in minutes
byType: {
gym: number;
class: number;
personal_training: number;
};
}>;
}
// Database configuration
@@ -0,0 +1,236 @@
import type { UserReport, User, Client, FitnessProfile } from "@fitai/shared";
// Mock data for testing
const mockUser: User = {
id: "user_123",
email: "john.doe@example.com",
firstName: "John",
lastName: "Doe",
role: "client",
phone: "555-1234",
createdAt: new Date("2024-01-15"),
updatedAt: new Date("2024-01-15"),
};
const mockClient: Client = {
id: "client_123",
userId: "user_123",
membershipType: "premium",
membershipStatus: "active",
joinDate: new Date("2024-01-15"),
};
const mockProfile: FitnessProfile = {
id: "profile_123",
userId: "user_123",
height: 175,
weight: 70,
age: 30,
gender: "male",
activityLevel: "moderately_active",
createdAt: new Date("2024-01-15"),
updatedAt: new Date("2024-01-15"),
};
const mockReport: UserReport = {
userId: "user_123",
user: mockUser,
client: mockClient,
fitnessProfile: mockProfile,
reportPeriod: {
startDate: "2024-01-01",
endDate: "2024-01-31",
},
weeklyCheckIns: [
{
weekStart: "2024-01-01",
weekEnd: "2024-01-07",
totalCheckIns: 4,
totalTimeMinutes: 240,
averageDurationMinutes: 60,
checkInsByType: [
{ type: "gym", count: 3 },
{ type: "class", count: 1 },
],
},
{
weekStart: "2024-01-08",
weekEnd: "2024-01-14",
totalCheckIns: 5,
totalTimeMinutes: 300,
averageDurationMinutes: 60,
checkInsByType: [
{ type: "gym", count: 4 },
{ type: "class", count: 1 },
],
},
],
nutrition: {
dailySummaries: [
{
date: "2024-01-01",
totalCalories: 2000,
calorieGoal: 2200,
caloriesDelta: -200,
mealsCount: 3,
},
{
date: "2024-01-02",
totalCalories: 2300,
calorieGoal: 2200,
caloriesDelta: 100,
mealsCount: 4,
},
],
averageDailyCalories: 2150,
totalDays: 2,
daysMetGoal: 1,
},
hydration: {
dailySummaries: [
{
date: "2024-01-01",
totalWater: 2000,
waterGoal: 2500,
hydrationPercentage: 80,
},
{
date: "2024-01-02",
totalWater: 2600,
waterGoal: 2500,
hydrationPercentage: 104,
},
],
averageDailyWater: 2300,
totalDays: 2,
daysMetGoal: 1,
},
goals: {
active: [
{
id: "goal_1",
userId: "user_123",
goalType: "weight_target",
title: "Lose 5kg",
description: "Lose weight for summer",
targetValue: 65,
currentValue: 70,
unit: "kg",
startDate: new Date("2024-01-01"),
status: "active",
progress: 0,
priority: "high",
createdAt: new Date("2024-01-01"),
updatedAt: new Date("2024-01-01"),
},
],
completed: [
{
id: "goal_2",
userId: "user_123",
goalType: "strength_milestone",
title: "Bench press 100kg",
targetValue: 100,
currentValue: 100,
unit: "kg",
startDate: new Date("2023-11-01"),
completedDate: new Date("2023-12-15"),
status: "completed",
progress: 100,
priority: "medium",
createdAt: new Date("2023-11-01"),
updatedAt: new Date("2023-12-15"),
},
],
totalActive: 1,
totalCompleted: 1,
averageProgress: 0,
},
profileHistory: [
{
changeType: "weight",
fieldName: "weight",
previousValue: "72",
newValue: "70",
changedAt: new Date("2024-01-15"),
},
],
recommendations: {
accepted: [
{
id: "rec_1",
userId: "user_123",
fitnessProfileId: "profile_123",
recommendationText: "Increase protein intake to 150g per day",
activityPlan: "High protein diet",
dietPlan: "Protein focused",
status: "approved",
generatedAt: new Date("2024-01-10"),
approvedAt: new Date("2024-01-11"),
approvedBy: "user_trainer",
createdAt: new Date("2024-01-10"),
updatedAt: new Date("2024-01-11"),
},
],
rejected: [],
pending: [
{
id: "rec_2",
userId: "user_123",
fitnessProfileId: "profile_123",
recommendationText: "Try HIIT workouts 3 times per week",
activityPlan: "HIIT training",
dietPlan: "No change",
status: "pending",
generatedAt: new Date("2024-01-20"),
createdAt: new Date("2024-01-20"),
updatedAt: new Date("2024-01-20"),
},
],
totalAccepted: 1,
totalRejected: 0,
totalPending: 1,
},
generatedAt: new Date(),
};
// Test function
export async function testPDFGeneration() {
try {
const { generateReportPDFBase64 } = await import(
"@/lib/pdf/report-helpers"
);
console.log("Generating PDF from mock report...");
const pdfBase64 = generateReportPDFBase64(mockReport);
console.log(`PDF generated successfully!`);
console.log(`PDF size: ${Math.round(pdfBase64.length / 1024)} KB`);
// Verify it's a valid base64 string
if (pdfBase64.startsWith("JVBERi0")) {
console.log("✓ PDF is valid (starts with PDF header)");
} else {
console.log("✗ PDF is invalid");
}
return true;
} catch (error) {
console.error("✗ PDF generation failed:", error);
return false;
}
}
// Run if executed directly
if (typeof window === "undefined" && process.argv[1]?.includes("test-pdf")) {
testPDFGeneration()
.then((success) => {
process.exit(success ? 0 : 1);
})
.catch((err) => {
console.error(err);
process.exit(1);
});
}
export default testPDFGeneration;
+316
View File
@@ -0,0 +1,316 @@
import jsPDF from "jspdf";
export interface ChartData {
labels: string[];
datasets: {
label: string;
data: number[];
color?: string;
}[];
}
export interface ChartConfig {
title?: string;
width: number;
height: number;
}
/**
* Generate a bar chart image as a data URL
*/
export function generateBarChart(data: ChartData, config: ChartConfig): string {
const canvas = createCanvas(config.width, config.height);
const ctx = canvas.getContext("2d");
if (!ctx) {
throw new Error("Could not get canvas context");
}
const padding = 40;
const chartWidth = config.width - padding * 2;
const chartHeight = config.height - padding * 2;
const barWidth = chartWidth / data.labels.length - 10;
// Clear canvas
ctx.fillStyle = "#ffffff";
ctx.fillRect(0, 0, config.width, config.height);
// Add title if provided
if (config.title) {
ctx.fillStyle = "#000000";
ctx.font = "bold 14px Arial";
ctx.textAlign = "center";
ctx.fillText(config.title, config.width / 2, 20);
}
// Calculate max value
const maxValue = Math.max(
...data.datasets.flatMap((dataset) => dataset.data),
);
// Draw bars
data.datasets.forEach((dataset, datasetIndex) => {
const color = dataset.color || getDefaultColor(datasetIndex);
ctx.fillStyle = color;
dataset.data.forEach((value, index) => {
const x =
padding +
index * (barWidth + 10) +
(datasetIndex * barWidth) / data.datasets.length;
const barHeight = (value / maxValue) * chartHeight;
const y = config.height - padding - barHeight;
ctx.fillRect(x, y, barWidth / data.datasets.length - 2, barHeight);
// Draw value on top
ctx.fillStyle = "#000000";
ctx.font = "10px Arial";
ctx.textAlign = "center";
ctx.fillText(
value.toString(),
x + barWidth / data.datasets.length / 2,
y - 5,
);
ctx.fillStyle = color;
});
});
// Draw labels
ctx.fillStyle = "#000000";
ctx.font = "11px Arial";
ctx.textAlign = "center";
data.labels.forEach((label, index) => {
const x = padding + index * (barWidth + 10) + barWidth / 2;
ctx.fillText(label, x, config.height - padding + 15);
});
// Draw axes
ctx.strokeStyle = "#cccccc";
ctx.beginPath();
ctx.moveTo(padding, padding);
ctx.lineTo(padding, config.height - padding);
ctx.lineTo(config.width - padding, config.height - padding);
ctx.stroke();
return canvas.toDataURL("image/png");
}
/**
* Generate a line chart image as a data URL
*/
export function generateLineChart(
data: ChartData,
config: ChartConfig,
): string {
const canvas = createCanvas(config.width, config.height);
const ctx = canvas.getContext("2d");
if (!ctx) {
throw new Error("Could not get canvas context");
}
const padding = 40;
const chartWidth = config.width - padding * 2;
const chartHeight = config.height - padding * 2;
// Clear canvas
ctx.fillStyle = "#ffffff";
ctx.fillRect(0, 0, config.width, config.height);
// Add title if provided
if (config.title) {
ctx.fillStyle = "#000000";
ctx.font = "bold 14px Arial";
ctx.textAlign = "center";
ctx.fillText(config.title, config.width / 2, 20);
}
// Calculate max value
const maxValue = Math.max(
...data.datasets.flatMap((dataset) => dataset.data),
1,
);
// Draw grid lines
ctx.strokeStyle = "#f0f0f0";
ctx.lineWidth = 1;
for (let i = 0; i <= 5; i++) {
const y = padding + (i * chartHeight) / 5;
ctx.beginPath();
ctx.moveTo(padding, y);
ctx.lineTo(config.width - padding, y);
ctx.stroke();
// Draw value labels
ctx.fillStyle = "#666666";
ctx.font = "10px Arial";
ctx.textAlign = "right";
const value = Math.round(maxValue - (i * maxValue) / 5);
ctx.fillText(value.toString(), padding - 5, y + 3);
}
// Draw lines
data.datasets.forEach((dataset, datasetIndex) => {
const color = dataset.color || getDefaultColor(datasetIndex);
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.beginPath();
dataset.data.forEach((value, index) => {
const x = padding + (index * chartWidth) / (data.labels.length - 1);
const y = config.height - padding - (value / maxValue) * chartHeight;
if (index === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
});
ctx.stroke();
// Draw points
ctx.fillStyle = color;
dataset.data.forEach((value, index) => {
const x = padding + (index * chartWidth) / (data.labels.length - 1);
const y = config.height - padding - (value / maxValue) * chartHeight;
ctx.beginPath();
ctx.arc(x, y, 4, 0, 2 * Math.PI);
ctx.fill();
});
});
// Draw labels
ctx.fillStyle = "#000000";
ctx.font = "11px Arial";
ctx.textAlign = "center";
data.labels.forEach((label, index) => {
const x = padding + (index * chartWidth) / (data.labels.length - 1);
ctx.fillText(label, x, config.height - padding + 15);
});
// Draw axes
ctx.strokeStyle = "#cccccc";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(padding, padding);
ctx.lineTo(padding, config.height - padding);
ctx.lineTo(config.width - padding, config.height - padding);
ctx.stroke();
return canvas.toDataURL("image/png");
}
/**
* Generate a pie chart image as a data URL
*/
export function generatePieChart(data: ChartData, config: ChartConfig): string {
const canvas = createCanvas(config.width, config.height);
const ctx = canvas.getContext("2d");
if (!ctx) {
throw new Error("Could not get canvas context");
}
const centerX = config.width / 2;
const centerY = config.height / 2;
const radius = Math.min(config.width, config.height) / 2 - 40;
// Clear canvas
ctx.fillStyle = "#ffffff";
ctx.fillRect(0, 0, config.width, config.height);
// Add title if provided
if (config.title) {
ctx.fillStyle = "#000000";
ctx.font = "bold 14px Arial";
ctx.textAlign = "center";
ctx.fillText(config.title, config.width / 2, 20);
}
// Calculate total
const total = data.datasets[0]?.data.reduce((sum, val) => sum + val, 0) || 0;
// Draw pie slices
let startAngle = 0;
data.datasets[0]?.data.forEach((value, index) => {
const sliceAngle = (value / total) * 2 * Math.PI;
const color = data.datasets[0].color || getDefaultColor(index);
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo(centerX, centerY);
ctx.arc(centerX, centerY, radius, startAngle, startAngle + sliceAngle);
ctx.closePath();
ctx.fill();
// Draw label
const midAngle = startAngle + sliceAngle / 2;
const labelX = centerX + (radius + 20) * Math.cos(midAngle);
const labelY = centerY + (radius + 20) * Math.sin(midAngle);
ctx.fillStyle = "#000000";
ctx.font = "11px Arial";
ctx.textAlign = "center";
ctx.fillText(`${data.labels[index]}: ${value}`, labelX, labelY);
startAngle += sliceAngle;
});
return canvas.toDataURL("image/png");
}
/**
* Helper function to create a canvas element
*/
function createCanvas(width: number, height: number): HTMLCanvasElement {
if (typeof document !== "undefined") {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
return canvas;
}
// Fallback for server-side rendering
const Canvas = require("canvas");
return new Canvas(width, height);
}
/**
* Get default color for a dataset index
*/
function getDefaultColor(index: number): string {
const colors = [
"#3c82e1", // primary
"#2ea643", // success
"#f0a500", // warning
"#d73232", // destructive
"#8b5cf6", // purple
"#06b6d4", // cyan
];
return colors[index % colors.length];
}
/**
* Add chart image to PDF
*/
export function addChartToPDF(
pdf: jsPDF,
imageData: string,
x: number,
y: number,
width?: number,
height?: number,
): void {
const imgWidth = width || 150;
const imgHeight = height || 80;
pdf.addImage(imageData, "PNG", x, y, imgWidth, imgHeight);
}
export default {
generateBarChart,
generateLineChart,
generatePieChart,
addChartToPDF,
};
+15
View File
@@ -0,0 +1,15 @@
export { default as PDFGenerator } from "./pdf-generator";
export {
default as ChartGenerator,
generateBarChart,
generateLineChart,
generatePieChart,
addChartToPDF,
type ChartData,
type ChartConfig,
} from "./chart-generator";
export {
generateReportPDF,
generateReportPDFBase64,
saveReportPDF,
} from "./report-helpers";
+821
View File
@@ -0,0 +1,821 @@
import jsPDF from "jspdf";
import autoTable from "jspdf-autotable";
import type { UserReport } from "@fitai/shared";
export class PDFGenerator {
private doc: jsPDF;
private pageWidth: number;
private pageHeight: number;
private margin: { top: number; right: number; bottom: number; left: number };
private currentY: number = 0;
// Theme colors (from globals.css)
private colors = {
primary: [60, 130, 225] as [number, number, number],
primaryDark: [40, 80, 180] as [number, number, number],
success: [46, 160, 67] as [number, number, number],
warning: [240, 185, 11] as [number, number, number],
destructive: [215, 50, 50] as [number, number, number],
text: [30, 30, 30] as [number, number, number],
textLight: [100, 100, 100] as [number, number, number],
background: [245, 245, 245] as [number, number, number],
white: [255, 255, 255] as [number, number, number],
};
constructor() {
this.doc = new jsPDF({
orientation: "portrait",
unit: "mm",
format: "a4",
});
this.pageWidth = this.doc.internal.pageSize.getWidth();
this.pageHeight = this.doc.internal.pageSize.getHeight();
this.margin = { top: 20, right: 20, bottom: 20, left: 20 };
}
/**
* Generate a complete user report PDF
*/
generateUserReport(report: UserReport): jsPDF {
this.addHeader(report);
this.addUserInfo(report);
this.addReportPeriod(report);
this.addWeeklyCheckIns(report.weeklyCheckIns);
this.addNutritionSummary(report.nutrition);
this.addHydrationSummary(report.hydration);
this.addGoalsSummary(report.goals);
this.addProfileHistory(report.profileHistory);
this.addRecommendations(report.recommendations);
this.addFooter();
return this.doc;
}
/**
* Save PDF to file
*/
save(filename: string): void {
this.doc.save(filename);
}
/**
* Get PDF as blob for API response
*/
toBlob(): Blob {
return this.doc.output("blob");
}
/**
* Get PDF as base64 for API response
*/
toBase64(): string {
return this.doc.output("datauristring").split(",")[1];
}
/**
* Add header with title and FitAI branding
*/
private addHeader(report: UserReport): void {
this.currentY = this.margin.top;
// Header background
this.doc.setFillColor(...this.colors.primary);
this.doc.rect(0, 0, this.pageWidth, 35, "F");
// Title
this.doc.setTextColor(...this.colors.white);
this.doc.setFontSize(24);
this.doc.setFont("helvetica", "bold");
this.doc.text("FitAI User Report", this.margin.left, 20);
// Subtitle
this.doc.setFontSize(10);
this.doc.setFont("helvetica", "normal");
this.doc.text(
`Generated: ${new Date().toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
})}`,
this.margin.left,
28,
);
// FitAI logo text
this.doc.setFontSize(14);
this.doc.setFont("helvetica", "bold");
this.doc.text("FitAI", this.pageWidth - this.margin.right - 20, 20);
this.currentY = 45;
}
/**
* Add user information section
*/
private addUserInfo(report: UserReport): void {
this.checkPageBreak(30);
this.doc.setTextColor(...this.colors.primary);
this.doc.setFontSize(14);
this.doc.setFont("helvetica", "bold");
this.doc.text("User Information", this.margin.left, this.currentY);
this.currentY += 8;
const user = report.user;
const client = report.client;
const profile = report.fitnessProfile;
const userInfo = [
["Name", `${user.firstName} ${user.lastName}`],
["Email", user.email],
["Phone", user.phone || "N/A"],
["Role", user.role],
[
"Member Since",
user.createdAt ? new Date(user.createdAt).toLocaleDateString() : "N/A",
],
];
if (client) {
userInfo.push([
"Membership",
`${client.membershipType} - ${client.membershipStatus}`,
]);
userInfo.push([
"Join Date",
new Date(client.joinDate).toLocaleDateString(),
]);
}
if (profile) {
userInfo.push([
"Fitness Profile",
`Height: ${profile.height || "N/A"}cm, Weight: ${profile.weight || "N/A"}kg`,
]);
}
autoTable(this.doc, {
startY: this.currentY,
body: userInfo,
theme: "plain",
margin: { left: this.margin.left, right: this.margin.right },
columnStyles: {
0: { fontStyle: "bold", cellWidth: 40, fontSize: 10 },
1: { cellWidth: "auto", fontSize: 10 },
},
styles: {
cellPadding: 3,
textColor: this.colors.text,
},
tableWidth: "auto" as any,
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 15;
}
/**
* Add report period section
*/
private addReportPeriod(report: UserReport): void {
this.checkPageBreak(25);
this.doc.setTextColor(...this.colors.primary);
this.doc.setFontSize(14);
this.doc.setFont("helvetica", "bold");
this.doc.text("Report Period", this.margin.left, this.currentY);
this.currentY += 8;
const periodInfo = [
["Start Date", report.reportPeriod.startDate],
["End Date", report.reportPeriod.endDate],
[
"Duration",
`${Math.ceil(
(new Date(report.reportPeriod.endDate).getTime() -
new Date(report.reportPeriod.startDate).getTime()) /
(1000 * 60 * 60 * 24),
)} days`,
],
];
autoTable(this.doc, {
startY: this.currentY,
body: periodInfo,
theme: "plain",
margin: { left: this.margin.left, right: this.margin.right },
columnStyles: {
0: { fontStyle: "bold", cellWidth: 40, fontSize: 10 },
1: { cellWidth: "auto", fontSize: 10 },
},
styles: {
cellPadding: 3,
textColor: this.colors.text,
},
tableWidth: "auto" as any,
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 15;
}
/**
* Add weekly check-ins section
*/
private addWeeklyCheckIns(
weeklyCheckIns: UserReport["weeklyCheckIns"],
): void {
this.checkPageBreak(50);
this.doc.setTextColor(...this.colors.primary);
this.doc.setFontSize(14);
this.doc.setFont("helvetica", "bold");
this.doc.text("Weekly Check-ins", this.margin.left, this.currentY);
this.currentY += 8;
if (weeklyCheckIns.length === 0) {
this.doc.setTextColor(...this.colors.textLight);
this.doc.setFontSize(10);
this.doc.setFont("helvetica", "italic");
this.doc.text(
"No check-in data available for this period.",
this.margin.left,
this.currentY,
);
this.currentY += 15;
return;
}
const tableData = weeklyCheckIns.map((week) => [
week.weekStart,
week.weekEnd,
week.totalCheckIns.toString(),
`${week.totalTimeMinutes} min`,
`${week.averageDurationMinutes} min`,
]);
autoTable(this.doc, {
startY: this.currentY,
head: [
["Week Start", "Week End", "Check-ins", "Total Time", "Avg Duration"],
],
body: tableData,
theme: "striped",
headStyles: {
fillColor: this.colors.primary,
textColor: this.colors.white,
fontStyle: "bold",
fontSize: 9,
},
bodyStyles: {
fontSize: 9,
textColor: this.colors.text,
},
alternateRowStyles: {
fillColor: [250, 250, 250],
},
margin: { left: this.margin.left, right: this.margin.right },
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 15;
}
/**
* Add nutrition summary section
*/
private addNutritionSummary(nutrition: UserReport["nutrition"]): void {
this.checkPageBreak(60);
this.doc.setTextColor(...this.colors.primary);
this.doc.setFontSize(14);
this.doc.setFont("helvetica", "bold");
this.doc.text("Nutrition Summary", this.margin.left, this.currentY);
this.currentY += 8;
const summaryStats = [
["Total Days Tracked", nutrition.totalDays.toString()],
[
"Average Daily Calories",
nutrition.averageDailyCalories.toLocaleString(),
],
[
"Days Met Goal (±10%)",
`${nutrition.daysMetGoal} / ${nutrition.totalDays}`,
],
];
autoTable(this.doc, {
startY: this.currentY,
body: summaryStats,
theme: "plain",
margin: { left: this.margin.left, right: this.margin.right },
columnStyles: {
0: { fontStyle: "bold", cellWidth: 60, fontSize: 10 },
1: { cellWidth: "auto", fontSize: 10 },
},
styles: {
cellPadding: 3,
textColor: this.colors.text,
},
tableWidth: "auto" as any,
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 10;
// Add daily breakdown if available
if (
nutrition.dailySummaries.length > 0 &&
nutrition.dailySummaries.length <= 14
) {
this.doc.setTextColor(...this.colors.textLight);
this.doc.setFontSize(10);
this.doc.setFont("helvetica", "italic");
this.doc.text("Daily Breakdown", this.margin.left, this.currentY);
this.currentY += 5;
const dailyData = nutrition.dailySummaries.map((day) => [
day.date,
day.totalCalories.toLocaleString(),
day.calorieGoal.toLocaleString(),
day.caloriesDelta > 0
? `+${day.caloriesDelta}`
: day.caloriesDelta.toString(),
day.mealsCount.toString(),
]);
autoTable(this.doc, {
startY: this.currentY,
head: [["Date", "Calories", "Goal", "Delta", "Meals"]],
body: dailyData,
theme: "striped",
headStyles: {
fillColor: this.colors.primary,
textColor: this.colors.white,
fontStyle: "bold",
fontSize: 8,
},
bodyStyles: {
fontSize: 8,
textColor: this.colors.text,
},
alternateRowStyles: {
fillColor: [250, 250, 250],
},
margin: { left: this.margin.left, right: this.margin.right },
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 15;
} else if (nutrition.dailySummaries.length > 14) {
this.doc.setTextColor(...this.colors.textLight);
this.doc.setFontSize(9);
this.doc.text(
`Daily breakdown (${nutrition.dailySummaries.length} days) - See dashboard for detailed view.`,
this.margin.left,
this.currentY,
);
this.currentY += 15;
}
}
/**
* Add hydration summary section
*/
private addHydrationSummary(hydration: UserReport["hydration"]): void {
this.checkPageBreak(50);
this.doc.setTextColor(...this.colors.primary);
this.doc.setFontSize(14);
this.doc.setFont("helvetica", "bold");
this.doc.text("Hydration Summary", this.margin.left, this.currentY);
this.currentY += 8;
const summaryStats = [
["Total Days Tracked", hydration.totalDays.toString()],
["Average Daily Water", `${hydration.averageDailyWater} ml`],
["Days Met Goal", `${hydration.daysMetGoal} / ${hydration.totalDays}`],
];
autoTable(this.doc, {
startY: this.currentY,
body: summaryStats,
theme: "plain",
margin: { left: this.margin.left, right: this.margin.right },
columnStyles: {
0: { fontStyle: "bold", cellWidth: 60, fontSize: 10 },
1: { cellWidth: "auto", fontSize: 10 },
},
styles: {
cellPadding: 3,
textColor: this.colors.text,
},
tableWidth: "auto" as any,
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 10;
// Add daily breakdown if available
if (
hydration.dailySummaries.length > 0 &&
hydration.dailySummaries.length <= 14
) {
this.doc.setTextColor(...this.colors.textLight);
this.doc.setFontSize(10);
this.doc.setFont("helvetica", "italic");
this.doc.text("Daily Breakdown", this.margin.left, this.currentY);
this.currentY += 5;
const dailyData = hydration.dailySummaries.map((day) => [
day.date,
`${day.totalWater} ml`,
`${day.waterGoal} ml`,
`${day.hydrationPercentage}%`,
]);
autoTable(this.doc, {
startY: this.currentY,
head: [["Date", "Total Water", "Goal", "Achievement"]],
body: dailyData,
theme: "striped",
headStyles: {
fillColor: this.colors.primary,
textColor: this.colors.white,
fontStyle: "bold",
fontSize: 8,
},
bodyStyles: {
fontSize: 8,
textColor: this.colors.text,
},
alternateRowStyles: {
fillColor: [250, 250, 250],
},
margin: { left: this.margin.left, right: this.margin.right },
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 15;
} else if (hydration.dailySummaries.length > 14) {
this.doc.setTextColor(...this.colors.textLight);
this.doc.setFontSize(9);
this.doc.text(
`Daily breakdown (${hydration.dailySummaries.length} days) - See dashboard for detailed view.`,
this.margin.left,
this.currentY,
);
this.currentY += 15;
}
}
/**
* Add goals summary section
*/
private addGoalsSummary(goals: UserReport["goals"]): void {
this.checkPageBreak(60);
this.doc.setTextColor(...this.colors.primary);
this.doc.setFontSize(14);
this.doc.setFont("helvetica", "bold");
this.doc.text("Fitness Goals", this.margin.left, this.currentY);
this.currentY += 8;
const summaryStats = [
["Active Goals", goals.totalActive.toString()],
["Completed Goals", goals.totalCompleted.toString()],
["Average Progress", `${goals.averageProgress}%`],
];
autoTable(this.doc, {
startY: this.currentY,
body: summaryStats,
theme: "plain",
margin: { left: this.margin.left, right: this.margin.right },
columnStyles: {
0: { fontStyle: "bold", cellWidth: 60, fontSize: 10 },
1: { cellWidth: "auto", fontSize: 10 },
},
styles: {
cellPadding: 3,
textColor: this.colors.text,
},
tableWidth: "auto" as any,
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 10;
// Add active goals
if (goals.active.length > 0) {
this.doc.setTextColor(...this.colors.success);
this.doc.setFontSize(10);
this.doc.setFont("helvetica", "bold");
this.doc.text("Active Goals", this.margin.left, this.currentY);
this.currentY += 5;
const activeGoals = goals.active.map((goal) => [
goal.title,
goal.goalType,
`${goal.progress}%`,
goal.priority,
]);
autoTable(this.doc, {
startY: this.currentY,
head: [["Title", "Type", "Progress", "Priority"]],
body: activeGoals,
theme: "striped",
headStyles: {
fillColor: this.colors.success,
textColor: this.colors.white,
fontStyle: "bold",
fontSize: 8,
},
bodyStyles: {
fontSize: 8,
textColor: this.colors.text,
},
alternateRowStyles: {
fillColor: [250, 250, 250],
},
margin: { left: this.margin.left, right: this.margin.right },
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 10;
}
// Add completed goals
if (goals.completed.length > 0) {
this.doc.setTextColor(...this.colors.primary);
this.doc.setFontSize(10);
this.doc.setFont("helvetica", "bold");
this.doc.text("Completed Goals", this.margin.left, this.currentY);
this.currentY += 5;
const completedGoals = goals.completed.map((goal) => [
goal.title,
goal.goalType,
goal.completedDate
? new Date(goal.completedDate).toLocaleDateString()
: "N/A",
]);
autoTable(this.doc, {
startY: this.currentY,
head: [["Title", "Type", "Completed Date"]],
body: completedGoals,
theme: "striped",
headStyles: {
fillColor: this.colors.primary,
textColor: this.colors.white,
fontStyle: "bold",
fontSize: 8,
},
bodyStyles: {
fontSize: 8,
textColor: this.colors.text,
},
alternateRowStyles: {
fillColor: [250, 250, 250],
},
margin: { left: this.margin.left, right: this.margin.right },
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 15;
}
}
/**
* Add profile history section
*/
private addProfileHistory(history: UserReport["profileHistory"]): void {
this.checkPageBreak(50);
this.doc.setTextColor(...this.colors.primary);
this.doc.setFontSize(14);
this.doc.setFont("helvetica", "bold");
this.doc.text("Fitness Profile Changes", this.margin.left, this.currentY);
this.currentY += 8;
if (history.length === 0) {
this.doc.setTextColor(...this.colors.textLight);
this.doc.setFontSize(10);
this.doc.setFont("helvetica", "italic");
this.doc.text(
"No profile changes recorded during this period.",
this.margin.left,
this.currentY,
);
this.currentY += 15;
return;
}
const historyData = history.map((item) => [
item.fieldName,
item.changeType,
item.previousValue || "N/A",
item.newValue || "N/A",
new Date(item.changedAt).toLocaleDateString(),
]);
autoTable(this.doc, {
startY: this.currentY,
head: [["Field", "Change Type", "Previous", "New", "Date"]],
body: historyData,
theme: "striped",
headStyles: {
fillColor: this.colors.primary,
textColor: this.colors.white,
fontStyle: "bold",
fontSize: 8,
},
bodyStyles: {
fontSize: 8,
textColor: this.colors.text,
},
alternateRowStyles: {
fillColor: [250, 250, 250],
},
margin: { left: this.margin.left, right: this.margin.right },
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 15;
}
/**
* Add recommendations section
*/
private addRecommendations(
recommendations: UserReport["recommendations"],
): void {
this.checkPageBreak(60);
this.doc.setTextColor(...this.colors.primary);
this.doc.setFontSize(14);
this.doc.setFont("helvetica", "bold");
this.doc.text("AI Recommendations", this.margin.left, this.currentY);
this.currentY += 8;
const summaryStats = [
["Accepted", recommendations.totalAccepted.toString()],
["Rejected", recommendations.totalRejected.toString()],
["Pending", recommendations.totalPending.toString()],
];
autoTable(this.doc, {
startY: this.currentY,
body: summaryStats,
theme: "plain",
margin: { left: this.margin.left, right: this.margin.right },
columnStyles: {
0: { fontStyle: "bold", cellWidth: 60, fontSize: 10 },
1: { cellWidth: "auto", fontSize: 10 },
},
styles: {
cellPadding: 3,
textColor: this.colors.text,
},
tableWidth: "auto" as any,
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 10;
// Add accepted recommendations
if (recommendations.accepted.length > 0) {
this.doc.setTextColor(...this.colors.success);
this.doc.setFontSize(10);
this.doc.setFont("helvetica", "bold");
this.doc.text(
"Accepted Recommendations",
this.margin.left,
this.currentY,
);
this.currentY += 5;
const acceptedRecs = recommendations.accepted.map((rec) => [
rec.recommendationText.substring(0, 80) +
(rec.recommendationText.length > 80 ? "..." : ""),
new Date(rec.generatedAt).toLocaleDateString(),
]);
autoTable(this.doc, {
startY: this.currentY,
head: [["Recommendation", "Generated"]],
body: acceptedRecs,
theme: "striped",
headStyles: {
fillColor: this.colors.success,
textColor: this.colors.white,
fontStyle: "bold",
fontSize: 8,
},
bodyStyles: {
fontSize: 8,
textColor: this.colors.text,
},
alternateRowStyles: {
fillColor: [250, 250, 250],
},
margin: { left: this.margin.left, right: this.margin.right },
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 10;
}
// Add pending recommendations
if (recommendations.pending.length > 0) {
this.doc.setTextColor(...this.colors.warning);
this.doc.setFontSize(10);
this.doc.setFont("helvetica", "bold");
this.doc.text("Pending Recommendations", this.margin.left, this.currentY);
this.currentY += 5;
const pendingRecs = recommendations.pending.map((rec) => [
rec.recommendationText.substring(0, 80) +
(rec.recommendationText.length > 80 ? "..." : ""),
new Date(rec.generatedAt).toLocaleDateString(),
]);
autoTable(this.doc, {
startY: this.currentY,
head: [["Recommendation", "Generated"]],
body: pendingRecs,
theme: "striped",
headStyles: {
fillColor: this.colors.warning,
textColor: this.colors.text,
fontStyle: "bold",
fontSize: 8,
},
bodyStyles: {
fontSize: 8,
textColor: this.colors.text,
},
alternateRowStyles: {
fillColor: [250, 250, 250],
},
margin: { left: this.margin.left, right: this.margin.right },
});
this.currentY = (this.doc as any).lastAutoTable.finalY + 15;
}
}
/**
* Add footer to each page
*/
private addFooter(): void {
const pageCount = this.doc.getNumberOfPages();
for (let i = 1; i <= pageCount; i++) {
this.doc.setPage(i);
// Footer line
this.doc.setDrawColor(...this.colors.primary);
this.doc.setLineWidth(0.5);
this.doc.line(
this.margin.left,
this.pageHeight - 15,
this.pageWidth - this.margin.right,
this.pageHeight - 15,
);
// Footer text
this.doc.setTextColor(...this.colors.textLight);
this.doc.setFontSize(8);
this.doc.setFont("helvetica", "normal");
this.doc.text(
`FitAI User Report - Page ${i} of ${pageCount}`,
this.margin.left,
this.pageHeight - 10,
);
this.doc.text(
"Generated by FitAI",
this.pageWidth - this.margin.right - 30,
this.pageHeight - 10,
);
}
}
/**
* Check if we need a page break
*/
private checkPageBreak(requiredSpace: number): void {
if (this.currentY + requiredSpace > this.pageHeight - this.margin.bottom) {
this.doc.addPage();
this.currentY = this.margin.top;
}
}
}
export default PDFGenerator;
+35
View File
@@ -0,0 +1,35 @@
import type { UserReport } from "@fitai/shared";
import PDFGenerator from "./pdf-generator";
/**
* Generate a PDF report from a UserReport object
*/
export function generateReportPDF(report: UserReport): Blob {
const generator = new PDFGenerator();
generator.generateUserReport(report);
return generator.toBlob();
}
/**
* Generate a PDF report and return as base64 string
*/
export function generateReportPDFBase64(report: UserReport): string {
const generator = new PDFGenerator();
generator.generateUserReport(report);
return generator.toBase64();
}
/**
* Generate a PDF report and save to file
*/
export function saveReportPDF(report: UserReport, filename: string): void {
const generator = new PDFGenerator();
generator.generateUserReport(report);
generator.save(filename);
}
export default {
generateReportPDF,
generateReportPDFBase64,
saveReportPDF,
};
+153
View File
@@ -0,0 +1,153 @@
/**
* ISO Week Calculation Utilities
*
* Helper functions for ISO 8601 week calculations
* Week starts on Monday and ends on Sunday
*/
/**
* Get the Monday of the ISO week for a given date
*/
export function getWeekStart(date: Date): Date {
const d = new Date(date);
const day = d.getDay();
const diff = d.getDate() - day + (day === 0 ? -6 : 1);
d.setDate(diff);
d.setHours(0, 0, 0, 0);
return d;
}
/**
* Get the Sunday of the ISO week for a given date
*/
export function getWeekEnd(date: Date): Date {
const monday = getWeekStart(date);
const sunday = new Date(monday);
sunday.setDate(sunday.getDate() + 6);
sunday.setHours(23, 59, 59, 999);
return sunday;
}
/**
* Get ISO week number (1-53)
*/
export function getISOWeek(date: Date): number {
const d = new Date(
Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()),
);
const dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
}
/**
* Format date as YYYY-MM-DD
*/
export function formatDate(date: Date): string {
return date.toISOString().split("T")[0];
}
/**
* Get all weeks in a date range
*/
export function getWeeksInRange(
startDate: string,
endDate: string,
): Array<{
weekStart: string;
weekEnd: string;
weekNumber: number;
}> {
const weeks: Array<{
weekStart: string;
weekEnd: string;
weekNumber: number;
}> = [];
const start = new Date(startDate);
const end = new Date(endDate);
const current = getWeekStart(start);
while (current <= end) {
const weekStart = getWeekStart(current);
const weekEnd = getWeekEnd(current);
weeks.push({
weekStart: formatDate(weekStart),
weekEnd: formatDate(weekEnd),
weekNumber: getISOWeek(current),
});
current.setDate(current.getDate() + 7);
}
return weeks;
}
/**
* Test ISO week calculations
*/
export function testISOWeekCalculations(): void {
console.log("ISO Week Calculation Tests\n");
const testDates = [
{
date: "2024-01-01",
expected: { monday: "2024-01-01", sunday: "2024-01-07", week: 1 },
},
{
date: "2024-01-02",
expected: { monday: "2024-01-01", sunday: "2024-01-07", week: 1 },
},
{
date: "2024-01-07",
expected: { monday: "2024-01-01", sunday: "2024-01-07", week: 1 },
},
{
date: "2024-01-08",
expected: { monday: "2024-01-08", sunday: "2024-01-14", week: 2 },
},
{
date: "2024-12-31",
expected: { monday: "2024-12-30", sunday: "2025-01-05", week: 1 },
}, // Week 1 of 2025
];
testDates.forEach(({ date, expected }) => {
const d = new Date(date);
const monday = formatDate(getWeekStart(d));
const sunday = formatDate(getWeekEnd(d));
const week = getISOWeek(d);
const mondayMatch = monday === expected.monday;
const sundayMatch = sunday === expected.sunday;
const weekMatch = week === expected.week;
console.log(`Date: ${date}`);
console.log(
` Monday: ${monday} ${mondayMatch ? "✓" : "✗ (expected: " + expected.monday + ")"}`,
);
console.log(
` Sunday: ${sunday} ${sundayMatch ? "✓" : "✗ (expected: " + expected.sunday + ")"}`,
);
console.log(
` Week: ${week} ${weekMatch ? "✓" : "✗ (expected: " + expected.week + ")"}`,
);
console.log();
});
}
// Run tests if executed directly
if (typeof require !== "undefined" && require.main === module) {
testISOWeekCalculations();
}
export default {
getWeekStart,
getWeekEnd,
getISOWeek,
formatDate,
getWeeksInRange,
testISOWeekCalculations,
};