Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3ec1460c4e | |||
| f56f2b7c88 | |||
| 8ff652459d | |||
| fb937e795b | |||
| 6d6f906a9a | |||
| 3de1b2ef0c | |||
| b07b586eef | |||
| ffcd8983b4 | |||
| cdf0088b0f | |||
| 152608731b | |||
| 291a90d401 | |||
| 8c5deed4c2 | |||
| b19bcf02c2 | |||
| 27a9910dbd | |||
| eb2e445398 | |||
| 61b8812808 | |||
| f7838bd919 | |||
| b0fd3f4187 |
+2
-1
@@ -118,4 +118,5 @@ EXPIRY_WARNING_DAYS=30 # Days before expiry to show yellow warning
|
|||||||
|
|
||||||
# UI defaults
|
# UI defaults
|
||||||
# DEFAULT_LANGUAGE=en # en or de
|
# DEFAULT_LANGUAGE=en # en or de
|
||||||
# DEFAULT_STOCK_CALCULATION_MODE=automatic # automatic or manual
|
# DEFAULT_STOCK_CALCULATION_MODE=automatic # automatic or manual
|
||||||
|
# DEFAULT_SHARE_STOCK_STATUS=true # Show stock status on shared schedule links
|
||||||
@@ -18,6 +18,38 @@ You are the release manager for **MedAssist-ng**. Your job is to guide code from
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## PR Strategy: One PR per Feature/Fix
|
||||||
|
|
||||||
|
**Each feature or bug fix MUST be submitted as its own separate PR.** Do NOT bundle multiple unrelated changes into a single PR.
|
||||||
|
|
||||||
|
**Why:**
|
||||||
|
- Each change gets its own PR number for release notes (e.g., `(#140)`, `(#141)`)
|
||||||
|
- CI tests each change in isolation — failures are easy to trace
|
||||||
|
- Git blame and rollbacks are precise
|
||||||
|
- Code review stays focused
|
||||||
|
|
||||||
|
**Rules:**
|
||||||
|
- One logical change = one branch = one PR
|
||||||
|
- If a bug fix is discovered while working on a feature, create a **separate branch and PR** for the fix
|
||||||
|
- Related changes (e.g., a feature + its tests) belong in the **same** PR
|
||||||
|
- Squash-merge is still used — keeps `main` history clean with one commit per PR
|
||||||
|
- Branch naming reflects the change: `fix/bottle-stock-calc`, `feat/theme-dropdown`, etc.
|
||||||
|
|
||||||
|
**Example — bad (bundled):**
|
||||||
|
```
|
||||||
|
PR #138: "feat: theme dropdown, fix bottle bugs, fix planner, fix reminders"
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example — good (separate):**
|
||||||
|
```
|
||||||
|
PR #138: "fix: bottle-type stock calculations across all subsystems"
|
||||||
|
PR #139: "fix: intake reminder past-intake seeding"
|
||||||
|
PR #140: "feat: theme dropdown with Light/Dark/System options"
|
||||||
|
PR #141: "fix: planner checkbox layout on single line"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Task 1: Branch, PR, and Merge Workflow
|
## Task 1: Branch, PR, and Merge Workflow
|
||||||
|
|
||||||
When code changes (features or bug fixes) are complete and tested locally:
|
When code changes (features or bug fixes) are complete and tested locally:
|
||||||
@@ -179,8 +211,8 @@ The version number is displayed in the **About modal** (Settings → About) as a
|
|||||||
|
|
||||||
### After Tagging
|
### After Tagging
|
||||||
|
|
||||||
- The `docker-build.yml` workflow automatically builds and pushes Docker images to GHCR.
|
- The `docker-build.yml` workflow automatically builds and pushes Docker images to GHCR with both versioned tags (`1.8.7`, `1.8`) and `latest`.
|
||||||
- The `version-bump.yml` workflow automatically updates `package.json` versions if needed.
|
- The `update-test-badges.yml` workflow runs automatically after a successful Docker build to update test count badges in the README.
|
||||||
- Track progress: `https://github.com/DanielVolz/medassist-ng/actions`
|
- Track progress: `https://github.com/DanielVolz/medassist-ng/actions`
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -212,19 +244,20 @@ Read the actual code changes (not just commit messages) to understand what was a
|
|||||||
- Use `### Heading` for sections
|
- Use `### Heading` for sections
|
||||||
- Use **bold** for feature names in bullet points
|
- Use **bold** for feature names in bullet points
|
||||||
- Keep descriptions on the same line as the feature name
|
- Keep descriptions on the same line as the feature name
|
||||||
- Minimal emoji usage (sparingly, not on every line)
|
- **No emojis** — do not use emoji in headings or bullet points
|
||||||
|
- **Include commit references** — each bullet point must end with the PR number (e.g., `(#136)`) or short commit hash (e.g., `(ab12cd3)`) linking to the commit/PR. Use PR numbers when available.
|
||||||
- Always end with "Where to Find It" section
|
- Always end with "Where to Find It" section
|
||||||
- End with: `**Full Changelog**: https://github.com/DanielVolz/medassist-ng/compare/vPREV...vNEW`
|
- End with: `**Full Changelog**: https://github.com/DanielVolz/medassist-ng/compare/vPREV...vNEW`
|
||||||
|
|
||||||
**ONLY include user-relevant changes.** DO NOT include:
|
**ONLY include user-relevant changes.** DO NOT include:
|
||||||
- ❌ Technical implementation details (new columns, endpoints, database changes)
|
- Technical implementation details (new columns, endpoints, database changes)
|
||||||
- ❌ Number of tests added
|
- Number of tests added
|
||||||
- ❌ Internal API changes (unless breaking)
|
- Internal API changes (unless breaking)
|
||||||
- ❌ Excessive emoji on every bullet point
|
- Emojis anywhere in the release notes
|
||||||
- ❌ .gitignore changes or other developer-only file changes
|
- .gitignore changes or other developer-only file changes
|
||||||
- ❌ AI/Copilot instruction updates
|
- AI/Copilot instruction updates
|
||||||
- ❌ CI/CD workflow changes (unless affecting users)
|
- CI/CD workflow changes (unless affecting users)
|
||||||
- ❌ Code refactoring without user-visible changes
|
- Code refactoring without user-visible changes
|
||||||
|
|
||||||
### Example: Good Release Notes
|
### Example: Good Release Notes
|
||||||
|
|
||||||
@@ -235,14 +268,14 @@ This release introduces a medication refill tracking feature and improves the mo
|
|||||||
|
|
||||||
### New Features
|
### New Features
|
||||||
|
|
||||||
- **Medication Refill**: Track when you refill your medications with a single click. Add full packs or individual pills and view complete refill history.
|
- **Medication Refill**: Track when you refill your medications with a single click. Add full packs or individual pills and view complete refill history. (#120)
|
||||||
- **Automatic Stock Updates**: Stock levels are automatically recalculated after each refill.
|
- **Automatic Stock Updates**: Stock levels are automatically recalculated after each refill. (#120)
|
||||||
- **Refill History**: Each medication shows a complete history of all refills with timestamps.
|
- **Refill History**: Each medication shows a complete history of all refills with timestamps. (#122)
|
||||||
|
|
||||||
### Mobile Improvements
|
### Improvements
|
||||||
|
|
||||||
- **Centered Tooltips**: Info tooltips now display centered on screen for better readability.
|
- **Centered Tooltips**: Info tooltips now display centered on screen for better readability. (#125)
|
||||||
- **Touch-friendly**: Tooltips close automatically when scrolling on touch devices.
|
- **Touch-friendly**: Tooltips close automatically when scrolling on touch devices. (#125)
|
||||||
|
|
||||||
### Where to Find It
|
### Where to Find It
|
||||||
|
|
||||||
@@ -294,6 +327,30 @@ gh release create vX.Y.Z --title "vX.Y.Z" --notes "RELEASE_NOTES_HERE"
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Task 5: README Update Check (MANDATORY for new features)
|
||||||
|
|
||||||
|
When the release includes **new features** (minor or major version bump), you MUST check whether the `README.md` needs to be updated **before** executing the release.
|
||||||
|
|
||||||
|
### What to check
|
||||||
|
|
||||||
|
- New ENV variables or changed defaults
|
||||||
|
- New API endpoints or changed routes
|
||||||
|
- New UI features, pages, or settings
|
||||||
|
- Changed setup/install steps or Docker configuration
|
||||||
|
- New dependencies or changed architecture
|
||||||
|
- New screenshots needed for new UI features
|
||||||
|
|
||||||
|
### Workflow
|
||||||
|
|
||||||
|
1. Review the changes included in the release
|
||||||
|
2. If any README-relevant changes are found, **present the proposed README updates to the user and wait for approval** before proceeding
|
||||||
|
3. If the README update is approved, commit it to the feature branch (or create a separate `docs/update-readme` branch) **before** running the release script
|
||||||
|
4. Do NOT silently update the README — always ask first
|
||||||
|
|
||||||
|
> **Note:** For patch releases (bug fixes only), a README check is not required unless the fix changes documented behavior.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Complete Workflow Summary
|
## Complete Workflow Summary
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -308,11 +365,12 @@ Ready for release?
|
|||||||
↓
|
↓
|
||||||
5. Check current version (git tag + package.json)
|
5. Check current version (git tag + package.json)
|
||||||
6. Analyze changes → determine SemVer level
|
6. Analyze changes → determine SemVer level
|
||||||
7. Run ./scripts/release.sh <patch|minor|major>
|
7. If minor/major: check README.md for needed updates (Task 5)
|
||||||
|
8. Run ./scripts/release.sh <patch|minor|major>
|
||||||
(or manually: branch → version bump → PR → CI → merge → tag)
|
(or manually: branch → version bump → PR → CI → merge → tag)
|
||||||
↓
|
↓
|
||||||
8. Write release notes (mandatory for minor/major)
|
9. Write release notes (mandatory for minor/major)
|
||||||
9. Publish GitHub release
|
10. Publish GitHub release
|
||||||
↓
|
↓
|
||||||
Docker images built automatically via CI
|
Docker images built automatically via CI
|
||||||
```
|
```
|
||||||
@@ -138,11 +138,16 @@ Push to main / Tag created
|
|||||||
↓
|
↓
|
||||||
┌─────────────────────────────────────┐
|
┌─────────────────────────────────────┐
|
||||||
│ docker-build.yml │
|
│ docker-build.yml │
|
||||||
│ ├─ backend-test (parallel) │
|
│ └─ build-and-push │
|
||||||
│ ├─ frontend-build (parallel) │
|
|
||||||
│ └─ build-and-push (after tests) │
|
|
||||||
│ ├─ Build Docker images │
|
│ ├─ Build Docker images │
|
||||||
│ └─ Push to GHCR │
|
│ └─ Push to GHCR │
|
||||||
|
│ (Tag builds also set "latest") │
|
||||||
|
└─────────────────────────────────────┘
|
||||||
|
↓ After successful build
|
||||||
|
┌─────────────────────────────────────┐
|
||||||
|
│ update-test-badges.yml │
|
||||||
|
│ (workflow_run after docker-build) │
|
||||||
|
│ └─ Run tests, update badge counts │
|
||||||
└─────────────────────────────────────┘
|
└─────────────────────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -179,7 +184,9 @@ gh pr merge --squash --delete-branch
|
|||||||
| File | Trigger | Purpose |
|
| File | Trigger | Purpose |
|
||||||
|------|---------|--------|
|
|------|---------|--------|
|
||||||
| `.github/workflows/test.yml` | Pull Requests | Run tests, block PR on failures |
|
| `.github/workflows/test.yml` | Pull Requests | Run tests, block PR on failures |
|
||||||
| `.github/workflows/docker-build.yml` | Push to main, Tags | Tests + Build and push Docker images |
|
| `.github/workflows/docker-build.yml` | Push to main, Tags | Build and push Docker images (+ create GitHub release on tags) |
|
||||||
|
| `.github/workflows/update-test-badges.yml` | After successful docker-build | Update test count badges in README |
|
||||||
|
| `.github/workflows/codeql.yml` | Push to main, PRs, Weekly | Security analysis |
|
||||||
|
|
||||||
## Key Patterns
|
## Key Patterns
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,6 @@ name: Build and Push Docker Images
|
|||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [main]
|
branches: [main]
|
||||||
paths:
|
|
||||||
- 'backend/**'
|
|
||||||
- 'frontend/**'
|
|
||||||
- 'docker-compose*.yml'
|
|
||||||
- '.github/workflows/docker-build.yml'
|
|
||||||
tags: ['v*']
|
tags: ['v*']
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
@@ -26,9 +21,13 @@ env:
|
|||||||
jobs:
|
jobs:
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
# Build and Push Docker Images
|
# Build and Push Docker Images
|
||||||
|
# Triggered on pushes to main (tagged as "main") and version tags (v*).
|
||||||
# Tests are NOT run here — branch protection on main requires all PR checks
|
# Tests are NOT run here — branch protection on main requires all PR checks
|
||||||
# (backend-test + frontend-build from test.yml) to pass before merge.
|
# (backend-test + frontend-build from test.yml) to pass before merge.
|
||||||
# Tags are created from main, so code is already tested.
|
# Tags are created from main, so code is already tested.
|
||||||
|
#
|
||||||
|
# main push → "main" tag only (for testing before release)
|
||||||
|
# Tag builds → semver tags (e.g., 1.9.0, 1.9) plus "latest"
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
build-and-push:
|
build-and-push:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -68,7 +67,7 @@ jobs:
|
|||||||
type=semver,pattern={{version}}
|
type=semver,pattern={{version}}
|
||||||
type=semver,pattern={{major}}.{{minor}}
|
type=semver,pattern={{major}}.{{minor}}
|
||||||
type=raw,value=${{ github.event.inputs.tag || 'latest' }},enable=${{ github.event_name == 'workflow_dispatch' }}
|
type=raw,value=${{ github.event.inputs.tag || 'latest' }},enable=${{ github.event_name == 'workflow_dispatch' }}
|
||||||
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
|
type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
|
||||||
|
|
||||||
- name: Build and push
|
- name: Build and push
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v5
|
||||||
|
|||||||
@@ -1,78 +0,0 @@
|
|||||||
name: Create Release
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
tags: ['v*']
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
release:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- name: Get version info
|
|
||||||
id: version
|
|
||||||
run: |
|
|
||||||
CURRENT_TAG=${GITHUB_REF#refs/tags/}
|
|
||||||
VERSION=${CURRENT_TAG#v}
|
|
||||||
echo "tag=$CURRENT_TAG" >> $GITHUB_OUTPUT
|
|
||||||
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
|
||||||
|
|
||||||
# Get previous tag
|
|
||||||
PREV_TAG=$(git tag --sort=-v:refname | grep -A1 "^${CURRENT_TAG}$" | tail -1)
|
|
||||||
if [ "$PREV_TAG" = "$CURRENT_TAG" ]; then
|
|
||||||
PREV_TAG=""
|
|
||||||
fi
|
|
||||||
echo "previous_tag=$PREV_TAG" >> $GITHUB_OUTPUT
|
|
||||||
|
|
||||||
- name: Generate release template
|
|
||||||
run: |
|
|
||||||
cat > release_notes.md << 'EOF'
|
|
||||||
## What's New
|
|
||||||
|
|
||||||
<!--
|
|
||||||
Write 1-2 sentences describing the main changes in this release.
|
|
||||||
Example: This release introduces a medication refill tracking feature and improves the mobile user experience.
|
|
||||||
-->
|
|
||||||
|
|
||||||
### New Features
|
|
||||||
|
|
||||||
<!-- List new features with **bold** names and descriptions -->
|
|
||||||
- **Feature Name**: Description of the feature
|
|
||||||
|
|
||||||
### Improvements
|
|
||||||
|
|
||||||
<!-- List improvements and fixes -->
|
|
||||||
- **Improvement**: Description
|
|
||||||
|
|
||||||
### Where to Find It
|
|
||||||
|
|
||||||
<!-- Tell users where they can access new features -->
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Docker Images
|
|
||||||
|
|
||||||
```bash
|
|
||||||
docker pull ghcr.io/danielvolz/medassist-ng-backend:${{ steps.version.outputs.version }}
|
|
||||||
docker pull ghcr.io/danielvolz/medassist-ng-frontend:${{ steps.version.outputs.version }}
|
|
||||||
```
|
|
||||||
|
|
||||||
**Full Changelog**: https://github.com/DanielVolz/medassist-ng/compare/${{ steps.version.outputs.previous_tag }}...${{ steps.version.outputs.tag }}
|
|
||||||
EOF
|
|
||||||
|
|
||||||
- name: Create Draft Release
|
|
||||||
uses: softprops/action-gh-release@v1
|
|
||||||
with:
|
|
||||||
body_path: release_notes.md
|
|
||||||
draft: true
|
|
||||||
generate_release_notes: false
|
|
||||||
name: "Release ${{ steps.version.outputs.tag }}"
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
@@ -2,13 +2,10 @@ name: Update Test Badges
|
|||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
push:
|
workflow_run:
|
||||||
|
workflows: ["Build and Push Docker Images"]
|
||||||
|
types: [completed]
|
||||||
branches: [main]
|
branches: [main]
|
||||||
paths:
|
|
||||||
- 'backend/src/**'
|
|
||||||
- 'frontend/src/**'
|
|
||||||
- 'backend/package.json'
|
|
||||||
- 'frontend/package.json'
|
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
@@ -17,6 +14,8 @@ jobs:
|
|||||||
update-badges:
|
update-badges:
|
||||||
name: Update Test Count Badges
|
name: Update Test Count Badges
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
# Only run after successful docker builds, not failed ones
|
||||||
|
if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
|
|||||||
@@ -1,57 +0,0 @@
|
|||||||
name: Version Bump on Release
|
|
||||||
|
|
||||||
on:
|
|
||||||
release:
|
|
||||||
types: [published]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
version-bump:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
ref: main
|
|
||||||
token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Get version from tag
|
|
||||||
id: version
|
|
||||||
run: |
|
|
||||||
# Extract version from tag (e.g., v1.6.0 -> 1.6.0)
|
|
||||||
VERSION="${GITHUB_REF_NAME#v}"
|
|
||||||
echo "version=$VERSION" >> $GITHUB_OUTPUT
|
|
||||||
echo "Extracted version: $VERSION"
|
|
||||||
|
|
||||||
- name: Update package.json versions
|
|
||||||
run: |
|
|
||||||
VERSION="${{ steps.version.outputs.version }}"
|
|
||||||
|
|
||||||
# Update backend/package.json
|
|
||||||
jq --arg v "$VERSION" '.version = $v' backend/package.json > backend/package.json.tmp
|
|
||||||
mv backend/package.json.tmp backend/package.json
|
|
||||||
|
|
||||||
# Update frontend/package.json
|
|
||||||
jq --arg v "$VERSION" '.version = $v' frontend/package.json > frontend/package.json.tmp
|
|
||||||
mv frontend/package.json.tmp frontend/package.json
|
|
||||||
|
|
||||||
echo "Updated versions to $VERSION"
|
|
||||||
cat backend/package.json | head -5
|
|
||||||
cat frontend/package.json | head -5
|
|
||||||
|
|
||||||
- name: Commit and push
|
|
||||||
run: |
|
|
||||||
git config user.name "github-actions[bot]"
|
|
||||||
git config user.email "github-actions[bot]@users.noreply.github.com"
|
|
||||||
|
|
||||||
git add backend/package.json frontend/package.json
|
|
||||||
|
|
||||||
# Only commit if there are changes
|
|
||||||
if git diff --staged --quiet; then
|
|
||||||
echo "No version changes needed"
|
|
||||||
else
|
|
||||||
git commit -m "chore: bump version to ${{ steps.version.outputs.version }} [skip ci]"
|
|
||||||
git push origin main
|
|
||||||
fi
|
|
||||||
@@ -18,8 +18,8 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<img src="https://img.shields.io/badge/Backend_Tests-494%2F494-brightgreen?logo=vitest" alt="Backend Tests 454/454" />
|
<img src="https://img.shields.io/badge/Backend_Tests-504%2F504-brightgreen?logo=vitest" alt="Backend Tests 454/454" />
|
||||||
<img src="https://img.shields.io/badge/Frontend_Tests-639%2F639-brightgreen?logo=vitest" alt="Frontend Tests 611/611" />
|
<img src="https://img.shields.io/badge/Frontend_Tests-662%2F662-brightgreen?logo=vitest" alt="Frontend Tests 611/611" />
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
### 🤖 AI-Generated Code
|
### 🤖 AI-Generated Code
|
||||||
@@ -120,7 +120,7 @@ Share your medication schedule with others via a public link.
|
|||||||
</details>
|
</details>
|
||||||
|
|
||||||
### Smart Inventory
|
### Smart Inventory
|
||||||
- Track exact stock: packs, blisters, and loose pills
|
- Track exact stock: packs, blisters, bottles, and loose pills
|
||||||
- Display remaining days of supply
|
- Display remaining days of supply
|
||||||
- Automatic calculation based on intake schedule
|
- Automatic calculation based on intake schedule
|
||||||
|
|
||||||
@@ -141,6 +141,7 @@ Share your medication schedule with others via a public link.
|
|||||||
### Trip Planner
|
### Trip Planner
|
||||||
- Calculate how many pills you need for a trip or date range
|
- Calculate how many pills you need for a trip or date range
|
||||||
- Plan ahead for vacations, business trips, or hospital stays
|
- Plan ahead for vacations, business trips, or hospital stays
|
||||||
|
- Send demand reports via email or push notification
|
||||||
|
|
||||||
### Multi-Person Support
|
### Multi-Person Support
|
||||||
- Manage medications for multiple people
|
- Manage medications for multiple people
|
||||||
@@ -254,6 +255,14 @@ Configure push notifications in Settings → Push, or set defaults via environme
|
|||||||
| `DEFAULT_SHOUTRRR_STOCK_REMINDERS` | `true` | Send stock warnings via push |
|
| `DEFAULT_SHOUTRRR_STOCK_REMINDERS` | `true` | Send stock warnings via push |
|
||||||
| `DEFAULT_SHOUTRRR_INTAKE_REMINDERS` | `true` | Send intake reminders via push |
|
| `DEFAULT_SHOUTRRR_INTAKE_REMINDERS` | `true` | Send intake reminders via push |
|
||||||
|
|
||||||
|
### Default User Settings
|
||||||
|
|
||||||
|
These defaults are applied when a new user is created. Once a user saves settings in the app, their values take precedence.
|
||||||
|
|
||||||
|
| Variable | Default | Description |
|
||||||
|
|----------|---------|-------------|
|
||||||
|
| `DEFAULT_SHARE_STOCK_STATUS` | `true` | Show stock status (Normal/Low/Critical) on shared schedule links |
|
||||||
|
|
||||||
#### URL Examples
|
#### URL Examples
|
||||||
|
|
||||||
**ntfy** (free, self-hostable):
|
**ntfy** (free, self-hostable):
|
||||||
|
|||||||
@@ -5,6 +5,6 @@ export default defineConfig({
|
|||||||
out: "./drizzle",
|
out: "./drizzle",
|
||||||
dialect: "sqlite",
|
dialect: "sqlite",
|
||||||
dbCredentials: {
|
dbCredentials: {
|
||||||
url: process.env.DATABASE_URL || "./data/medassist.db",
|
url: process.env.DATABASE_URL || "./data/medassist-ng.db",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
ALTER TABLE `user_settings` ADD `last_stock_reminder_sent` text;--> statement-breakpoint
|
||||||
|
ALTER TABLE `user_settings` ADD `last_stock_reminder_channel` text;--> statement-breakpoint
|
||||||
|
ALTER TABLE `user_settings` ADD `last_stock_reminder_med_names` text;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE `user_settings` ADD `share_stock_status` integer DEFAULT true NOT NULL;
|
||||||
@@ -0,0 +1,907 @@
|
|||||||
|
{
|
||||||
|
"version": "6",
|
||||||
|
"dialect": "sqlite",
|
||||||
|
"id": "7cd75e33-b3d8-4930-a60b-2a0a9f644c6d",
|
||||||
|
"prevId": "fb61e5fd-152d-4e61-8836-e2fd1d28e3f0",
|
||||||
|
"tables": {
|
||||||
|
"dose_tracking": {
|
||||||
|
"name": "dose_tracking",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"dose_id": {
|
||||||
|
"name": "dose_id",
|
||||||
|
"type": "text(255)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"taken_at": {
|
||||||
|
"name": "taken_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "(strftime('%s','now'))"
|
||||||
|
},
|
||||||
|
"marked_by": {
|
||||||
|
"name": "marked_by",
|
||||||
|
"type": "text(100)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"dismissed": {
|
||||||
|
"name": "dismissed",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {},
|
||||||
|
"foreignKeys": {
|
||||||
|
"dose_tracking_user_id_users_id_fk": {
|
||||||
|
"name": "dose_tracking_user_id_users_id_fk",
|
||||||
|
"tableFrom": "dose_tracking",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"medications": {
|
||||||
|
"name": "medications",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"name": "name",
|
||||||
|
"type": "text(100)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"generic_name": {
|
||||||
|
"name": "generic_name",
|
||||||
|
"type": "text(100)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"taken_by_json": {
|
||||||
|
"name": "taken_by_json",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'[]'"
|
||||||
|
},
|
||||||
|
"package_type": {
|
||||||
|
"name": "package_type",
|
||||||
|
"type": "text(20)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'blister'"
|
||||||
|
},
|
||||||
|
"pack_count": {
|
||||||
|
"name": "pack_count",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 1
|
||||||
|
},
|
||||||
|
"blisters_per_pack": {
|
||||||
|
"name": "blisters_per_pack",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 1
|
||||||
|
},
|
||||||
|
"pills_per_blister": {
|
||||||
|
"name": "pills_per_blister",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 1
|
||||||
|
},
|
||||||
|
"total_pills": {
|
||||||
|
"name": "total_pills",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"loose_tablets": {
|
||||||
|
"name": "loose_tablets",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 0
|
||||||
|
},
|
||||||
|
"stock_adjustment": {
|
||||||
|
"name": "stock_adjustment",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 0
|
||||||
|
},
|
||||||
|
"last_stock_correction_at": {
|
||||||
|
"name": "last_stock_correction_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"pill_weight_mg": {
|
||||||
|
"name": "pill_weight_mg",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"dose_unit": {
|
||||||
|
"name": "dose_unit",
|
||||||
|
"type": "text(20)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'mg'"
|
||||||
|
},
|
||||||
|
"usage_json": {
|
||||||
|
"name": "usage_json",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'[]'"
|
||||||
|
},
|
||||||
|
"every_json": {
|
||||||
|
"name": "every_json",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'[]'"
|
||||||
|
},
|
||||||
|
"start_json": {
|
||||||
|
"name": "start_json",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'[]'"
|
||||||
|
},
|
||||||
|
"intakes_json": {
|
||||||
|
"name": "intakes_json",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'[]'"
|
||||||
|
},
|
||||||
|
"image_url": {
|
||||||
|
"name": "image_url",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"expiry_date": {
|
||||||
|
"name": "expiry_date",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"notes": {
|
||||||
|
"name": "notes",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"intake_reminders_enabled": {
|
||||||
|
"name": "intake_reminders_enabled",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"dismissed_until": {
|
||||||
|
"name": "dismissed_until",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"updated_at": {
|
||||||
|
"name": "updated_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "CURRENT_TIMESTAMP"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {},
|
||||||
|
"foreignKeys": {
|
||||||
|
"medications_user_id_users_id_fk": {
|
||||||
|
"name": "medications_user_id_users_id_fk",
|
||||||
|
"tableFrom": "medications",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"refill_history": {
|
||||||
|
"name": "refill_history",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"medication_id": {
|
||||||
|
"name": "medication_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"packs_added": {
|
||||||
|
"name": "packs_added",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 0
|
||||||
|
},
|
||||||
|
"loose_pills_added": {
|
||||||
|
"name": "loose_pills_added",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 0
|
||||||
|
},
|
||||||
|
"refill_date": {
|
||||||
|
"name": "refill_date",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "(strftime('%s','now'))"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {},
|
||||||
|
"foreignKeys": {
|
||||||
|
"refill_history_medication_id_medications_id_fk": {
|
||||||
|
"name": "refill_history_medication_id_medications_id_fk",
|
||||||
|
"tableFrom": "refill_history",
|
||||||
|
"tableTo": "medications",
|
||||||
|
"columnsFrom": [
|
||||||
|
"medication_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
},
|
||||||
|
"refill_history_user_id_users_id_fk": {
|
||||||
|
"name": "refill_history_user_id_users_id_fk",
|
||||||
|
"tableFrom": "refill_history",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"refresh_tokens": {
|
||||||
|
"name": "refresh_tokens",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"token_id": {
|
||||||
|
"name": "token_id",
|
||||||
|
"type": "text(255)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"expires_at": {
|
||||||
|
"name": "expires_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"rotated_at": {
|
||||||
|
"name": "rotated_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"revoked": {
|
||||||
|
"name": "revoked",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"created_at": {
|
||||||
|
"name": "created_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "CURRENT_TIMESTAMP"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {
|
||||||
|
"refresh_tokens_token_id_unique": {
|
||||||
|
"name": "refresh_tokens_token_id_unique",
|
||||||
|
"columns": [
|
||||||
|
"token_id"
|
||||||
|
],
|
||||||
|
"isUnique": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"foreignKeys": {
|
||||||
|
"refresh_tokens_user_id_users_id_fk": {
|
||||||
|
"name": "refresh_tokens_user_id_users_id_fk",
|
||||||
|
"tableFrom": "refresh_tokens",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"share_tokens": {
|
||||||
|
"name": "share_tokens",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"token": {
|
||||||
|
"name": "token",
|
||||||
|
"type": "text(64)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"taken_by": {
|
||||||
|
"name": "taken_by",
|
||||||
|
"type": "text(100)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"schedule_days": {
|
||||||
|
"name": "schedule_days",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 30
|
||||||
|
},
|
||||||
|
"created_at": {
|
||||||
|
"name": "created_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "CURRENT_TIMESTAMP"
|
||||||
|
},
|
||||||
|
"expires_at": {
|
||||||
|
"name": "expires_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {
|
||||||
|
"share_tokens_token_unique": {
|
||||||
|
"name": "share_tokens_token_unique",
|
||||||
|
"columns": [
|
||||||
|
"token"
|
||||||
|
],
|
||||||
|
"isUnique": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"foreignKeys": {
|
||||||
|
"share_tokens_user_id_users_id_fk": {
|
||||||
|
"name": "share_tokens_user_id_users_id_fk",
|
||||||
|
"tableFrom": "share_tokens",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"user_settings": {
|
||||||
|
"name": "user_settings",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"email_enabled": {
|
||||||
|
"name": "email_enabled",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"notification_email": {
|
||||||
|
"name": "notification_email",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"email_stock_reminders": {
|
||||||
|
"name": "email_stock_reminders",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"email_intake_reminders": {
|
||||||
|
"name": "email_intake_reminders",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"shoutrrr_enabled": {
|
||||||
|
"name": "shoutrrr_enabled",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"shoutrrr_url": {
|
||||||
|
"name": "shoutrrr_url",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"shoutrrr_stock_reminders": {
|
||||||
|
"name": "shoutrrr_stock_reminders",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"shoutrrr_intake_reminders": {
|
||||||
|
"name": "shoutrrr_intake_reminders",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"reminder_days_before": {
|
||||||
|
"name": "reminder_days_before",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 7
|
||||||
|
},
|
||||||
|
"repeat_daily_reminders": {
|
||||||
|
"name": "repeat_daily_reminders",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"skip_reminders_for_taken_doses": {
|
||||||
|
"name": "skip_reminders_for_taken_doses",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"repeat_reminders_enabled": {
|
||||||
|
"name": "repeat_reminders_enabled",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"reminder_repeat_interval_minutes": {
|
||||||
|
"name": "reminder_repeat_interval_minutes",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 30
|
||||||
|
},
|
||||||
|
"max_nagging_reminders": {
|
||||||
|
"name": "max_nagging_reminders",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 5
|
||||||
|
},
|
||||||
|
"low_stock_days": {
|
||||||
|
"name": "low_stock_days",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 30
|
||||||
|
},
|
||||||
|
"normal_stock_days": {
|
||||||
|
"name": "normal_stock_days",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 90
|
||||||
|
},
|
||||||
|
"high_stock_days": {
|
||||||
|
"name": "high_stock_days",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 180
|
||||||
|
},
|
||||||
|
"expiry_warning_days": {
|
||||||
|
"name": "expiry_warning_days",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 90
|
||||||
|
},
|
||||||
|
"language": {
|
||||||
|
"name": "language",
|
||||||
|
"type": "text(10)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'en'"
|
||||||
|
},
|
||||||
|
"stock_calculation_mode": {
|
||||||
|
"name": "stock_calculation_mode",
|
||||||
|
"type": "text(20)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'automatic'"
|
||||||
|
},
|
||||||
|
"last_auto_email_sent": {
|
||||||
|
"name": "last_auto_email_sent",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_notification_type": {
|
||||||
|
"name": "last_notification_type",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_notification_channel": {
|
||||||
|
"name": "last_notification_channel",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_reminder_med_name": {
|
||||||
|
"name": "last_reminder_med_name",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_reminder_taken_by": {
|
||||||
|
"name": "last_reminder_taken_by",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_stock_reminder_sent": {
|
||||||
|
"name": "last_stock_reminder_sent",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_stock_reminder_channel": {
|
||||||
|
"name": "last_stock_reminder_channel",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_stock_reminder_med_names": {
|
||||||
|
"name": "last_stock_reminder_med_names",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"updated_at": {
|
||||||
|
"name": "updated_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "CURRENT_TIMESTAMP"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {
|
||||||
|
"user_settings_user_id_unique": {
|
||||||
|
"name": "user_settings_user_id_unique",
|
||||||
|
"columns": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"isUnique": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"foreignKeys": {
|
||||||
|
"user_settings_user_id_users_id_fk": {
|
||||||
|
"name": "user_settings_user_id_users_id_fk",
|
||||||
|
"tableFrom": "user_settings",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"users": {
|
||||||
|
"name": "users",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"username": {
|
||||||
|
"name": "username",
|
||||||
|
"type": "text(100)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"password_hash": {
|
||||||
|
"name": "password_hash",
|
||||||
|
"type": "text(255)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"avatar_url": {
|
||||||
|
"name": "avatar_url",
|
||||||
|
"type": "text(255)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"auth_provider": {
|
||||||
|
"name": "auth_provider",
|
||||||
|
"type": "text(50)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'local'"
|
||||||
|
},
|
||||||
|
"oidc_subject": {
|
||||||
|
"name": "oidc_subject",
|
||||||
|
"type": "text(255)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"is_active": {
|
||||||
|
"name": "is_active",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"last_login_at": {
|
||||||
|
"name": "last_login_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"created_at": {
|
||||||
|
"name": "created_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "CURRENT_TIMESTAMP"
|
||||||
|
},
|
||||||
|
"updated_at": {
|
||||||
|
"name": "updated_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "CURRENT_TIMESTAMP"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {
|
||||||
|
"users_username_unique": {
|
||||||
|
"name": "users_username_unique",
|
||||||
|
"columns": [
|
||||||
|
"username"
|
||||||
|
],
|
||||||
|
"isUnique": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"foreignKeys": {},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"views": {},
|
||||||
|
"enums": {},
|
||||||
|
"_meta": {
|
||||||
|
"schemas": {},
|
||||||
|
"tables": {},
|
||||||
|
"columns": {}
|
||||||
|
},
|
||||||
|
"internal": {
|
||||||
|
"indexes": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,915 @@
|
|||||||
|
{
|
||||||
|
"version": "6",
|
||||||
|
"dialect": "sqlite",
|
||||||
|
"id": "b6f1ee4b-cc31-4060-a4d4-bcd4fdc5bd87",
|
||||||
|
"prevId": "7cd75e33-b3d8-4930-a60b-2a0a9f644c6d",
|
||||||
|
"tables": {
|
||||||
|
"dose_tracking": {
|
||||||
|
"name": "dose_tracking",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"dose_id": {
|
||||||
|
"name": "dose_id",
|
||||||
|
"type": "text(255)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"taken_at": {
|
||||||
|
"name": "taken_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "(strftime('%s','now'))"
|
||||||
|
},
|
||||||
|
"marked_by": {
|
||||||
|
"name": "marked_by",
|
||||||
|
"type": "text(100)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"dismissed": {
|
||||||
|
"name": "dismissed",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {},
|
||||||
|
"foreignKeys": {
|
||||||
|
"dose_tracking_user_id_users_id_fk": {
|
||||||
|
"name": "dose_tracking_user_id_users_id_fk",
|
||||||
|
"tableFrom": "dose_tracking",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"medications": {
|
||||||
|
"name": "medications",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"name": "name",
|
||||||
|
"type": "text(100)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"generic_name": {
|
||||||
|
"name": "generic_name",
|
||||||
|
"type": "text(100)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"taken_by_json": {
|
||||||
|
"name": "taken_by_json",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'[]'"
|
||||||
|
},
|
||||||
|
"package_type": {
|
||||||
|
"name": "package_type",
|
||||||
|
"type": "text(20)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'blister'"
|
||||||
|
},
|
||||||
|
"pack_count": {
|
||||||
|
"name": "pack_count",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 1
|
||||||
|
},
|
||||||
|
"blisters_per_pack": {
|
||||||
|
"name": "blisters_per_pack",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 1
|
||||||
|
},
|
||||||
|
"pills_per_blister": {
|
||||||
|
"name": "pills_per_blister",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 1
|
||||||
|
},
|
||||||
|
"total_pills": {
|
||||||
|
"name": "total_pills",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"loose_tablets": {
|
||||||
|
"name": "loose_tablets",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 0
|
||||||
|
},
|
||||||
|
"stock_adjustment": {
|
||||||
|
"name": "stock_adjustment",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 0
|
||||||
|
},
|
||||||
|
"last_stock_correction_at": {
|
||||||
|
"name": "last_stock_correction_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"pill_weight_mg": {
|
||||||
|
"name": "pill_weight_mg",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"dose_unit": {
|
||||||
|
"name": "dose_unit",
|
||||||
|
"type": "text(20)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'mg'"
|
||||||
|
},
|
||||||
|
"usage_json": {
|
||||||
|
"name": "usage_json",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'[]'"
|
||||||
|
},
|
||||||
|
"every_json": {
|
||||||
|
"name": "every_json",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'[]'"
|
||||||
|
},
|
||||||
|
"start_json": {
|
||||||
|
"name": "start_json",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'[]'"
|
||||||
|
},
|
||||||
|
"intakes_json": {
|
||||||
|
"name": "intakes_json",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'[]'"
|
||||||
|
},
|
||||||
|
"image_url": {
|
||||||
|
"name": "image_url",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"expiry_date": {
|
||||||
|
"name": "expiry_date",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"notes": {
|
||||||
|
"name": "notes",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"intake_reminders_enabled": {
|
||||||
|
"name": "intake_reminders_enabled",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"dismissed_until": {
|
||||||
|
"name": "dismissed_until",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"updated_at": {
|
||||||
|
"name": "updated_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "CURRENT_TIMESTAMP"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {},
|
||||||
|
"foreignKeys": {
|
||||||
|
"medications_user_id_users_id_fk": {
|
||||||
|
"name": "medications_user_id_users_id_fk",
|
||||||
|
"tableFrom": "medications",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"refill_history": {
|
||||||
|
"name": "refill_history",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"medication_id": {
|
||||||
|
"name": "medication_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"packs_added": {
|
||||||
|
"name": "packs_added",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 0
|
||||||
|
},
|
||||||
|
"loose_pills_added": {
|
||||||
|
"name": "loose_pills_added",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 0
|
||||||
|
},
|
||||||
|
"refill_date": {
|
||||||
|
"name": "refill_date",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "(strftime('%s','now'))"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {},
|
||||||
|
"foreignKeys": {
|
||||||
|
"refill_history_medication_id_medications_id_fk": {
|
||||||
|
"name": "refill_history_medication_id_medications_id_fk",
|
||||||
|
"tableFrom": "refill_history",
|
||||||
|
"tableTo": "medications",
|
||||||
|
"columnsFrom": [
|
||||||
|
"medication_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
},
|
||||||
|
"refill_history_user_id_users_id_fk": {
|
||||||
|
"name": "refill_history_user_id_users_id_fk",
|
||||||
|
"tableFrom": "refill_history",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"refresh_tokens": {
|
||||||
|
"name": "refresh_tokens",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"token_id": {
|
||||||
|
"name": "token_id",
|
||||||
|
"type": "text(255)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"expires_at": {
|
||||||
|
"name": "expires_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"rotated_at": {
|
||||||
|
"name": "rotated_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"revoked": {
|
||||||
|
"name": "revoked",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"created_at": {
|
||||||
|
"name": "created_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "CURRENT_TIMESTAMP"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {
|
||||||
|
"refresh_tokens_token_id_unique": {
|
||||||
|
"name": "refresh_tokens_token_id_unique",
|
||||||
|
"columns": [
|
||||||
|
"token_id"
|
||||||
|
],
|
||||||
|
"isUnique": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"foreignKeys": {
|
||||||
|
"refresh_tokens_user_id_users_id_fk": {
|
||||||
|
"name": "refresh_tokens_user_id_users_id_fk",
|
||||||
|
"tableFrom": "refresh_tokens",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"share_tokens": {
|
||||||
|
"name": "share_tokens",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"token": {
|
||||||
|
"name": "token",
|
||||||
|
"type": "text(64)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"taken_by": {
|
||||||
|
"name": "taken_by",
|
||||||
|
"type": "text(100)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"schedule_days": {
|
||||||
|
"name": "schedule_days",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 30
|
||||||
|
},
|
||||||
|
"created_at": {
|
||||||
|
"name": "created_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "CURRENT_TIMESTAMP"
|
||||||
|
},
|
||||||
|
"expires_at": {
|
||||||
|
"name": "expires_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {
|
||||||
|
"share_tokens_token_unique": {
|
||||||
|
"name": "share_tokens_token_unique",
|
||||||
|
"columns": [
|
||||||
|
"token"
|
||||||
|
],
|
||||||
|
"isUnique": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"foreignKeys": {
|
||||||
|
"share_tokens_user_id_users_id_fk": {
|
||||||
|
"name": "share_tokens_user_id_users_id_fk",
|
||||||
|
"tableFrom": "share_tokens",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"user_settings": {
|
||||||
|
"name": "user_settings",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"user_id": {
|
||||||
|
"name": "user_id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"email_enabled": {
|
||||||
|
"name": "email_enabled",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"notification_email": {
|
||||||
|
"name": "notification_email",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"email_stock_reminders": {
|
||||||
|
"name": "email_stock_reminders",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"email_intake_reminders": {
|
||||||
|
"name": "email_intake_reminders",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"shoutrrr_enabled": {
|
||||||
|
"name": "shoutrrr_enabled",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"shoutrrr_url": {
|
||||||
|
"name": "shoutrrr_url",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"shoutrrr_stock_reminders": {
|
||||||
|
"name": "shoutrrr_stock_reminders",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"shoutrrr_intake_reminders": {
|
||||||
|
"name": "shoutrrr_intake_reminders",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"reminder_days_before": {
|
||||||
|
"name": "reminder_days_before",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 7
|
||||||
|
},
|
||||||
|
"repeat_daily_reminders": {
|
||||||
|
"name": "repeat_daily_reminders",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"skip_reminders_for_taken_doses": {
|
||||||
|
"name": "skip_reminders_for_taken_doses",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"repeat_reminders_enabled": {
|
||||||
|
"name": "repeat_reminders_enabled",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": false
|
||||||
|
},
|
||||||
|
"reminder_repeat_interval_minutes": {
|
||||||
|
"name": "reminder_repeat_interval_minutes",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 30
|
||||||
|
},
|
||||||
|
"max_nagging_reminders": {
|
||||||
|
"name": "max_nagging_reminders",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 5
|
||||||
|
},
|
||||||
|
"low_stock_days": {
|
||||||
|
"name": "low_stock_days",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 30
|
||||||
|
},
|
||||||
|
"normal_stock_days": {
|
||||||
|
"name": "normal_stock_days",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 90
|
||||||
|
},
|
||||||
|
"high_stock_days": {
|
||||||
|
"name": "high_stock_days",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 180
|
||||||
|
},
|
||||||
|
"expiry_warning_days": {
|
||||||
|
"name": "expiry_warning_days",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": 90
|
||||||
|
},
|
||||||
|
"language": {
|
||||||
|
"name": "language",
|
||||||
|
"type": "text(10)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'en'"
|
||||||
|
},
|
||||||
|
"stock_calculation_mode": {
|
||||||
|
"name": "stock_calculation_mode",
|
||||||
|
"type": "text(20)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'automatic'"
|
||||||
|
},
|
||||||
|
"share_stock_status": {
|
||||||
|
"name": "share_stock_status",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"last_auto_email_sent": {
|
||||||
|
"name": "last_auto_email_sent",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_notification_type": {
|
||||||
|
"name": "last_notification_type",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_notification_channel": {
|
||||||
|
"name": "last_notification_channel",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_reminder_med_name": {
|
||||||
|
"name": "last_reminder_med_name",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_reminder_taken_by": {
|
||||||
|
"name": "last_reminder_taken_by",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_stock_reminder_sent": {
|
||||||
|
"name": "last_stock_reminder_sent",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_stock_reminder_channel": {
|
||||||
|
"name": "last_stock_reminder_channel",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"last_stock_reminder_med_names": {
|
||||||
|
"name": "last_stock_reminder_med_names",
|
||||||
|
"type": "text",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"updated_at": {
|
||||||
|
"name": "updated_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "CURRENT_TIMESTAMP"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {
|
||||||
|
"user_settings_user_id_unique": {
|
||||||
|
"name": "user_settings_user_id_unique",
|
||||||
|
"columns": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"isUnique": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"foreignKeys": {
|
||||||
|
"user_settings_user_id_users_id_fk": {
|
||||||
|
"name": "user_settings_user_id_users_id_fk",
|
||||||
|
"tableFrom": "user_settings",
|
||||||
|
"tableTo": "users",
|
||||||
|
"columnsFrom": [
|
||||||
|
"user_id"
|
||||||
|
],
|
||||||
|
"columnsTo": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"onDelete": "cascade",
|
||||||
|
"onUpdate": "no action"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
},
|
||||||
|
"users": {
|
||||||
|
"name": "users",
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": true,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": true
|
||||||
|
},
|
||||||
|
"username": {
|
||||||
|
"name": "username",
|
||||||
|
"type": "text(100)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"password_hash": {
|
||||||
|
"name": "password_hash",
|
||||||
|
"type": "text(255)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"avatar_url": {
|
||||||
|
"name": "avatar_url",
|
||||||
|
"type": "text(255)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"auth_provider": {
|
||||||
|
"name": "auth_provider",
|
||||||
|
"type": "text(50)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "'local'"
|
||||||
|
},
|
||||||
|
"oidc_subject": {
|
||||||
|
"name": "oidc_subject",
|
||||||
|
"type": "text(255)",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"is_active": {
|
||||||
|
"name": "is_active",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": true
|
||||||
|
},
|
||||||
|
"last_login_at": {
|
||||||
|
"name": "last_login_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": false,
|
||||||
|
"autoincrement": false
|
||||||
|
},
|
||||||
|
"created_at": {
|
||||||
|
"name": "created_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "CURRENT_TIMESTAMP"
|
||||||
|
},
|
||||||
|
"updated_at": {
|
||||||
|
"name": "updated_at",
|
||||||
|
"type": "integer",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"autoincrement": false,
|
||||||
|
"default": "CURRENT_TIMESTAMP"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"indexes": {
|
||||||
|
"users_username_unique": {
|
||||||
|
"name": "users_username_unique",
|
||||||
|
"columns": [
|
||||||
|
"username"
|
||||||
|
],
|
||||||
|
"isUnique": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"foreignKeys": {},
|
||||||
|
"compositePrimaryKeys": {},
|
||||||
|
"uniqueConstraints": {},
|
||||||
|
"checkConstraints": {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"views": {},
|
||||||
|
"enums": {},
|
||||||
|
"_meta": {
|
||||||
|
"schemas": {},
|
||||||
|
"tables": {},
|
||||||
|
"columns": {}
|
||||||
|
},
|
||||||
|
"internal": {
|
||||||
|
"indexes": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -43,6 +43,20 @@
|
|||||||
"when": 1769893708813,
|
"when": 1769893708813,
|
||||||
"tag": "0005_add_intakes_json",
|
"tag": "0005_add_intakes_json",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 6,
|
||||||
|
"version": "6",
|
||||||
|
"when": 1770626907896,
|
||||||
|
"tag": "0006_add_stock_reminder_tracking",
|
||||||
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 7,
|
||||||
|
"version": "6",
|
||||||
|
"when": 1770659669121,
|
||||||
|
"tag": "0007_add_share_stock_status",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "medassist-ng-backend",
|
"name": "medassist-ng-backend",
|
||||||
"version": "1.8.3",
|
"version": "1.9.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "medassist-ng-backend",
|
"name": "medassist-ng-backend",
|
||||||
"version": "1.8.3",
|
"version": "1.9.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fastify/cookie": "^10.0.1",
|
"@fastify/cookie": "^10.0.1",
|
||||||
"@fastify/cors": "^10.0.1",
|
"@fastify/cors": "^10.0.1",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "medassist-ng-backend",
|
"name": "medassist-ng-backend",
|
||||||
"version": "1.8.6",
|
"version": "1.9.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
+25
-25
@@ -3,7 +3,7 @@ import { resolve } from "node:path";
|
|||||||
import { type Client, createClient } from "@libsql/client";
|
import { type Client, createClient } from "@libsql/client";
|
||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
import { drizzle } from "drizzle-orm/libsql";
|
import { drizzle } from "drizzle-orm/libsql";
|
||||||
|
import { log } from "../utils/logger.js";
|
||||||
// Import utilities from db-utils (side-effect-free)
|
// Import utilities from db-utils (side-effect-free)
|
||||||
import {
|
import {
|
||||||
ensureDataDirectory,
|
ensureDataDirectory,
|
||||||
@@ -40,34 +40,34 @@ dotenv.config({ path: envPath });
|
|||||||
// Use absolute path to ensure it works in Docker
|
// Use absolute path to ensure it works in Docker
|
||||||
const { dataDir, dbPath, url } = getDbPaths();
|
const { dataDir, dbPath, url } = getDbPaths();
|
||||||
|
|
||||||
console.log(`[DB] Data directory: ${dataDir}`);
|
log.debug(`[DB] Data directory: ${dataDir}`);
|
||||||
console.log(`[DB] Database path: ${dbPath}`);
|
log.debug(`[DB] Database path: ${dbPath}`);
|
||||||
console.log(`[DB] Database URL: ${url}`);
|
log.debug(`[DB] Database URL: ${url}`);
|
||||||
|
|
||||||
// Ensure data directory exists and is writable
|
// Ensure data directory exists and is writable
|
||||||
const dirResult = ensureDataDirectory(dataDir);
|
const dirResult = ensureDataDirectory(dataDir);
|
||||||
if (!dirResult.success) {
|
if (!dirResult.success) {
|
||||||
console.error(`[DB] ERROR: Cannot access data directory: ${dirResult.error}`);
|
log.error(`[DB] ERROR: Cannot access data directory: ${dirResult.error}`);
|
||||||
console.error(`[DB] Please ensure the volume mount has correct permissions.`);
|
log.error(`[DB] Please ensure the volume mount has correct permissions.`);
|
||||||
console.error(`[DB] Try running on host: sudo chown -R 1000:1000 ${dataDir}`);
|
log.error(`[DB] Try running on host: sudo chown -R 1000:1000 ${dataDir}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
} else {
|
} else {
|
||||||
console.log(`[DB] Data directory is writable`);
|
log.debug(`[DB] Data directory is writable`);
|
||||||
|
|
||||||
// Log directory stats
|
// Log directory stats
|
||||||
const stats = statSync(dataDir);
|
const stats = statSync(dataDir);
|
||||||
console.log(`[DB] Directory permissions: ${stats.mode.toString(8)}`);
|
log.debug(`[DB] Directory permissions: ${stats.mode.toString(8)}`);
|
||||||
console.log(`[DB] Directory UID: ${stats.uid}, GID: ${stats.gid}`);
|
log.debug(`[DB] Directory UID: ${stats.uid}, GID: ${stats.gid}`);
|
||||||
console.log(`[DB] Write test successful`);
|
log.debug(`[DB] Write test successful`);
|
||||||
}
|
}
|
||||||
|
|
||||||
let client: Client;
|
let client: Client;
|
||||||
try {
|
try {
|
||||||
client = createClient({ url });
|
client = createClient({ url });
|
||||||
console.log(`[DB] Database client created successfully`);
|
log.debug(`[DB] Database client created successfully`);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error(`[DB] ERROR: Failed to create database client: ${err.message}`);
|
log.error(`[DB] ERROR: Failed to create database client: ${err.message}`);
|
||||||
console.error(`[DB] Database path: ${dbPath}`);
|
log.error(`[DB] Database path: ${dbPath}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,46 +76,46 @@ export const db = drizzle(client);
|
|||||||
// Auto-run migrations (self-healing database)
|
// Auto-run migrations (self-healing database)
|
||||||
async function runMigrations() {
|
async function runMigrations() {
|
||||||
// Run drizzle-kit generated migrations
|
// Run drizzle-kit generated migrations
|
||||||
console.log(`[DB] Running drizzle migrations...`);
|
log.info(`[DB] Running migrations...`);
|
||||||
const migrateResult = await runDrizzleMigrations(db);
|
const migrateResult = await runDrizzleMigrations(db);
|
||||||
if (!migrateResult.success) {
|
if (!migrateResult.success) {
|
||||||
console.error(`[DB] Migration error:`, migrateResult.error);
|
log.error(`[DB] Migration error: ${migrateResult.error}`);
|
||||||
} else if (migrateResult.warning) {
|
} else if (migrateResult.warning) {
|
||||||
console.log(`[DB] Migration warning:`, migrateResult.warning);
|
log.warn(`[DB] Migration warning: ${migrateResult.warning}`);
|
||||||
} else {
|
} else {
|
||||||
console.log(`[DB] Drizzle migrations completed`);
|
log.debug(`[DB] Drizzle migrations completed`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run ALTER TABLE migrations for backward compatibility
|
// Run ALTER TABLE migrations for backward compatibility
|
||||||
const alterResult = await runAlterMigrations(client);
|
const alterResult = await runAlterMigrations(client);
|
||||||
if (alterResult.errors.length > 0) {
|
if (alterResult.errors.length > 0) {
|
||||||
alterResult.errors.forEach((err) => console.error(`[DB] ALTER migration error:`, err));
|
alterResult.errors.forEach((err) => log.error(`[DB] ALTER migration error: ${err}`));
|
||||||
}
|
}
|
||||||
console.log(`[DB] Tables verified/created`);
|
log.debug(`[DB] Tables verified/created`);
|
||||||
|
|
||||||
// Repair dose IDs with trailing hyphens (from frontend takenBy bug)
|
// Repair dose IDs with trailing hyphens (from frontend takenBy bug)
|
||||||
const trailingResult = await repairTrailingHyphenDoseIds(client);
|
const trailingResult = await repairTrailingHyphenDoseIds(client);
|
||||||
if (trailingResult.repaired > 0) {
|
if (trailingResult.repaired > 0) {
|
||||||
console.log(`[DB] Repaired ${trailingResult.repaired} dose IDs with trailing hyphens`);
|
log.info(`[DB] Repaired ${trailingResult.repaired} dose IDs with trailing hyphens`);
|
||||||
}
|
}
|
||||||
if (trailingResult.errors.length > 0) {
|
if (trailingResult.errors.length > 0) {
|
||||||
trailingResult.errors.forEach((err) => console.error(`[DB] Trailing-hyphen repair error:`, err));
|
trailingResult.errors.forEach((err) => log.error(`[DB] Trailing-hyphen repair error: ${err}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Repair orphaned dose tracking IDs from past schedule changes
|
// Repair orphaned dose tracking IDs from past schedule changes
|
||||||
const repairResult = await repairOrphanedDoseIds(client);
|
const repairResult = await repairOrphanedDoseIds(client);
|
||||||
if (repairResult.repaired > 0) {
|
if (repairResult.repaired > 0) {
|
||||||
console.log(`[DB] Repaired ${repairResult.repaired} orphaned dose tracking IDs`);
|
log.info(`[DB] Repaired ${repairResult.repaired} orphaned dose tracking IDs`);
|
||||||
}
|
}
|
||||||
if (repairResult.errors.length > 0) {
|
if (repairResult.errors.length > 0) {
|
||||||
repairResult.errors.forEach((err) => console.error(`[DB] Dose repair error:`, err));
|
repairResult.errors.forEach((err) => log.error(`[DB] Dose repair error: ${err}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
// If auth is disabled, ensure a default user exists (ID=1)
|
// If auth is disabled, ensure a default user exists (ID=1)
|
||||||
const authEnabled = process.env.AUTH_ENABLED === "true";
|
const authEnabled = process.env.AUTH_ENABLED === "true";
|
||||||
const created = await ensureDefaultUser(client, authEnabled);
|
const created = await ensureDefaultUser(client, authEnabled);
|
||||||
if (created) {
|
if (created) {
|
||||||
console.log(`[DB] Created default user for auth-disabled mode`);
|
log.info(`[DB] Created default user for auth-disabled mode`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -88,10 +88,10 @@ export async function runDrizzleMigrations(
|
|||||||
await migrate(database, { migrationsFolder });
|
await migrate(database, { migrationsFolder });
|
||||||
return { success: true };
|
return { success: true };
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
// If the error is "duplicate column", it means the schema is already up-to-date
|
// If the error is about existing schema objects, the DB is already up-to-date
|
||||||
// This happens when ALTER migrations in client.ts have already added the columns
|
// This happens when ALTER migrations in client.ts have already added the columns,
|
||||||
// We consider this a success with a warning, not a failure
|
// or when tables were created before drizzle migrations were introduced
|
||||||
if (err.message?.includes("duplicate column")) {
|
if (err.message?.includes("duplicate column") || err.message?.includes("already exists")) {
|
||||||
return { success: true, warning: `Schema already up-to-date: ${err.message}` };
|
return { success: true, warning: `Schema already up-to-date: ${err.message}` };
|
||||||
}
|
}
|
||||||
return { success: false, error: err.message };
|
return { success: false, error: err.message };
|
||||||
@@ -129,6 +129,12 @@ export async function runAlterMigrations(client: Client): Promise<{ success: boo
|
|||||||
`ALTER TABLE medications ADD COLUMN dose_unit text DEFAULT 'mg'`,
|
`ALTER TABLE medications ADD COLUMN dose_unit text DEFAULT 'mg'`,
|
||||||
// Added for intake-level takenBy: unified intakes structure
|
// Added for intake-level takenBy: unified intakes structure
|
||||||
`ALTER TABLE medications ADD COLUMN intakes_json text NOT NULL DEFAULT '[]'`,
|
`ALTER TABLE medications ADD COLUMN intakes_json text NOT NULL DEFAULT '[]'`,
|
||||||
|
// Added for separate stock reminder tracking
|
||||||
|
`ALTER TABLE user_settings ADD COLUMN last_stock_reminder_sent text`,
|
||||||
|
`ALTER TABLE user_settings ADD COLUMN last_stock_reminder_channel text`,
|
||||||
|
`ALTER TABLE user_settings ADD COLUMN last_stock_reminder_med_names text`,
|
||||||
|
// Added for share stock visibility toggle
|
||||||
|
`ALTER TABLE user_settings ADD COLUMN share_stock_status integer NOT NULL DEFAULT 1`,
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const sql of alterMigrations) {
|
for (const sql of alterMigrations) {
|
||||||
|
|||||||
@@ -63,17 +63,17 @@ export function getStatementPreview(stmt: string, maxLength: number = 50): strin
|
|||||||
const url = "file:./data/medassist-ng.db";
|
const url = "file:./data/medassist-ng.db";
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log("Starting database setup...");
|
console.log("[DB] Starting database setup...");
|
||||||
console.log("Database URL:", url);
|
console.log("[DB] Database URL:", url);
|
||||||
console.log("Migrations folder:", migrationsFolder);
|
console.log("[DB] Migrations folder:", migrationsFolder);
|
||||||
|
|
||||||
const client = createClient({ url });
|
const client = createClient({ url });
|
||||||
const db = drizzle(client);
|
const db = drizzle(client);
|
||||||
|
|
||||||
console.log("Running drizzle migrations...");
|
console.log("[DB] Running drizzle migrations...");
|
||||||
await migrate(db, { migrationsFolder });
|
await migrate(db, { migrationsFolder });
|
||||||
|
|
||||||
console.log("Database setup complete!");
|
console.log("[DB] Database setup complete!");
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -86,12 +86,18 @@ export const userSettings = sqliteTable("user_settings", {
|
|||||||
language: text("language", { length: 10 }).notNull().default("en"),
|
language: text("language", { length: 10 }).notNull().default("en"),
|
||||||
// Stock calculation mode: "automatic" (schedule-based) or "manual" (only marked doses)
|
// Stock calculation mode: "automatic" (schedule-based) or "manual" (only marked doses)
|
||||||
stockCalculationMode: text("stock_calculation_mode", { length: 20 }).notNull().default("automatic"),
|
stockCalculationMode: text("stock_calculation_mode", { length: 20 }).notNull().default("automatic"),
|
||||||
// Last notification tracking
|
// Whether shared schedule links show stock status (Critical/Low/Normal) to intake users
|
||||||
|
shareStockStatus: integer("share_stock_status", { mode: "boolean" }).notNull().default(true),
|
||||||
|
// Last notification tracking (intake reminders)
|
||||||
lastAutoEmailSent: text("last_auto_email_sent"),
|
lastAutoEmailSent: text("last_auto_email_sent"),
|
||||||
lastNotificationType: text("last_notification_type"),
|
lastNotificationType: text("last_notification_type"),
|
||||||
lastNotificationChannel: text("last_notification_channel"),
|
lastNotificationChannel: text("last_notification_channel"),
|
||||||
lastReminderMedName: text("last_reminder_med_name"),
|
lastReminderMedName: text("last_reminder_med_name"),
|
||||||
lastReminderTakenBy: text("last_reminder_taken_by"),
|
lastReminderTakenBy: text("last_reminder_taken_by"),
|
||||||
|
// Last stock reminder tracking (separate from intake)
|
||||||
|
lastStockReminderSent: text("last_stock_reminder_sent"),
|
||||||
|
lastStockReminderChannel: text("last_stock_reminder_channel"),
|
||||||
|
lastStockReminderMedNames: text("last_stock_reminder_med_names"),
|
||||||
// Timestamps
|
// Timestamps
|
||||||
updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().default(sql`CURRENT_TIMESTAMP`),
|
updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().default(sql`CURRENT_TIMESTAMP`),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -64,20 +64,29 @@ function getRegionFromTimezone(): string | undefined {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TranslationKeys = {
|
type TranslationKeys = {
|
||||||
// Stock reminder email
|
// Stock reminder (shared across email + push)
|
||||||
stockReminder: {
|
stockReminder: {
|
||||||
subject: string;
|
subject: string;
|
||||||
title: string;
|
title: string;
|
||||||
description: string;
|
description: string;
|
||||||
|
descriptionEmpty: string;
|
||||||
|
descriptionMixed: string;
|
||||||
alertSingle: string;
|
alertSingle: string;
|
||||||
alertMultiple: string;
|
alertMultiple: string;
|
||||||
|
alertEmptySingle: string;
|
||||||
|
alertEmptyMultiple: string;
|
||||||
|
alertLowSingle: string;
|
||||||
|
alertLowMultiple: string;
|
||||||
|
alertLowStockSingle: string;
|
||||||
|
alertLowStockMultiple: string;
|
||||||
|
descriptionLow: string;
|
||||||
tableHeaders: {
|
tableHeaders: {
|
||||||
medication: string;
|
medication: string;
|
||||||
pills: string;
|
pills: string;
|
||||||
days: string;
|
days: string;
|
||||||
runsOut: string;
|
runsOut: string;
|
||||||
};
|
};
|
||||||
footer: string;
|
now: string;
|
||||||
repeatDailyNote: string;
|
repeatDailyNote: string;
|
||||||
};
|
};
|
||||||
// Intake reminder email
|
// Intake reminder email
|
||||||
@@ -94,7 +103,6 @@ type TranslationKeys = {
|
|||||||
};
|
};
|
||||||
pills: string;
|
pills: string;
|
||||||
takenBy: string;
|
takenBy: string;
|
||||||
footer: string;
|
|
||||||
};
|
};
|
||||||
// Push notifications
|
// Push notifications
|
||||||
push: {
|
push: {
|
||||||
@@ -107,35 +115,68 @@ type TranslationKeys = {
|
|||||||
repeatDailyNote: string;
|
repeatDailyNote: string;
|
||||||
empty: string;
|
empty: string;
|
||||||
low: string;
|
low: string;
|
||||||
|
critical: string;
|
||||||
|
lowStock: string;
|
||||||
reorderNow: string;
|
reorderNow: string;
|
||||||
emptySection: string;
|
emptySection: string;
|
||||||
lowSection: string;
|
lowSection: string;
|
||||||
|
criticalSection: string;
|
||||||
|
lowStockSection: string;
|
||||||
|
};
|
||||||
|
// Demand calculator email
|
||||||
|
demandCalculator: {
|
||||||
|
subject: string;
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
summaryOutOfStock: string;
|
||||||
|
summaryAllOk: string;
|
||||||
|
tableHeaders: {
|
||||||
|
medication: string;
|
||||||
|
usage: string;
|
||||||
|
needed: string;
|
||||||
|
available: string;
|
||||||
|
status: string;
|
||||||
|
};
|
||||||
|
statusEnough: string;
|
||||||
|
statusEmpty: string;
|
||||||
};
|
};
|
||||||
// Common
|
// Common
|
||||||
common: {
|
common: {
|
||||||
pill: string;
|
pill: string;
|
||||||
pills: string;
|
pills: string;
|
||||||
|
blister: string;
|
||||||
|
blisters: string;
|
||||||
day: string;
|
day: string;
|
||||||
days: string;
|
days: string;
|
||||||
soon: string;
|
soon: string;
|
||||||
|
footer: string;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const translations: Record<Language, TranslationKeys> = {
|
const translations: Record<Language, TranslationKeys> = {
|
||||||
en: {
|
en: {
|
||||||
stockReminder: {
|
stockReminder: {
|
||||||
subject: "MedAssist-ng Auto-Reminder: {count} Medication{s} Running Low",
|
subject: "MedAssist-ng Auto-Reminder: {count} Medication{s} Running Critically Low",
|
||||||
title: "⚠️ MedAssist-ng - Automatic Reorder Reminder",
|
title: "⚠️ MedAssist-ng - Automatic Reorder Reminder",
|
||||||
description: "The following medications are running low and need to be reordered:",
|
description: "The following medications are running critically low and need to be reordered:",
|
||||||
alertSingle: "⚠️ 1 medication running low!",
|
descriptionEmpty: "The following medications are empty and need to be reordered immediately:",
|
||||||
alertMultiple: "⚠️ {count} medications running low!",
|
descriptionMixed: "The following medications need to be reordered:",
|
||||||
|
alertSingle: "⚠️ 1 medication running critically low!",
|
||||||
|
alertMultiple: "⚠️ {count} medications running critically low!",
|
||||||
|
alertEmptySingle: "🚨 1 medication empty - reorder immediately!",
|
||||||
|
alertEmptyMultiple: "🚨 {count} medications empty - reorder immediately!",
|
||||||
|
alertLowSingle: "⚠️ 1 medication running critically low",
|
||||||
|
alertLowMultiple: "⚠️ {count} medications running critically low",
|
||||||
|
alertLowStockSingle: "⚠️ 1 medication running low",
|
||||||
|
alertLowStockMultiple: "⚠️ {count} medications running low",
|
||||||
|
descriptionLow: "The following medications are running low and should be reordered soon:",
|
||||||
tableHeaders: {
|
tableHeaders: {
|
||||||
medication: "Medication",
|
medication: "Medication",
|
||||||
pills: "Pills",
|
pills: "Pills",
|
||||||
days: "Days",
|
days: "Days",
|
||||||
runsOut: "Runs Out",
|
runsOut: "Runs Out",
|
||||||
},
|
},
|
||||||
footer: "🤖 Automatic reminder from MedAssist-ng",
|
now: "NOW",
|
||||||
repeatDailyNote: "You are receiving this daily reminder because 'Repeat Daily' is enabled in settings.",
|
repeatDailyNote: "You are receiving this daily reminder because 'Repeat Daily' is enabled in settings.",
|
||||||
},
|
},
|
||||||
intakeReminder: {
|
intakeReminder: {
|
||||||
@@ -151,44 +192,75 @@ const translations: Record<Language, TranslationKeys> = {
|
|||||||
},
|
},
|
||||||
pills: "pills",
|
pills: "pills",
|
||||||
takenBy: "for {name}",
|
takenBy: "for {name}",
|
||||||
footer: "🤖 Automatic reminder from MedAssist-ng",
|
|
||||||
},
|
},
|
||||||
push: {
|
push: {
|
||||||
stockTitle: "MedAssist-ng: 1 Medication Running Low",
|
stockTitle: "MedAssist-ng: 1 Medication Running Critically Low",
|
||||||
stockTitleMultiple: "MedAssist-ng: {count} Medications Running Low",
|
stockTitleMultiple: "MedAssist-ng: {count} Medications Running Critically Low",
|
||||||
intakeTitle: "💊 Medication Reminder in {minutes} min",
|
intakeTitle: "💊 Reminder: Medication intake in {minutes} min",
|
||||||
pillsLeft: "{count} pills",
|
pillsLeft: "{count} pills",
|
||||||
daysLeft: "{count} days left",
|
daysLeft: "{count} days left",
|
||||||
pillsAt: "{count} pills at {time}",
|
pillsAt: "{count} pills at {time}",
|
||||||
repeatDailyNote: "(Daily reminder enabled)",
|
repeatDailyNote: "(Daily reminder enabled)",
|
||||||
empty: "Empty",
|
empty: "Empty",
|
||||||
low: "Low",
|
low: "Critical",
|
||||||
|
critical: "Critical",
|
||||||
|
lowStock: "Low",
|
||||||
reorderNow: "Reorder Now!",
|
reorderNow: "Reorder Now!",
|
||||||
emptySection: "EMPTY (reorder immediately)",
|
emptySection: "Empty (reorder immediately)",
|
||||||
lowSection: "RUNNING LOW (reorder soon)",
|
lowSection: "Running critically low",
|
||||||
|
criticalSection: "Running critically low",
|
||||||
|
lowStockSection: "Running low",
|
||||||
|
},
|
||||||
|
demandCalculator: {
|
||||||
|
subject: "MedAssist-ng - Supply Overview ({from} - {until})",
|
||||||
|
title: "MedAssist-ng - Demand Calculator",
|
||||||
|
description: "Supply overview from {from} to {until}",
|
||||||
|
summaryOutOfStock: "⚠️ {count} medication{s} will be out of stock during this period.",
|
||||||
|
summaryAllOk: "✓ All medications have sufficient supply for this period.",
|
||||||
|
tableHeaders: {
|
||||||
|
medication: "Medication",
|
||||||
|
usage: "Usage",
|
||||||
|
needed: "Blisters needed",
|
||||||
|
available: "Available",
|
||||||
|
status: "Status",
|
||||||
|
},
|
||||||
|
statusEnough: "✓ Enough",
|
||||||
|
statusEmpty: "✗ Empty",
|
||||||
},
|
},
|
||||||
common: {
|
common: {
|
||||||
pill: "pill",
|
pill: "pill",
|
||||||
pills: "pills",
|
pills: "pills",
|
||||||
|
blister: "blister",
|
||||||
|
blisters: "blisters",
|
||||||
day: "day",
|
day: "day",
|
||||||
days: "days",
|
days: "days",
|
||||||
soon: "soon",
|
soon: "soon",
|
||||||
|
footer: "🤖 Sent from MedAssist-ng",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
de: {
|
de: {
|
||||||
stockReminder: {
|
stockReminder: {
|
||||||
subject: "MedAssist-ng Auto-Erinnerung: {count} Medikament{e} wird knapp",
|
subject: "MedAssist-ng Auto-Erinnerung: {count} Medikament{e} kritisch niedrig",
|
||||||
title: "⚠️ MedAssist-ng - Automatische Nachbestell-Erinnerung",
|
title: "⚠️ MedAssist-ng - Automatische Nachbestell-Erinnerung",
|
||||||
description: "Die folgenden Medikamente gehen zur Neige und sollten nachbestellt werden:",
|
description: "Die folgenden Medikamente sind kritisch niedrig und sollten nachbestellt werden:",
|
||||||
alertSingle: "⚠️ 1 Medikament wird knapp!",
|
descriptionEmpty: "Die folgenden Medikamente sind leer und müssen sofort nachbestellt werden:",
|
||||||
alertMultiple: "⚠️ {count} Medikamente werden knapp!",
|
descriptionMixed: "Die folgenden Medikamente müssen nachbestellt werden:",
|
||||||
|
alertSingle: "⚠️ 1 Medikament kritisch niedrig!",
|
||||||
|
alertMultiple: "⚠️ {count} Medikamente kritisch niedrig!",
|
||||||
|
alertEmptySingle: "🚨 1 Medikament leer - sofort nachbestellen!",
|
||||||
|
alertEmptyMultiple: "🚨 {count} Medikamente leer - sofort nachbestellen!",
|
||||||
|
alertLowSingle: "⚠️ 1 Medikament kritisch niedrig",
|
||||||
|
alertLowMultiple: "⚠️ {count} Medikamente kritisch niedrig",
|
||||||
|
alertLowStockSingle: "⚠️ 1 Medikament niedrig",
|
||||||
|
alertLowStockMultiple: "⚠️ {count} Medikamente niedrig",
|
||||||
|
descriptionLow: "Die folgenden Medikamente werden knapp und sollten bald nachbestellt werden:",
|
||||||
tableHeaders: {
|
tableHeaders: {
|
||||||
medication: "Medikament",
|
medication: "Medikament",
|
||||||
pills: "Tabletten",
|
pills: "Tabletten",
|
||||||
days: "Tage",
|
days: "Tage",
|
||||||
runsOut: "Aufgebraucht",
|
runsOut: "Aufgebraucht",
|
||||||
},
|
},
|
||||||
footer: "🤖 Automatische Erinnerung von MedAssist-ng",
|
now: "JETZT",
|
||||||
repeatDailyNote:
|
repeatDailyNote:
|
||||||
"Sie erhalten diese tägliche Erinnerung, weil 'Täglich wiederholen' in den Einstellungen aktiviert ist.",
|
"Sie erhalten diese tägliche Erinnerung, weil 'Täglich wiederholen' in den Einstellungen aktiviert ist.",
|
||||||
},
|
},
|
||||||
@@ -205,28 +277,50 @@ const translations: Record<Language, TranslationKeys> = {
|
|||||||
},
|
},
|
||||||
pills: "Tabletten",
|
pills: "Tabletten",
|
||||||
takenBy: "für {name}",
|
takenBy: "für {name}",
|
||||||
footer: "🤖 Automatische Erinnerung von MedAssist-ng",
|
|
||||||
},
|
},
|
||||||
push: {
|
push: {
|
||||||
stockTitle: "MedAssist-ng: 1 Medikament wird knapp",
|
stockTitle: "MedAssist-ng: 1 Medikament kritisch niedrig",
|
||||||
stockTitleMultiple: "MedAssist-ng: {count} Medikamente werden knapp",
|
stockTitleMultiple: "MedAssist-ng: {count} Medikamente kritisch niedrig",
|
||||||
intakeTitle: "💊 Einnahme-Erinnerung in {minutes} Min.",
|
intakeTitle: "💊 Erinnerung: Medikamenteneinnahme in {minutes} Min.",
|
||||||
pillsLeft: "{count} Tabletten",
|
pillsLeft: "{count} Tabletten",
|
||||||
daysLeft: "{count} Tage übrig",
|
daysLeft: "{count} Tage übrig",
|
||||||
pillsAt: "{count} Tabletten um {time}",
|
pillsAt: "{count} Tabletten um {time}",
|
||||||
repeatDailyNote: "(Tägliche Erinnerung aktiviert)",
|
repeatDailyNote: "(Tägliche Erinnerung aktiviert)",
|
||||||
empty: "Leer",
|
empty: "Leer",
|
||||||
low: "Knapp",
|
low: "Kritisch",
|
||||||
|
critical: "Kritisch",
|
||||||
|
lowStock: "Niedrig",
|
||||||
reorderNow: "Jetzt nachbestellen!",
|
reorderNow: "Jetzt nachbestellen!",
|
||||||
emptySection: "LEER (sofort nachbestellen)",
|
emptySection: "Leer (sofort nachbestellen)",
|
||||||
lowSection: "WIRD KNAPP (bald nachbestellen)",
|
lowSection: "Kritisch niedrig",
|
||||||
|
criticalSection: "Kritisch niedrig",
|
||||||
|
lowStockSection: "Niedrig",
|
||||||
|
},
|
||||||
|
demandCalculator: {
|
||||||
|
subject: "MedAssist-ng - Bestandsübersicht ({from} - {until})",
|
||||||
|
title: "MedAssist-ng - Bedarfsrechner",
|
||||||
|
description: "Bestandsübersicht von {from} bis {until}",
|
||||||
|
summaryOutOfStock: "⚠️ {count} Medikament{e} wird im Zeitraum nicht ausreichen.",
|
||||||
|
summaryAllOk: "✓ Alle Medikamente reichen für diesen Zeitraum.",
|
||||||
|
tableHeaders: {
|
||||||
|
medication: "Medikament",
|
||||||
|
usage: "Verbrauch",
|
||||||
|
needed: "Blister benötigt",
|
||||||
|
available: "Verfügbar",
|
||||||
|
status: "Status",
|
||||||
|
},
|
||||||
|
statusEnough: "✓ Ausreichend",
|
||||||
|
statusEmpty: "✗ Leer",
|
||||||
},
|
},
|
||||||
common: {
|
common: {
|
||||||
pill: "Tablette",
|
pill: "Tablette",
|
||||||
pills: "Tabletten",
|
pills: "Tabletten",
|
||||||
|
blister: "Blister",
|
||||||
|
blisters: "Blister",
|
||||||
day: "Tag",
|
day: "Tag",
|
||||||
days: "Tage",
|
days: "Tage",
|
||||||
soon: "bald",
|
soon: "bald",
|
||||||
|
footer: "🤖 Gesendet von MedAssist-ng",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -264,3 +358,38 @@ export function getDateLocale(language: Language): string {
|
|||||||
return "en-US";
|
return "en-US";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the app URL from the first CORS_ORIGINS entry.
|
||||||
|
* Falls back to empty string if not set.
|
||||||
|
*/
|
||||||
|
export function getAppUrl(): string {
|
||||||
|
const origins = process.env.CORS_ORIGINS || "";
|
||||||
|
return origins.split(",")[0]?.trim() || "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the unified footer as HTML with MedAssist-ng as a link to the instance.
|
||||||
|
* @param variant - 'planner' uses the Medication Planner footer text
|
||||||
|
*/
|
||||||
|
export function getFooterHtml(language: Language): string {
|
||||||
|
const tr = getTranslations(language);
|
||||||
|
const appUrl = getAppUrl();
|
||||||
|
const appName = appUrl
|
||||||
|
? `<a href="${appUrl}" style="color: #6b7280; text-decoration: underline;">MedAssist-ng</a>`
|
||||||
|
: "MedAssist-ng";
|
||||||
|
return tr.common.footer.replace("MedAssist-ng", appName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the unified footer as plain text.
|
||||||
|
* @param variant - 'planner' uses the Medication Planner footer text
|
||||||
|
*/
|
||||||
|
export function getFooterPlain(language: Language): string {
|
||||||
|
const tr = getTranslations(language);
|
||||||
|
const appUrl = getAppUrl();
|
||||||
|
if (appUrl) {
|
||||||
|
return `${tr.common.footer} (${appUrl})`;
|
||||||
|
}
|
||||||
|
return tr.common.footer;
|
||||||
|
}
|
||||||
|
|||||||
@@ -126,9 +126,11 @@ export async function createApp(options?: {
|
|||||||
// Server initialization (runs on import)
|
// Server initialization (runs on import)
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
|
import { log } from "./utils/logger.js";
|
||||||
|
|
||||||
// Wait for database migrations before anything else
|
// Wait for database migrations before anything else
|
||||||
await migrationsReady;
|
await migrationsReady;
|
||||||
console.log("[DB] Migrations complete, starting server...");
|
log.info("[DB] Migrations complete, starting server...");
|
||||||
|
|
||||||
// Ensure images directory exists
|
// Ensure images directory exists
|
||||||
const imagesDir = ensureImagesDirectory();
|
const imagesDir = ensureImagesDirectory();
|
||||||
@@ -197,12 +199,14 @@ const start = async () => {
|
|||||||
// Start the automatic reminder scheduler
|
// Start the automatic reminder scheduler
|
||||||
startReminderScheduler({
|
startReminderScheduler({
|
||||||
info: (msg) => app.log.info(msg),
|
info: (msg) => app.log.info(msg),
|
||||||
|
debug: (msg) => app.log.debug(msg),
|
||||||
error: (msg) => app.log.error(msg),
|
error: (msg) => app.log.error(msg),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start the intake reminder scheduler (checks every minute)
|
// Start the intake reminder scheduler (checks every minute)
|
||||||
startIntakeReminderScheduler({
|
startIntakeReminderScheduler({
|
||||||
info: (msg) => app.log.info(msg),
|
info: (msg) => app.log.info(msg),
|
||||||
|
debug: (msg) => app.log.debug(msg),
|
||||||
error: (msg) => app.log.error(msg),
|
error: (msg) => app.log.error(msg),
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ export async function getAnonymousUserId(): Promise<number> {
|
|||||||
`);
|
`);
|
||||||
|
|
||||||
anonymousUserVerified = true;
|
anonymousUserVerified = true;
|
||||||
console.log(`Created anonymous user with fixed ID ${ANONYMOUS_USER_ID} for no-auth mode`);
|
|
||||||
|
|
||||||
return ANONYMOUS_USER_ID;
|
return ANONYMOUS_USER_ID;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ const inventorySchema = z.object({
|
|||||||
pillsPerBlister: z.number().int().min(1).default(1),
|
pillsPerBlister: z.number().int().min(1).default(1),
|
||||||
looseTablets: z.number().int().min(0).default(0),
|
looseTablets: z.number().int().min(0).default(0),
|
||||||
stockAdjustment: z.number().int().default(0), // Manual stock correction
|
stockAdjustment: z.number().int().default(0), // Manual stock correction
|
||||||
|
packageType: z.enum(["blister", "bottle"]).default("blister"),
|
||||||
});
|
});
|
||||||
|
|
||||||
const medicationExportSchema = z.object({
|
const medicationExportSchema = z.object({
|
||||||
@@ -276,6 +277,7 @@ export async function exportRoutes(app: FastifyInstance) {
|
|||||||
pillsPerBlister: med.pillsPerBlister ?? 1,
|
pillsPerBlister: med.pillsPerBlister ?? 1,
|
||||||
looseTablets: med.looseTablets ?? 0,
|
looseTablets: med.looseTablets ?? 0,
|
||||||
stockAdjustment: med.stockAdjustment ?? 0,
|
stockAdjustment: med.stockAdjustment ?? 0,
|
||||||
|
packageType: med.packageType ?? "blister",
|
||||||
},
|
},
|
||||||
pillWeightMg: med.pillWeightMg,
|
pillWeightMg: med.pillWeightMg,
|
||||||
doseUnit: med.doseUnit ?? "mg",
|
doseUnit: med.doseUnit ?? "mg",
|
||||||
@@ -490,6 +492,7 @@ export async function exportRoutes(app: FastifyInstance) {
|
|||||||
name: med.name,
|
name: med.name,
|
||||||
genericName: med.genericName || null,
|
genericName: med.genericName || null,
|
||||||
takenByJson,
|
takenByJson,
|
||||||
|
packageType: med.inventory.packageType ?? "blister",
|
||||||
packCount: med.inventory.packCount,
|
packCount: med.inventory.packCount,
|
||||||
blistersPerPack: med.inventory.blistersPerPack,
|
blistersPerPack: med.inventory.blistersPerPack,
|
||||||
pillsPerBlister: med.inventory.pillsPerBlister,
|
pillsPerBlister: med.inventory.pillsPerBlister,
|
||||||
|
|||||||
@@ -656,7 +656,13 @@ export async function medicationRoutes(app: FastifyInstance) {
|
|||||||
const blistersPerPack = row.blistersPerPack ?? 1;
|
const blistersPerPack = row.blistersPerPack ?? 1;
|
||||||
const looseTablets = row.looseTablets ?? 0;
|
const looseTablets = row.looseTablets ?? 0;
|
||||||
const stockAdjustment = row.stockAdjustment ?? 0;
|
const stockAdjustment = row.stockAdjustment ?? 0;
|
||||||
const originalTotalPills = packCount * blistersPerPack * pillsPerBlister + looseTablets + stockAdjustment;
|
const packageType = row.packageType ?? "blister";
|
||||||
|
|
||||||
|
// For bottle type, looseTablets IS the current stock (no blister math)
|
||||||
|
const originalTotalPills =
|
||||||
|
packageType === "bottle"
|
||||||
|
? looseTablets + stockAdjustment
|
||||||
|
: packCount * blistersPerPack * pillsPerBlister + looseTablets + stockAdjustment;
|
||||||
|
|
||||||
// Calculate consumption based on ACTUAL taken doses from dose_tracking
|
// Calculate consumption based on ACTUAL taken doses from dose_tracking
|
||||||
// This ensures Planner shows the same "current stock" as the Dashboard/Modal
|
// This ensures Planner shows the same "current stock" as the Dashboard/Modal
|
||||||
@@ -725,9 +731,13 @@ export async function medicationRoutes(app: FastifyInstance) {
|
|||||||
const currentStock = Math.max(0, originalTotalPills - consumedUntilNow);
|
const currentStock = Math.max(0, originalTotalPills - consumedUntilNow);
|
||||||
|
|
||||||
// Calculate usage for the planning period
|
// Calculate usage for the planning period
|
||||||
|
// Always use the user-selected start date for the usage calculation.
|
||||||
|
// Using max(now, start) would cause asymmetric counting when now falls
|
||||||
|
// between morning and evening doses on the start day (e.g., morning dose
|
||||||
|
// skipped but evening counted), leading to confusing off-by-one results.
|
||||||
|
// The stock already reflects consumed doses, so no double-counting occurs.
|
||||||
// When includeUntilStart is true, calculate from now to end (useful for trip planning)
|
// When includeUntilStart is true, calculate from now to end (useful for trip planning)
|
||||||
// When false, calculate from max(now, start) to end (default behavior)
|
const effectivePlannerStart = includeUntilStart ? now : start;
|
||||||
const effectivePlannerStart = includeUntilStart ? now : new Date(Math.max(now.getTime(), start.getTime()));
|
|
||||||
const usageTotal = calculateUsageInRange(blisters, effectivePlannerStart, end);
|
const usageTotal = calculateUsageInRange(blisters, effectivePlannerStart, end);
|
||||||
|
|
||||||
const blistersNeeded = pillsPerBlister > 0 ? Math.ceil(usageTotal / pillsPerBlister) : 0;
|
const blistersNeeded = pillsPerBlister > 0 ? Math.ceil(usageTotal / pillsPerBlister) : 0;
|
||||||
@@ -735,18 +745,27 @@ export async function medicationRoutes(app: FastifyInstance) {
|
|||||||
// Calculate AVAILABLE = stock AFTER the planned period (currentStock - usageTotal)
|
// Calculate AVAILABLE = stock AFTER the planned period (currentStock - usageTotal)
|
||||||
const availableAfterPeriod = Math.max(0, currentStock - usageTotal);
|
const availableAfterPeriod = Math.max(0, currentStock - usageTotal);
|
||||||
|
|
||||||
// Calculate stock breakdown for availableAfterPeriod
|
let fullBlisters: number;
|
||||||
// Consumption order: loose pills first, then from blisters
|
let loosePills: number;
|
||||||
const totalConsumedByEnd = originalTotalPills - availableAfterPeriod;
|
|
||||||
const looseConsumedByEnd = Math.min(totalConsumedByEnd, looseTablets);
|
|
||||||
const loosePillsRemaining = Math.max(0, looseTablets - looseConsumedByEnd);
|
|
||||||
const blisterPillsConsumed = totalConsumedByEnd - looseConsumedByEnd;
|
|
||||||
const originalBlisterPills = originalTotalPills - looseTablets;
|
|
||||||
const blisterPillsRemaining = Math.max(0, originalBlisterPills - blisterPillsConsumed);
|
|
||||||
|
|
||||||
const fullBlisters = pillsPerBlister > 0 ? Math.floor(blisterPillsRemaining / pillsPerBlister) : 0;
|
if (packageType === "bottle") {
|
||||||
const openBlisterPills = pillsPerBlister > 0 ? blisterPillsRemaining % pillsPerBlister : 0;
|
// Bottle type: no blisters, everything is loose pills
|
||||||
const loosePills = loosePillsRemaining + openBlisterPills; // Combine open blister + remaining loose
|
fullBlisters = 0;
|
||||||
|
loosePills = availableAfterPeriod;
|
||||||
|
} else {
|
||||||
|
// Blister type: calculate stock breakdown
|
||||||
|
// Consumption order: loose pills first, then from blisters
|
||||||
|
const totalConsumedByEnd = originalTotalPills - availableAfterPeriod;
|
||||||
|
const looseConsumedByEnd = Math.min(totalConsumedByEnd, looseTablets);
|
||||||
|
const loosePillsRemaining = Math.max(0, looseTablets - looseConsumedByEnd);
|
||||||
|
const blisterPillsConsumed = totalConsumedByEnd - looseConsumedByEnd;
|
||||||
|
const originalBlisterPills = originalTotalPills - looseTablets;
|
||||||
|
const blisterPillsRemaining = Math.max(0, originalBlisterPills - blisterPillsConsumed);
|
||||||
|
|
||||||
|
fullBlisters = pillsPerBlister > 0 ? Math.floor(blisterPillsRemaining / pillsPerBlister) : 0;
|
||||||
|
const openBlisterPills = pillsPerBlister > 0 ? blisterPillsRemaining % pillsPerBlister : 0;
|
||||||
|
loosePills = loosePillsRemaining + openBlisterPills; // Combine open blister + remaining loose
|
||||||
|
}
|
||||||
|
|
||||||
const enough = currentStock >= usageTotal;
|
const enough = currentStock >= usageTotal;
|
||||||
return {
|
return {
|
||||||
@@ -759,6 +778,7 @@ export async function medicationRoutes(app: FastifyInstance) {
|
|||||||
fullBlisters,
|
fullBlisters,
|
||||||
loosePills,
|
loosePills,
|
||||||
enough,
|
enough,
|
||||||
|
packageType,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -824,12 +844,28 @@ function calculateUsageInRange(
|
|||||||
end: Date
|
end: Date
|
||||||
) {
|
) {
|
||||||
let total = 0;
|
let total = 0;
|
||||||
|
const msPerDay = 86400000;
|
||||||
blisters.forEach((blister) => {
|
blisters.forEach((blister) => {
|
||||||
const blisterStart = parseLocalDateTime(blister.start);
|
const blisterStart = parseLocalDateTime(blister.start);
|
||||||
if (Number.isNaN(blisterStart.getTime())) return;
|
if (Number.isNaN(blisterStart.getTime())) return;
|
||||||
// iterate occurrences from blisterStart up to end
|
|
||||||
for (let dt = new Date(blisterStart); dt < end; dt.setDate(dt.getDate() + blister.every)) {
|
const every = Math.max(1, blister.every);
|
||||||
if (dt >= start && dt < end) total += blister.usage;
|
|
||||||
|
// Skip ahead to the first occurrence at or after start to avoid
|
||||||
|
// iterating through months/years of past doses
|
||||||
|
const dt = new Date(blisterStart);
|
||||||
|
if (dt < start) {
|
||||||
|
const daysToSkip = Math.floor((start.getTime() - dt.getTime()) / (every * msPerDay));
|
||||||
|
dt.setDate(dt.getDate() + daysToSkip * every);
|
||||||
|
// Fine-tune: advance until we reach or pass start
|
||||||
|
while (dt < start) {
|
||||||
|
dt.setDate(dt.getDate() + every);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count occurrences in [start, end)
|
||||||
|
for (; dt < end; dt.setDate(dt.getDate() + every)) {
|
||||||
|
total += blister.usage;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return Number(total.toFixed(2));
|
return Number(total.toFixed(2));
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ export async function oidcRoutes(app: FastifyInstance) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Set cookies (use app's centralized cookie options)
|
// Set cookies (use app's centralized cookie options)
|
||||||
console.log(
|
request.log.debug(
|
||||||
`[OIDC] Setting cookies for user ${user.username}, NODE_ENV=${env.NODE_ENV}, secure=${app.config.cookieOptions.secure}`
|
`[OIDC] Setting cookies for user ${user.username}, NODE_ENV=${env.NODE_ENV}, secure=${app.config.cookieOptions.secure}`
|
||||||
);
|
);
|
||||||
setAuthCookies(app, reply, accessToken, refreshToken);
|
setAuthCookies(app, reply, accessToken, refreshToken);
|
||||||
@@ -241,12 +241,12 @@ async function findOrCreateOIDCUser(
|
|||||||
if (existingByUsername.authProvider === "local" && !existingByUsername.oidcSubject) {
|
if (existingByUsername.authProvider === "local" && !existingByUsername.oidcSubject) {
|
||||||
// Local user exists without SSO - link this OIDC account to existing user
|
// Local user exists without SSO - link this OIDC account to existing user
|
||||||
await db.update(users).set({ oidcSubject: oidcSubject }).where(eq(users.id, existingByUsername.id));
|
await db.update(users).set({ oidcSubject: oidcSubject }).where(eq(users.id, existingByUsername.id));
|
||||||
console.log(`[OIDC] Linked OIDC to existing local user: ${username}`);
|
// Linked OIDC to existing local user
|
||||||
return { id: existingByUsername.id, username: existingByUsername.username };
|
return { id: existingByUsername.id, username: existingByUsername.username };
|
||||||
} else if (existingByUsername.oidcSubject && existingByUsername.oidcSubject !== oidcSubject) {
|
} else if (existingByUsername.oidcSubject && existingByUsername.oidcSubject !== oidcSubject) {
|
||||||
// User already has a DIFFERENT OIDC subject - create new user with suffix
|
// User already has a DIFFERENT OIDC subject - create new user with suffix
|
||||||
username = `${username}_sso`;
|
username = `${username}_sso`;
|
||||||
console.log(`[OIDC] Username collision (different OIDC subject), using: ${username}`);
|
// Username collision (different OIDC subject), use suffixed name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,7 +268,7 @@ async function findOrCreateOIDCUser(
|
|||||||
})
|
})
|
||||||
.returning({ id: users.id, username: users.username });
|
.returning({ id: users.id, username: users.username });
|
||||||
|
|
||||||
console.log(`[OIDC] Created new user: ${newUser.username} (ID: ${newUser.id})`);
|
// New OIDC user created
|
||||||
return newUser;
|
return newUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+278
-191
@@ -1,6 +1,13 @@
|
|||||||
import type { FastifyInstance, FastifyRequest } from "fastify";
|
import type { FastifyInstance, FastifyRequest } from "fastify";
|
||||||
import nodemailer from "nodemailer";
|
import nodemailer from "nodemailer";
|
||||||
import { getDateLocale, getTranslations, type Language, t } from "../i18n/translations.js";
|
import {
|
||||||
|
getDateLocale,
|
||||||
|
getFooterHtml,
|
||||||
|
getFooterPlain,
|
||||||
|
getTranslations,
|
||||||
|
type Language,
|
||||||
|
t,
|
||||||
|
} from "../i18n/translations.js";
|
||||||
import { getAnonymousUserId, requireAuth } from "../plugins/auth.js";
|
import { getAnonymousUserId, requireAuth } from "../plugins/auth.js";
|
||||||
import { env } from "../plugins/env.js";
|
import { env } from "../plugins/env.js";
|
||||||
import { updateReminderSentTime, updateUserReminderSentTime } from "../services/reminder-scheduler.js";
|
import { updateReminderSentTime, updateUserReminderSentTime } from "../services/reminder-scheduler.js";
|
||||||
@@ -29,6 +36,7 @@ type PlannerRow = {
|
|||||||
fullBlisters: number;
|
fullBlisters: number;
|
||||||
loosePills: number;
|
loosePills: number;
|
||||||
enough: boolean;
|
enough: boolean;
|
||||||
|
packageType?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
type SendEmailBody = {
|
type SendEmailBody = {
|
||||||
@@ -44,6 +52,7 @@ type LowStockItem = {
|
|||||||
medsLeft: number;
|
medsLeft: number;
|
||||||
daysLeft: number | null;
|
daysLeft: number | null;
|
||||||
depletionDate: string | null;
|
depletionDate: string | null;
|
||||||
|
isCritical?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type ReminderEmailBody = {
|
type ReminderEmailBody = {
|
||||||
@@ -68,32 +77,28 @@ export async function plannerRoutes(app: FastifyInstance) {
|
|||||||
return authUser.id;
|
return authUser.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Demand calculator notification (supports email and push)
|
||||||
app.post<{ Body: SendEmailBody }>("/planner/send-email", async (request, reply) => {
|
app.post<{ Body: SendEmailBody }>("/planner/send-email", async (request, reply) => {
|
||||||
const { email, from, until, rows, language: bodyLanguage } = request.body;
|
const { email, from, until, rows, language: bodyLanguage } = request.body;
|
||||||
|
|
||||||
if (!email || !rows || rows.length === 0) {
|
if (!rows || rows.length === 0) {
|
||||||
return reply.status(400).send({ error: "Missing email or planner data" });
|
return reply.status(400).send({ error: "Missing planner data" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const smtpHost = process.env.SMTP_HOST;
|
// Load user settings for notification channels
|
||||||
const smtpUser = process.env.SMTP_USER;
|
const userId = await getUserId(request);
|
||||||
const smtpPass = process.env.SMTP_TOKEN || process.env.SMTP_PASS; // Token takes precedence
|
const userSettings = await loadUserSettings(userId);
|
||||||
const smtpPort = parseInt(process.env.SMTP_PORT ?? "587", 10);
|
const notificationSettings = {
|
||||||
const smtpSecure = process.env.SMTP_SECURE === "true";
|
emailEnabled: userSettings.emailEnabled,
|
||||||
const smtpFrom = process.env.SMTP_FROM ?? smtpUser;
|
shoutrrrEnabled: userSettings.shoutrrrEnabled,
|
||||||
|
shoutrrrUrl: userSettings.shoutrrrUrl || "",
|
||||||
if (!smtpHost || !smtpUser) {
|
};
|
||||||
return reply.status(400).send({ error: "SMTP not configured" });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get locale from user settings or use the language passed in the body
|
// Get locale from user settings or use the language passed in the body
|
||||||
let language: Language = bodyLanguage || "en";
|
const language: Language = (userSettings.language as Language) || bodyLanguage || "en";
|
||||||
const authUser = request.user as unknown as AuthUser | null;
|
|
||||||
if (authUser?.id) {
|
|
||||||
const userSettings = await loadUserSettings(authUser.id);
|
|
||||||
language = userSettings.language;
|
|
||||||
}
|
|
||||||
const locale = getDateLocale(language);
|
const locale = getDateLocale(language);
|
||||||
|
const tr = getTranslations(language);
|
||||||
|
const dc = tr.demandCalculator;
|
||||||
|
|
||||||
// Format dates for display - escape to prevent XSS even though toLocaleDateString should be safe
|
// Format dates for display - escape to prevent XSS even though toLocaleDateString should be safe
|
||||||
const fromDate = escapeHtml(
|
const fromDate = escapeHtml(
|
||||||
@@ -111,47 +116,93 @@ export async function plannerRoutes(app: FastifyInstance) {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// Build HTML table with horizontal scroll for mobile
|
const outOfStockCount = rows.filter((r) => !r.enough).length;
|
||||||
// Escape/coerce all user-provided values to prevent XSS
|
const summaryText = outOfStockCount > 0 ? t(dc.summaryOutOfStock, { count: outOfStockCount }) : dc.summaryAllOk;
|
||||||
const tableRows = rows
|
|
||||||
.map((row) => {
|
// Build plain text (shared between email and push)
|
||||||
const safeName = escapeHtml(row.medicationName);
|
const plainText = `${dc.title}
|
||||||
const safeTotalPills = Number(row.totalPills) || 0;
|
${t(dc.description, { from: fromDate, until: untilDate })}
|
||||||
const safePlannerUsage = Number(row.plannerUsage) || 0;
|
|
||||||
const safeBlistersNeeded = Number(row.blistersNeeded) || 0;
|
${summaryText}
|
||||||
const safeBlisterSize = Number(row.blisterSize) || 0;
|
|
||||||
const safeFullBlisters = Number(row.fullBlisters) || 0;
|
${rows
|
||||||
const safeLoosePills = Number(row.loosePills) || 0;
|
.map((r) => {
|
||||||
return `
|
const isBottle = r.packageType === "bottle";
|
||||||
|
const usage = `${r.plannerUsage} ${tr.common.pills}`;
|
||||||
|
const needed = isBottle ? "–" : `${r.blistersNeeded} × ${r.blisterSize}`;
|
||||||
|
const loosePills = Math.round((Number(r.loosePills) || 0) * 10) / 10;
|
||||||
|
const available = isBottle
|
||||||
|
? `${loosePills} ${tr.common.pills}`
|
||||||
|
: `${r.fullBlisters} ${tr.common.blisters}${loosePills > 0 ? ` + ${loosePills} ${tr.common.pills}` : ""}`;
|
||||||
|
const status = r.enough ? dc.statusEnough : dc.statusEmpty;
|
||||||
|
return `${r.medicationName}: ${usage}, ${needed}, ${available} - ${status}`;
|
||||||
|
})
|
||||||
|
.join("\n")}
|
||||||
|
|
||||||
|
---
|
||||||
|
${getFooterPlain(language)}`;
|
||||||
|
|
||||||
|
const results: { email?: boolean; push?: boolean; errors: string[] } = { errors: [] };
|
||||||
|
|
||||||
|
// Send email if enabled
|
||||||
|
if (notificationSettings.emailEnabled && email) {
|
||||||
|
const smtpHost = process.env.SMTP_HOST;
|
||||||
|
const smtpUser = process.env.SMTP_USER;
|
||||||
|
const smtpPass = process.env.SMTP_TOKEN || process.env.SMTP_PASS; // Token takes precedence
|
||||||
|
const smtpPort = parseInt(process.env.SMTP_PORT ?? "587", 10);
|
||||||
|
const smtpSecure = process.env.SMTP_SECURE === "true";
|
||||||
|
const smtpFrom = process.env.SMTP_FROM ?? smtpUser;
|
||||||
|
|
||||||
|
if (smtpHost && smtpUser) {
|
||||||
|
// Build HTML table with horizontal scroll for mobile
|
||||||
|
// Escape/coerce all user-provided values to prevent XSS
|
||||||
|
const tableRows = rows
|
||||||
|
.map((row) => {
|
||||||
|
const safeName = escapeHtml(row.medicationName);
|
||||||
|
const safePlannerUsage = Number(row.plannerUsage) || 0;
|
||||||
|
const safeBlistersNeeded = Number(row.blistersNeeded) || 0;
|
||||||
|
const safeBlisterSize = Number(row.blisterSize) || 0;
|
||||||
|
const safeFullBlisters = Number(row.fullBlisters) || 0;
|
||||||
|
const safeLoosePills = Math.round((Number(row.loosePills) || 0) * 10) / 10;
|
||||||
|
const isBottle = row.packageType === "bottle";
|
||||||
|
|
||||||
|
// "Blisters needed" column: dash for bottles
|
||||||
|
const neededCell = isBottle ? "–" : `${safeBlistersNeeded} × ${safeBlisterSize}`;
|
||||||
|
|
||||||
|
// "Available" column: match frontend format
|
||||||
|
let availableCell: string;
|
||||||
|
if (isBottle) {
|
||||||
|
availableCell = `${safeLoosePills} ${tr.common.pills}`;
|
||||||
|
} else {
|
||||||
|
availableCell = `${safeFullBlisters} ${tr.common.blisters}`;
|
||||||
|
if (safeLoosePills > 0) {
|
||||||
|
availableCell += ` + ${safeLoosePills} ${tr.common.pills}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return `
|
||||||
<tr>
|
<tr>
|
||||||
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; white-space: nowrap;">${safeName}</td>
|
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; white-space: nowrap;">${safeName}</td>
|
||||||
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap;"><strong>${safeTotalPills}</strong></td>
|
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap;"><strong>${safePlannerUsage}</strong> ${tr.common.pills}</td>
|
||||||
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap;"><strong>${safePlannerUsage}</strong></td>
|
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap;">${neededCell}</td>
|
||||||
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap;">${safeBlistersNeeded} × ${safeBlisterSize}</td>
|
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap;">${availableCell}</td>
|
||||||
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap;">${safeFullBlisters}${safeLoosePills > 0 ? ` (+${safeLoosePills})` : ""}</td>
|
|
||||||
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap;">
|
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap;">
|
||||||
<span style="display: inline-block; padding: 4px 10px; border-radius: 12px; font-size: 12px; font-weight: 600; ${
|
<span style="display: inline-block; padding: 4px 10px; border-radius: 12px; font-size: 12px; font-weight: 600; ${
|
||||||
row.enough ? "background: #d1fae5; color: #065f46;" : "background: #fee2e2; color: #991b1b;"
|
row.enough ? "background: #d1fae5; color: #065f46;" : "background: #fee2e2; color: #991b1b;"
|
||||||
}">
|
}">
|
||||||
${row.enough ? "✓ OK" : "✗ Out of Stock"}
|
${row.enough ? dc.statusEnough : dc.statusEmpty}
|
||||||
</span>
|
</span>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
`;
|
`;
|
||||||
})
|
})
|
||||||
.join("");
|
.join("");
|
||||||
|
|
||||||
const outOfStockCount = rows.filter((r) => !r.enough).length;
|
const html = `
|
||||||
const summaryText =
|
|
||||||
outOfStockCount > 0
|
|
||||||
? `⚠️ ${outOfStockCount} medication${outOfStockCount > 1 ? "s" : ""} will be out of stock during this period.`
|
|
||||||
: "✓ All medications have sufficient supply for this period.";
|
|
||||||
|
|
||||||
const html = `
|
|
||||||
<div style="font-family: system-ui, -apple-system, sans-serif; max-width: 100%; margin: 0 auto; padding: 12px; background: #f9fafb;">
|
<div style="font-family: system-ui, -apple-system, sans-serif; max-width: 100%; margin: 0 auto; padding: 12px; background: #f9fafb;">
|
||||||
<div style="background: white; border-radius: 12px; padding: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
|
<div style="background: white; border-radius: 12px; padding: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
|
||||||
<h2 style="color: #1f2937; margin: 0 0 8px; font-size: 18px;">MedAssist-ng - Demand Calculator</h2>
|
<h2 style="color: #1f2937; margin: 0 0 8px; font-size: 18px;">${dc.title}</h2>
|
||||||
<p style="color: #6b7280; margin: 0 0 16px; font-size: 13px;">Supply overview from <strong>${fromDate}</strong> to <strong>${untilDate}</strong></p>
|
<p style="color: #6b7280; margin: 0 0 16px; font-size: 13px;">${t(dc.description, { from: `<strong>${fromDate}</strong>`, until: `<strong>${untilDate}</strong>` })}</p>
|
||||||
|
|
||||||
<div style="padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; ${
|
<div style="padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; ${
|
||||||
outOfStockCount > 0
|
outOfStockCount > 0
|
||||||
@@ -167,12 +218,11 @@ export async function plannerRoutes(app: FastifyInstance) {
|
|||||||
<table style="width: 100%; border-collapse: collapse; background: white; min-width: 550px;">
|
<table style="width: 100%; border-collapse: collapse; background: white; min-width: 550px;">
|
||||||
<thead>
|
<thead>
|
||||||
<tr style="background: #f3f4f6;">
|
<tr style="background: #f3f4f6;">
|
||||||
<th style="padding: 10px 12px; text-align: left; font-size: 11px; text-transform: uppercase; color: #6b7280; letter-spacing: 0.05em; white-space: nowrap;">Medication</th>
|
<th style="padding: 10px 12px; text-align: left; font-size: 11px; text-transform: uppercase; color: #6b7280; letter-spacing: 0.05em; white-space: nowrap;">${dc.tableHeaders.medication}</th>
|
||||||
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; letter-spacing: 0.05em; white-space: nowrap;">Stock</th>
|
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; letter-spacing: 0.05em; white-space: nowrap;">${dc.tableHeaders.usage}</th>
|
||||||
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; letter-spacing: 0.05em; white-space: nowrap;">Usage</th>
|
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; letter-spacing: 0.05em; white-space: nowrap;">${dc.tableHeaders.needed}</th>
|
||||||
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; letter-spacing: 0.05em; white-space: nowrap;">Needed</th>
|
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; letter-spacing: 0.05em; white-space: nowrap;">${dc.tableHeaders.available}</th>
|
||||||
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; letter-spacing: 0.05em; white-space: nowrap;">Available</th>
|
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; letter-spacing: 0.05em; white-space: nowrap;">${dc.tableHeaders.status}</th>
|
||||||
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; letter-spacing: 0.05em; white-space: nowrap;">Status</th>
|
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -182,44 +232,76 @@ export async function plannerRoutes(app: FastifyInstance) {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<hr style="border: none; border-top: 1px solid #e5e7eb; margin: 16px 0;" />
|
<hr style="border: none; border-top: 1px solid #e5e7eb; margin: 16px 0;" />
|
||||||
<p style="color: #9ca3af; font-size: 11px; margin: 0;">Sent from MedAssist-ng Medication Planner</p>
|
<p style="color: #9ca3af; font-size: 11px; margin: 0;">${getFooterHtml(language)}</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const plainText = `MedAssist-ng - Demand Calculator
|
try {
|
||||||
Supply overview from ${fromDate} to ${untilDate}
|
const transporter = nodemailer.createTransport({
|
||||||
|
host: smtpHost,
|
||||||
|
port: smtpPort,
|
||||||
|
secure: smtpSecure,
|
||||||
|
auth: {
|
||||||
|
user: smtpUser,
|
||||||
|
pass: smtpPass ?? "",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
${summaryText}
|
await transporter.sendMail({
|
||||||
|
from: smtpFrom,
|
||||||
|
to: email,
|
||||||
|
subject: t(dc.subject, { from: fromDate, until: untilDate }),
|
||||||
|
text: plainText,
|
||||||
|
html,
|
||||||
|
});
|
||||||
|
|
||||||
${rows.map((r) => `${r.medicationName}: ${r.totalPills} pills in stock, ${r.plannerUsage} pills needed, ${r.fullBlisters} blisters available${r.loosePills > 0 ? ` (+${r.loosePills} loose)` : ""} (${r.blistersNeeded} needed) - ${r.enough ? "Enough" : "OUT OF STOCK"}`).join("\n")}
|
results.email = true;
|
||||||
|
} catch (error) {
|
||||||
|
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
||||||
|
results.errors.push(`Email: ${errorMessage}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
---
|
// Send push notification if enabled
|
||||||
Sent from MedAssist-ng Medication Planner`;
|
if (notificationSettings.shoutrrrEnabled && notificationSettings.shoutrrrUrl) {
|
||||||
|
const pushTitle = t(dc.subject, { from: fromDate, until: untilDate });
|
||||||
|
const pushMessage = `${summaryText}\n\n${rows
|
||||||
|
.map((r) => {
|
||||||
|
const usage = `${r.plannerUsage} ${tr.common.pills}`;
|
||||||
|
const status = r.enough ? dc.statusEnough : dc.statusEmpty;
|
||||||
|
return `${r.enough ? "✓" : "✗"} ${r.medicationName}: ${usage} - ${status}`;
|
||||||
|
})
|
||||||
|
.join("\n")}\n\n---\n${getFooterPlain(language)}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const transporter = nodemailer.createTransport({
|
const pushResult = await sendShoutrrrNotification(notificationSettings.shoutrrrUrl, pushTitle, pushMessage);
|
||||||
host: smtpHost,
|
if (pushResult.success) {
|
||||||
port: smtpPort,
|
results.push = true;
|
||||||
secure: smtpSecure,
|
} else {
|
||||||
auth: {
|
results.errors.push(`Push: ${pushResult.error}`);
|
||||||
user: smtpUser,
|
}
|
||||||
pass: smtpPass ?? "",
|
} catch (error) {
|
||||||
},
|
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
||||||
|
results.errors.push(`Push: ${errorMessage}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build response message
|
||||||
|
const sentChannels: string[] = [];
|
||||||
|
if (results.email) sentChannels.push("email");
|
||||||
|
if (results.push) sentChannels.push("push");
|
||||||
|
|
||||||
|
if (sentChannels.length > 0) {
|
||||||
|
return reply.send({
|
||||||
|
success: true,
|
||||||
|
message: `Notification sent via ${sentChannels.join(" and ")}`,
|
||||||
});
|
});
|
||||||
|
} else if (results.errors.length > 0) {
|
||||||
await transporter.sendMail({
|
return reply.status(500).send({ error: results.errors.join("; ") });
|
||||||
from: smtpFrom,
|
} else {
|
||||||
to: email,
|
return reply.status(400).send({ error: "No notification channels configured" });
|
||||||
subject: `MedAssist-ng - Supply Overview (${fromDate} - ${untilDate})`,
|
|
||||||
text: plainText,
|
|
||||||
html,
|
|
||||||
});
|
|
||||||
|
|
||||||
return reply.send({ success: true, message: "Email sent successfully" });
|
|
||||||
} catch (error) {
|
|
||||||
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
||||||
return reply.status(500).send({ error: `Failed to send email: ${errorMessage}` });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -240,11 +322,66 @@ Sent from MedAssist-ng Medication Planner`;
|
|||||||
shoutrrrUrl: userSettings.shoutrrrUrl || "",
|
shoutrrrUrl: userSettings.shoutrrrUrl || "",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Get translations based on user language
|
||||||
|
const language = (userSettings.language as Language) || "en";
|
||||||
|
const tr = getTranslations(language);
|
||||||
|
|
||||||
const results: { email?: boolean; push?: boolean; errors: string[] } = { errors: [] };
|
const results: { email?: boolean; push?: boolean; errors: string[] } = { errors: [] };
|
||||||
|
|
||||||
// Separate empty from low stock medications
|
// Separate into 3 categories: empty, critical, and low stock
|
||||||
const emptyMeds = lowStock.filter((r) => r.medsLeft <= 0);
|
const emptyMeds = lowStock.filter((r) => r.medsLeft <= 0);
|
||||||
const lowMeds = lowStock.filter((r) => r.medsLeft > 0);
|
const criticalMeds = lowStock.filter((r) => r.medsLeft > 0 && r.isCritical !== false);
|
||||||
|
const lowStockMeds = lowStock.filter((r) => r.medsLeft > 0 && r.isCritical === false);
|
||||||
|
|
||||||
|
// Build shared notification content (method-agnostic)
|
||||||
|
const titleParts: string[] = [];
|
||||||
|
if (emptyMeds.length > 0) {
|
||||||
|
titleParts.push(`🚨 ${emptyMeds.length} ${tr.push.empty}`);
|
||||||
|
}
|
||||||
|
if (criticalMeds.length > 0) {
|
||||||
|
titleParts.push(`🚨 ${criticalMeds.length} ${tr.push.critical}`);
|
||||||
|
}
|
||||||
|
if (lowStockMeds.length > 0) {
|
||||||
|
titleParts.push(`⚠️ ${lowStockMeds.length} ${tr.push.lowStock}`);
|
||||||
|
}
|
||||||
|
const notificationTitle = `MedAssist: ${titleParts.join(", ")} - ${tr.push.reorderNow}`;
|
||||||
|
|
||||||
|
// Build description text
|
||||||
|
let descriptionText: string;
|
||||||
|
if (emptyMeds.length > 0 && (criticalMeds.length > 0 || lowStockMeds.length > 0)) {
|
||||||
|
descriptionText = tr.stockReminder.descriptionMixed;
|
||||||
|
} else if (emptyMeds.length > 0) {
|
||||||
|
descriptionText = tr.stockReminder.descriptionEmpty;
|
||||||
|
} else if (criticalMeds.length > 0) {
|
||||||
|
descriptionText = tr.stockReminder.description;
|
||||||
|
} else {
|
||||||
|
descriptionText = tr.stockReminder.descriptionLow;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build section-based message (shared between email plain text and push)
|
||||||
|
const messageParts: string[] = [];
|
||||||
|
if (emptyMeds.length > 0) {
|
||||||
|
messageParts.push(`🚨 ${tr.push.emptySection}:`);
|
||||||
|
emptyMeds.forEach((r) => messageParts.push(` • ${r.name}`));
|
||||||
|
}
|
||||||
|
if (criticalMeds.length > 0) {
|
||||||
|
if (messageParts.length > 0) messageParts.push("");
|
||||||
|
messageParts.push(`🚨 ${tr.push.criticalSection}:`);
|
||||||
|
criticalMeds.forEach((r) =>
|
||||||
|
messageParts.push(
|
||||||
|
` • ${r.name}: ${t(tr.push.pillsLeft, { count: r.medsLeft })}, ${t(tr.push.daysLeft, { count: r.daysLeft ?? 0 })}`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (lowStockMeds.length > 0) {
|
||||||
|
if (messageParts.length > 0) messageParts.push("");
|
||||||
|
messageParts.push(`⚠️ ${tr.push.lowStockSection}:`);
|
||||||
|
lowStockMeds.forEach((r) =>
|
||||||
|
messageParts.push(
|
||||||
|
` • ${r.name}: ${t(tr.push.pillsLeft, { count: r.medsLeft })}, ${t(tr.push.daysLeft, { count: r.daysLeft ?? 0 })}`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Send email if enabled
|
// Send email if enabled
|
||||||
if (notificationSettings.emailEnabled && email) {
|
if (notificationSettings.emailEnabled && email) {
|
||||||
@@ -256,52 +393,59 @@ Sent from MedAssist-ng Medication Planner`;
|
|||||||
const smtpFrom = process.env.SMTP_FROM ?? smtpUser;
|
const smtpFrom = process.env.SMTP_FROM ?? smtpUser;
|
||||||
|
|
||||||
if (smtpHost && smtpUser) {
|
if (smtpHost && smtpUser) {
|
||||||
// Build subject line based on what we have
|
// Build subject line from shared title parts
|
||||||
let subjectText: string;
|
const subjectText = titleParts.join(", ");
|
||||||
if (emptyMeds.length > 0 && lowMeds.length > 0) {
|
|
||||||
subjectText = `🚨 ${emptyMeds.length} Empty, ⚠️ ${lowMeds.length} Running Low`;
|
|
||||||
} else if (emptyMeds.length > 0) {
|
|
||||||
subjectText = `🚨 ${emptyMeds.length} Medication${emptyMeds.length > 1 ? "s" : ""} Empty`;
|
|
||||||
} else {
|
|
||||||
subjectText = `⚠️ ${lowMeds.length} Medication${lowMeds.length > 1 ? "s" : ""} Running Low`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build alert box based on what we have
|
// Build alert boxes for each category
|
||||||
let alertHtml: string;
|
const alertParts: string[] = [];
|
||||||
if (emptyMeds.length > 0 && lowMeds.length > 0) {
|
|
||||||
alertHtml = `
|
if (emptyMeds.length > 0) {
|
||||||
|
const emptyAlert =
|
||||||
|
emptyMeds.length === 1
|
||||||
|
? tr.stockReminder.alertEmptySingle
|
||||||
|
: t(tr.stockReminder.alertEmptyMultiple, { count: emptyMeds.length });
|
||||||
|
alertParts.push(`
|
||||||
<div style="padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; background: #fef2f2; border: 1px solid #dc2626;">
|
<div style="padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; background: #fef2f2; border: 1px solid #dc2626;">
|
||||||
<p style="margin: 0; color: #dc2626; font-weight: 600; font-size: 13px;">
|
<p style="margin: 0; color: #dc2626; font-weight: 600; font-size: 13px;">
|
||||||
🚨 ${emptyMeds.length} medication${emptyMeds.length > 1 ? "s" : ""} EMPTY - reorder immediately!
|
${emptyAlert}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>`);
|
||||||
<div style="padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; background: #fffbeb; border: 1px solid #f59e0b;">
|
|
||||||
<p style="margin: 0; color: #b45309; font-weight: 500; font-size: 13px;">
|
|
||||||
⚠️ ${lowMeds.length} medication${lowMeds.length > 1 ? "s" : ""} running low - reorder soon
|
|
||||||
</p>
|
|
||||||
</div>`;
|
|
||||||
} else if (emptyMeds.length > 0) {
|
|
||||||
alertHtml = `
|
|
||||||
<div style="padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; background: #fef2f2; border: 1px solid #dc2626;">
|
|
||||||
<p style="margin: 0; color: #dc2626; font-weight: 600; font-size: 13px;">
|
|
||||||
🚨 ${emptyMeds.length} medication${emptyMeds.length > 1 ? "s" : ""} EMPTY - reorder immediately!
|
|
||||||
</p>
|
|
||||||
</div>`;
|
|
||||||
} else {
|
|
||||||
alertHtml = `
|
|
||||||
<div style="padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; background: #fffbeb; border: 1px solid #f59e0b;">
|
|
||||||
<p style="margin: 0; color: #b45309; font-weight: 500; font-size: 13px;">
|
|
||||||
⚠️ ${lowMeds.length} medication${lowMeds.length > 1 ? "s" : ""} running low - reorder soon
|
|
||||||
</p>
|
|
||||||
</div>`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (criticalMeds.length > 0) {
|
||||||
|
const criticalAlert =
|
||||||
|
criticalMeds.length === 1
|
||||||
|
? tr.stockReminder.alertLowSingle
|
||||||
|
: t(tr.stockReminder.alertLowMultiple, { count: criticalMeds.length });
|
||||||
|
alertParts.push(`
|
||||||
|
<div style="padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; background: #fff7ed; border: 1px solid #ea580c;">
|
||||||
|
<p style="margin: 0; color: #c2410c; font-weight: 600; font-size: 13px;">
|
||||||
|
${criticalAlert}
|
||||||
|
</p>
|
||||||
|
</div>`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lowStockMeds.length > 0) {
|
||||||
|
const lowAlert =
|
||||||
|
lowStockMeds.length === 1
|
||||||
|
? tr.stockReminder.alertLowStockSingle
|
||||||
|
: t(tr.stockReminder.alertLowStockMultiple, { count: lowStockMeds.length });
|
||||||
|
alertParts.push(`
|
||||||
|
<div style="padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; background: #fffbeb; border: 1px solid #f59e0b;">
|
||||||
|
<p style="margin: 0; color: #b45309; font-weight: 500; font-size: 13px;">
|
||||||
|
${lowAlert}
|
||||||
|
</p>
|
||||||
|
</div>`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const alertHtml = alertParts.join("");
|
||||||
|
|
||||||
// Build table rows with status indicator
|
// Build table rows with status indicator
|
||||||
const buildTableRow = (row: LowStockItem) => {
|
const buildTableRow = (row: LowStockItem) => {
|
||||||
const isEmpty = row.medsLeft <= 0;
|
const isEmpty = row.medsLeft <= 0;
|
||||||
const statusIcon = isEmpty ? "🚨" : "⚠️";
|
const isCritical = row.isCritical !== false;
|
||||||
const rowBg = isEmpty ? "#fef2f2" : "white";
|
const statusIcon = isEmpty ? "🚨" : isCritical ? "🚨" : "⚠️";
|
||||||
// Escape user-provided strings and coerce numbers to prevent XSS
|
const rowBg = isEmpty ? "#fef2f2" : isCritical ? "#fff7ed" : "white";
|
||||||
const safeName = escapeHtml(row.name);
|
const safeName = escapeHtml(row.name);
|
||||||
const safeMedsLeft = Number(row.medsLeft) || 0;
|
const safeMedsLeft = Number(row.medsLeft) || 0;
|
||||||
const safeDaysLeft = Number(row.daysLeft) || 0;
|
const safeDaysLeft = Number(row.daysLeft) || 0;
|
||||||
@@ -311,26 +455,16 @@ Sent from MedAssist-ng Medication Planner`;
|
|||||||
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; white-space: nowrap;">${statusIcon} ${safeName}</td>
|
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; white-space: nowrap;">${statusIcon} ${safeName}</td>
|
||||||
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap; ${isEmpty ? "color: #dc2626; font-weight: 600;" : ""}"><strong>${safeMedsLeft}</strong></td>
|
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap; ${isEmpty ? "color: #dc2626; font-weight: 600;" : ""}"><strong>${safeMedsLeft}</strong></td>
|
||||||
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap;">${safeDaysLeft}</td>
|
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap;">${safeDaysLeft}</td>
|
||||||
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap;">${isEmpty ? "<strong>NOW</strong>" : safeDepletionDate}</td>
|
<td style="padding: 10px 12px; border-bottom: 1px solid #e5e7eb; text-align: center; white-space: nowrap;">${isEmpty ? `<strong>${tr.stockReminder.now}</strong>` : safeDepletionDate}</td>
|
||||||
</tr>`;
|
</tr>`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const tableRows = lowStock.map(buildTableRow).join("");
|
const tableRows = lowStock.map(buildTableRow).join("");
|
||||||
|
|
||||||
// Build description text
|
|
||||||
let descriptionText: string;
|
|
||||||
if (emptyMeds.length > 0 && lowMeds.length > 0) {
|
|
||||||
descriptionText = "The following medications need to be reordered:";
|
|
||||||
} else if (emptyMeds.length > 0) {
|
|
||||||
descriptionText = "The following medications are EMPTY and need to be reordered immediately:";
|
|
||||||
} else {
|
|
||||||
descriptionText = "The following medications are running low and need to be reordered:";
|
|
||||||
}
|
|
||||||
|
|
||||||
const html = `
|
const html = `
|
||||||
<div style="font-family: system-ui, -apple-system, sans-serif; max-width: 100%; margin: 0 auto; padding: 12px; background: #f9fafb;">
|
<div style="font-family: system-ui, -apple-system, sans-serif; max-width: 100%; margin: 0 auto; padding: 12px; background: #f9fafb;">
|
||||||
<div style="background: white; border-radius: 12px; padding: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
|
<div style="background: white; border-radius: 12px; padding: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
|
||||||
<h2 style="color: #1f2937; margin: 0 0 8px; font-size: 18px;">${emptyMeds.length > 0 ? "🚨" : "⚠️"} MedAssist-ng - Reorder Reminder</h2>
|
<h2 style="color: #1f2937; margin: 0 0 8px; font-size: 18px;">${emptyMeds.length > 0 ? "🚨" : "⚠️"} MedAssist-ng - ${tr.push.reorderNow}</h2>
|
||||||
<p style="color: #6b7280; margin: 0 0 16px; font-size: 13px;">${descriptionText}</p>
|
<p style="color: #6b7280; margin: 0 0 16px; font-size: 13px;">${descriptionText}</p>
|
||||||
|
|
||||||
${alertHtml}
|
${alertHtml}
|
||||||
@@ -339,10 +473,10 @@ Sent from MedAssist-ng Medication Planner`;
|
|||||||
<table style="width: 100%; border-collapse: collapse; background: white; min-width: 400px;">
|
<table style="width: 100%; border-collapse: collapse; background: white; min-width: 400px;">
|
||||||
<thead>
|
<thead>
|
||||||
<tr style="background: #f3f4f6;">
|
<tr style="background: #f3f4f6;">
|
||||||
<th style="padding: 10px 12px; text-align: left; font-size: 11px; text-transform: uppercase; color: #6b7280; white-space: nowrap;">Medication</th>
|
<th style="padding: 10px 12px; text-align: left; font-size: 11px; text-transform: uppercase; color: #6b7280; white-space: nowrap;">${tr.stockReminder.tableHeaders.medication}</th>
|
||||||
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; white-space: nowrap;">Pills</th>
|
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; white-space: nowrap;">${tr.stockReminder.tableHeaders.pills}</th>
|
||||||
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; white-space: nowrap;">Days</th>
|
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; white-space: nowrap;">${tr.stockReminder.tableHeaders.days}</th>
|
||||||
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; white-space: nowrap;">Runs Out</th>
|
<th style="padding: 10px 12px; text-align: center; font-size: 11px; text-transform: uppercase; color: #6b7280; white-space: nowrap;">${tr.stockReminder.tableHeaders.runsOut}</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -352,33 +486,12 @@ Sent from MedAssist-ng Medication Planner`;
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<hr style="border: none; border-top: 1px solid #e5e7eb; margin: 16px 0;" />
|
<hr style="border: none; border-top: 1px solid #e5e7eb; margin: 16px 0;" />
|
||||||
<p style="color: #9ca3af; font-size: 11px; margin: 0;">Sent from MedAssist-ng Medication Planner</p>
|
<p style="color: #9ca3af; font-size: 11px; margin: 0;">${getFooterHtml(language)}</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// Build plain text with sections
|
const plainText = `MedAssist-ng - ${tr.push.reorderNow}\n\n${messageParts.join("\n")}\n\n---\n${getFooterPlain(language)}`;
|
||||||
let plainTextContent: string;
|
|
||||||
if (emptyMeds.length > 0 && lowMeds.length > 0) {
|
|
||||||
plainTextContent = `🚨 EMPTY (reorder immediately):
|
|
||||||
${emptyMeds.map((r) => ` • ${r.name}`).join("\n")}
|
|
||||||
|
|
||||||
⚠️ RUNNING LOW (reorder soon):
|
|
||||||
${lowMeds.map((r) => ` • ${r.name}: ${r.medsLeft} pills left, ${r.daysLeft ?? 0} days remaining`).join("\n")}`;
|
|
||||||
} else if (emptyMeds.length > 0) {
|
|
||||||
plainTextContent = `🚨 EMPTY (reorder immediately):
|
|
||||||
${emptyMeds.map((r) => ` • ${r.name}`).join("\n")}`;
|
|
||||||
} else {
|
|
||||||
plainTextContent = `⚠️ Running low:
|
|
||||||
${lowMeds.map((r) => ` • ${r.name}: ${r.medsLeft} pills left, ${r.daysLeft ?? 0} days remaining, runs out ${r.depletionDate ?? "soon"}`).join("\n")}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const plainText = `MedAssist-ng - Reorder Reminder
|
|
||||||
|
|
||||||
${plainTextContent}
|
|
||||||
|
|
||||||
---
|
|
||||||
Sent from MedAssist-ng Medication Planner`;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const transporter = nodemailer.createTransport({
|
const transporter = nodemailer.createTransport({
|
||||||
@@ -409,38 +522,10 @@ Sent from MedAssist-ng Medication Planner`;
|
|||||||
|
|
||||||
// Send push notification if enabled
|
// Send push notification if enabled
|
||||||
if (notificationSettings.shoutrrrEnabled && notificationSettings.shoutrrrUrl) {
|
if (notificationSettings.shoutrrrEnabled && notificationSettings.shoutrrrUrl) {
|
||||||
// Get translations based on user language (default to 'en')
|
const message = messageParts.join("\n") + `\n\n---\n${getFooterPlain(language)}`;
|
||||||
const tr = getTranslations((userSettings.language as Language) || "en");
|
|
||||||
|
|
||||||
// Build clear title
|
|
||||||
const titleParts: string[] = [];
|
|
||||||
if (emptyMeds.length > 0) {
|
|
||||||
titleParts.push(`🚨 ${emptyMeds.length} ${tr.push.empty}`);
|
|
||||||
}
|
|
||||||
if (lowMeds.length > 0) {
|
|
||||||
titleParts.push(`⚠️ ${lowMeds.length} ${tr.push.low}`);
|
|
||||||
}
|
|
||||||
const title = `MedAssist: ${titleParts.join(", ")} - ${tr.push.reorderNow}`;
|
|
||||||
|
|
||||||
// Build clear message with sections
|
|
||||||
const messageParts: string[] = [];
|
|
||||||
if (emptyMeds.length > 0) {
|
|
||||||
messageParts.push(`🚨 ${tr.push.emptySection}:`);
|
|
||||||
emptyMeds.forEach((r) => messageParts.push(` • ${r.name}`));
|
|
||||||
}
|
|
||||||
if (lowMeds.length > 0) {
|
|
||||||
if (emptyMeds.length > 0) messageParts.push("");
|
|
||||||
messageParts.push(`⚠️ ${tr.push.lowSection}:`);
|
|
||||||
lowMeds.forEach((r) =>
|
|
||||||
messageParts.push(
|
|
||||||
` • ${r.name}: ${t(tr.push.pillsLeft, { count: r.medsLeft })}, ${t(tr.push.daysLeft, { count: r.daysLeft ?? 0 })}`
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const message = messageParts.join("\n");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const pushResult = await sendShoutrrrNotification(notificationSettings.shoutrrrUrl, title, message);
|
const pushResult = await sendShoutrrrNotification(notificationSettings.shoutrrrUrl, notificationTitle, message);
|
||||||
if (pushResult.success) {
|
if (pushResult.success) {
|
||||||
results.push = true;
|
results.push = true;
|
||||||
} else {
|
} else {
|
||||||
@@ -458,7 +543,9 @@ Sent from MedAssist-ng Medication Planner`;
|
|||||||
updateReminderSentTime("stock", channel);
|
updateReminderSentTime("stock", channel);
|
||||||
|
|
||||||
// Also update user settings in database so frontend can display the info
|
// Also update user settings in database so frontend can display the info
|
||||||
await updateUserReminderSentTime(userId, "stock", channel);
|
const firstMed = lowStock[0];
|
||||||
|
const medNames = lowStock.length > 1 ? `${firstMed.name} (+${lowStock.length - 1})` : firstMed?.name;
|
||||||
|
await updateUserReminderSentTime(userId, "stock", channel, medNames);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build response message
|
// Build response message
|
||||||
|
|||||||
@@ -78,9 +78,13 @@ export async function refillRoutes(app: FastifyInstance) {
|
|||||||
})
|
})
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
// Calculate pills added for response
|
// Calculate pills added for response (packageType-aware)
|
||||||
const pillsPerPack = med.blistersPerPack * med.pillsPerBlister;
|
const isBottle = (med.packageType ?? "blister") === "bottle";
|
||||||
const totalPillsAdded = packsAdded * pillsPerPack + loosePillsAdded;
|
const pillsPerPack = isBottle ? 0 : med.blistersPerPack * med.pillsPerBlister;
|
||||||
|
const totalPillsAdded = isBottle ? loosePillsAdded : packsAdded * pillsPerPack + loosePillsAdded;
|
||||||
|
const newTotalPills = isBottle
|
||||||
|
? newLooseTablets + (med.stockAdjustment ?? 0)
|
||||||
|
: newPackCount * pillsPerPack + newLooseTablets + (med.stockAdjustment ?? 0);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
@@ -94,7 +98,7 @@ export async function refillRoutes(app: FastifyInstance) {
|
|||||||
newStock: {
|
newStock: {
|
||||||
packCount: newPackCount,
|
packCount: newPackCount,
|
||||||
looseTablets: newLooseTablets,
|
looseTablets: newLooseTablets,
|
||||||
totalPills: newPackCount * pillsPerPack + newLooseTablets,
|
totalPills: newTotalPills,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@@ -120,13 +124,14 @@ export async function refillRoutes(app: FastifyInstance) {
|
|||||||
.where(eq(refillHistory.medicationId, medId))
|
.where(eq(refillHistory.medicationId, medId))
|
||||||
.orderBy(desc(refillHistory.refillDate));
|
.orderBy(desc(refillHistory.refillDate));
|
||||||
|
|
||||||
const pillsPerPack = med.blistersPerPack * med.pillsPerBlister;
|
const isBottle = (med.packageType ?? "blister") === "bottle";
|
||||||
|
const pillsPerPack = isBottle ? 0 : med.blistersPerPack * med.pillsPerBlister;
|
||||||
|
|
||||||
return refills.map((r) => ({
|
return refills.map((r) => ({
|
||||||
id: r.id,
|
id: r.id,
|
||||||
packsAdded: r.packsAdded,
|
packsAdded: r.packsAdded,
|
||||||
loosePillsAdded: r.loosePillsAdded,
|
loosePillsAdded: r.loosePillsAdded,
|
||||||
totalPillsAdded: r.packsAdded * pillsPerPack + r.loosePillsAdded,
|
totalPillsAdded: isBottle ? r.loosePillsAdded : r.packsAdded * pillsPerPack + r.loosePillsAdded,
|
||||||
refillDate: r.refillDate,
|
refillDate: r.refillDate,
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -30,11 +30,15 @@ export type UserSettings = {
|
|||||||
highStockDays: number;
|
highStockDays: number;
|
||||||
language: Language;
|
language: Language;
|
||||||
stockCalculationMode: "automatic" | "manual";
|
stockCalculationMode: "automatic" | "manual";
|
||||||
|
shareStockStatus: boolean;
|
||||||
lastAutoEmailSent: string | null;
|
lastAutoEmailSent: string | null;
|
||||||
lastNotificationType: string | null;
|
lastNotificationType: string | null;
|
||||||
lastNotificationChannel: string | null;
|
lastNotificationChannel: string | null;
|
||||||
lastReminderMedName: string | null;
|
lastReminderMedName: string | null;
|
||||||
lastReminderTakenBy: string | null;
|
lastReminderTakenBy: string | null;
|
||||||
|
lastStockReminderSent: string | null;
|
||||||
|
lastStockReminderChannel: string | null;
|
||||||
|
lastStockReminderMedNames: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
type SettingsBody = {
|
type SettingsBody = {
|
||||||
@@ -57,6 +61,7 @@ type SettingsBody = {
|
|||||||
maxNaggingReminders: number;
|
maxNaggingReminders: number;
|
||||||
language: string;
|
language: string;
|
||||||
stockCalculationMode: "automatic" | "manual";
|
stockCalculationMode: "automatic" | "manual";
|
||||||
|
shareStockStatus: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type TestEmailBody = {
|
type TestEmailBody = {
|
||||||
@@ -104,11 +109,15 @@ function getDefaultSettings() {
|
|||||||
highStockDays: envInt("DEFAULT_HIGH_STOCK_DAYS", 180),
|
highStockDays: envInt("DEFAULT_HIGH_STOCK_DAYS", 180),
|
||||||
language: (process.env.DEFAULT_LANGUAGE as "en" | "de") || "en",
|
language: (process.env.DEFAULT_LANGUAGE as "en" | "de") || "en",
|
||||||
stockCalculationMode: (process.env.DEFAULT_STOCK_CALCULATION_MODE as "automatic" | "manual") || "automatic",
|
stockCalculationMode: (process.env.DEFAULT_STOCK_CALCULATION_MODE as "automatic" | "manual") || "automatic",
|
||||||
|
shareStockStatus: envBool("DEFAULT_SHARE_STOCK_STATUS", true),
|
||||||
lastAutoEmailSent: null,
|
lastAutoEmailSent: null,
|
||||||
lastNotificationType: null,
|
lastNotificationType: null,
|
||||||
lastNotificationChannel: null,
|
lastNotificationChannel: null,
|
||||||
lastReminderMedName: null,
|
lastReminderMedName: null,
|
||||||
lastReminderTakenBy: null,
|
lastReminderTakenBy: null,
|
||||||
|
lastStockReminderSent: null,
|
||||||
|
lastStockReminderChannel: null,
|
||||||
|
lastStockReminderMedNames: null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,11 +163,15 @@ export async function loadUserSettings(userId: number): Promise<UserSettings> {
|
|||||||
highStockDays: settings.highStockDays,
|
highStockDays: settings.highStockDays,
|
||||||
language: settings.language as Language,
|
language: settings.language as Language,
|
||||||
stockCalculationMode: (settings.stockCalculationMode as "automatic" | "manual") ?? "automatic",
|
stockCalculationMode: (settings.stockCalculationMode as "automatic" | "manual") ?? "automatic",
|
||||||
|
shareStockStatus: settings.shareStockStatus ?? true,
|
||||||
lastAutoEmailSent: settings.lastAutoEmailSent,
|
lastAutoEmailSent: settings.lastAutoEmailSent,
|
||||||
lastNotificationType: settings.lastNotificationType,
|
lastNotificationType: settings.lastNotificationType,
|
||||||
lastNotificationChannel: settings.lastNotificationChannel,
|
lastNotificationChannel: settings.lastNotificationChannel,
|
||||||
lastReminderMedName: settings.lastReminderMedName ?? null,
|
lastReminderMedName: settings.lastReminderMedName ?? null,
|
||||||
lastReminderTakenBy: settings.lastReminderTakenBy ?? null,
|
lastReminderTakenBy: settings.lastReminderTakenBy ?? null,
|
||||||
|
lastStockReminderSent: settings.lastStockReminderSent ?? null,
|
||||||
|
lastStockReminderChannel: settings.lastStockReminderChannel ?? null,
|
||||||
|
lastStockReminderMedNames: settings.lastStockReminderMedNames ?? null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,11 +199,15 @@ export async function getAllUserSettings(): Promise<UserSettings[]> {
|
|||||||
highStockDays: settings.highStockDays,
|
highStockDays: settings.highStockDays,
|
||||||
language: settings.language as Language,
|
language: settings.language as Language,
|
||||||
stockCalculationMode: (settings.stockCalculationMode as "automatic" | "manual") ?? "automatic",
|
stockCalculationMode: (settings.stockCalculationMode as "automatic" | "manual") ?? "automatic",
|
||||||
|
shareStockStatus: settings.shareStockStatus ?? true,
|
||||||
lastAutoEmailSent: settings.lastAutoEmailSent,
|
lastAutoEmailSent: settings.lastAutoEmailSent,
|
||||||
lastNotificationType: settings.lastNotificationType,
|
lastNotificationType: settings.lastNotificationType,
|
||||||
lastNotificationChannel: settings.lastNotificationChannel,
|
lastNotificationChannel: settings.lastNotificationChannel,
|
||||||
lastReminderMedName: settings.lastReminderMedName ?? null,
|
lastReminderMedName: settings.lastReminderMedName ?? null,
|
||||||
lastReminderTakenBy: settings.lastReminderTakenBy ?? null,
|
lastReminderTakenBy: settings.lastReminderTakenBy ?? null,
|
||||||
|
lastStockReminderSent: settings.lastStockReminderSent ?? null,
|
||||||
|
lastStockReminderChannel: settings.lastStockReminderChannel ?? null,
|
||||||
|
lastStockReminderMedNames: settings.lastStockReminderMedNames ?? null,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,6 +258,7 @@ export async function settingsRoutes(app: FastifyInstance) {
|
|||||||
maxNaggingReminders: settings.maxNaggingReminders ?? 5,
|
maxNaggingReminders: settings.maxNaggingReminders ?? 5,
|
||||||
language: settings.language,
|
language: settings.language,
|
||||||
stockCalculationMode: settings.stockCalculationMode ?? "automatic",
|
stockCalculationMode: settings.stockCalculationMode ?? "automatic",
|
||||||
|
shareStockStatus: settings.shareStockStatus ?? true,
|
||||||
// SMTP settings (from .env - shared/server-configured)
|
// SMTP settings (from .env - shared/server-configured)
|
||||||
smtpHost: process.env.SMTP_HOST ?? "",
|
smtpHost: process.env.SMTP_HOST ?? "",
|
||||||
smtpPort: parseInt(process.env.SMTP_PORT ?? "587", 10),
|
smtpPort: parseInt(process.env.SMTP_PORT ?? "587", 10),
|
||||||
@@ -254,6 +272,10 @@ export async function settingsRoutes(app: FastifyInstance) {
|
|||||||
lastNotificationChannel: settings.lastNotificationChannel,
|
lastNotificationChannel: settings.lastNotificationChannel,
|
||||||
lastReminderMedName: settings.lastReminderMedName ?? null,
|
lastReminderMedName: settings.lastReminderMedName ?? null,
|
||||||
lastReminderTakenBy: settings.lastReminderTakenBy ?? null,
|
lastReminderTakenBy: settings.lastReminderTakenBy ?? null,
|
||||||
|
// Stock reminder tracking (separate from intake)
|
||||||
|
lastStockReminderSent: settings.lastStockReminderSent ?? null,
|
||||||
|
lastStockReminderChannel: settings.lastStockReminderChannel ?? null,
|
||||||
|
lastStockReminderMedNames: settings.lastStockReminderMedNames ?? null,
|
||||||
// Server settings (from .env, read-only)
|
// Server settings (from .env, read-only)
|
||||||
expiryWarningDays: parseInt(process.env.EXPIRY_WARNING_DAYS ?? "30", 10),
|
expiryWarningDays: parseInt(process.env.EXPIRY_WARNING_DAYS ?? "30", 10),
|
||||||
});
|
});
|
||||||
@@ -296,6 +318,7 @@ export async function settingsRoutes(app: FastifyInstance) {
|
|||||||
highStockDays: body.highStockDays ?? 180,
|
highStockDays: body.highStockDays ?? 180,
|
||||||
language: body.language ?? "en",
|
language: body.language ?? "en",
|
||||||
stockCalculationMode: body.stockCalculationMode ?? "automatic",
|
stockCalculationMode: body.stockCalculationMode ?? "automatic",
|
||||||
|
shareStockStatus: body.shareStockStatus ?? true,
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -114,7 +114,9 @@ export async function shareRoutes(app: FastifyInstance) {
|
|||||||
const takenByArray = parseTakenByJson(med.takenByJson);
|
const takenByArray = parseTakenByJson(med.takenByJson);
|
||||||
|
|
||||||
const totalPills =
|
const totalPills =
|
||||||
med.packCount * med.blistersPerPack * med.pillsPerBlister + med.looseTablets + (med.stockAdjustment ?? 0);
|
(med.packageType ?? "blister") === "bottle"
|
||||||
|
? med.looseTablets + (med.stockAdjustment ?? 0)
|
||||||
|
: med.packCount * med.blistersPerPack * med.pillsPerBlister + med.looseTablets + (med.stockAdjustment ?? 0);
|
||||||
return {
|
return {
|
||||||
id: med.id,
|
id: med.id,
|
||||||
name: med.name,
|
name: med.name,
|
||||||
@@ -123,6 +125,7 @@ export async function shareRoutes(app: FastifyInstance) {
|
|||||||
doseUnit: med.doseUnit ?? "mg",
|
doseUnit: med.doseUnit ?? "mg",
|
||||||
imageUrl: med.imageUrl,
|
imageUrl: med.imageUrl,
|
||||||
totalPills,
|
totalPills,
|
||||||
|
packageType: med.packageType ?? "blister",
|
||||||
packCount: med.packCount,
|
packCount: med.packCount,
|
||||||
blistersPerPack: med.blistersPerPack,
|
blistersPerPack: med.blistersPerPack,
|
||||||
looseTablets: med.looseTablets,
|
looseTablets: med.looseTablets,
|
||||||
@@ -132,6 +135,8 @@ export async function shareRoutes(app: FastifyInstance) {
|
|||||||
blisters, // Legacy format for backward compat
|
blisters, // Legacy format for backward compat
|
||||||
dismissedUntil: med.dismissedUntil,
|
dismissedUntil: med.dismissedUntil,
|
||||||
updatedAt: med.updatedAt, // For filtering out doses from previous schedule configurations
|
updatedAt: med.updatedAt, // For filtering out doses from previous schedule configurations
|
||||||
|
lastStockCorrectionAt: med.lastStockCorrectionAt?.getTime() ?? null,
|
||||||
|
stockAdjustment: med.stockAdjustment ?? 0,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -142,7 +147,13 @@ export async function shareRoutes(app: FastifyInstance) {
|
|||||||
medications: medicationsWithBlisters,
|
medications: medicationsWithBlisters,
|
||||||
stockThresholds: {
|
stockThresholds: {
|
||||||
lowStockDays: settings?.lowStockDays ?? 30,
|
lowStockDays: settings?.lowStockDays ?? 30,
|
||||||
|
normalStockDays: settings?.normalStockDays ?? 60,
|
||||||
|
highStockDays: settings?.highStockDays ?? 90,
|
||||||
|
reminderDaysBefore: settings?.reminderDaysBefore ?? 7,
|
||||||
|
expiryWarningDays: settings?.expiryWarningDays ?? 90,
|
||||||
},
|
},
|
||||||
|
stockCalculationMode: (settings?.stockCalculationMode as "automatic" | "manual") ?? "automatic",
|
||||||
|
shareStockStatus: settings?.shareStockStatus ?? true,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,16 @@ import nodemailer from "nodemailer";
|
|||||||
import { db } from "../db/client.js";
|
import { db } from "../db/client.js";
|
||||||
import { getDataDir } from "../db/db-utils.js";
|
import { getDataDir } from "../db/db-utils.js";
|
||||||
import { doseTracking, medications } from "../db/schema.js";
|
import { doseTracking, medications } from "../db/schema.js";
|
||||||
import { getDateLocale, getTranslations, type Language, t } from "../i18n/translations.js";
|
import {
|
||||||
|
getDateLocale,
|
||||||
|
getFooterHtml,
|
||||||
|
getFooterPlain,
|
||||||
|
getTranslations,
|
||||||
|
type Language,
|
||||||
|
t,
|
||||||
|
} from "../i18n/translations.js";
|
||||||
import { getAllUserSettings, sendShoutrrrNotification, type UserSettings } from "../routes/settings.js";
|
import { getAllUserSettings, sendShoutrrrNotification, type UserSettings } from "../routes/settings.js";
|
||||||
|
import type { ServiceLogger } from "../utils/logger.js";
|
||||||
// Import shared utilities
|
// Import shared utilities
|
||||||
import {
|
import {
|
||||||
cleanOldIntakeReminders,
|
cleanOldIntakeReminders,
|
||||||
@@ -149,7 +157,7 @@ async function sendIntakeReminderEmail(
|
|||||||
|
|
||||||
<hr style="border: none; border-top: 1px solid #e5e7eb; margin: 16px 0;" />
|
<hr style="border: none; border-top: 1px solid #e5e7eb; margin: 16px 0;" />
|
||||||
<p style="color: #9ca3af; font-size: 11px; margin: 0;">
|
<p style="color: #9ca3af; font-size: 11px; margin: 0;">
|
||||||
${tr.intakeReminder.footer}
|
${getFooterHtml(language)}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -178,7 +186,7 @@ ${intakes
|
|||||||
.join("\n")}
|
.join("\n")}
|
||||||
|
|
||||||
---
|
---
|
||||||
${tr.intakeReminder.footer}`;
|
${getFooterPlain(language)}`;
|
||||||
|
|
||||||
const subject = isRepeat
|
const subject = isRepeat
|
||||||
? `[Reminder] ${t(tr.intakeReminder.subject, { medications: intakes.map((i) => i.medName).join(", ") })}`
|
? `[Reminder] ${t(tr.intakeReminder.subject, { medications: intakes.map((i) => i.medName).join(", ") })}`
|
||||||
@@ -210,21 +218,18 @@ ${tr.intakeReminder.footer}`;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkAndSendIntakeReminders(logger: {
|
async function checkAndSendIntakeReminders(logger: ServiceLogger): Promise<void> {
|
||||||
info: (msg: string) => void;
|
logger.debug(`[IntakeReminder] Checking for intake reminders...`);
|
||||||
error: (msg: string) => void;
|
|
||||||
}): Promise<void> {
|
|
||||||
logger.info(`[IntakeReminder] Checking for intake reminders...`);
|
|
||||||
|
|
||||||
// Get all user settings to iterate over each user
|
// Get all user settings to iterate over each user
|
||||||
const allUserSettings = await getAllUserSettings();
|
const allUserSettings = await getAllUserSettings();
|
||||||
|
|
||||||
if (allUserSettings.length === 0) {
|
if (allUserSettings.length === 0) {
|
||||||
logger.info(`[IntakeReminder] No users with settings found`);
|
logger.debug(`[IntakeReminder] No users with settings found`);
|
||||||
return; // No users with settings
|
return; // No users with settings
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`[IntakeReminder] Found ${allUserSettings.length} users to check`);
|
logger.debug(`[IntakeReminder] Found ${allUserSettings.length} users to check`);
|
||||||
|
|
||||||
for (const userSettings of allUserSettings) {
|
for (const userSettings of allUserSettings) {
|
||||||
await checkAndSendIntakeRemindersForUser(userSettings, logger);
|
await checkAndSendIntakeRemindersForUser(userSettings, logger);
|
||||||
@@ -233,12 +238,12 @@ async function checkAndSendIntakeReminders(logger: {
|
|||||||
|
|
||||||
async function checkAndSendIntakeRemindersForUser(
|
async function checkAndSendIntakeRemindersForUser(
|
||||||
settings: UserSettings & { userId: number },
|
settings: UserSettings & { userId: number },
|
||||||
logger: { info: (msg: string) => void; error: (msg: string) => void }
|
logger: ServiceLogger
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const language = settings.language;
|
const language = settings.language;
|
||||||
const tr = getTranslations(language);
|
const tr = getTranslations(language);
|
||||||
|
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] Checking user ${settings.userId} - repeat:${settings.repeatRemindersEnabled} skip:${settings.skipRemindersForTakenDoses}`
|
`[IntakeReminder] Checking user ${settings.userId} - repeat:${settings.repeatRemindersEnabled} skip:${settings.skipRemindersForTakenDoses}`
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -247,13 +252,13 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
const shoutrrrEnabled = settings.shoutrrrEnabled && settings.shoutrrrUrl && settings.shoutrrrIntakeReminders;
|
const shoutrrrEnabled = settings.shoutrrrEnabled && settings.shoutrrrUrl && settings.shoutrrrIntakeReminders;
|
||||||
|
|
||||||
if (!emailEnabled && !shoutrrrEnabled) {
|
if (!emailEnabled && !shoutrrrEnabled) {
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] User ${settings.userId}: No intake notifications enabled (email:${emailEnabled}, shoutrrr:${shoutrrrEnabled})`
|
`[IntakeReminder] User ${settings.userId}: No intake notifications enabled (email:${emailEnabled}, shoutrrr:${shoutrrrEnabled})`
|
||||||
);
|
);
|
||||||
return; // No intake reminder notifications enabled for this user
|
return; // No intake reminder notifications enabled for this user
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] User ${settings.userId}: Notifications enabled (email:${emailEnabled}, shoutrrr:${shoutrrrEnabled})`
|
`[IntakeReminder] User ${settings.userId}: Notifications enabled (email:${emailEnabled}, shoutrrr:${shoutrrrEnabled})`
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -266,11 +271,13 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
const medsWithReminders = rows.filter((row) => row.intakeRemindersEnabled);
|
const medsWithReminders = rows.filter((row) => row.intakeRemindersEnabled);
|
||||||
|
|
||||||
if (medsWithReminders.length === 0) {
|
if (medsWithReminders.length === 0) {
|
||||||
logger.info(`[IntakeReminder] User ${settings.userId}: No medications have reminders enabled`);
|
logger.debug(`[IntakeReminder] User ${settings.userId}: No medications have reminders enabled`);
|
||||||
return; // No medications have reminders enabled for this user
|
return; // No medications have reminders enabled for this user
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`[IntakeReminder] User ${settings.userId}: Found ${medsWithReminders.length} medications with reminders`);
|
logger.debug(
|
||||||
|
`[IntakeReminder] User ${settings.userId}: Found ${medsWithReminders.length} medications with reminders`
|
||||||
|
);
|
||||||
|
|
||||||
const state = loadIntakeReminderState();
|
const state = loadIntakeReminderState();
|
||||||
const allUpcoming: (UpcomingIntake & { medicationId: number; blisterIndex: number })[] = [];
|
const allUpcoming: (UpcomingIntake & { medicationId: number; blisterIndex: number })[] = [];
|
||||||
@@ -285,7 +292,7 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
const todayEnd = new Date(now.toLocaleString("en-US", { timeZone: tz }));
|
const todayEnd = new Date(now.toLocaleString("en-US", { timeZone: tz }));
|
||||||
todayEnd.setHours(23, 59, 59, 999);
|
todayEnd.setHours(23, 59, 59, 999);
|
||||||
|
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] User ${settings.userId}: Today range: ${todayStart.toISOString()} to ${todayEnd.toISOString()}`
|
`[IntakeReminder] User ${settings.userId}: Today range: ${todayStart.toISOString()} to ${todayEnd.toISOString()}`
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -300,7 +307,7 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
// Medication-level takenBy (for fallback/display purposes)
|
// Medication-level takenBy (for fallback/display purposes)
|
||||||
const medicationTakenBy = parseTakenByJson(med.takenByJson);
|
const medicationTakenBy = parseTakenByJson(med.takenByJson);
|
||||||
|
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] User ${settings.userId}: Processing medication "${med.name}" with ${intakes.length} intakes`
|
`[IntakeReminder] User ${settings.userId}: Processing medication "${med.name}" with ${intakes.length} intakes`
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -308,7 +315,7 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
const intakesWithReminders = intakes.filter((intake, idx) => {
|
const intakesWithReminders = intakes.filter((intake, idx) => {
|
||||||
const hasReminder = intake.intakeRemindersEnabled || med.intakeRemindersEnabled;
|
const hasReminder = intake.intakeRemindersEnabled || med.intakeRemindersEnabled;
|
||||||
if (!hasReminder) {
|
if (!hasReminder) {
|
||||||
logger.info(`[IntakeReminder] User ${settings.userId}: Intake ${idx} has reminders disabled, skipping`);
|
logger.debug(`[IntakeReminder] User ${settings.userId}: Intake ${idx} has reminders disabled, skipping`);
|
||||||
}
|
}
|
||||||
return hasReminder;
|
return hasReminder;
|
||||||
});
|
});
|
||||||
@@ -316,7 +323,7 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
// Process each intake separately to track blisterIndex
|
// Process each intake separately to track blisterIndex
|
||||||
intakesWithReminders.forEach((intake, blisterIndex) => {
|
intakesWithReminders.forEach((intake, blisterIndex) => {
|
||||||
const actualIndex = intakes.indexOf(intake); // Get the actual index in original array
|
const actualIndex = intakes.indexOf(intake); // Get the actual index in original array
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] User ${settings.userId}: Intake ${actualIndex} - start: ${intake.start}, every: ${intake.every} days, usage: ${intake.usage}, takenBy: ${intake.takenBy || "(none)"}`
|
`[IntakeReminder] User ${settings.userId}: Intake ${actualIndex} - start: ${intake.start}, every: ${intake.every} days, usage: ${intake.usage}, takenBy: ${intake.takenBy || "(none)"}`
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -333,7 +340,7 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
med.id,
|
med.id,
|
||||||
med.doseUnit ?? "mg"
|
med.doseUnit ?? "mg"
|
||||||
);
|
);
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] User ${settings.userId}: Intake ${actualIndex} found ${upcomingIntakes.length} upcoming intakes (reminder window)`
|
`[IntakeReminder] User ${settings.userId}: Intake ${actualIndex} found ${upcomingIntakes.length} upcoming intakes (reminder window)`
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -358,13 +365,13 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
med.id,
|
med.id,
|
||||||
med.doseUnit ?? "mg"
|
med.doseUnit ?? "mg"
|
||||||
);
|
);
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] User ${settings.userId}: Intake ${actualIndex} - all today's intakes: ${allTodaysIntakes.length}, times: ${allTodaysIntakes.map((i) => i.intakeTime.toISOString()).join(", ")}`
|
`[IntakeReminder] User ${settings.userId}: Intake ${actualIndex} - all today's intakes: ${allTodaysIntakes.length}, times: ${allTodaysIntakes.map((i) => i.intakeTime.toISOString()).join(", ")}`
|
||||||
);
|
);
|
||||||
const missedIntakes = allTodaysIntakes.filter(
|
const missedIntakes = allTodaysIntakes.filter(
|
||||||
(todayIntake) => todayIntake.intakeTime.getTime() < now.getTime()
|
(todayIntake) => todayIntake.intakeTime.getTime() < now.getTime()
|
||||||
);
|
);
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] User ${settings.userId}: Intake ${actualIndex} found ${missedIntakes.length} missed intakes (past intake time)`
|
`[IntakeReminder] User ${settings.userId}: Intake ${actualIndex} found ${missedIntakes.length} missed intakes (past intake time)`
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -383,10 +390,10 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`[IntakeReminder] User ${settings.userId}: Total ${allUpcoming.length} intakes for today`);
|
logger.debug(`[IntakeReminder] User ${settings.userId}: Total ${allUpcoming.length} intakes for today`);
|
||||||
|
|
||||||
if (allUpcoming.length === 0) {
|
if (allUpcoming.length === 0) {
|
||||||
logger.info(`[IntakeReminder] User ${settings.userId}: No intakes for today`);
|
logger.debug(`[IntakeReminder] User ${settings.userId}: No intakes for today`);
|
||||||
return; // No upcoming intakes for today
|
return; // No upcoming intakes for today
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -409,15 +416,23 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
if (!existingEntry) {
|
if (!existingEntry) {
|
||||||
// New dose - send first reminder
|
// New dose - send first reminder
|
||||||
if (isIntakePast) {
|
if (isIntakePast) {
|
||||||
// Already missed - this is first nagging reminder (count=1)
|
// Intake time already passed and we have no state entry — this means the scheduler
|
||||||
remindersToSend.push({ ...intake, currentSendCount: 1, maxReminders, isAdvanceReminder: false });
|
// was not aware of this intake before it happened (e.g., user just enabled reminders).
|
||||||
logger.info(
|
// Seed the state as already handled so repeat reminders can track from here,
|
||||||
`[IntakeReminder] User ${settings.userId}: First nagging for missed "${intake.medName}" at ${intake.intakeTimeStr} (1/${maxReminders})`
|
// but do NOT send a notification for intakes that were missed before tracking started.
|
||||||
|
state.reminders[key] = {
|
||||||
|
firstSentAt: nowMs,
|
||||||
|
lastSentAt: nowMs,
|
||||||
|
sendCount: 0,
|
||||||
|
advanceSent: false,
|
||||||
|
};
|
||||||
|
logger.debug(
|
||||||
|
`[IntakeReminder] User ${settings.userId}: Seeding state for past "${intake.medName}" at ${intake.intakeTimeStr} (no notification — first detection)`
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// Upcoming - this is advance reminder (no counter)
|
// Upcoming - this is advance reminder (no counter)
|
||||||
remindersToSend.push({ ...intake, currentSendCount: 0, maxReminders, isAdvanceReminder: true });
|
remindersToSend.push({ ...intake, currentSendCount: 0, maxReminders, isAdvanceReminder: true });
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] User ${settings.userId}: Advance reminder for "${intake.medName}" at ${intake.intakeTimeStr}`
|
`[IntakeReminder] User ${settings.userId}: Advance reminder for "${intake.medName}" at ${intake.intakeTimeStr}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -432,13 +447,13 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
|
|
||||||
if (currentNaggingCount >= maxReminders) {
|
if (currentNaggingCount >= maxReminders) {
|
||||||
// Max nagging reminders reached - stop
|
// Max nagging reminders reached - stop
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] User ${settings.userId}: Max nagging (${maxReminders}) reached for "${intake.medName}" at ${intake.intakeTimeStr}`
|
`[IntakeReminder] User ${settings.userId}: Max nagging (${maxReminders}) reached for "${intake.medName}" at ${intake.intakeTimeStr}`
|
||||||
);
|
);
|
||||||
} else if (timeSinceLastReminder >= intervalMs) {
|
} else if (timeSinceLastReminder >= intervalMs) {
|
||||||
const nextSendCount = currentNaggingCount + 1;
|
const nextSendCount = currentNaggingCount + 1;
|
||||||
remindersToSend.push({ ...intake, currentSendCount: nextSendCount, maxReminders, isAdvanceReminder: false });
|
remindersToSend.push({ ...intake, currentSendCount: nextSendCount, maxReminders, isAdvanceReminder: false });
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] User ${settings.userId}: Nagging reminder for "${intake.medName}" at ${intake.intakeTimeStr} (${nextSendCount}/${maxReminders})`
|
`[IntakeReminder] User ${settings.userId}: Nagging reminder for "${intake.medName}" at ${intake.intakeTimeStr} (${nextSendCount}/${maxReminders})`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -478,7 +493,7 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
const doseId = `${intake.medicationId}-${intake.blisterIndex}-${dateOnlyMs}-${intake.takenBy}`;
|
const doseId = `${intake.medicationId}-${intake.blisterIndex}-${dateOnlyMs}-${intake.takenBy}`;
|
||||||
const isTaken = takenDoseIds.has(doseId);
|
const isTaken = takenDoseIds.has(doseId);
|
||||||
if (isTaken) {
|
if (isTaken) {
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] User ${settings.userId}: Skipping "${intake.medName}" - dose ${doseId} already taken`
|
`[IntakeReminder] User ${settings.userId}: Skipping "${intake.medName}" - dose ${doseId} already taken`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -488,7 +503,7 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
const doseId = `${intake.medicationId}-${intake.blisterIndex}-${dateOnlyMs}`;
|
const doseId = `${intake.medicationId}-${intake.blisterIndex}-${dateOnlyMs}`;
|
||||||
const isTaken = takenDoseIds.has(doseId);
|
const isTaken = takenDoseIds.has(doseId);
|
||||||
if (isTaken) {
|
if (isTaken) {
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[IntakeReminder] User ${settings.userId}: Skipping "${intake.medName}" - dose ${doseId} already taken`
|
`[IntakeReminder] User ${settings.userId}: Skipping "${intake.medName}" - dose ${doseId} already taken`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -497,7 +512,7 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (remindersToSend.length === 0) {
|
if (remindersToSend.length === 0) {
|
||||||
logger.info(`[IntakeReminder] User ${settings.userId}: All doses taken, skipping reminders`);
|
logger.debug(`[IntakeReminder] User ${settings.userId}: All doses taken, skipping reminders`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -551,7 +566,10 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
if (hasNaggingReminder && highestSendCount > 0) {
|
if (hasNaggingReminder && highestSendCount > 0) {
|
||||||
// Nagging reminder - show counter
|
// Nagging reminder - show counter
|
||||||
const counterStr = `(${highestSendCount}/${maxReminderCount})`;
|
const counterStr = `(${highestSendCount}/${maxReminderCount})`;
|
||||||
title = language === "de" ? `⚠️ Medikamenten-Erinnerung ${counterStr}` : `⚠️ Medication Reminder ${counterStr}`;
|
title =
|
||||||
|
language === "de"
|
||||||
|
? `⚠️ Erinnerung: Medikamenteneinnahme ${counterStr}`
|
||||||
|
: `⚠️ Reminder: Medication intake ${counterStr}`;
|
||||||
} else {
|
} else {
|
||||||
// Advance reminder - no counter
|
// Advance reminder - no counter
|
||||||
title = t(tr.push.intakeTitle, { minutes: REMINDER_MINUTES_BEFORE });
|
title = t(tr.push.intakeTitle, { minutes: REMINDER_MINUTES_BEFORE });
|
||||||
@@ -590,7 +608,9 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
}
|
}
|
||||||
return `• ${i.medName}${takenByStr}: ${dosage} @ ${i.intakeTimeStr}`;
|
return `• ${i.medName}${takenByStr}: ${dosage} @ ${i.intakeTimeStr}`;
|
||||||
})
|
})
|
||||||
.join("\n") + repeatNote;
|
.join("\n") +
|
||||||
|
repeatNote +
|
||||||
|
`\n\n---\n${getFooterPlain(language)}`;
|
||||||
|
|
||||||
const result = await sendShoutrrrNotification(settings.shoutrrrUrl!, title, message);
|
const result = await sendShoutrrrNotification(settings.shoutrrrUrl!, title, message);
|
||||||
shoutrrrSuccess = result.success;
|
shoutrrrSuccess = result.success;
|
||||||
@@ -668,10 +688,7 @@ async function checkAndSendIntakeRemindersForUser(
|
|||||||
|
|
||||||
let intakeCheckInterval: NodeJS.Timeout | null = null;
|
let intakeCheckInterval: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
export function startIntakeReminderScheduler(logger: {
|
export function startIntakeReminderScheduler(logger: ServiceLogger): void {
|
||||||
info: (msg: string) => void;
|
|
||||||
error: (msg: string) => void;
|
|
||||||
}): void {
|
|
||||||
logger.info(`[IntakeReminder] Starting intake reminder scheduler (checks every minute)...`);
|
logger.info(`[IntakeReminder] Starting intake reminder scheduler (checks every minute)...`);
|
||||||
|
|
||||||
// Run immediately on start
|
// Run immediately on start
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import nodemailer from "nodemailer";
|
|||||||
import { db } from "../db/client.js";
|
import { db } from "../db/client.js";
|
||||||
import { getDataDir } from "../db/db-utils.js";
|
import { getDataDir } from "../db/db-utils.js";
|
||||||
import { medications, userSettings } from "../db/schema.js";
|
import { medications, userSettings } from "../db/schema.js";
|
||||||
import { getTranslations, type Language, t } from "../i18n/translations.js";
|
import { getFooterHtml, getFooterPlain, getTranslations, type Language, t } from "../i18n/translations.js";
|
||||||
import { getAllUserSettings, sendShoutrrrNotification, type UserSettings } from "../routes/settings.js";
|
import { getAllUserSettings, sendShoutrrrNotification, type UserSettings } from "../routes/settings.js";
|
||||||
|
import type { ServiceLogger } from "../utils/logger.js";
|
||||||
// Import shared utilities
|
// Import shared utilities
|
||||||
import {
|
import {
|
||||||
type Blister,
|
type Blister,
|
||||||
@@ -63,6 +63,7 @@ export function updateReminderSentTime(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update user settings in database when reminder is sent
|
// Update user settings in database when reminder is sent
|
||||||
|
// Stock and intake reminders are tracked separately so neither overwrites the other
|
||||||
export async function updateUserReminderSentTime(
|
export async function updateUserReminderSentTime(
|
||||||
userId: number,
|
userId: number,
|
||||||
type: "stock" | "intake" = "stock",
|
type: "stock" | "intake" = "stock",
|
||||||
@@ -71,16 +72,30 @@ export async function updateUserReminderSentTime(
|
|||||||
takenBy?: string
|
takenBy?: string
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const now = new Date().toISOString();
|
const now = new Date().toISOString();
|
||||||
await db
|
if (type === "stock") {
|
||||||
.update(userSettings)
|
// Write to dedicated stock reminder columns only — do NOT touch the shared
|
||||||
.set({
|
// lastNotificationType column, as that would block intake reminder display
|
||||||
lastAutoEmailSent: now,
|
await db
|
||||||
lastNotificationType: type,
|
.update(userSettings)
|
||||||
lastNotificationChannel: channel,
|
.set({
|
||||||
lastReminderMedName: medName ?? null,
|
lastStockReminderSent: now,
|
||||||
lastReminderTakenBy: takenBy ?? null,
|
lastStockReminderChannel: channel,
|
||||||
})
|
lastStockReminderMedNames: medName ?? null,
|
||||||
.where(eq(userSettings.userId, userId));
|
})
|
||||||
|
.where(eq(userSettings.userId, userId));
|
||||||
|
} else {
|
||||||
|
// Write to intake reminder columns
|
||||||
|
await db
|
||||||
|
.update(userSettings)
|
||||||
|
.set({
|
||||||
|
lastAutoEmailSent: now,
|
||||||
|
lastNotificationType: type,
|
||||||
|
lastNotificationChannel: channel,
|
||||||
|
lastReminderMedName: medName ?? null,
|
||||||
|
lastReminderTakenBy: takenBy ?? null,
|
||||||
|
})
|
||||||
|
.where(eq(userSettings.userId, userId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseBlistersFromRow(row: { usageJson: string; everyJson: string; startJson: string }): Blister[] {
|
function parseBlistersFromRow(row: { usageJson: string; everyJson: string; startJson: string }): Blister[] {
|
||||||
@@ -106,7 +121,9 @@ async function getMedicationsNeedingReminder(
|
|||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
const blisters = parseBlistersFromRow(row);
|
const blisters = parseBlistersFromRow(row);
|
||||||
const totalPills =
|
const totalPills =
|
||||||
row.packCount * row.blistersPerPack * row.pillsPerBlister + row.looseTablets + (row.stockAdjustment ?? 0);
|
(row.packageType ?? "blister") === "bottle"
|
||||||
|
? row.looseTablets + (row.stockAdjustment ?? 0)
|
||||||
|
: row.packCount * row.blistersPerPack * row.pillsPerBlister + row.looseTablets + (row.stockAdjustment ?? 0);
|
||||||
const { daysLeft, depletionDate } = calculateDepletionInfo({ count: totalPills, blisters }, language);
|
const { daysLeft, depletionDate } = calculateDepletionInfo({ count: totalPills, blisters }, language);
|
||||||
|
|
||||||
// Check if medication runs out within reminderDaysBefore days
|
// Check if medication runs out within reminderDaysBefore days
|
||||||
@@ -189,7 +206,7 @@ async function sendReminderEmail(
|
|||||||
|
|
||||||
<hr style="border: none; border-top: 1px solid #e5e7eb; margin: 16px 0;" />
|
<hr style="border: none; border-top: 1px solid #e5e7eb; margin: 16px 0;" />
|
||||||
<p style="color: #9ca3af; font-size: 11px; margin: 0;">
|
<p style="color: #9ca3af; font-size: 11px; margin: 0;">
|
||||||
${tr.stockReminder.footer}
|
${getFooterHtml(language)}
|
||||||
</p>
|
</p>
|
||||||
${isRepeatDaily ? `<p style="color: #9ca3af; font-size: 11px; margin: 8px 0 0 0; font-style: italic;">${tr.stockReminder.repeatDailyNote}</p>` : ""}
|
${isRepeatDaily ? `<p style="color: #9ca3af; font-size: 11px; margin: 8px 0 0 0; font-style: italic;">${tr.stockReminder.repeatDailyNote}</p>` : ""}
|
||||||
</div>
|
</div>
|
||||||
@@ -203,7 +220,7 @@ ${tr.stockReminder.description}
|
|||||||
${lowStock.map((r) => `${r.name}: ${r.medsLeft} ${tr.common.pills}, ${r.daysLeft ?? 0} ${tr.common.days}, ${tr.stockReminder.tableHeaders.runsOut}: ${r.depletionDate ?? tr.common.soon}`).join("\n")}
|
${lowStock.map((r) => `${r.name}: ${r.medsLeft} ${tr.common.pills}, ${r.daysLeft ?? 0} ${tr.common.days}, ${tr.stockReminder.tableHeaders.runsOut}: ${r.depletionDate ?? tr.common.soon}`).join("\n")}
|
||||||
|
|
||||||
---
|
---
|
||||||
${tr.stockReminder.footer}${isRepeatDaily ? `\n\n${tr.stockReminder.repeatDailyNote}` : ""}`;
|
${getFooterPlain(language)}${isRepeatDaily ? `\n\n${tr.stockReminder.repeatDailyNote}` : ""}`;
|
||||||
|
|
||||||
const subjectPlural = lowStock.length === 1 ? "" : language === "de" ? "e" : "s";
|
const subjectPlural = lowStock.length === 1 ? "" : language === "de" ? "e" : "s";
|
||||||
const subject = t(tr.stockReminder.subject, { count: lowStock.length, s: subjectPlural, e: subjectPlural });
|
const subject = t(tr.stockReminder.subject, { count: lowStock.length, s: subjectPlural, e: subjectPlural });
|
||||||
@@ -234,15 +251,12 @@ ${tr.stockReminder.footer}${isRepeatDaily ? `\n\n${tr.stockReminder.repeatDailyN
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkAndSendReminder(logger: {
|
async function checkAndSendReminder(logger: ServiceLogger): Promise<void> {
|
||||||
info: (msg: string) => void;
|
|
||||||
error: (msg: string) => void;
|
|
||||||
}): Promise<void> {
|
|
||||||
// Get all user settings to iterate over each user
|
// Get all user settings to iterate over each user
|
||||||
const allUserSettings = await getAllUserSettings();
|
const allUserSettings = await getAllUserSettings();
|
||||||
|
|
||||||
if (allUserSettings.length === 0) {
|
if (allUserSettings.length === 0) {
|
||||||
logger.info("[Reminder] No users with settings found");
|
logger.debug("[Reminder] No users with settings found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,7 +267,7 @@ async function checkAndSendReminder(logger: {
|
|||||||
|
|
||||||
async function checkAndSendReminderForUser(
|
async function checkAndSendReminderForUser(
|
||||||
settings: UserSettings & { userId: number },
|
settings: UserSettings & { userId: number },
|
||||||
logger: { info: (msg: string) => void; error: (msg: string) => void }
|
logger: ServiceLogger
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const language = settings.language;
|
const language = settings.language;
|
||||||
const tr = getTranslations(language);
|
const tr = getTranslations(language);
|
||||||
@@ -306,30 +320,30 @@ async function checkAndSendReminderForUser(
|
|||||||
|
|
||||||
// Send Shoutrrr notification if enabled
|
// Send Shoutrrr notification if enabled
|
||||||
if (shoutrrrEnabled) {
|
if (shoutrrrEnabled) {
|
||||||
// Separate empty from low stock medications
|
// Separate empty from critical stock medications (all auto-reminder meds are critical by definition)
|
||||||
const emptyMeds = allLowStock.filter((m) => m.medsLeft <= 0);
|
const emptyMeds = allLowStock.filter((m) => m.medsLeft <= 0);
|
||||||
const lowMeds = allLowStock.filter((m) => m.medsLeft > 0);
|
const criticalMeds = allLowStock.filter((m) => m.medsLeft > 0);
|
||||||
|
|
||||||
// Build clear title
|
// Build clear title
|
||||||
const titleParts: string[] = [];
|
const titleParts: string[] = [];
|
||||||
if (emptyMeds.length > 0) {
|
if (emptyMeds.length > 0) {
|
||||||
titleParts.push(`🚨 ${emptyMeds.length} ${tr.push.empty || "Empty"}`);
|
titleParts.push(`🚨 ${emptyMeds.length} ${tr.push.empty || "Empty"}`);
|
||||||
}
|
}
|
||||||
if (lowMeds.length > 0) {
|
if (criticalMeds.length > 0) {
|
||||||
titleParts.push(`⚠️ ${lowMeds.length} ${tr.push.low || "Low"}`);
|
titleParts.push(`🚨 ${criticalMeds.length} ${tr.push.critical || "Critical"}`);
|
||||||
}
|
}
|
||||||
const title = `MedAssist: ${titleParts.join(", ")} - ${tr.push.reorderNow || "Reorder Now!"}`;
|
const title = `MedAssist: ${titleParts.join(", ")} - ${tr.push.reorderNow || "Reorder Now!"}`;
|
||||||
|
|
||||||
// Build clear message with sections
|
// Build clear message with sections
|
||||||
const messageParts: string[] = [];
|
const messageParts: string[] = [];
|
||||||
if (emptyMeds.length > 0) {
|
if (emptyMeds.length > 0) {
|
||||||
messageParts.push(`🚨 ${tr.push.emptySection || "EMPTY (reorder immediately)"}:`);
|
messageParts.push(`🚨 ${tr.push.emptySection || "Empty (reorder immediately)"}:`);
|
||||||
emptyMeds.forEach((m) => messageParts.push(` • ${m.name}`));
|
emptyMeds.forEach((m) => messageParts.push(` • ${m.name}`));
|
||||||
}
|
}
|
||||||
if (lowMeds.length > 0) {
|
if (criticalMeds.length > 0) {
|
||||||
if (emptyMeds.length > 0) messageParts.push("");
|
if (emptyMeds.length > 0) messageParts.push("");
|
||||||
messageParts.push(`⚠️ ${tr.push.lowSection || "RUNNING LOW (reorder soon)"}:`);
|
messageParts.push(`🚨 ${tr.push.criticalSection || "Running critically low"}:`);
|
||||||
lowMeds.forEach((m) =>
|
criticalMeds.forEach((m) =>
|
||||||
messageParts.push(
|
messageParts.push(
|
||||||
` • ${m.name}: ${t(tr.push.pillsLeft, { count: m.medsLeft })}, ${t(tr.push.daysLeft, { count: m.daysLeft ?? 0 })}`
|
` • ${m.name}: ${t(tr.push.pillsLeft, { count: m.medsLeft })}, ${t(tr.push.daysLeft, { count: m.daysLeft ?? 0 })}`
|
||||||
)
|
)
|
||||||
@@ -341,7 +355,7 @@ async function checkAndSendReminderForUser(
|
|||||||
messageParts.push(tr.push.repeatDailyNote);
|
messageParts.push(tr.push.repeatDailyNote);
|
||||||
}
|
}
|
||||||
|
|
||||||
const message = messageParts.join("\n");
|
const message = messageParts.join("\n") + `\n\n---\n${getFooterPlain(language)}`;
|
||||||
|
|
||||||
const result = await sendShoutrrrNotification(settings.shoutrrrUrl!, title, message);
|
const result = await sendShoutrrrNotification(settings.shoutrrrUrl!, title, message);
|
||||||
shoutrrrSuccess = result.success;
|
shoutrrrSuccess = result.success;
|
||||||
@@ -375,7 +389,7 @@ async function checkAndSendReminderForUser(
|
|||||||
|
|
||||||
let schedulerTimeout: NodeJS.Timeout | null = null;
|
let schedulerTimeout: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
function scheduleNextCheck(logger: { info: (msg: string) => void; error: (msg: string) => void }): void {
|
function scheduleNextCheck(logger: ServiceLogger): void {
|
||||||
const msUntilNext = getMsUntilNextCheck(REMINDER_HOUR);
|
const msUntilNext = getMsUntilNextCheck(REMINDER_HOUR);
|
||||||
const nextTime = getNextScheduledTime(REMINDER_HOUR);
|
const nextTime = getNextScheduledTime(REMINDER_HOUR);
|
||||||
|
|
||||||
@@ -386,7 +400,7 @@ function scheduleNextCheck(logger: { info: (msg: string) => void; error: (msg: s
|
|||||||
nextScheduledCheck: nextTime.toISOString(),
|
nextScheduledCheck: nextTime.toISOString(),
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(
|
logger.debug(
|
||||||
`[Reminder] Next check scheduled for ${formatInTimezone(nextTime)} (${getTimezone()}) (in ${Math.round(msUntilNext / 1000 / 60)} minutes)`
|
`[Reminder] Next check scheduled for ${formatInTimezone(nextTime)} (${getTimezone()}) (in ${Math.round(msUntilNext / 1000 / 60)} minutes)`
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -397,7 +411,7 @@ function scheduleNextCheck(logger: { info: (msg: string) => void; error: (msg: s
|
|||||||
}, msUntilNext);
|
}, msUntilNext);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function startReminderScheduler(logger: { info: (msg: string) => void; error: (msg: string) => void }): void {
|
export function startReminderScheduler(logger: ServiceLogger): void {
|
||||||
logger.info(`[Reminder] Starting reminder scheduler (timezone: ${getTimezone()})...`);
|
logger.info(`[Reminder] Starting reminder scheduler (timezone: ${getTimezone()})...`);
|
||||||
|
|
||||||
// Check if we need to run immediately (missed today's check)
|
// Check if we need to run immediately (missed today's check)
|
||||||
|
|||||||
@@ -126,11 +126,15 @@ async function createSchema(client: Client) {
|
|||||||
expiry_warning_days integer NOT NULL DEFAULT 90,
|
expiry_warning_days integer NOT NULL DEFAULT 90,
|
||||||
language text NOT NULL DEFAULT 'en',
|
language text NOT NULL DEFAULT 'en',
|
||||||
stock_calculation_mode text NOT NULL DEFAULT 'automatic',
|
stock_calculation_mode text NOT NULL DEFAULT 'automatic',
|
||||||
|
share_stock_status integer NOT NULL DEFAULT 1,
|
||||||
last_auto_email_sent text,
|
last_auto_email_sent text,
|
||||||
last_notification_type text,
|
last_notification_type text,
|
||||||
last_notification_channel text,
|
last_notification_channel text,
|
||||||
last_reminder_med_name text,
|
last_reminder_med_name text,
|
||||||
last_reminder_taken_by text,
|
last_reminder_taken_by text,
|
||||||
|
last_stock_reminder_sent text,
|
||||||
|
last_stock_reminder_channel text,
|
||||||
|
last_stock_reminder_med_names text,
|
||||||
updated_at integer NOT NULL DEFAULT (strftime('%s','now')),
|
updated_at integer NOT NULL DEFAULT (strftime('%s','now')),
|
||||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||||
)`,
|
)`,
|
||||||
@@ -2214,4 +2218,250 @@ describe("E2E Tests with Real Routes", () => {
|
|||||||
expect(medsResponse.json()[0].packCount).toBe(10);
|
expect(medsResponse.json()[0].packCount).toBe(10);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Package Type (bottle vs blister) Tests
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
describe("Package type handling (bottle vs blister)", () => {
|
||||||
|
const bottleMedication = {
|
||||||
|
name: "Vitamin D Drops",
|
||||||
|
packageType: "bottle",
|
||||||
|
packCount: 0,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 1,
|
||||||
|
looseTablets: 120,
|
||||||
|
blisters: [{ usage: 1, every: 1, start: "2025-01-01T08:00:00.000Z" }],
|
||||||
|
};
|
||||||
|
|
||||||
|
const blisterMedication = {
|
||||||
|
name: "Aspirin Blister",
|
||||||
|
packageType: "blister",
|
||||||
|
packCount: 2,
|
||||||
|
blistersPerPack: 3,
|
||||||
|
pillsPerBlister: 10,
|
||||||
|
looseTablets: 5,
|
||||||
|
blisters: [{ usage: 1, every: 1, start: "2025-01-01T08:00:00.000Z" }],
|
||||||
|
};
|
||||||
|
|
||||||
|
it("should create and return bottle type medication", async () => {
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/medications",
|
||||||
|
payload: bottleMedication,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
const data = response.json();
|
||||||
|
expect(data.packageType).toBe("bottle");
|
||||||
|
expect(data.looseTablets).toBe(120);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return packageType in shared schedule for bottle type", async () => {
|
||||||
|
// Create bottle medication with takenBy
|
||||||
|
await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/medications",
|
||||||
|
payload: { ...bottleMedication, takenBy: ["Daniel"] },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create share token
|
||||||
|
const shareResponse = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/share",
|
||||||
|
payload: { takenBy: "Daniel", scheduleDays: 30 },
|
||||||
|
});
|
||||||
|
expect(shareResponse.statusCode).toBe(200);
|
||||||
|
const { token } = shareResponse.json();
|
||||||
|
|
||||||
|
// Get shared schedule
|
||||||
|
const scheduleResponse = await app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: `/share/${token}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(scheduleResponse.statusCode).toBe(200);
|
||||||
|
const data = scheduleResponse.json();
|
||||||
|
expect(data.medications).toHaveLength(1);
|
||||||
|
expect(data.medications[0].packageType).toBe("bottle");
|
||||||
|
// Bottle totalPills = looseTablets + stockAdjustment (no blister math)
|
||||||
|
expect(data.medications[0].totalPills).toBe(120);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should calculate correct totalPills for shared blister medication", async () => {
|
||||||
|
await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/medications",
|
||||||
|
payload: { ...blisterMedication, takenBy: ["Daniel"] },
|
||||||
|
});
|
||||||
|
|
||||||
|
const shareResponse = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/share",
|
||||||
|
payload: { takenBy: "Daniel", scheduleDays: 30 },
|
||||||
|
});
|
||||||
|
const { token } = shareResponse.json();
|
||||||
|
|
||||||
|
const scheduleResponse = await app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: `/share/${token}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(scheduleResponse.statusCode).toBe(200);
|
||||||
|
const data = scheduleResponse.json();
|
||||||
|
expect(data.medications).toHaveLength(1);
|
||||||
|
expect(data.medications[0].packageType).toBe("blister");
|
||||||
|
// Blister totalPills = 2 * 3 * 10 + 5 = 65
|
||||||
|
expect(data.medications[0].totalPills).toBe(65);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should calculate correct refill totalPillsAdded for bottle type", async () => {
|
||||||
|
const createResponse = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/medications",
|
||||||
|
payload: bottleMedication,
|
||||||
|
});
|
||||||
|
const medId = createResponse.json().id;
|
||||||
|
|
||||||
|
// Refill bottle: only loosePillsAdded matters, packs should add 0 pills
|
||||||
|
const refillResponse = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: `/medications/${medId}/refill`,
|
||||||
|
payload: { packsAdded: 0, loosePillsAdded: 30 },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(refillResponse.statusCode).toBe(200);
|
||||||
|
const data = refillResponse.json();
|
||||||
|
expect(data.refill.totalPillsAdded).toBe(30);
|
||||||
|
// newStock.totalPills should be looseTablets only (no blister math)
|
||||||
|
expect(data.newStock.totalPills).toBe(150); // 120 + 30
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should calculate correct refill totalPillsAdded for blister type", async () => {
|
||||||
|
const createResponse = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/medications",
|
||||||
|
payload: blisterMedication,
|
||||||
|
});
|
||||||
|
const medId = createResponse.json().id;
|
||||||
|
|
||||||
|
// Refill blister: 1 pack = 3 blisters * 10 pills = 30 pills + 5 loose
|
||||||
|
const refillResponse = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: `/medications/${medId}/refill`,
|
||||||
|
payload: { packsAdded: 1, loosePillsAdded: 5 },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(refillResponse.statusCode).toBe(200);
|
||||||
|
const data = refillResponse.json();
|
||||||
|
expect(data.refill.totalPillsAdded).toBe(35); // 1*30 + 5
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return correct totalPillsAdded in refill history for bottle type", async () => {
|
||||||
|
const createResponse = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/medications",
|
||||||
|
payload: bottleMedication,
|
||||||
|
});
|
||||||
|
const medId = createResponse.json().id;
|
||||||
|
|
||||||
|
// Add refill
|
||||||
|
await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: `/medications/${medId}/refill`,
|
||||||
|
payload: { packsAdded: 0, loosePillsAdded: 25 },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get refill history
|
||||||
|
const historyResponse = await app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: `/medications/${medId}/refills`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(historyResponse.statusCode).toBe(200);
|
||||||
|
const refills = historyResponse.json();
|
||||||
|
expect(refills).toHaveLength(1);
|
||||||
|
// For bottle type, totalPillsAdded = loosePillsAdded only
|
||||||
|
expect(refills[0].totalPillsAdded).toBe(25);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should export and import bottle type medication correctly", async () => {
|
||||||
|
// Create bottle medication
|
||||||
|
await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/medications",
|
||||||
|
payload: bottleMedication,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Export
|
||||||
|
const exportResponse = await app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: "/export",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(exportResponse.statusCode).toBe(200);
|
||||||
|
const exportData = exportResponse.json();
|
||||||
|
expect(exportData.medications).toHaveLength(1);
|
||||||
|
expect(exportData.medications[0].inventory.packageType).toBe("bottle");
|
||||||
|
expect(exportData.medications[0].inventory.looseTablets).toBe(120);
|
||||||
|
|
||||||
|
// Clear and re-import
|
||||||
|
await clearData(testClient);
|
||||||
|
await testClient.execute(
|
||||||
|
"INSERT INTO users (id, username, auth_provider) VALUES (999999999, '__anonymous__', 'anonymous')"
|
||||||
|
);
|
||||||
|
|
||||||
|
const importResponse = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/import",
|
||||||
|
payload: exportData,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(importResponse.statusCode).toBe(200);
|
||||||
|
expect(importResponse.json().success).toBe(true);
|
||||||
|
|
||||||
|
// Verify imported medication has correct packageType
|
||||||
|
const medsResponse = await app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: "/medications",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(medsResponse.json()).toHaveLength(1);
|
||||||
|
const med = medsResponse.json()[0];
|
||||||
|
expect(med.name).toBe("Vitamin D Drops");
|
||||||
|
expect(med.packageType).toBe("bottle");
|
||||||
|
expect(med.looseTablets).toBe(120);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should default to blister when importing without packageType", async () => {
|
||||||
|
const importData = {
|
||||||
|
version: "1.0",
|
||||||
|
exportedAt: new Date().toISOString(),
|
||||||
|
medications: [
|
||||||
|
{
|
||||||
|
_exportId: "med-1",
|
||||||
|
name: "Old Export Med",
|
||||||
|
inventory: { packCount: 2, blistersPerPack: 3, pillsPerBlister: 10, looseTablets: 0 },
|
||||||
|
schedules: [{ usage: 1, every: 1, start: "2025-01-01T08:00:00.000Z" }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
const importResponse = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/import",
|
||||||
|
payload: importData,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(importResponse.statusCode).toBe(200);
|
||||||
|
|
||||||
|
const medsResponse = await app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: "/medications",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(medsResponse.json()).toHaveLength(1);
|
||||||
|
expect(medsResponse.json()[0].packageType).toBe("blister");
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -121,11 +121,15 @@ async function createSchema(client: Client) {
|
|||||||
expiry_warning_days integer NOT NULL DEFAULT 90,
|
expiry_warning_days integer NOT NULL DEFAULT 90,
|
||||||
language text NOT NULL DEFAULT 'en',
|
language text NOT NULL DEFAULT 'en',
|
||||||
stock_calculation_mode text NOT NULL DEFAULT 'automatic',
|
stock_calculation_mode text NOT NULL DEFAULT 'automatic',
|
||||||
|
share_stock_status integer NOT NULL DEFAULT 1,
|
||||||
last_auto_email_sent text,
|
last_auto_email_sent text,
|
||||||
last_notification_type text,
|
last_notification_type text,
|
||||||
last_notification_channel text,
|
last_notification_channel text,
|
||||||
last_reminder_med_name text,
|
last_reminder_med_name text,
|
||||||
last_reminder_taken_by text,
|
last_reminder_taken_by text,
|
||||||
|
last_stock_reminder_sent text,
|
||||||
|
last_stock_reminder_channel text,
|
||||||
|
last_stock_reminder_med_names text,
|
||||||
updated_at integer NOT NULL DEFAULT (strftime('%s','now')),
|
updated_at integer NOT NULL DEFAULT (strftime('%s','now')),
|
||||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||||
)`,
|
)`,
|
||||||
@@ -1181,6 +1185,92 @@ describe("Integration Tests", () => {
|
|||||||
expect(data[0].plannerUsage).toBe(10);
|
expect(data[0].plannerUsage).toBe(10);
|
||||||
expect(data[0].enough).toBe(true); // 45 > 10
|
expect(data[0].enough).toBe(true); // 45 > 10
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should use user-selected start date, not current time (fix asymmetric counting)", async () => {
|
||||||
|
// Regression test: When a planner range starts today, the old code used
|
||||||
|
// max(now, start) as the effective start. If now was between the morning
|
||||||
|
// dose (07:00) and evening dose (20:00), morning was skipped but evening
|
||||||
|
// counted, giving an asymmetric result (e.g., 5 instead of 6).
|
||||||
|
//
|
||||||
|
// Example: medication with daily morning (07:00) + evening (20:00) intakes,
|
||||||
|
// planner range [today 01:00, today+3 01:00).
|
||||||
|
// Old code at 15:00: morning 07:00 < 15:00 → skipped, evening 20:00 ≥ 15:00 → counted
|
||||||
|
// Result: 2 morning + 3 evening = 5 instead of 3+3 = 6.
|
||||||
|
|
||||||
|
// Use a past start date so the intakes predate the planner range
|
||||||
|
const intakeStart = "2025-01-01T07:00:00.000Z";
|
||||||
|
const intakeEvening = "2025-01-01T20:00:00.000Z";
|
||||||
|
|
||||||
|
// Plan range: Feb 9 00:00 to Feb 12 00:00 UTC (3 full days)
|
||||||
|
const planStart = "2026-02-09T00:00:00.000Z";
|
||||||
|
const planEnd = "2026-02-12T00:00:00.000Z";
|
||||||
|
|
||||||
|
await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/medications",
|
||||||
|
payload: {
|
||||||
|
name: "Twice Daily Med Asymmetric",
|
||||||
|
packCount: 5,
|
||||||
|
blistersPerPack: 5,
|
||||||
|
pillsPerBlister: 10,
|
||||||
|
blisters: [
|
||||||
|
{ usage: 1, every: 1, start: intakeStart },
|
||||||
|
{ usage: 1, every: 1, start: intakeEvening },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/medications/usage",
|
||||||
|
payload: {
|
||||||
|
startDate: planStart,
|
||||||
|
endDate: planEnd,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
const data = response.json();
|
||||||
|
// Both morning and evening should have exactly 3 occurrences each
|
||||||
|
// (Feb 9, 10, 11) for a total of 6, regardless of current time
|
||||||
|
expect(data[0].plannerUsage).toBe(6);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle planner range starting before blister start", async () => {
|
||||||
|
// Blister starts on Feb 10, planner range starts Feb 9
|
||||||
|
// Should only count doses from Feb 10 onwards
|
||||||
|
const intakeMorning = "2026-02-10T07:00:00.000Z";
|
||||||
|
const intakeEvening = "2026-02-10T20:00:00.000Z";
|
||||||
|
|
||||||
|
await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/medications",
|
||||||
|
payload: {
|
||||||
|
name: "Recent Start Med",
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 30,
|
||||||
|
blisters: [
|
||||||
|
{ usage: 1, every: 1, start: intakeMorning },
|
||||||
|
{ usage: 1, every: 1, start: intakeEvening },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/medications/usage",
|
||||||
|
payload: {
|
||||||
|
startDate: "2026-02-09T00:00:00.000Z",
|
||||||
|
endDate: "2026-02-12T00:00:00.000Z",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
const data = response.json();
|
||||||
|
// Only Feb 10 and Feb 11 have doses (blister starts Feb 10)
|
||||||
|
expect(data[0].plannerUsage).toBe(4); // 2 days × 2 intakes
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -110,11 +110,15 @@ async function createSchema(client: Client) {
|
|||||||
expiry_warning_days integer NOT NULL DEFAULT 90,
|
expiry_warning_days integer NOT NULL DEFAULT 90,
|
||||||
language text NOT NULL DEFAULT 'en',
|
language text NOT NULL DEFAULT 'en',
|
||||||
stock_calculation_mode text NOT NULL DEFAULT 'automatic',
|
stock_calculation_mode text NOT NULL DEFAULT 'automatic',
|
||||||
|
share_stock_status integer NOT NULL DEFAULT 1,
|
||||||
last_auto_email_sent text,
|
last_auto_email_sent text,
|
||||||
last_notification_type text,
|
last_notification_type text,
|
||||||
last_notification_channel text,
|
last_notification_channel text,
|
||||||
last_reminder_med_name text,
|
last_reminder_med_name text,
|
||||||
last_reminder_taken_by text,
|
last_reminder_taken_by text,
|
||||||
|
last_stock_reminder_sent text,
|
||||||
|
last_stock_reminder_channel text,
|
||||||
|
last_stock_reminder_med_names text,
|
||||||
updated_at integer NOT NULL DEFAULT (strftime('%s','now')),
|
updated_at integer NOT NULL DEFAULT (strftime('%s','now')),
|
||||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||||
)`,
|
)`,
|
||||||
@@ -161,21 +165,6 @@ describe("Planner Routes", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("POST /planner/send-email", () => {
|
describe("POST /planner/send-email", () => {
|
||||||
it("should reject request with missing email", async () => {
|
|
||||||
const response = await app.inject({
|
|
||||||
method: "POST",
|
|
||||||
url: "/planner/send-email",
|
|
||||||
payload: {
|
|
||||||
from: "2025-01-01",
|
|
||||||
until: "2025-01-31",
|
|
||||||
rows: [{ medicationName: "Test", totalPills: 10, plannerUsage: 5, enough: true }],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(response.statusCode).toBe(400);
|
|
||||||
expect(response.json()).toEqual({ error: "Missing email or planner data" });
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should reject request with missing rows", async () => {
|
it("should reject request with missing rows", async () => {
|
||||||
const response = await app.inject({
|
const response = await app.inject({
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -189,10 +178,16 @@ describe("Planner Routes", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(response.statusCode).toBe(400);
|
expect(response.statusCode).toBe(400);
|
||||||
expect(response.json()).toEqual({ error: "Missing email or planner data" });
|
expect(response.json()).toEqual({ error: "Missing planner data" });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should reject when SMTP is not configured", async () => {
|
it("should return error when no notification channels configured", async () => {
|
||||||
|
// User settings exist but email/shoutrrr disabled
|
||||||
|
await testClient.execute({
|
||||||
|
sql: `INSERT INTO user_settings (user_id, email_enabled, shoutrrr_enabled, language) VALUES (?, 0, 0, 'en')`,
|
||||||
|
args: [999999999],
|
||||||
|
});
|
||||||
|
|
||||||
const response = await app.inject({
|
const response = await app.inject({
|
||||||
method: "POST",
|
method: "POST",
|
||||||
url: "/planner/send-email",
|
url: "/planner/send-email",
|
||||||
@@ -217,7 +212,7 @@ describe("Planner Routes", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(response.statusCode).toBe(400);
|
expect(response.statusCode).toBe(400);
|
||||||
expect(response.json()).toEqual({ error: "SMTP not configured" });
|
expect(response.json()).toEqual({ error: "No notification channels configured" });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should send email successfully when SMTP is configured", async () => {
|
it("should send email successfully when SMTP is configured", async () => {
|
||||||
@@ -226,6 +221,12 @@ describe("Planner Routes", () => {
|
|||||||
process.env.SMTP_USER = "user@test.com";
|
process.env.SMTP_USER = "user@test.com";
|
||||||
process.env.SMTP_PASS = "password";
|
process.env.SMTP_PASS = "password";
|
||||||
|
|
||||||
|
// Enable email in user settings
|
||||||
|
await testClient.execute({
|
||||||
|
sql: `INSERT INTO user_settings (user_id, email_enabled, shoutrrr_enabled, language) VALUES (?, 1, 0, 'en')`,
|
||||||
|
args: [999999999],
|
||||||
|
});
|
||||||
|
|
||||||
mockSendMail.mockResolvedValueOnce({ messageId: "123" });
|
mockSendMail.mockResolvedValueOnce({ messageId: "123" });
|
||||||
|
|
||||||
const response = await app.inject({
|
const response = await app.inject({
|
||||||
@@ -253,7 +254,7 @@ describe("Planner Routes", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
expect(response.json()).toEqual({ success: true, message: "Email sent successfully" });
|
expect(response.json()).toEqual({ success: true, message: "Notification sent via email" });
|
||||||
expect(mockSendMail).toHaveBeenCalledTimes(1);
|
expect(mockSendMail).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
@@ -267,6 +268,11 @@ describe("Planner Routes", () => {
|
|||||||
process.env.SMTP_USER = "user@test.com";
|
process.env.SMTP_USER = "user@test.com";
|
||||||
process.env.SMTP_PASS = "password";
|
process.env.SMTP_PASS = "password";
|
||||||
|
|
||||||
|
await testClient.execute({
|
||||||
|
sql: `INSERT INTO user_settings (user_id, email_enabled, shoutrrr_enabled, language) VALUES (?, 1, 0, 'en')`,
|
||||||
|
args: [999999999],
|
||||||
|
});
|
||||||
|
|
||||||
mockSendMail.mockResolvedValueOnce({ messageId: "123" });
|
mockSendMail.mockResolvedValueOnce({ messageId: "123" });
|
||||||
|
|
||||||
const response = await app.inject({
|
const response = await app.inject({
|
||||||
@@ -308,7 +314,7 @@ describe("Planner Routes", () => {
|
|||||||
|
|
||||||
// Check that HTML contains out of stock warning
|
// Check that HTML contains out of stock warning
|
||||||
const mailCall = mockSendMail.mock.calls[0][0];
|
const mailCall = mockSendMail.mock.calls[0][0];
|
||||||
expect(mailCall.html).toContain("Out of Stock");
|
expect(mailCall.html).toContain("Empty");
|
||||||
expect(mailCall.html).toContain("1 medication");
|
expect(mailCall.html).toContain("1 medication");
|
||||||
|
|
||||||
delete process.env.SMTP_HOST;
|
delete process.env.SMTP_HOST;
|
||||||
@@ -321,6 +327,11 @@ describe("Planner Routes", () => {
|
|||||||
process.env.SMTP_USER = "user@test.com";
|
process.env.SMTP_USER = "user@test.com";
|
||||||
process.env.SMTP_PASS = "password";
|
process.env.SMTP_PASS = "password";
|
||||||
|
|
||||||
|
await testClient.execute({
|
||||||
|
sql: `INSERT INTO user_settings (user_id, email_enabled, shoutrrr_enabled, language) VALUES (?, 1, 0, 'en')`,
|
||||||
|
args: [999999999],
|
||||||
|
});
|
||||||
|
|
||||||
mockSendMail.mockRejectedValueOnce(new Error("Connection refused"));
|
mockSendMail.mockRejectedValueOnce(new Error("Connection refused"));
|
||||||
|
|
||||||
const response = await app.inject({
|
const response = await app.inject({
|
||||||
@@ -347,7 +358,7 @@ describe("Planner Routes", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(response.statusCode).toBe(500);
|
expect(response.statusCode).toBe(500);
|
||||||
expect(response.json().error).toContain("Failed to send email");
|
expect(response.json().error).toContain("Email:");
|
||||||
expect(response.json().error).toContain("Connection refused");
|
expect(response.json().error).toContain("Connection refused");
|
||||||
|
|
||||||
delete process.env.SMTP_HOST;
|
delete process.env.SMTP_HOST;
|
||||||
@@ -360,6 +371,12 @@ describe("Planner Routes", () => {
|
|||||||
process.env.SMTP_USER = "user@test.com";
|
process.env.SMTP_USER = "user@test.com";
|
||||||
process.env.SMTP_PASS = "password";
|
process.env.SMTP_PASS = "password";
|
||||||
|
|
||||||
|
// User settings with German language
|
||||||
|
await testClient.execute({
|
||||||
|
sql: `INSERT INTO user_settings (user_id, email_enabled, shoutrrr_enabled, language) VALUES (?, 1, 0, 'de')`,
|
||||||
|
args: [999999999],
|
||||||
|
});
|
||||||
|
|
||||||
mockSendMail.mockResolvedValueOnce({ messageId: "123" });
|
mockSendMail.mockResolvedValueOnce({ messageId: "123" });
|
||||||
|
|
||||||
const response = await app.inject({
|
const response = await app.inject({
|
||||||
@@ -390,12 +407,178 @@ describe("Planner Routes", () => {
|
|||||||
|
|
||||||
// German date format should be used
|
// German date format should be used
|
||||||
const mailCall = mockSendMail.mock.calls[0][0];
|
const mailCall = mockSendMail.mock.calls[0][0];
|
||||||
expect(mailCall.subject).toContain("Supply Overview");
|
expect(mailCall.subject).toContain("Bestandsübersicht");
|
||||||
|
|
||||||
delete process.env.SMTP_HOST;
|
delete process.env.SMTP_HOST;
|
||||||
delete process.env.SMTP_USER;
|
delete process.env.SMTP_USER;
|
||||||
delete process.env.SMTP_PASS;
|
delete process.env.SMTP_PASS;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should send push notification when shoutrrr is enabled", async () => {
|
||||||
|
await testClient.execute({
|
||||||
|
sql: `INSERT INTO user_settings (user_id, email_enabled, shoutrrr_enabled, shoutrrr_url, language) VALUES (?, 0, 1, 'ntfy://localhost/test', 'en')`,
|
||||||
|
args: [999999999],
|
||||||
|
});
|
||||||
|
|
||||||
|
mockSendShoutrrr.mockResolvedValueOnce({ success: true });
|
||||||
|
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/planner/send-email",
|
||||||
|
payload: {
|
||||||
|
email: "test@example.com",
|
||||||
|
from: "2025-01-01",
|
||||||
|
until: "2025-01-31",
|
||||||
|
rows: [
|
||||||
|
{
|
||||||
|
medicationId: 1,
|
||||||
|
medicationName: "Aspirin",
|
||||||
|
totalPills: 30,
|
||||||
|
plannerUsage: 10,
|
||||||
|
blisterSize: 10,
|
||||||
|
blistersNeeded: 1,
|
||||||
|
fullBlisters: 3,
|
||||||
|
loosePills: 0,
|
||||||
|
enough: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.json()).toEqual({ success: true, message: "Notification sent via push" });
|
||||||
|
expect(mockSendShoutrrr).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
// Verify push message contains medication info
|
||||||
|
const [_url, title, message] = mockSendShoutrrr.mock.calls[0];
|
||||||
|
expect(title).toContain("Supply Overview");
|
||||||
|
expect(message).toContain("Aspirin");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should send both email and push when both enabled", async () => {
|
||||||
|
process.env.SMTP_HOST = "smtp.test.com";
|
||||||
|
process.env.SMTP_USER = "user@test.com";
|
||||||
|
process.env.SMTP_PASS = "password";
|
||||||
|
|
||||||
|
await testClient.execute({
|
||||||
|
sql: `INSERT INTO user_settings (user_id, email_enabled, shoutrrr_enabled, shoutrrr_url, language) VALUES (?, 1, 1, 'ntfy://localhost/test', 'en')`,
|
||||||
|
args: [999999999],
|
||||||
|
});
|
||||||
|
|
||||||
|
mockSendMail.mockResolvedValueOnce({ messageId: "123" });
|
||||||
|
mockSendShoutrrr.mockResolvedValueOnce({ success: true });
|
||||||
|
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/planner/send-email",
|
||||||
|
payload: {
|
||||||
|
email: "test@example.com",
|
||||||
|
from: "2025-01-01",
|
||||||
|
until: "2025-01-31",
|
||||||
|
rows: [
|
||||||
|
{
|
||||||
|
medicationId: 1,
|
||||||
|
medicationName: "Aspirin",
|
||||||
|
totalPills: 5,
|
||||||
|
plannerUsage: 30,
|
||||||
|
blisterSize: 10,
|
||||||
|
blistersNeeded: 3,
|
||||||
|
fullBlisters: 0,
|
||||||
|
loosePills: 5,
|
||||||
|
enough: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.json()).toEqual({ success: true, message: "Notification sent via email and push" });
|
||||||
|
expect(mockSendMail).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockSendShoutrrr).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
// Verify push message contains out of stock info
|
||||||
|
const [_url, _title, message] = mockSendShoutrrr.mock.calls[0];
|
||||||
|
expect(message).toContain("Aspirin");
|
||||||
|
expect(message).toContain("Empty");
|
||||||
|
|
||||||
|
delete process.env.SMTP_HOST;
|
||||||
|
delete process.env.SMTP_USER;
|
||||||
|
delete process.env.SMTP_PASS;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should send push with German translations", async () => {
|
||||||
|
await testClient.execute({
|
||||||
|
sql: `INSERT INTO user_settings (user_id, email_enabled, shoutrrr_enabled, shoutrrr_url, language) VALUES (?, 0, 1, 'ntfy://localhost/test', 'de')`,
|
||||||
|
args: [999999999],
|
||||||
|
});
|
||||||
|
|
||||||
|
mockSendShoutrrr.mockResolvedValueOnce({ success: true });
|
||||||
|
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/planner/send-email",
|
||||||
|
payload: {
|
||||||
|
email: "test@example.com",
|
||||||
|
from: "2025-01-01",
|
||||||
|
until: "2025-01-31",
|
||||||
|
rows: [
|
||||||
|
{
|
||||||
|
medicationId: 1,
|
||||||
|
medicationName: "Aspirin",
|
||||||
|
totalPills: 5,
|
||||||
|
plannerUsage: 30,
|
||||||
|
blisterSize: 10,
|
||||||
|
blistersNeeded: 3,
|
||||||
|
fullBlisters: 0,
|
||||||
|
loosePills: 5,
|
||||||
|
enough: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
|
||||||
|
// Check German translations in push
|
||||||
|
const [_url, title] = mockSendShoutrrr.mock.calls[0];
|
||||||
|
expect(title).toContain("Bestandsübersicht");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle push error gracefully", async () => {
|
||||||
|
await testClient.execute({
|
||||||
|
sql: `INSERT INTO user_settings (user_id, email_enabled, shoutrrr_enabled, shoutrrr_url, language) VALUES (?, 0, 1, 'ntfy://localhost/test', 'en')`,
|
||||||
|
args: [999999999],
|
||||||
|
});
|
||||||
|
|
||||||
|
mockSendShoutrrr.mockResolvedValueOnce({ success: false, error: "Connection failed" });
|
||||||
|
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/planner/send-email",
|
||||||
|
payload: {
|
||||||
|
email: "test@example.com",
|
||||||
|
from: "2025-01-01",
|
||||||
|
until: "2025-01-31",
|
||||||
|
rows: [
|
||||||
|
{
|
||||||
|
medicationId: 1,
|
||||||
|
medicationName: "Aspirin",
|
||||||
|
totalPills: 30,
|
||||||
|
plannerUsage: 10,
|
||||||
|
blisterSize: 10,
|
||||||
|
blistersNeeded: 1,
|
||||||
|
fullBlisters: 3,
|
||||||
|
loosePills: 0,
|
||||||
|
enough: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(500);
|
||||||
|
expect(response.json().error).toContain("Push:");
|
||||||
|
expect(response.json().error).toContain("Connection failed");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("POST /reminder/send-email", () => {
|
describe("POST /reminder/send-email", () => {
|
||||||
@@ -503,10 +686,10 @@ describe("Planner Routes", () => {
|
|||||||
|
|
||||||
expect(response.statusCode).toBe(200);
|
expect(response.statusCode).toBe(200);
|
||||||
|
|
||||||
// Check email contains EMPTY warning
|
// Check email contains empty warning
|
||||||
const mailCall = mockSendMail.mock.calls[0][0];
|
const mailCall = mockSendMail.mock.calls[0][0];
|
||||||
expect(mailCall.subject).toContain("Empty");
|
expect(mailCall.subject).toContain("Empty");
|
||||||
expect(mailCall.html).toContain("EMPTY");
|
expect(mailCall.html).toContain("empty");
|
||||||
|
|
||||||
delete process.env.SMTP_HOST;
|
delete process.env.SMTP_HOST;
|
||||||
delete process.env.SMTP_USER;
|
delete process.env.SMTP_USER;
|
||||||
@@ -541,7 +724,7 @@ describe("Planner Routes", () => {
|
|||||||
|
|
||||||
const mailCall = mockSendMail.mock.calls[0][0];
|
const mailCall = mockSendMail.mock.calls[0][0];
|
||||||
expect(mailCall.subject).toContain("Empty");
|
expect(mailCall.subject).toContain("Empty");
|
||||||
expect(mailCall.subject).toContain("Running Low");
|
expect(mailCall.subject).toContain("Critical");
|
||||||
|
|
||||||
delete process.env.SMTP_HOST;
|
delete process.env.SMTP_HOST;
|
||||||
delete process.env.SMTP_USER;
|
delete process.env.SMTP_USER;
|
||||||
@@ -698,5 +881,103 @@ describe("Planner Routes", () => {
|
|||||||
expect(response.json().error).toContain("Push:");
|
expect(response.json().error).toContain("Push:");
|
||||||
expect(response.json().error).toContain("Network error");
|
expect(response.json().error).toContain("Network error");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should differentiate critical and low stock in push notification", async () => {
|
||||||
|
await testClient.execute({
|
||||||
|
sql: `INSERT INTO user_settings (user_id, email_enabled, shoutrrr_enabled, shoutrrr_url, language) VALUES (?, 0, 1, 'ntfy://localhost/test', 'en')`,
|
||||||
|
args: [999999999],
|
||||||
|
});
|
||||||
|
|
||||||
|
mockSendShoutrrr.mockResolvedValueOnce({ success: true });
|
||||||
|
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/reminder/send-email",
|
||||||
|
payload: {
|
||||||
|
email: "test@example.com",
|
||||||
|
lowStock: [
|
||||||
|
{ name: "Aspirin", medsLeft: 5, daysLeft: 3, depletionDate: "2025-01-03", isCritical: true },
|
||||||
|
{ name: "Ibuprofen", medsLeft: 49, daysLeft: 24, depletionDate: "2025-01-24", isCritical: false },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
|
||||||
|
const [_url, title, message] = mockSendShoutrrr.mock.calls[0];
|
||||||
|
// Title should contain both Critical and Low labels
|
||||||
|
expect(title).toContain("Critical");
|
||||||
|
expect(title).toContain("Low");
|
||||||
|
// Message should have separate sections
|
||||||
|
expect(message).toContain("Running critically low");
|
||||||
|
expect(message).toContain("Aspirin");
|
||||||
|
expect(message).toContain("Running low");
|
||||||
|
expect(message).toContain("Ibuprofen");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should differentiate critical and low stock in email", async () => {
|
||||||
|
process.env.SMTP_HOST = "smtp.test.com";
|
||||||
|
process.env.SMTP_USER = "user@test.com";
|
||||||
|
process.env.SMTP_PASS = "password";
|
||||||
|
|
||||||
|
await testClient.execute({
|
||||||
|
sql: `INSERT INTO user_settings (user_id, email_enabled, shoutrrr_enabled, language) VALUES (?, 1, 0, 'en')`,
|
||||||
|
args: [999999999],
|
||||||
|
});
|
||||||
|
|
||||||
|
mockSendMail.mockResolvedValueOnce({ messageId: "123" });
|
||||||
|
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/reminder/send-email",
|
||||||
|
payload: {
|
||||||
|
email: "test@example.com",
|
||||||
|
lowStock: [
|
||||||
|
{ name: "Aspirin", medsLeft: 5, daysLeft: 3, depletionDate: "2025-01-03", isCritical: true },
|
||||||
|
{ name: "Ibuprofen", medsLeft: 49, daysLeft: 24, depletionDate: "2025-01-24", isCritical: false },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
|
||||||
|
const mailCall = mockSendMail.mock.calls[0][0];
|
||||||
|
// Subject should contain both Critical and Low
|
||||||
|
expect(mailCall.subject).toContain("Critical");
|
||||||
|
expect(mailCall.subject).toContain("Low");
|
||||||
|
// HTML should have separate alert boxes
|
||||||
|
expect(mailCall.html).toContain("critically low");
|
||||||
|
expect(mailCall.html).toContain("running low");
|
||||||
|
|
||||||
|
delete process.env.SMTP_HOST;
|
||||||
|
delete process.env.SMTP_USER;
|
||||||
|
delete process.env.SMTP_PASS;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should label all meds as critical when isCritical not provided", async () => {
|
||||||
|
await testClient.execute({
|
||||||
|
sql: `INSERT INTO user_settings (user_id, email_enabled, shoutrrr_enabled, shoutrrr_url, language) VALUES (?, 0, 1, 'ntfy://localhost/test', 'en')`,
|
||||||
|
args: [999999999],
|
||||||
|
});
|
||||||
|
|
||||||
|
mockSendShoutrrr.mockResolvedValueOnce({ success: true });
|
||||||
|
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "POST",
|
||||||
|
url: "/reminder/send-email",
|
||||||
|
payload: {
|
||||||
|
email: "test@example.com",
|
||||||
|
lowStock: [{ name: "Aspirin", medsLeft: 5, daysLeft: 3, depletionDate: "2025-01-03" }],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
|
||||||
|
const [_url, title, message] = mockSendShoutrrr.mock.calls[0];
|
||||||
|
// Should be treated as critical (backwards compat)
|
||||||
|
expect(title).toContain("Critical");
|
||||||
|
expect(title).not.toContain("Low");
|
||||||
|
expect(message).toContain("Running critically low");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ async function registerSettingsRoutes(ctx: TestContext) {
|
|||||||
expiryWarningDays: 90,
|
expiryWarningDays: 90,
|
||||||
language: "en",
|
language: "en",
|
||||||
stockCalculationMode: "automatic",
|
stockCalculationMode: "automatic",
|
||||||
|
shareStockStatus: true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,6 +77,7 @@ async function registerSettingsRoutes(ctx: TestContext) {
|
|||||||
expiryWarningDays: s.expiry_warning_days,
|
expiryWarningDays: s.expiry_warning_days,
|
||||||
language: s.language,
|
language: s.language,
|
||||||
stockCalculationMode: s.stock_calculation_mode,
|
stockCalculationMode: s.stock_calculation_mode,
|
||||||
|
shareStockStatus: Boolean(s.share_stock_status ?? 1),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -102,6 +104,7 @@ async function registerSettingsRoutes(ctx: TestContext) {
|
|||||||
expiryWarningDays?: number;
|
expiryWarningDays?: number;
|
||||||
language?: string;
|
language?: string;
|
||||||
stockCalculationMode?: "automatic" | "manual";
|
stockCalculationMode?: "automatic" | "manual";
|
||||||
|
shareStockStatus?: boolean;
|
||||||
};
|
};
|
||||||
}>("/settings", async (request, reply) => {
|
}>("/settings", async (request, reply) => {
|
||||||
const userId = 1;
|
const userId = 1;
|
||||||
@@ -150,8 +153,8 @@ async function registerSettingsRoutes(ctx: TestContext) {
|
|||||||
reminder_days_before, repeat_daily_reminders, skip_reminders_for_taken_doses,
|
reminder_days_before, repeat_daily_reminders, skip_reminders_for_taken_doses,
|
||||||
repeat_reminders_enabled, reminder_repeat_interval_minutes, max_nagging_reminders,
|
repeat_reminders_enabled, reminder_repeat_interval_minutes, max_nagging_reminders,
|
||||||
low_stock_days, normal_stock_days, high_stock_days,
|
low_stock_days, normal_stock_days, high_stock_days,
|
||||||
expiry_warning_days, language, stock_calculation_mode
|
expiry_warning_days, language, stock_calculation_mode, share_stock_status
|
||||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||||
args: [
|
args: [
|
||||||
userId,
|
userId,
|
||||||
body.emailEnabled ? 1 : 0,
|
body.emailEnabled ? 1 : 0,
|
||||||
@@ -174,6 +177,7 @@ async function registerSettingsRoutes(ctx: TestContext) {
|
|||||||
body.expiryWarningDays ?? 90,
|
body.expiryWarningDays ?? 90,
|
||||||
body.language || "en",
|
body.language || "en",
|
||||||
body.stockCalculationMode || "automatic",
|
body.stockCalculationMode || "automatic",
|
||||||
|
body.shareStockStatus !== false ? 1 : 0,
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -200,6 +204,7 @@ async function registerSettingsRoutes(ctx: TestContext) {
|
|||||||
expiry_warning_days = ?,
|
expiry_warning_days = ?,
|
||||||
language = ?,
|
language = ?,
|
||||||
stock_calculation_mode = ?,
|
stock_calculation_mode = ?,
|
||||||
|
share_stock_status = ?,
|
||||||
updated_at = strftime('%s','now')
|
updated_at = strftime('%s','now')
|
||||||
WHERE user_id = ?`,
|
WHERE user_id = ?`,
|
||||||
args: [
|
args: [
|
||||||
@@ -223,6 +228,7 @@ async function registerSettingsRoutes(ctx: TestContext) {
|
|||||||
body.expiryWarningDays ?? 90,
|
body.expiryWarningDays ?? 90,
|
||||||
body.language || "en",
|
body.language || "en",
|
||||||
body.stockCalculationMode || "automatic",
|
body.stockCalculationMode || "automatic",
|
||||||
|
body.shareStockStatus !== false ? 1 : 0,
|
||||||
userId,
|
userId,
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
@@ -542,6 +548,64 @@ describe("Settings API", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Share Stock Status
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
describe("Share Stock Status", () => {
|
||||||
|
it("should default to true (show stock on shared links)", async () => {
|
||||||
|
const response = await ctx.app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: "/settings",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.json().shareStockStatus).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should disable share stock status", async () => {
|
||||||
|
const response = await ctx.app.inject({
|
||||||
|
method: "PUT",
|
||||||
|
url: "/settings",
|
||||||
|
payload: { shareStockStatus: false },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
|
||||||
|
const getResponse = await ctx.app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: "/settings",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(getResponse.json().shareStockStatus).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should re-enable share stock status", async () => {
|
||||||
|
// Disable first
|
||||||
|
await ctx.app.inject({
|
||||||
|
method: "PUT",
|
||||||
|
url: "/settings",
|
||||||
|
payload: { shareStockStatus: false },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Re-enable
|
||||||
|
const response = await ctx.app.inject({
|
||||||
|
method: "PUT",
|
||||||
|
url: "/settings",
|
||||||
|
payload: { shareStockStatus: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
|
||||||
|
const getResponse = await ctx.app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: "/settings",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(getResponse.json().shareStockStatus).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Repeat Reminders & Skip Reminders Settings
|
// Repeat Reminders & Skip Reminders Settings
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -216,13 +216,14 @@ export interface UpdateUserSettingsOptions {
|
|||||||
userId: number;
|
userId: number;
|
||||||
stockCalculationMode?: "automatic" | "manual";
|
stockCalculationMode?: "automatic" | "manual";
|
||||||
lowStockDays?: number;
|
lowStockDays?: number;
|
||||||
|
shareStockStatus?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create or update user settings
|
* Create or update user settings
|
||||||
*/
|
*/
|
||||||
export async function setUserSettings(client: Client, options: UpdateUserSettingsOptions): Promise<void> {
|
export async function setUserSettings(client: Client, options: UpdateUserSettingsOptions): Promise<void> {
|
||||||
const { userId, stockCalculationMode = "automatic", lowStockDays = 30 } = options;
|
const { userId, stockCalculationMode = "automatic", lowStockDays = 30, shareStockStatus } = options;
|
||||||
|
|
||||||
// Check if settings exist
|
// Check if settings exist
|
||||||
const existing = await client.execute({
|
const existing = await client.execute({
|
||||||
@@ -232,13 +233,19 @@ export async function setUserSettings(client: Client, options: UpdateUserSetting
|
|||||||
|
|
||||||
if (existing.rows.length > 0) {
|
if (existing.rows.length > 0) {
|
||||||
await client.execute({
|
await client.execute({
|
||||||
sql: `UPDATE user_settings SET stock_calculation_mode = ?, low_stock_days = ? WHERE user_id = ?`,
|
sql: `UPDATE user_settings SET stock_calculation_mode = ?, low_stock_days = ?${shareStockStatus !== undefined ? ", share_stock_status = ?" : ""} WHERE user_id = ?`,
|
||||||
args: [stockCalculationMode, lowStockDays, userId],
|
args:
|
||||||
|
shareStockStatus !== undefined
|
||||||
|
? [stockCalculationMode, lowStockDays, shareStockStatus ? 1 : 0, userId]
|
||||||
|
: [stockCalculationMode, lowStockDays, userId],
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
await client.execute({
|
await client.execute({
|
||||||
sql: `INSERT INTO user_settings (user_id, stock_calculation_mode, low_stock_days) VALUES (?, ?, ?)`,
|
sql: `INSERT INTO user_settings (user_id, stock_calculation_mode, low_stock_days${shareStockStatus !== undefined ? ", share_stock_status" : ""}) VALUES (?, ?, ?${shareStockStatus !== undefined ? ", ?" : ""})`,
|
||||||
args: [userId, stockCalculationMode, lowStockDays],
|
args:
|
||||||
|
shareStockStatus !== undefined
|
||||||
|
? [userId, stockCalculationMode, lowStockDays, shareStockStatus ? 1 : 0]
|
||||||
|
: [userId, stockCalculationMode, lowStockDays],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
createTestMedication,
|
createTestMedication,
|
||||||
createTestShareToken,
|
createTestShareToken,
|
||||||
createTestUser,
|
createTestUser,
|
||||||
|
setUserSettings,
|
||||||
type TestContext,
|
type TestContext,
|
||||||
} from "./setup.js";
|
} from "./setup.js";
|
||||||
|
|
||||||
@@ -141,6 +142,14 @@ async function registerShareRoutes(ctx: TestContext) {
|
|||||||
|
|
||||||
const lowStockDays = settingsResult.rows.length > 0 ? (settingsResult.rows[0].low_stock_days as number) : 30;
|
const lowStockDays = settingsResult.rows.length > 0 ? (settingsResult.rows[0].low_stock_days as number) : 30;
|
||||||
|
|
||||||
|
// Get shareStockStatus setting
|
||||||
|
const shareStockResult = await client.execute({
|
||||||
|
sql: `SELECT share_stock_status FROM user_settings WHERE user_id = ?`,
|
||||||
|
args: [share.user_id],
|
||||||
|
});
|
||||||
|
const shareStockStatus =
|
||||||
|
shareStockResult.rows.length > 0 ? Boolean(shareStockResult.rows[0].share_stock_status ?? 1) : true;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
takenBy: share.taken_by,
|
takenBy: share.taken_by,
|
||||||
sharedBy: share.owner_username,
|
sharedBy: share.owner_username,
|
||||||
@@ -149,6 +158,7 @@ async function registerShareRoutes(ctx: TestContext) {
|
|||||||
stockThresholds: {
|
stockThresholds: {
|
||||||
lowStockDays,
|
lowStockDays,
|
||||||
},
|
},
|
||||||
|
shareStockStatus,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -421,6 +431,41 @@ describe("Share Link API", () => {
|
|||||||
expect(med.blisters).toHaveLength(1);
|
expect(med.blisters).toHaveLength(1);
|
||||||
expect(med.blisters[0].usage).toBe(1);
|
expect(med.blisters[0].usage).toBe(1);
|
||||||
expect(med.blisters[0].every).toBe(1);
|
expect(med.blisters[0].every).toBe(1);
|
||||||
|
|
||||||
|
// shareStockStatus should default to true
|
||||||
|
expect(data.shareStockStatus).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should respect shareStockStatus setting when disabled", async () => {
|
||||||
|
// Create medication
|
||||||
|
await createTestMedication(ctx.client, {
|
||||||
|
userId,
|
||||||
|
name: "TestMed",
|
||||||
|
takenBy: ["Daniel"],
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 10,
|
||||||
|
looseTablets: 0,
|
||||||
|
blisters: [{ usage: 1, every: 1, start: "2025-01-01T08:00:00.000Z" }],
|
||||||
|
});
|
||||||
|
|
||||||
|
// Set shareStockStatus to false
|
||||||
|
await setUserSettings(ctx.client, { userId, shareStockStatus: false });
|
||||||
|
|
||||||
|
// Create share token
|
||||||
|
const token = await createTestShareToken(ctx.client, {
|
||||||
|
userId,
|
||||||
|
takenBy: "Daniel",
|
||||||
|
scheduleDays: 30,
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await ctx.app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: `/share/${token}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.statusCode).toBe(200);
|
||||||
|
expect(response.json().shareStockStatus).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return 404 for invalid token", async () => {
|
it("should return 404 for invalid token", async () => {
|
||||||
|
|||||||
@@ -69,8 +69,8 @@ describe("Translations Module", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should replace multiple placeholders", () => {
|
it("should replace multiple placeholders", () => {
|
||||||
const result = t("{count} {type} running low", { count: 3, type: "medications" });
|
const result = t("{count} {type} running critically low", { count: 3, type: "medications" });
|
||||||
expect(result).toBe("3 medications running low");
|
expect(result).toBe("3 medications running critically low");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should replace same placeholder multiple times", () => {
|
it("should replace same placeholder multiple times", () => {
|
||||||
@@ -98,7 +98,7 @@ describe("Translations Module", () => {
|
|||||||
|
|
||||||
// Stock reminder subject
|
// Stock reminder subject
|
||||||
const subject = t(translations.stockReminder.subject, { count: 3, s: "s" });
|
const subject = t(translations.stockReminder.subject, { count: 3, s: "s" });
|
||||||
expect(subject).toBe("MedAssist-ng Auto-Reminder: 3 Medications Running Low");
|
expect(subject).toBe("MedAssist-ng Auto-Reminder: 3 Medications Running Critically Low");
|
||||||
|
|
||||||
// Intake reminder description
|
// Intake reminder description
|
||||||
const description = t(translations.intakeReminder.description, { minutes: 30 });
|
const description = t(translations.intakeReminder.description, { minutes: 30 });
|
||||||
@@ -113,7 +113,7 @@ describe("Translations Module", () => {
|
|||||||
const translations = getTranslations("de");
|
const translations = getTranslations("de");
|
||||||
|
|
||||||
const subject = t(translations.stockReminder.subject, { count: 2, e: "e" });
|
const subject = t(translations.stockReminder.subject, { count: 2, e: "e" });
|
||||||
expect(subject).toBe("MedAssist-ng Auto-Erinnerung: 2 Medikamente wird knapp");
|
expect(subject).toBe("MedAssist-ng Auto-Erinnerung: 2 Medikamente kritisch niedrig");
|
||||||
|
|
||||||
const takenBy = t(translations.intakeReminder.takenBy, { name: "Daniel" });
|
const takenBy = t(translations.intakeReminder.takenBy, { name: "Daniel" });
|
||||||
expect(takenBy).toBe("für Daniel");
|
expect(takenBy).toBe("für Daniel");
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/**
|
||||||
|
* Simple startup logger that respects LOG_LEVEL environment variable.
|
||||||
|
* Used for code that runs before Fastify is initialized (db/client.ts, migrations).
|
||||||
|
* Once Fastify is running, use app.log instead.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const LOG_LEVELS: Record<string, number> = {
|
||||||
|
silent: 60,
|
||||||
|
fatal: 60,
|
||||||
|
error: 50,
|
||||||
|
warn: 40,
|
||||||
|
info: 30,
|
||||||
|
debug: 20,
|
||||||
|
trace: 10,
|
||||||
|
};
|
||||||
|
|
||||||
|
function getLevel(): number {
|
||||||
|
const envLevel = (process.env.LOG_LEVEL || "info").toLowerCase();
|
||||||
|
return LOG_LEVELS[envLevel] ?? LOG_LEVELS.info;
|
||||||
|
}
|
||||||
|
|
||||||
|
function shouldLog(level: string): boolean {
|
||||||
|
return LOG_LEVELS[level] >= getLevel();
|
||||||
|
}
|
||||||
|
|
||||||
|
export const log = {
|
||||||
|
debug(msg: string): void {
|
||||||
|
if (shouldLog("debug")) console.log(msg);
|
||||||
|
},
|
||||||
|
info(msg: string): void {
|
||||||
|
if (shouldLog("info")) console.log(msg);
|
||||||
|
},
|
||||||
|
warn(msg: string): void {
|
||||||
|
if (shouldLog("warn")) console.warn(msg);
|
||||||
|
},
|
||||||
|
error(msg: string): void {
|
||||||
|
if (shouldLog("error")) console.error(msg);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Logger interface for services that receive a logger from the caller */
|
||||||
|
export type ServiceLogger = {
|
||||||
|
info: (msg: string) => void;
|
||||||
|
debug: (msg: string) => void;
|
||||||
|
error: (msg: string) => void;
|
||||||
|
};
|
||||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "medassist-ng-frontend",
|
"name": "medassist-ng-frontend",
|
||||||
"version": "1.8.3",
|
"version": "1.9.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "medassist-ng-frontend",
|
"name": "medassist-ng-frontend",
|
||||||
"version": "1.8.3",
|
"version": "1.9.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"i18next": "^24.2.2",
|
"i18next": "^24.2.2",
|
||||||
"i18next-browser-languagedetector": "^8.0.4",
|
"i18next-browser-languagedetector": "^8.0.4",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "medassist-ng-frontend",
|
"name": "medassist-ng-frontend",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "1.8.6",
|
"version": "1.9.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import { FRONTEND_VERSION, GITHUB_URL } from "../App";
|
|||||||
interface UpdateCheckResult {
|
interface UpdateCheckResult {
|
||||||
status: "up-to-date" | "update-available" | "error";
|
status: "up-to-date" | "update-available" | "error";
|
||||||
latestVersion?: string;
|
latestVersion?: string;
|
||||||
lastChecked?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AboutModalProps {
|
interface AboutModalProps {
|
||||||
@@ -18,21 +17,10 @@ export default function AboutModal({ isOpen, onClose }: AboutModalProps) {
|
|||||||
const [isChecking, setIsChecking] = useState(false);
|
const [isChecking, setIsChecking] = useState(false);
|
||||||
const [updateCheckResult, setUpdateCheckResult] = useState<UpdateCheckResult | null>(null);
|
const [updateCheckResult, setUpdateCheckResult] = useState<UpdateCheckResult | null>(null);
|
||||||
|
|
||||||
// Load cached update check result on mount
|
// Reset check result when modal opens so stale results are never shown
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isOpen) return;
|
if (isOpen) {
|
||||||
|
setUpdateCheckResult(null);
|
||||||
// Load cached update check result
|
|
||||||
const cached = sessionStorage.getItem("updateCheckResult");
|
|
||||||
if (cached) {
|
|
||||||
try {
|
|
||||||
const parsed = JSON.parse(cached);
|
|
||||||
if (parsed && typeof parsed === "object") {
|
|
||||||
setUpdateCheckResult(parsed);
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, [isOpen]);
|
}, [isOpen]);
|
||||||
|
|
||||||
@@ -49,14 +37,10 @@ export default function AboutModal({ isOpen, onClose }: AboutModalProps) {
|
|||||||
const latestVersion = (data.tag_name || "").replace(/^v/, "");
|
const latestVersion = (data.tag_name || "").replace(/^v/, "");
|
||||||
const currentVersion = FRONTEND_VERSION.replace(/^v/, "");
|
const currentVersion = FRONTEND_VERSION.replace(/^v/, "");
|
||||||
const isUpToDate = latestVersion === currentVersion;
|
const isUpToDate = latestVersion === currentVersion;
|
||||||
const result: UpdateCheckResult = {
|
setUpdateCheckResult({
|
||||||
status: isUpToDate ? "up-to-date" : "update-available",
|
status: isUpToDate ? "up-to-date" : "update-available",
|
||||||
latestVersion,
|
latestVersion,
|
||||||
lastChecked: new Date().toISOString(),
|
});
|
||||||
};
|
|
||||||
setUpdateCheckResult(result);
|
|
||||||
// Cache the result
|
|
||||||
sessionStorage.setItem("updateCheckResult", JSON.stringify(result));
|
|
||||||
} catch {
|
} catch {
|
||||||
setUpdateCheckResult({ status: "error" });
|
setUpdateCheckResult({ status: "error" });
|
||||||
} finally {
|
} finally {
|
||||||
@@ -114,11 +98,11 @@ export default function AboutModal({ isOpen, onClose }: AboutModalProps) {
|
|||||||
{updateCheckResult && (
|
{updateCheckResult && (
|
||||||
<div className={`about-update-result ${updateCheckResult.status}`}>
|
<div className={`about-update-result ${updateCheckResult.status}`}>
|
||||||
{updateCheckResult.status === "up-to-date" && (
|
{updateCheckResult.status === "up-to-date" && (
|
||||||
<span className="update-status-text">✓ {t("about.upToDate", "You are up to date!")}</span>
|
<span className="update-status-text">✓ {t("about.upToDate", "You are up to date!")}</span>
|
||||||
)}
|
)}
|
||||||
{updateCheckResult.status === "update-available" && (
|
{updateCheckResult.status === "update-available" && (
|
||||||
<span className="update-status-text">
|
<span className="update-status-text">
|
||||||
⬆ {t("about.updateAvailable", "Update available")}:{" "}
|
⬆ {t("about.updateAvailable", "Update available")}:{" "}
|
||||||
<strong>v{updateCheckResult.latestVersion}</strong>
|
<strong>v{updateCheckResult.latestVersion}</strong>
|
||||||
<a
|
<a
|
||||||
href={`${GITHUB_URL}/releases/latest`}
|
href={`${GITHUB_URL}/releases/latest`}
|
||||||
@@ -131,11 +115,8 @@ export default function AboutModal({ isOpen, onClose }: AboutModalProps) {
|
|||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
{updateCheckResult.status === "error" && (
|
{updateCheckResult.status === "error" && (
|
||||||
<span className="update-status-text">⚠ {t("about.checkFailed", "Could not check for updates")}</span>
|
<span className="update-status-text">
|
||||||
)}
|
⚠ {t("about.checkFailed", "Could not check for updates")}
|
||||||
{updateCheckResult.lastChecked && (
|
|
||||||
<span className="update-last-checked">
|
|
||||||
{t("about.lastChecked", "Last checked")}: {new Date(updateCheckResult.lastChecked).toLocaleString()}
|
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
/**
|
/**
|
||||||
* AppHeader - Main application header with navigation and user menu
|
* AppHeader - Main application header with navigation and user menu
|
||||||
*/
|
*/
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useLocation, useNavigate } from "react-router-dom";
|
import { useLocation, useNavigate } from "react-router-dom";
|
||||||
import { useUnsavedChanges } from "../context";
|
import { useUnsavedChanges } from "../context";
|
||||||
|
import type { ThemePreference } from "../hooks";
|
||||||
import { useTheme } from "../hooks";
|
import { useTheme } from "../hooks";
|
||||||
import { useAuth } from "./Auth";
|
import { useAuth } from "./Auth";
|
||||||
|
|
||||||
@@ -19,9 +20,25 @@ export function AppHeader({ onOpenProfile, onOpenAbout }: AppHeaderProps) {
|
|||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const currentPath = location.pathname;
|
const currentPath = location.pathname;
|
||||||
const { user, authState, logout } = useAuth();
|
const { user, authState, logout } = useAuth();
|
||||||
const { theme, toggleTheme } = useTheme();
|
const { theme, themePreference, setThemePreference } = useTheme();
|
||||||
const { confirmNavigation } = useUnsavedChanges();
|
const { confirmNavigation } = useUnsavedChanges();
|
||||||
|
|
||||||
|
// Theme dropdown state
|
||||||
|
const [themeMenuOpen, setThemeMenuOpen] = useState(false);
|
||||||
|
const themeMenuRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
// Close theme dropdown when clicking outside
|
||||||
|
useEffect(() => {
|
||||||
|
if (!themeMenuOpen) return;
|
||||||
|
const handleClickOutside = (e: MouseEvent) => {
|
||||||
|
if (themeMenuRef.current && !themeMenuRef.current.contains(e.target as Node)) {
|
||||||
|
setThemeMenuOpen(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
document.addEventListener("click", handleClickOutside);
|
||||||
|
return () => document.removeEventListener("click", handleClickOutside);
|
||||||
|
}, [themeMenuOpen]);
|
||||||
|
|
||||||
// Safe navigation that checks for unsaved changes first
|
// Safe navigation that checks for unsaved changes first
|
||||||
const safeNavigate = async (path: string) => {
|
const safeNavigate = async (path: string) => {
|
||||||
if (await confirmNavigation()) {
|
if (await confirmNavigation()) {
|
||||||
@@ -94,13 +111,62 @@ export function AppHeader({ onOpenProfile, onOpenAbout }: AppHeaderProps) {
|
|||||||
⚙️
|
⚙️
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
<button
|
<div className={`theme-menu ${themeMenuOpen ? "open" : ""}`} ref={themeMenuRef}>
|
||||||
className="icon-btn"
|
<button className="icon-btn" onClick={() => setThemeMenuOpen(!themeMenuOpen)} title={t("theme.title")}>
|
||||||
onClick={toggleTheme}
|
{theme === "dark" ? "🌙" : "☀️"}
|
||||||
title={theme === "dark" ? t("tooltips.lightMode") : t("tooltips.darkMode")}
|
</button>
|
||||||
>
|
<div className="theme-dropdown">
|
||||||
{theme === "dark" ? "☀️" : "🌙"}
|
<button
|
||||||
</button>
|
className={`theme-dropdown-item${themePreference === "light" ? " active" : ""}`}
|
||||||
|
onClick={() => {
|
||||||
|
setThemePreference("light");
|
||||||
|
setThemeMenuOpen(false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<circle cx="12" cy="12" r="5" />
|
||||||
|
<line x1="12" y1="1" x2="12" y2="3" />
|
||||||
|
<line x1="12" y1="21" x2="12" y2="23" />
|
||||||
|
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64" />
|
||||||
|
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78" />
|
||||||
|
<line x1="1" y1="12" x2="3" y2="12" />
|
||||||
|
<line x1="21" y1="12" x2="23" y2="12" />
|
||||||
|
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36" />
|
||||||
|
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22" />
|
||||||
|
</svg>
|
||||||
|
{t("theme.light")}
|
||||||
|
{themePreference === "light" && <span className="theme-check">✓</span>}
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className={`theme-dropdown-item${themePreference === "dark" ? " active" : ""}`}
|
||||||
|
onClick={() => {
|
||||||
|
setThemePreference("dark");
|
||||||
|
setThemeMenuOpen(false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
|
||||||
|
</svg>
|
||||||
|
{t("theme.dark")}
|
||||||
|
{themePreference === "dark" && <span className="theme-check">✓</span>}
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className={`theme-dropdown-item${themePreference === "system" ? " active" : ""}`}
|
||||||
|
onClick={() => {
|
||||||
|
setThemePreference("system");
|
||||||
|
setThemeMenuOpen(false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<rect x="2" y="3" width="20" height="14" rx="2" ry="2" />
|
||||||
|
<line x1="8" y1="21" x2="16" y2="21" />
|
||||||
|
<line x1="12" y1="17" x2="12" y2="21" />
|
||||||
|
</svg>
|
||||||
|
{t("theme.system")}
|
||||||
|
{themePreference === "system" && <span className="theme-check">✓</span>}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
{authState?.authEnabled && user && (
|
{authState?.authEnabled && user && (
|
||||||
<div className={`user-menu ${userDropdownOpen ? "open" : ""}`}>
|
<div className={`user-menu ${userDropdownOpen ? "open" : ""}`}>
|
||||||
<button className="user-menu-btn" onClick={() => setUserDropdownOpen(!userDropdownOpen)}>
|
<button className="user-menu-btn" onClick={() => setUserDropdownOpen(!userDropdownOpen)}>
|
||||||
|
|||||||
@@ -195,6 +195,16 @@ export function MedDetailModal({
|
|||||||
<span className={`med-detail-value ${textClass}`}>
|
<span className={`med-detail-value ${textClass}`}>
|
||||||
{currentStock} /{" "}
|
{currentStock} /{" "}
|
||||||
{selectedMed.packageType === "bottle" ? (selectedMed.totalPills ?? packageSize) : packageSize}
|
{selectedMed.packageType === "bottle" ? (selectedMed.totalPills ?? packageSize) : packageSize}
|
||||||
|
{currentStock >
|
||||||
|
(selectedMed.packageType === "bottle" ? (selectedMed.totalPills ?? packageSize) : packageSize) && (
|
||||||
|
<span
|
||||||
|
className="info-tooltip tooltip-align-left warning-text"
|
||||||
|
data-tooltip={t("tooltips.stockExceedsCapacity")}
|
||||||
|
>
|
||||||
|
{" "}
|
||||||
|
⚠️
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -202,7 +212,10 @@ export function MedDetailModal({
|
|||||||
|
|
||||||
{/* Package Details Section */}
|
{/* Package Details Section */}
|
||||||
<div className="med-detail-section">
|
<div className="med-detail-section">
|
||||||
<h3>{t("modal.packageDetails")}</h3>
|
<h3>
|
||||||
|
{t("modal.packageDetails")} (
|
||||||
|
{selectedMed.packageType === "bottle" ? t("form.packageTypeBottle") : t("form.packageTypeBlister")})
|
||||||
|
</h3>
|
||||||
<div className="med-detail-grid">
|
<div className="med-detail-grid">
|
||||||
{selectedMed.packageType === "blister" ? (
|
{selectedMed.packageType === "blister" ? (
|
||||||
<>
|
<>
|
||||||
@@ -263,7 +276,12 @@ export function MedDetailModal({
|
|||||||
</h3>
|
</h3>
|
||||||
<div className="med-detail-schedules">
|
<div className="med-detail-schedules">
|
||||||
{selectedMed.blisters.map((blister, idx) => {
|
{selectedMed.blisters.map((blister, idx) => {
|
||||||
const personCount = Math.max(1, selectedMed.takenBy?.length || 1);
|
// When using new intakes format with per-intake takenBy,
|
||||||
|
// each intake already represents one person's dose — don't multiply.
|
||||||
|
// For legacy intakes (no per-intake takenBy), multiply by personCount.
|
||||||
|
const intake = selectedMed.intakes?.[idx];
|
||||||
|
const hasPerIntakeTakenBy = !!intake?.takenBy;
|
||||||
|
const personCount = hasPerIntakeTakenBy ? 1 : Math.max(1, selectedMed.takenBy?.length || 1);
|
||||||
const totalUsage = blister.usage * personCount;
|
const totalUsage = blister.usage * personCount;
|
||||||
return (
|
return (
|
||||||
<div key={idx} className="med-schedule-item">
|
<div key={idx} className="med-schedule-item">
|
||||||
@@ -347,10 +365,14 @@ export function MedDetailModal({
|
|||||||
})}
|
})}
|
||||||
</span>
|
</span>
|
||||||
<span className="refill-amount">
|
<span className="refill-amount">
|
||||||
+
|
{(() => {
|
||||||
{entry.packsAdded * selectedMed.blistersPerPack * selectedMed.pillsPerBlister +
|
const total =
|
||||||
entry.loosePillsAdded}{" "}
|
selectedMed.packageType === "bottle"
|
||||||
{t("common.pills")}
|
? entry.loosePillsAdded
|
||||||
|
: entry.packsAdded * selectedMed.blistersPerPack * selectedMed.pillsPerBlister +
|
||||||
|
entry.loosePillsAdded;
|
||||||
|
return `+${total} ${total === 1 ? t("common.pill") : t("common.pills")}`;
|
||||||
|
})()}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
@@ -405,24 +427,38 @@ export function MedDetailModal({
|
|||||||
<p className="refill-med-name">{selectedMed.name}</p>
|
<p className="refill-med-name">{selectedMed.name}</p>
|
||||||
|
|
||||||
<div className="refill-form">
|
<div className="refill-form">
|
||||||
<label>
|
{selectedMed.packageType === "blister" ? (
|
||||||
{t("refill.packs")}
|
<>
|
||||||
<input
|
<label>
|
||||||
type="number"
|
{t("refill.packs")}
|
||||||
min="0"
|
<input
|
||||||
value={refillPacks}
|
type="number"
|
||||||
onChange={(e) => onRefillPacksChange(parseInt(e.target.value, 10) || 0)}
|
min="0"
|
||||||
/>
|
value={refillPacks}
|
||||||
</label>
|
onChange={(e) => onRefillPacksChange(parseInt(e.target.value, 10) || 0)}
|
||||||
<label>
|
/>
|
||||||
{t("refill.loosePills")}
|
</label>
|
||||||
<input
|
<label>
|
||||||
type="number"
|
{t("refill.loosePills")}
|
||||||
min="0"
|
<input
|
||||||
value={refillLoose}
|
type="number"
|
||||||
onChange={(e) => onRefillLooseChange(parseInt(e.target.value, 10) || 0)}
|
min="0"
|
||||||
/>
|
value={refillLoose}
|
||||||
</label>
|
onChange={(e) => onRefillLooseChange(parseInt(e.target.value, 10) || 0)}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<label>
|
||||||
|
{t("refill.pillsToAdd")}
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
min="0"
|
||||||
|
value={refillLoose}
|
||||||
|
onChange={(e) => onRefillLooseChange(parseInt(e.target.value, 10) || 0)}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="modal-footer">
|
<div className="modal-footer">
|
||||||
@@ -437,12 +473,17 @@ export function MedDetailModal({
|
|||||||
>
|
>
|
||||||
{refillSaving ? t("common.saving") : t("refill.button")}
|
{refillSaving ? t("common.saving") : t("refill.button")}
|
||||||
</button>
|
</button>
|
||||||
{(refillPacks > 0 || refillLoose > 0) && (
|
{(() => {
|
||||||
<span className="refill-preview">
|
const totalRefill =
|
||||||
+{refillPacks * selectedMed.blistersPerPack * selectedMed.pillsPerBlister + refillLoose}{" "}
|
selectedMed.packageType === "blister"
|
||||||
{t("common.pills")}
|
? refillPacks * selectedMed.blistersPerPack * selectedMed.pillsPerBlister + refillLoose
|
||||||
</span>
|
: refillLoose;
|
||||||
)}
|
return totalRefill > 0 ? (
|
||||||
|
<span className="refill-preview">
|
||||||
|
+{totalRefill} {totalRefill === 1 ? t("common.pill") : t("common.pills")}
|
||||||
|
</span>
|
||||||
|
) : null;
|
||||||
|
})()}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -469,50 +510,67 @@ export function MedDetailModal({
|
|||||||
{(() => {
|
{(() => {
|
||||||
const dbTotal = getMedTotal(selectedMed);
|
const dbTotal = getMedTotal(selectedMed);
|
||||||
const currentTotal = medCoverage ? Math.round(medCoverage.medsLeft) : dbTotal;
|
const currentTotal = medCoverage ? Math.round(medCoverage.medsLeft) : dbTotal;
|
||||||
const newTotal = editStockFullBlisters * selectedMed.pillsPerBlister + editStockPartialBlisterPills;
|
const isBottle = selectedMed.packageType === "bottle";
|
||||||
|
const newTotal = isBottle
|
||||||
|
? editStockPartialBlisterPills
|
||||||
|
: editStockFullBlisters * selectedMed.pillsPerBlister + editStockPartialBlisterPills;
|
||||||
const difference = newTotal - currentTotal;
|
const difference = newTotal - currentTotal;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="edit-stock-form">
|
<div className="edit-stock-form">
|
||||||
<label>
|
{isBottle ? (
|
||||||
{t("editStock.fullBlisters")}{" "}
|
<label>
|
||||||
{t("editStock.pillsPerBlister", { count: selectedMed.pillsPerBlister })}
|
{t("editStock.totalPills")}
|
||||||
<input
|
<input
|
||||||
type="number"
|
type="number"
|
||||||
min="0"
|
min="0"
|
||||||
value={editStockFullBlisters}
|
value={editStockPartialBlisterPills}
|
||||||
onChange={(e) => onEditStockFullBlistersChange(parseInt(e.target.value, 10) || 0)}
|
onChange={(e) => onEditStockPartialBlisterPillsChange(parseInt(e.target.value, 10) || 0)}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
<label>
|
) : (
|
||||||
{t("editStock.partialBlisterPills")}
|
<>
|
||||||
<input
|
<label>
|
||||||
type="number"
|
{t("editStock.fullBlisters")}{" "}
|
||||||
min={editStockFullBlisters > 0 ? -(selectedMed.pillsPerBlister - 1) : 0}
|
{t("editStock.pillsPerBlister", { count: selectedMed.pillsPerBlister })}
|
||||||
max={selectedMed.pillsPerBlister}
|
<input
|
||||||
value={editStockPartialBlisterPills}
|
type="number"
|
||||||
onChange={(e) => {
|
min="0"
|
||||||
const val = parseInt(e.target.value, 10) || 0;
|
value={editStockFullBlisters}
|
||||||
const min = editStockFullBlisters > 0 ? -(selectedMed.pillsPerBlister - 1) : 0;
|
onChange={(e) => onEditStockFullBlistersChange(parseInt(e.target.value, 10) || 0)}
|
||||||
const max = selectedMed.pillsPerBlister;
|
/>
|
||||||
onEditStockPartialBlisterPillsChange(Math.max(min, Math.min(val, max)));
|
</label>
|
||||||
}}
|
<label>
|
||||||
/>
|
{t("editStock.partialBlisterPills")}
|
||||||
</label>
|
<input
|
||||||
|
type="number"
|
||||||
|
min={editStockFullBlisters > 0 ? -(selectedMed.pillsPerBlister - 1) : 0}
|
||||||
|
max={selectedMed.pillsPerBlister}
|
||||||
|
value={editStockPartialBlisterPills}
|
||||||
|
onChange={(e) => {
|
||||||
|
const val = parseInt(e.target.value, 10) || 0;
|
||||||
|
const min = editStockFullBlisters > 0 ? -(selectedMed.pillsPerBlister - 1) : 0;
|
||||||
|
const max = selectedMed.pillsPerBlister;
|
||||||
|
onEditStockPartialBlisterPillsChange(Math.max(min, Math.min(val, max)));
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="edit-stock-summary">
|
<div className="edit-stock-summary">
|
||||||
<div className="summary-row">
|
<div className="summary-row">
|
||||||
<span>{t("editStock.currentTotal")}:</span>
|
<span>{t("editStock.currentTotal")}:</span>
|
||||||
<span>
|
<span>
|
||||||
{currentTotal} {t("common.pills")}
|
{currentTotal} {currentTotal === 1 ? t("common.pill") : t("common.pills")}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="summary-row">
|
<div className="summary-row">
|
||||||
<span>{t("editStock.newTotal")}:</span>
|
<span>{t("editStock.newTotal")}:</span>
|
||||||
<span>
|
<span>
|
||||||
{newTotal} {t("common.pills")}
|
{newTotal} {newTotal === 1 ? t("common.pill") : t("common.pills")}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
@@ -521,7 +579,7 @@ export function MedDetailModal({
|
|||||||
<span>{t("editStock.difference")}:</span>
|
<span>{t("editStock.difference")}:</span>
|
||||||
<span>
|
<span>
|
||||||
{difference > 0 ? "+" : ""}
|
{difference > 0 ? "+" : ""}
|
||||||
{difference} {t("common.pills")}
|
{difference} {Math.abs(difference) === 1 ? t("common.pill") : t("common.pills")}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -266,7 +266,8 @@ export function MobileEditModal({
|
|||||||
)}
|
)}
|
||||||
<div className="full">
|
<div className="full">
|
||||||
<p className="sub">
|
<p className="sub">
|
||||||
<strong>{t("form.total")}:</strong> {deriveTotalFromForm(form)} {t("common.pills")}
|
<strong>{t("form.total")}:</strong> {deriveTotalFromForm(form)}{" "}
|
||||||
|
{deriveTotalFromForm(form) === 1 ? t("common.pill") : t("common.pills")}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<label className="full">
|
<label className="full">
|
||||||
@@ -307,24 +308,38 @@ export function MobileEditModal({
|
|||||||
<div className="full refill-section">
|
<div className="full refill-section">
|
||||||
<h4 className="refill-title">{t("refill.title")}</h4>
|
<h4 className="refill-title">{t("refill.title")}</h4>
|
||||||
<div className="refill-form-inline">
|
<div className="refill-form-inline">
|
||||||
<label>
|
{form.packageType === "blister" ? (
|
||||||
{t("refill.packs")}
|
<>
|
||||||
<input
|
<label>
|
||||||
type="number"
|
{t("refill.packs")}
|
||||||
min="0"
|
<input
|
||||||
value={refillPacks}
|
type="number"
|
||||||
onChange={(e) => onRefillPacksChange(parseInt(e.target.value, 10) || 0)}
|
min="0"
|
||||||
/>
|
value={refillPacks}
|
||||||
</label>
|
onChange={(e) => onRefillPacksChange(parseInt(e.target.value, 10) || 0)}
|
||||||
<label>
|
/>
|
||||||
{t("refill.loosePills")}
|
</label>
|
||||||
<input
|
<label>
|
||||||
type="number"
|
{t("refill.loosePills")}
|
||||||
min="0"
|
<input
|
||||||
value={refillLoose}
|
type="number"
|
||||||
onChange={(e) => onRefillLooseChange(parseInt(e.target.value, 10) || 0)}
|
min="0"
|
||||||
/>
|
value={refillLoose}
|
||||||
</label>
|
onChange={(e) => onRefillLooseChange(parseInt(e.target.value, 10) || 0)}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<label>
|
||||||
|
{t("refill.pillsToAdd")}
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
min="0"
|
||||||
|
value={refillLoose}
|
||||||
|
onChange={(e) => onRefillLooseChange(parseInt(e.target.value, 10) || 0)}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="success"
|
className="success"
|
||||||
@@ -333,12 +348,18 @@ export function MobileEditModal({
|
|||||||
>
|
>
|
||||||
{refillSaving ? t("common.saving") : t("refill.button")}
|
{refillSaving ? t("common.saving") : t("refill.button")}
|
||||||
</button>
|
</button>
|
||||||
{(refillPacks > 0 || refillLoose > 0) && (
|
{(() => {
|
||||||
<span className="refill-preview">
|
const totalRefill =
|
||||||
+{refillPacks * Number(form.blistersPerPack || 0) * Number(form.pillsPerBlister || 1) + refillLoose}{" "}
|
form.packageType === "blister"
|
||||||
{t("common.pills")}
|
? refillPacks * Number(form.blistersPerPack || 0) * Number(form.pillsPerBlister || 1) +
|
||||||
</span>
|
refillLoose
|
||||||
)}
|
: refillLoose;
|
||||||
|
return totalRefill > 0 ? (
|
||||||
|
<span className="refill-preview">
|
||||||
|
+{totalRefill} {totalRefill === 1 ? t("common.pill") : t("common.pills")}
|
||||||
|
</span>
|
||||||
|
) : null;
|
||||||
|
})()}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -426,26 +447,29 @@ export function MobileEditModal({
|
|||||||
onChange={(e) => onSetIntakeValue(idx, "startTime", e.target.value)}
|
onChange={(e) => onSetIntakeValue(idx, "startTime", e.target.value)}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
<label className="compact full-row">
|
{form.takenBy.length === 0 ? null : (
|
||||||
<span>{t("form.blisters.takenByIntake")}</span>
|
<label className="compact full-row">
|
||||||
<select value={intake.takenBy} onChange={(e) => onSetIntakeValue(idx, "takenBy", e.target.value)}>
|
<span>{t("form.blisters.takenByIntake")}</span>
|
||||||
<option value="">{t("form.blisters.takenByEveryone")}</option>
|
<select value={intake.takenBy} onChange={(e) => onSetIntakeValue(idx, "takenBy", e.target.value)}>
|
||||||
{existingPeople.map((person) => (
|
{form.takenBy.map((person) => (
|
||||||
<option key={person} value={person}>
|
<option key={person} value={person}>
|
||||||
{person}
|
{person}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
<label className="toggle-switch small" title={t("form.blisters.remindTooltip")}>
|
)}
|
||||||
<input
|
<div className="remind-toggle-row" title={t("form.blisters.remindTooltip")}>
|
||||||
type="checkbox"
|
<span className="legend-hint">🔔</span>
|
||||||
checked={intake.intakeRemindersEnabled}
|
<label className="toggle-switch small">
|
||||||
onChange={(e) => onSetIntakeValue(idx, "intakeRemindersEnabled", e.target.checked)}
|
<input
|
||||||
/>
|
type="checkbox"
|
||||||
<span className="toggle-slider"></span>
|
checked={intake.intakeRemindersEnabled}
|
||||||
</label>
|
onChange={(e) => onSetIntakeValue(idx, "intakeRemindersEnabled", e.target.checked)}
|
||||||
<span className="legend-hint">🔔</span>
|
/>
|
||||||
|
<span className="toggle-slider"></span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
{form.intakes.length > 1 && (
|
{form.intakes.length > 1 && (
|
||||||
<button type="button" className="danger remove-blister-btn" onClick={() => onRemoveIntake(idx)}>
|
<button type="button" className="danger remove-blister-btn" onClick={() => onRemoveIntake(idx)}>
|
||||||
{t("common.remove")}
|
{t("common.remove")}
|
||||||
@@ -453,7 +477,11 @@ export function MobileEditModal({
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
<button type="button" className="ghost add-blister" onClick={() => onAddIntake()}>
|
<button
|
||||||
|
type="button"
|
||||||
|
className="ghost add-blister"
|
||||||
|
onClick={() => onAddIntake(form.takenBy.length === 1 ? form.takenBy[0] : undefined)}
|
||||||
|
>
|
||||||
+ {t("form.blisters.addIntake")}
|
+ {t("form.blisters.addIntake")}
|
||||||
</button>
|
</button>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// SharedSchedule Component - Public view for shared schedules
|
// SharedSchedule Component - Public view for shared schedules
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
import { useEffect, useMemo, useState } from "react";
|
import { useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
import type { ExpiredLinkData, SharedScheduleData } from "../types";
|
import type { ExpiredLinkData, SharedScheduleData } from "../types";
|
||||||
@@ -12,6 +12,22 @@ import { isDoseDismissed } from "../utils/schedule";
|
|||||||
import { loadCollapsedDaysFromStorage } from "../utils/storage";
|
import { loadCollapsedDaysFromStorage } from "../utils/storage";
|
||||||
import { MedicationAvatar } from "./MedicationAvatar";
|
import { MedicationAvatar } from "./MedicationAvatar";
|
||||||
|
|
||||||
|
// =============================================================================
|
||||||
|
// Stock status helper — identical to DashboardPage's getStockStatus
|
||||||
|
// =============================================================================
|
||||||
|
function getStockStatus(
|
||||||
|
daysLeft: number | null,
|
||||||
|
medsLeft: number,
|
||||||
|
thresholds: { lowStockDays: number; normalStockDays: number; highStockDays: number; reminderDaysBefore: number }
|
||||||
|
) {
|
||||||
|
if (medsLeft <= 0 || daysLeft === 0) return { className: "danger", label: "status.outOfStock" };
|
||||||
|
if (daysLeft === null) return { className: "success", label: "status.noSchedule" };
|
||||||
|
if (daysLeft <= thresholds.reminderDaysBefore) return { className: "danger", label: "status.criticalStock" };
|
||||||
|
if (daysLeft < thresholds.lowStockDays) return { className: "warning", label: "status.lowStock" };
|
||||||
|
if (daysLeft >= thresholds.highStockDays) return { className: "high", label: "status.highStock" };
|
||||||
|
return { className: "success", label: "status.normal" };
|
||||||
|
}
|
||||||
|
|
||||||
export function SharedSchedule() {
|
export function SharedSchedule() {
|
||||||
const { token } = useParams<{ token: string }>();
|
const { token } = useParams<{ token: string }>();
|
||||||
const { t, i18n } = useTranslation();
|
const { t, i18n } = useTranslation();
|
||||||
@@ -24,23 +40,60 @@ export function SharedSchedule() {
|
|||||||
const [lightboxImage, setLightboxImage] = useState<{ url: string; name: string } | null>(null);
|
const [lightboxImage, setLightboxImage] = useState<{ url: string; name: string } | null>(null);
|
||||||
const [showPastDays, setShowPastDays] = useState(false);
|
const [showPastDays, setShowPastDays] = useState(false);
|
||||||
const [showFutureDays, setShowFutureDays] = useState(false);
|
const [showFutureDays, setShowFutureDays] = useState(false);
|
||||||
const [theme, setTheme] = useState<"light" | "dark">(() => {
|
|
||||||
|
// Theme preference: light, dark, or system
|
||||||
|
type ThemePreference = "light" | "dark" | "system";
|
||||||
|
const [themePreference, setThemePreference] = useState<ThemePreference>(() => {
|
||||||
if (typeof window !== "undefined") {
|
if (typeof window !== "undefined") {
|
||||||
return (localStorage.getItem("theme") as "light" | "dark") || "dark";
|
const stored = localStorage.getItem("theme") as ThemePreference | null;
|
||||||
|
if (stored === "light" || stored === "dark" || stored === "system") return stored;
|
||||||
}
|
}
|
||||||
return "dark";
|
return "dark";
|
||||||
});
|
});
|
||||||
|
|
||||||
// Apply theme to document
|
function getSystemTheme(): "light" | "dark" {
|
||||||
useEffect(() => {
|
if (typeof window !== "undefined" && window.matchMedia?.("(prefers-color-scheme: light)").matches) {
|
||||||
document.documentElement.setAttribute("data-theme", theme);
|
return "light";
|
||||||
localStorage.setItem("theme", theme);
|
}
|
||||||
}, [theme]);
|
return "dark";
|
||||||
|
|
||||||
function toggleTheme() {
|
|
||||||
setTheme((prev) => (prev === "dark" ? "light" : "dark"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const resolvedTheme = themePreference === "system" ? getSystemTheme() : themePreference;
|
||||||
|
|
||||||
|
// Apply resolved theme to document
|
||||||
|
useEffect(() => {
|
||||||
|
document.documentElement.setAttribute("data-theme", resolvedTheme);
|
||||||
|
localStorage.setItem("theme", themePreference);
|
||||||
|
}, [themePreference, resolvedTheme]);
|
||||||
|
|
||||||
|
// Listen for system theme changes when preference is "system"
|
||||||
|
useEffect(() => {
|
||||||
|
if (themePreference !== "system") return;
|
||||||
|
const mq = window.matchMedia?.("(prefers-color-scheme: light)");
|
||||||
|
if (!mq) return;
|
||||||
|
const handler = () => {
|
||||||
|
const resolved = mq.matches ? "light" : "dark";
|
||||||
|
document.documentElement.setAttribute("data-theme", resolved);
|
||||||
|
};
|
||||||
|
mq.addEventListener("change", handler);
|
||||||
|
return () => mq.removeEventListener("change", handler);
|
||||||
|
}, [themePreference]);
|
||||||
|
|
||||||
|
// Theme dropdown state
|
||||||
|
const [themeMenuOpen, setThemeMenuOpen] = useState(false);
|
||||||
|
const themeMenuRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!themeMenuOpen) return;
|
||||||
|
const handleClickOutside = (e: MouseEvent) => {
|
||||||
|
if (themeMenuRef.current && !themeMenuRef.current.contains(e.target as Node)) {
|
||||||
|
setThemeMenuOpen(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
document.addEventListener("click", handleClickOutside);
|
||||||
|
return () => document.removeEventListener("click", handleClickOutside);
|
||||||
|
}, [themeMenuOpen]);
|
||||||
|
|
||||||
// Collapsed days state for SharedSchedule (token-specific localStorage)
|
// Collapsed days state for SharedSchedule (token-specific localStorage)
|
||||||
const [manuallyCollapsedDays, setManuallyCollapsedDays] = useState<Set<string>>(new Set());
|
const [manuallyCollapsedDays, setManuallyCollapsedDays] = useState<Set<string>>(new Set());
|
||||||
const [manuallyExpandedDays, setManuallyExpandedDays] = useState<Set<string>>(new Set());
|
const [manuallyExpandedDays, setManuallyExpandedDays] = useState<Set<string>>(new Set());
|
||||||
@@ -161,17 +214,6 @@ export function SharedSchedule() {
|
|||||||
return doseId;
|
return doseId;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count taken doses for a day/item (simplified - per-intake takenBy means one person per dose)
|
|
||||||
function _countTakenDoses(doses: Array<{ id: string; takenBy: string | null }>): { total: number; taken: number } {
|
|
||||||
let total = 0;
|
|
||||||
let taken = 0;
|
|
||||||
for (const d of doses) {
|
|
||||||
total++;
|
|
||||||
if (takenDoses.has(d.id)) taken++;
|
|
||||||
}
|
|
||||||
return { total, taken };
|
|
||||||
}
|
|
||||||
|
|
||||||
async function markDoseTaken(doseId: string) {
|
async function markDoseTaken(doseId: string) {
|
||||||
// Optimistic update
|
// Optimistic update
|
||||||
setTakenDoses((prev) => {
|
setTakenDoses((prev) => {
|
||||||
@@ -382,96 +424,189 @@ export function SharedSchedule() {
|
|||||||
return { todayDay: todayEntry || null, futureDays: future };
|
return { todayDay: todayEntry || null, futureDays: future };
|
||||||
}, [schedule, data?.scheduleDays, i18n.language]);
|
}, [schedule, data?.scheduleDays, i18n.language]);
|
||||||
|
|
||||||
// Build a map of medication name -> dismissedUntil date string
|
// Calculate coverage for stock status colors — matches main app's calculateCoverage logic
|
||||||
// This is robust against timestamp changes from schedule updates or timezone fixes
|
// Uses time-based automatic consumption (same as DashboardPage) for accurate stock levels
|
||||||
const dismissedUntilByMed = useMemo(() => {
|
|
||||||
if (!data) return new Map<string, string>();
|
|
||||||
const map = new Map<string, string>();
|
|
||||||
for (const med of data.medications) {
|
|
||||||
if (med.dismissedUntil) {
|
|
||||||
map.set(med.name, med.dismissedUntil);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}, [data]);
|
|
||||||
|
|
||||||
// Helper to check if a dose date is on or before the dismissedUntil date
|
|
||||||
function isDoseDismissedByName(doseTimestamp: number, medName: string): boolean {
|
|
||||||
const dismissedUntilDate = dismissedUntilByMed.get(medName);
|
|
||||||
if (!dismissedUntilDate) return false;
|
|
||||||
// Compare date strings (YYYY-MM-DD format sorts correctly)
|
|
||||||
const doseDate = new Date(doseTimestamp);
|
|
||||||
const doseDateStr = `${doseDate.getFullYear()}-${String(doseDate.getMonth() + 1).padStart(2, "0")}-${String(doseDate.getDate()).padStart(2, "0")}`;
|
|
||||||
return doseDateStr <= dismissedUntilDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate coverage for stock status colors (matches main app logic)
|
|
||||||
// This needs to account for taken doses and calculate depletion time
|
|
||||||
const { coverageByMed, depletionByMed } = useMemo(() => {
|
const { coverageByMed, depletionByMed } = useMemo(() => {
|
||||||
if (!data) return { coverageByMed: {}, depletionByMed: {} };
|
if (!data) return { coverageByMed: {}, depletionByMed: {} };
|
||||||
|
const MS_PER_DAY = 86_400_000;
|
||||||
|
const now = Date.now();
|
||||||
|
const calcMode = data.stockCalculationMode ?? "automatic";
|
||||||
const coverage: Record<string, { daysLeft: number | null; medsLeft: number; dailyUsage: number }> = {};
|
const coverage: Record<string, { daysLeft: number | null; medsLeft: number; dailyUsage: number }> = {};
|
||||||
const depletion: Record<string, number | null> = {};
|
const depletion: Record<string, number | null> = {};
|
||||||
|
|
||||||
// Calculate total pills taken per medication from takenDoses
|
|
||||||
// With per-intake takenBy, each dose.id is unique and already has person suffix if needed
|
|
||||||
const takenByMed: Record<string, number> = {};
|
|
||||||
for (const dose of schedule.flatMap((d) => d.meds.flatMap((m) => m.doses))) {
|
|
||||||
if (takenDoses.has(dose.id)) {
|
|
||||||
takenByMed[dose.medName] = (takenByMed[dose.medName] || 0) + dose.usage;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const med of data.medications) {
|
for (const med of data.medications) {
|
||||||
const totalCount = getMedTotal(med);
|
const intakes = med.intakes || med.blisters.map((b) => ({ ...b, takenBy: null as string | null }));
|
||||||
const taken = takenByMed[med.name] || 0;
|
const blisters = med.blisters;
|
||||||
const currentCount = Math.max(0, totalCount - taken);
|
|
||||||
// Calculate daily usage from intakes (or blisters for legacy)
|
|
||||||
const intakes = med.intakes || med.blisters;
|
|
||||||
const dailyUsage = intakes.reduce((sum, b) => sum + b.usage / b.every, 0);
|
|
||||||
const daysLeft = dailyUsage > 0 ? currentCount / dailyUsage : null;
|
|
||||||
coverage[med.name] = { daysLeft, medsLeft: currentCount, dailyUsage };
|
|
||||||
|
|
||||||
// Calculate depletion time (when medication will run out)
|
// Count unique people from all intakes (for per-intake takenBy)
|
||||||
if (dailyUsage > 0 && currentCount > 0) {
|
const uniquePeople = new Set<string>();
|
||||||
const daysUntilEmpty = currentCount / dailyUsage;
|
intakes.forEach((intake) => {
|
||||||
depletion[med.name] = Date.now() + daysUntilEmpty * 24 * 60 * 60 * 1000;
|
if (intake.takenBy) uniquePeople.add(intake.takenBy);
|
||||||
} else if (currentCount <= 0) {
|
});
|
||||||
depletion[med.name] = Date.now(); // Already empty
|
med.takenBy?.forEach((person) => uniquePeople.add(person));
|
||||||
|
const personCount = Math.max(1, uniquePeople.size || med.takenBy?.length || 1);
|
||||||
|
|
||||||
|
// Calculate daily consumption rate accounting for per-intake takenBy
|
||||||
|
let dailyRate = 0;
|
||||||
|
blisters.forEach((s, idx) => {
|
||||||
|
const baseRate = s.every > 0 ? s.usage / s.every : 0;
|
||||||
|
const intake = intakes[idx];
|
||||||
|
if (intake?.takenBy) {
|
||||||
|
dailyRate += baseRate; // Per-intake takenBy: 1 person
|
||||||
|
} else {
|
||||||
|
dailyRate += baseRate * personCount; // Legacy: all people
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let consumed = 0;
|
||||||
|
const stockCorrectionCutoff = med.lastStockCorrectionAt ? med.lastStockCorrectionAt : 0;
|
||||||
|
|
||||||
|
if (calcMode === "automatic") {
|
||||||
|
// Time-based: every scheduled dose counts as consumed once its time has passed
|
||||||
|
blisters.forEach((s, blisterIdx) => {
|
||||||
|
const blisterStart = new Date(s.start).getTime();
|
||||||
|
const period = Math.max(1, s.every) * MS_PER_DAY;
|
||||||
|
|
||||||
|
let effectiveStart: number;
|
||||||
|
if (stockCorrectionCutoff > 0 && stockCorrectionCutoff >= blisterStart) {
|
||||||
|
const elapsedSinceStart = stockCorrectionCutoff - blisterStart;
|
||||||
|
const periodsElapsed = Math.floor(elapsedSinceStart / period);
|
||||||
|
effectiveStart = blisterStart + (periodsElapsed + 1) * period;
|
||||||
|
} else {
|
||||||
|
effectiveStart = blisterStart;
|
||||||
|
}
|
||||||
|
if (Number.isNaN(effectiveStart)) return;
|
||||||
|
|
||||||
|
const intake = intakes[blisterIdx];
|
||||||
|
const intakePerson = intake?.takenBy;
|
||||||
|
const peopleForThisIntake = intakePerson ? [intakePerson] : med.takenBy?.length > 0 ? med.takenBy : [null];
|
||||||
|
|
||||||
|
let timeBasedConsumed = 0;
|
||||||
|
let lastAutoConsumedDateMs = 0;
|
||||||
|
|
||||||
|
if (effectiveStart <= now) {
|
||||||
|
const occurrences = Math.floor((now - effectiveStart) / period) + 1;
|
||||||
|
timeBasedConsumed = occurrences * s.usage * peopleForThisIntake.length;
|
||||||
|
const lastDoseTime = new Date(effectiveStart + (occurrences - 1) * period);
|
||||||
|
lastAutoConsumedDateMs = new Date(
|
||||||
|
lastDoseTime.getFullYear(),
|
||||||
|
lastDoseTime.getMonth(),
|
||||||
|
lastDoseTime.getDate()
|
||||||
|
).getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Early intakes: future doses already marked as taken
|
||||||
|
const stockCorrectionDateOnly =
|
||||||
|
stockCorrectionCutoff > 0
|
||||||
|
? new Date(
|
||||||
|
new Date(stockCorrectionCutoff).getFullYear(),
|
||||||
|
new Date(stockCorrectionCutoff).getMonth(),
|
||||||
|
new Date(stockCorrectionCutoff).getDate()
|
||||||
|
).getTime()
|
||||||
|
: 0;
|
||||||
|
const earlyCutoff = Math.max(lastAutoConsumedDateMs, stockCorrectionDateOnly);
|
||||||
|
|
||||||
|
let earlyTakenConsumed = 0;
|
||||||
|
for (const doseId of takenDoses) {
|
||||||
|
const parts = doseId.split("-");
|
||||||
|
if (parts.length >= 3) {
|
||||||
|
const medId = parseInt(parts[0], 10);
|
||||||
|
const bIdx = parseInt(parts[1], 10);
|
||||||
|
const timestamp = parseInt(parts[2], 10);
|
||||||
|
if (medId === med.id && bIdx === blisterIdx && timestamp > earlyCutoff) {
|
||||||
|
earlyTakenConsumed += s.usage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
consumed += timeBasedConsumed + earlyTakenConsumed;
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
depletion[med.name] = null; // No usage schedule
|
// Manual mode: only count explicitly taken doses
|
||||||
|
takenDoses.forEach((doseId) => {
|
||||||
|
const parts = doseId.split("-");
|
||||||
|
if (parts.length >= 3) {
|
||||||
|
const medId = parseInt(parts[0], 10);
|
||||||
|
const blisterIdx = parseInt(parts[1], 10);
|
||||||
|
const doseTimestamp = parseInt(parts[2], 10);
|
||||||
|
if (medId === med.id && blisters[blisterIdx]) {
|
||||||
|
const blisterStartDate = new Date(blisters[blisterIdx].start);
|
||||||
|
const blisterStartDateOnly = new Date(
|
||||||
|
blisterStartDate.getFullYear(),
|
||||||
|
blisterStartDate.getMonth(),
|
||||||
|
blisterStartDate.getDate()
|
||||||
|
).getTime();
|
||||||
|
const afterCorrection = stockCorrectionCutoff === 0 || doseTimestamp > stockCorrectionCutoff;
|
||||||
|
if (!Number.isNaN(blisterStartDateOnly) && doseTimestamp >= blisterStartDateOnly && afterCorrection) {
|
||||||
|
consumed += blisters[blisterIdx].usage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const totalPills = getMedTotal(med);
|
||||||
|
const medsLeft = Math.max(0, totalPills - consumed);
|
||||||
|
const rawDaysLeft = dailyRate > 0 ? medsLeft / dailyRate : null;
|
||||||
|
const daysLeft = rawDaysLeft !== null ? Math.max(0, Math.floor(rawDaysLeft)) : null;
|
||||||
|
const depletionMs = daysLeft !== null ? now + daysLeft * MS_PER_DAY : null;
|
||||||
|
|
||||||
|
coverage[med.name] = { daysLeft, medsLeft: Number(medsLeft.toFixed(1)), dailyUsage: dailyRate };
|
||||||
|
depletion[med.name] = depletionMs;
|
||||||
}
|
}
|
||||||
return { coverageByMed: coverage, depletionByMed: depletion };
|
return { coverageByMed: coverage, depletionByMed: depletion };
|
||||||
}, [data, schedule, takenDoses]);
|
}, [data, takenDoses]);
|
||||||
|
|
||||||
// Stock thresholds from user settings (provided by API) or defaults
|
// Stock thresholds from API — matches DashboardPage's StockThresholds type exactly
|
||||||
const lowStockDays = data?.stockThresholds?.lowStockDays ?? 30;
|
const stockThresholds = useMemo(
|
||||||
|
() => ({
|
||||||
|
lowStockDays: data?.stockThresholds?.lowStockDays ?? 30,
|
||||||
|
normalStockDays: data?.stockThresholds?.normalStockDays ?? 60,
|
||||||
|
highStockDays: data?.stockThresholds?.highStockDays ?? 90,
|
||||||
|
criticalStockDays: data?.stockThresholds?.reminderDaysBefore ?? 7,
|
||||||
|
expiryWarningDays: data?.stockThresholds?.expiryWarningDays ?? 90,
|
||||||
|
}),
|
||||||
|
[data]
|
||||||
|
);
|
||||||
|
|
||||||
// Get worst stock status for a day's medications (matches main app logic with depletion)
|
// Get worst stock status for a day's medications — identical to DashboardPage
|
||||||
const getDayStockStatus = (meds: { medName: string; lastWhen: number }[]) => {
|
function getDayStockStatus(meds: { medName: string; lastWhen: number }[]) {
|
||||||
const statuses = meds.map((item) => {
|
const statuses = meds.map((item) => {
|
||||||
const coverage = coverageByMed[item.medName];
|
const coverage = coverageByMed[item.medName];
|
||||||
const depletionTime = depletionByMed[item.medName];
|
const depletionTime = depletionByMed[item.medName];
|
||||||
|
if (typeof depletionTime === "number" && item.lastWhen > depletionTime) return "danger";
|
||||||
// Will be out of stock by this day?
|
|
||||||
if (typeof depletionTime === "number" && item.lastWhen > depletionTime) {
|
|
||||||
return "danger";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!coverage) return "success";
|
if (!coverage) return "success";
|
||||||
const { daysLeft, medsLeft } = coverage;
|
const status = getStockStatus(coverage.daysLeft, coverage.medsLeft, stockThresholds);
|
||||||
|
return status.className;
|
||||||
// Currently out of stock
|
|
||||||
if (medsLeft <= 0 || daysLeft === 0) return "danger";
|
|
||||||
// No schedule (can't calculate)
|
|
||||||
if (daysLeft === null) return "success";
|
|
||||||
// Low stock: < lowStockDays (warning)
|
|
||||||
if (daysLeft < lowStockDays) return "warning";
|
|
||||||
// Normal/High stock
|
|
||||||
return "success";
|
|
||||||
});
|
});
|
||||||
return statuses.includes("danger") ? "danger" : statuses.includes("warning") ? "warning" : "success";
|
return statuses.includes("danger") ? "danger" : statuses.includes("warning") ? "warning" : "success";
|
||||||
};
|
}
|
||||||
|
|
||||||
|
// Whether to show stock status indicators on the shared schedule
|
||||||
|
const showStock = data?.shareStockStatus !== false;
|
||||||
|
|
||||||
|
// Helper: check if a dose is "done" (taken, per-dose dismissed, or med-level dismissed)
|
||||||
|
function isDoseIdDone(doseId: string): boolean {
|
||||||
|
if (takenDoses.has(doseId)) return true;
|
||||||
|
if (dismissedDoses.has(doseId)) return true;
|
||||||
|
const parts = doseId.split("-");
|
||||||
|
if (parts.length >= 3) {
|
||||||
|
const medId = parts[0];
|
||||||
|
const med = data?.medications.find((m) => String(m.id) === medId);
|
||||||
|
if (med) {
|
||||||
|
if (isDoseDismissed(doseId, med.dismissedUntil ?? undefined)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Missed past dose IDs — matches DashboardPage's missedPastDoseIds logic
|
||||||
|
const missedPastDoseIds = useMemo(() => {
|
||||||
|
const allPastDoseIds = pastDays.flatMap((d) => d.meds.flatMap((m) => m.doses.map((dose) => dose.id)));
|
||||||
|
return allPastDoseIds.filter((id) => !isDoseIdDone(id));
|
||||||
|
}, [pastDays, takenDoses, dismissedDoses, data]);
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return (
|
return (
|
||||||
@@ -522,13 +657,62 @@ export function SharedSchedule() {
|
|||||||
💊 {t("share.scheduleFor")} {data.takenBy}
|
💊 {t("share.scheduleFor")} {data.takenBy}
|
||||||
</h1>
|
</h1>
|
||||||
<div className="shared-schedule-header-actions">
|
<div className="shared-schedule-header-actions">
|
||||||
<button
|
<div className={`theme-menu ${themeMenuOpen ? "open" : ""}`} ref={themeMenuRef}>
|
||||||
className="icon-btn"
|
<button className="icon-btn" onClick={() => setThemeMenuOpen(!themeMenuOpen)} title={t("theme.title")}>
|
||||||
onClick={toggleTheme}
|
{resolvedTheme === "dark" ? "🌙" : "☀️"}
|
||||||
title={theme === "dark" ? t("tooltips.lightMode") : t("tooltips.darkMode")}
|
</button>
|
||||||
>
|
<div className="theme-dropdown">
|
||||||
{theme === "dark" ? "☀️" : "🌙"}
|
<button
|
||||||
</button>
|
className={`theme-dropdown-item${themePreference === "light" ? " active" : ""}`}
|
||||||
|
onClick={() => {
|
||||||
|
setThemePreference("light");
|
||||||
|
setThemeMenuOpen(false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<circle cx="12" cy="12" r="5" />
|
||||||
|
<line x1="12" y1="1" x2="12" y2="3" />
|
||||||
|
<line x1="12" y1="21" x2="12" y2="23" />
|
||||||
|
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64" />
|
||||||
|
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78" />
|
||||||
|
<line x1="1" y1="12" x2="3" y2="12" />
|
||||||
|
<line x1="21" y1="12" x2="23" y2="12" />
|
||||||
|
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36" />
|
||||||
|
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22" />
|
||||||
|
</svg>
|
||||||
|
{t("theme.light")}
|
||||||
|
{themePreference === "light" && <span className="theme-check">✓</span>}
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className={`theme-dropdown-item${themePreference === "dark" ? " active" : ""}`}
|
||||||
|
onClick={() => {
|
||||||
|
setThemePreference("dark");
|
||||||
|
setThemeMenuOpen(false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
|
||||||
|
</svg>
|
||||||
|
{t("theme.dark")}
|
||||||
|
{themePreference === "dark" && <span className="theme-check">✓</span>}
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className={`theme-dropdown-item${themePreference === "system" ? " active" : ""}`}
|
||||||
|
onClick={() => {
|
||||||
|
setThemePreference("system");
|
||||||
|
setThemeMenuOpen(false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
|
||||||
|
<rect x="2" y="3" width="20" height="14" rx="2" ry="2" />
|
||||||
|
<line x1="8" y1="21" x2="16" y2="21" />
|
||||||
|
<line x1="12" y1="17" x2="12" y2="21" />
|
||||||
|
</svg>
|
||||||
|
{t("theme.system")}
|
||||||
|
{themePreference === "system" && <span className="theme-check">✓</span>}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p className="shared-schedule-period">
|
<p className="shared-schedule-period">
|
||||||
{t("share.period")}:{" "}
|
{t("share.period")}:{" "}
|
||||||
@@ -545,94 +729,54 @@ export function SharedSchedule() {
|
|||||||
<p className="shared-schedule-empty">{t("share.noSchedule")}</p>
|
<p className="shared-schedule-empty">{t("share.noSchedule")}</p>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
{/* Past days toggle */}
|
{/* Past days toggle — identical to DashboardPage */}
|
||||||
{pastDays.length > 0 &&
|
{pastDays.length > 0 &&
|
||||||
(() => {
|
(() => {
|
||||||
// Count all past doses (for display)
|
const missedCount = missedPastDoseIds.length;
|
||||||
// With per-intake takenBy, each dose.id is unique
|
|
||||||
const totalPastDoses = pastDays.flatMap((d) => d.meds.flatMap((m) => m.doses.map((dose) => dose.id)));
|
const totalPastDoses = pastDays.flatMap((d) => d.meds.flatMap((m) => m.doses.map((dose) => dose.id)));
|
||||||
// Count missed doses (not taken AND not dismissed AND not from previous schedule)
|
|
||||||
// Check: per-dose dismissed flag, medication-level dismissedUntil, and updatedAt
|
|
||||||
const missedPastDoses = totalPastDoses.filter((id) => {
|
|
||||||
if (takenDoses.has(id)) return false;
|
|
||||||
// Check if this dose is dismissed via per-dose flag from API
|
|
||||||
if (dismissedDoses.has(id)) return false;
|
|
||||||
// Check if dismissed via medication-level dismissedUntil date
|
|
||||||
const parts = id.split("-");
|
|
||||||
if (parts.length >= 3) {
|
|
||||||
const medId = parts[0];
|
|
||||||
const med = data?.medications.find((m) => String(m.id) === medId);
|
|
||||||
if (med) {
|
|
||||||
if (isDoseDismissed(id, med.dismissedUntil ?? undefined)) {
|
|
||||||
return false; // dismissed = not missed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true; // not taken, not dismissed = missed
|
|
||||||
}).length;
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div className="past-days-header">
|
||||||
className={`past-days-toggle ${showPastDays ? "expanded" : ""} ${missedPastDoses > 0 ? "has-missed" : ""}`}
|
<div
|
||||||
onClick={() => setShowPastDays(!showPastDays)}
|
className={`past-days-toggle ${showPastDays ? "expanded" : ""} ${missedCount > 0 ? "has-missed" : ""}`}
|
||||||
>
|
onClick={() => setShowPastDays(!showPastDays)}
|
||||||
<span className="past-days-icon">{showPastDays ? "▼" : "▶"}</span>
|
>
|
||||||
<span className="past-days-label">
|
<span className="past-days-icon">{showPastDays ? "▼" : "▶"}</span>
|
||||||
{showPastDays ? t("dashboard.schedules.hidePastDays") : t("dashboard.schedules.showPastDays")}
|
<span className="past-days-label">
|
||||||
</span>
|
{showPastDays ? t("dashboard.schedules.hidePastDays") : t("dashboard.schedules.showPastDays")}
|
||||||
<span className="past-days-count">
|
|
||||||
({t("dashboard.schedules.pastDaysCount", { count: pastDays.length })})
|
|
||||||
</span>
|
|
||||||
{missedPastDoses > 0 ? (
|
|
||||||
<span
|
|
||||||
className="past-days-warning"
|
|
||||||
title={t("dashboard.schedules.missedDoses", { count: missedPastDoses })}
|
|
||||||
>
|
|
||||||
⚠️ {missedPastDoses}
|
|
||||||
</span>
|
</span>
|
||||||
) : totalPastDoses.length > 0 ? (
|
<span className="past-days-count">
|
||||||
<span className="past-days-complete" title={t("dashboard.schedules.allTaken")}>
|
({t("dashboard.schedules.pastDaysCount", { count: pastDays.length })})
|
||||||
✓
|
|
||||||
</span>
|
</span>
|
||||||
) : null}
|
{missedCount > 0 ? (
|
||||||
|
<span
|
||||||
|
className="past-days-warning"
|
||||||
|
title={t("dashboard.schedules.missedDoses", { count: missedCount })}
|
||||||
|
>
|
||||||
|
⚠️ {missedCount}
|
||||||
|
</span>
|
||||||
|
) : totalPastDoses.length > 0 ? (
|
||||||
|
<span className="past-days-complete" title={t("dashboard.schedules.allTaken")}>
|
||||||
|
✓
|
||||||
|
</span>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
})()}
|
})()}
|
||||||
{/* Past days (when expanded) */}
|
{/* Past days (when expanded) — identical to DashboardPage */}
|
||||||
{showPastDays &&
|
{showPastDays &&
|
||||||
pastDays.map((day) => {
|
pastDays.map((day) => {
|
||||||
// Helper to check if a dose ID is "done" (taken or dismissed)
|
|
||||||
// Checks: per-dose dismissed flag and medication-level dismissedUntil
|
|
||||||
const isDoseIdDone = (doseId: string) => {
|
|
||||||
if (takenDoses.has(doseId)) return true;
|
|
||||||
// Check if this dose is dismissed via per-dose flag from API
|
|
||||||
if (dismissedDoses.has(doseId)) return true;
|
|
||||||
// Check if dismissed via medication-level dismissedUntil date
|
|
||||||
const parts = doseId.split("-");
|
|
||||||
if (parts.length >= 3) {
|
|
||||||
const medId = parts[0];
|
|
||||||
const med = data?.medications.find((m) => String(m.id) === medId);
|
|
||||||
if (med) {
|
|
||||||
if (isDoseDismissed(doseId, med.dismissedUntil ?? undefined)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
const allDoseIds = day.meds.flatMap((item) => item.doses.map((d) => d.id));
|
const allDoseIds = day.meds.flatMap((item) => item.doses.map((d) => d.id));
|
||||||
const allDayDone = allDoseIds.length > 0 && allDoseIds.every(isDoseIdDone);
|
const allDayTaken =
|
||||||
const doneCount = allDoseIds.filter(isDoseIdDone).length;
|
allDoseIds.length > 0 && allDoseIds.every((id) => takenDoses.has(id) || dismissedDoses.has(id));
|
||||||
|
const takenCount = allDoseIds.filter((id) => takenDoses.has(id) || dismissedDoses.has(id)).length;
|
||||||
const isManuallyExpanded = manuallyExpandedDays.has(day.dateStr);
|
const isManuallyExpanded = manuallyExpandedDays.has(day.dateStr);
|
||||||
const isCollapsed = !isManuallyExpanded;
|
const isCollapsed = !isManuallyExpanded;
|
||||||
|
|
||||||
// Calculate stock status for this day
|
|
||||||
const worstStatus = getDayStockStatus(day.meds);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={day.dateStr}
|
key={day.dateStr}
|
||||||
className={`day-block past ${isCollapsed ? "collapsed" : ""} ${allDayDone ? "all-taken" : ""} stock-${worstStatus}`}
|
className={`day-block past ${isCollapsed ? "collapsed" : ""} ${allDayTaken ? "all-taken" : allDoseIds.length > 0 ? "past-missed" : ""}`}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
className="day-divider clickable"
|
className="day-divider clickable"
|
||||||
@@ -642,18 +786,18 @@ export function SharedSchedule() {
|
|||||||
<span className="day-collapse-icon">{isCollapsed ? "▶" : "▼"}</span>
|
<span className="day-collapse-icon">{isCollapsed ? "▶" : "▼"}</span>
|
||||||
<span className="day-date">{day.dateStr}</span>
|
<span className="day-date">{day.dateStr}</span>
|
||||||
<span className="day-summary">
|
<span className="day-summary">
|
||||||
{allDayDone ? (
|
{allDayTaken ? (
|
||||||
<span className="day-complete">✓ {t("dashboard.schedules.allTaken")}</span>
|
<span className="day-complete">✓ {t("dashboard.schedules.allTaken")}</span>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<span
|
<span
|
||||||
className="day-warning"
|
className="day-warning"
|
||||||
title={t("dashboard.schedules.missedDoses", { count: allDoseIds.length - doneCount })}
|
title={t("dashboard.schedules.missedDoses", { count: allDoseIds.length - takenCount })}
|
||||||
>
|
>
|
||||||
⚠️
|
⚠️
|
||||||
</span>
|
</span>
|
||||||
<span className="day-progress">
|
<span className="day-progress">
|
||||||
{doneCount}/{allDoseIds.length}
|
{takenCount}/{allDoseIds.length}
|
||||||
</span>
|
</span>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
@@ -663,61 +807,48 @@ export function SharedSchedule() {
|
|||||||
day.meds.map((item) => {
|
day.meds.map((item) => {
|
||||||
const med = data.medications.find((m) => m.name === item.medName);
|
const med = data.medications.find((m) => m.name === item.medName);
|
||||||
const medCoverage = coverageByMed[item.medName];
|
const medCoverage = coverageByMed[item.medName];
|
||||||
const isEmpty = medCoverage ? medCoverage.medsLeft <= 0 : false;
|
const isEmpty = showStock && medCoverage ? medCoverage.medsLeft <= 0 : false;
|
||||||
const depletionTime = depletionByMed[item.medName];
|
const depletionTime = depletionByMed[item.medName];
|
||||||
const willBeOutOfStock = typeof depletionTime === "number" && item.lastWhen > depletionTime;
|
const willBeOutOfStock = typeof depletionTime === "number" && item.lastWhen > depletionTime;
|
||||||
|
const status = showStock
|
||||||
// Calculate status for this medication on this day
|
? willBeOutOfStock
|
||||||
let status: { className: string; label: string } | null = null;
|
? { className: "danger", label: "status.outOfStock" }
|
||||||
if (willBeOutOfStock) {
|
: medCoverage
|
||||||
status = { className: "danger", label: "status.outOfStock" };
|
? getStockStatus(medCoverage.daysLeft, medCoverage.medsLeft, stockThresholds)
|
||||||
} else if (medCoverage) {
|
: null
|
||||||
const { daysLeft, medsLeft } = medCoverage;
|
: null;
|
||||||
if (medsLeft <= 0 || daysLeft === 0) {
|
|
||||||
status = { className: "danger", label: "status.outOfStock" };
|
|
||||||
} else if (daysLeft !== null && daysLeft < lowStockDays) {
|
|
||||||
status = { className: "warning", label: "status.lowStock" };
|
|
||||||
} else {
|
|
||||||
status = { className: "success", label: "status.normal" };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const itemDoseIds = item.doses.map((d) => d.id);
|
const itemDoseIds = item.doses.map((d) => d.id);
|
||||||
// A dose is "done" if taken OR dismissed
|
const allTaken = itemDoseIds.every((id) => takenDoses.has(id));
|
||||||
const allDone = itemDoseIds.every(isDoseIdDone);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={`${day.dateStr}-${item.medName}`}
|
key={`${day.dateStr}-${item.medName}`}
|
||||||
className={`time-row ${allDone ? "taken" : ""}`}
|
className={`time-row ${allTaken ? "taken" : ""}`}
|
||||||
>
|
>
|
||||||
<div className="time-main">
|
<div className="time-main">
|
||||||
<div className="med-name">
|
<div className="med-name">
|
||||||
<span
|
<div
|
||||||
className={med?.imageUrl ? "clickable" : ""}
|
className={med?.imageUrl ? "med-avatar clickable" : ""}
|
||||||
onClick={() => med?.imageUrl && openLightbox(med.imageUrl, med.name)}
|
onClick={() => med?.imageUrl && openLightbox(med.imageUrl, med.name)}
|
||||||
>
|
>
|
||||||
<MedicationAvatar name={item.medName} imageUrl={med?.imageUrl} size="sm" />
|
<MedicationAvatar name={item.medName} imageUrl={med?.imageUrl} size="sm" />
|
||||||
</span>
|
</div>
|
||||||
<span className="med-name-text">{item.medName}</span>
|
<span className="med-name-text">{item.medName}</span>
|
||||||
{med?.genericName && <span className="med-generic-inline">({med.genericName})</span>}
|
{med?.genericName && <span className="med-generic-inline">({med.genericName})</span>}
|
||||||
</div>
|
</div>
|
||||||
<div className="tag-row">
|
<div className="tag-row">
|
||||||
<span className="tag subtle">
|
<span className="tag subtle">{t("common.pillsTotal", { count: item.total })}</span>
|
||||||
{item.total} {t("common.pills")} {t("common.total")}
|
{status && (
|
||||||
</span>
|
<span className={`status-chip small ${status.className}`}>{t(status.label)}</span>
|
||||||
{status && <span className={`tag ${status.className}`}>{t(status.label)}</span>}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="doses-col">
|
<div className="doses-col">
|
||||||
{item.doses.map((dose) => {
|
{item.doses.map((dose) => {
|
||||||
// Check: medication-level dismissedUntil and per-dose dismissed flag
|
|
||||||
const isMedLevelDismissed = isDoseDismissedByName(dose.when, dose.medName);
|
|
||||||
const isTaken = takenDoses.has(dose.id);
|
const isTaken = takenDoses.has(dose.id);
|
||||||
const isPerDoseDismissed = dismissedDoses.has(dose.id);
|
|
||||||
const isDone = isTaken || isPerDoseDismissed || isMedLevelDismissed;
|
|
||||||
return (
|
return (
|
||||||
<div key={dose.id} className={`dose-item past ${isDone ? "all-taken" : ""}`}>
|
<div key={dose.id} className="dose-item past">
|
||||||
<span className="dose-time">{dose.timeStr}</span>
|
<span className="dose-time">{dose.timeStr}</span>
|
||||||
<span className="dose-usage">
|
<span className="dose-usage">
|
||||||
{dose.usage} {dose.usage !== 1 ? t("common.pills") : t("common.pill")}
|
{dose.usage} {dose.usage !== 1 ? t("common.pills") : t("common.pill")}
|
||||||
@@ -725,26 +856,16 @@ export function SharedSchedule() {
|
|||||||
` (${dose.usage * med.pillWeightMg} ${med.doseUnit ?? "mg"})`}
|
` (${dose.usage * med.pillWeightMg} ${med.doseUnit ?? "mg"})`}
|
||||||
</span>
|
</span>
|
||||||
<div className="dose-checks">
|
<div className="dose-checks">
|
||||||
<div className={`dose-person ${isDone ? "taken" : ""}`}>
|
<div className={`dose-person ${isTaken ? "taken" : ""}`}>
|
||||||
{dose.takenBy && <span className="person-name">{dose.takenBy}</span>}
|
{dose.takenBy && <span className="person-name">{dose.takenBy}</span>}
|
||||||
{isDone ? (
|
{isTaken ? (
|
||||||
isTaken ? (
|
<button
|
||||||
<button
|
className="dose-btn undo"
|
||||||
className="dose-btn undo"
|
onClick={() => undoDoseTaken(dose.id)}
|
||||||
onClick={() => undoDoseTaken(dose.id)}
|
title={t("common.undo")}
|
||||||
title={t("common.undo")}
|
>
|
||||||
>
|
↩
|
||||||
↩
|
</button>
|
||||||
</button>
|
|
||||||
) : (
|
|
||||||
// Dismissed - show checkmark but no undo
|
|
||||||
<span
|
|
||||||
className="dose-btn dismissed"
|
|
||||||
title={t("dashboard.schedules.dismissed") ?? "Dismissed"}
|
|
||||||
>
|
|
||||||
✓
|
|
||||||
</span>
|
|
||||||
)
|
|
||||||
) : (
|
) : (
|
||||||
<button
|
<button
|
||||||
className="dose-btn take"
|
className="dose-btn take"
|
||||||
@@ -785,7 +906,7 @@ export function SharedSchedule() {
|
|||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={day.dateStr}
|
key={day.dateStr}
|
||||||
className={`day-block ${isCollapsed ? "collapsed" : ""} ${allDayTaken ? "all-taken" : ""} today stock-${worstStatus}`}
|
className={`day-block ${isCollapsed ? "collapsed" : ""} ${allDayTaken ? "all-taken" : ""} today stock-${showStock ? worstStatus : "success"}`}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
className="day-divider clickable"
|
className="day-divider clickable"
|
||||||
@@ -808,23 +929,16 @@ export function SharedSchedule() {
|
|||||||
day.meds.map((item) => {
|
day.meds.map((item) => {
|
||||||
const med = data.medications.find((m) => m.name === item.medName);
|
const med = data.medications.find((m) => m.name === item.medName);
|
||||||
const medCoverage = coverageByMed[item.medName];
|
const medCoverage = coverageByMed[item.medName];
|
||||||
const isEmpty = medCoverage ? medCoverage.medsLeft <= 0 : false;
|
const isEmpty = showStock && medCoverage ? medCoverage.medsLeft <= 0 : false;
|
||||||
const depletionTime = depletionByMed[item.medName];
|
const depletionTime = depletionByMed[item.medName];
|
||||||
const willBeOutOfStock = typeof depletionTime === "number" && item.lastWhen > depletionTime;
|
const willBeOutOfStock = typeof depletionTime === "number" && item.lastWhen > depletionTime;
|
||||||
|
const status = showStock
|
||||||
let status: { className: string; label: string } | null = null;
|
? willBeOutOfStock
|
||||||
if (willBeOutOfStock) {
|
? { className: "danger", label: "status.outOfStock" }
|
||||||
status = { className: "danger", label: "status.outOfStock" };
|
: medCoverage
|
||||||
} else if (medCoverage) {
|
? getStockStatus(medCoverage.daysLeft, medCoverage.medsLeft, stockThresholds)
|
||||||
const { daysLeft, medsLeft } = medCoverage;
|
: null
|
||||||
if (medsLeft <= 0 || daysLeft === 0) {
|
: null;
|
||||||
status = { className: "danger", label: "status.outOfStock" };
|
|
||||||
} else if (daysLeft !== null && daysLeft < lowStockDays) {
|
|
||||||
status = { className: "warning", label: "status.lowStock" };
|
|
||||||
} else {
|
|
||||||
status = { className: "success", label: "status.normal" };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const itemDoseIds = item.doses.map((d) => d.id);
|
const itemDoseIds = item.doses.map((d) => d.id);
|
||||||
const allTaken = itemDoseIds.every((id) => takenDoses.has(id));
|
const allTaken = itemDoseIds.every((id) => takenDoses.has(id));
|
||||||
@@ -835,20 +949,20 @@ export function SharedSchedule() {
|
|||||||
>
|
>
|
||||||
<div className="time-main">
|
<div className="time-main">
|
||||||
<div className="med-name">
|
<div className="med-name">
|
||||||
<span
|
<div
|
||||||
className={med?.imageUrl ? "clickable" : ""}
|
className={med?.imageUrl ? "med-avatar clickable" : ""}
|
||||||
onClick={() => med?.imageUrl && openLightbox(med.imageUrl, med.name)}
|
onClick={() => med?.imageUrl && openLightbox(med.imageUrl, med.name)}
|
||||||
>
|
>
|
||||||
<MedicationAvatar name={item.medName} imageUrl={med?.imageUrl} size="sm" />
|
<MedicationAvatar name={item.medName} imageUrl={med?.imageUrl} size="sm" />
|
||||||
</span>
|
</div>
|
||||||
<span className="med-name-text">{item.medName}</span>
|
<span className="med-name-text">{item.medName}</span>
|
||||||
{med?.genericName && <span className="med-generic-inline">({med.genericName})</span>}
|
{med?.genericName && <span className="med-generic-inline">({med.genericName})</span>}
|
||||||
</div>
|
</div>
|
||||||
<div className="tag-row">
|
<div className="tag-row">
|
||||||
<span className="tag subtle">
|
<span className="tag subtle">{t("common.pillsTotal", { count: item.total })}</span>
|
||||||
{item.total} {t("common.pills")} {t("common.total")}
|
{status && (
|
||||||
</span>
|
<span className={`status-chip small ${status.className}`}>{t(status.label)}</span>
|
||||||
{status && <span className={`tag ${status.className}`}>{t(status.label)}</span>}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="doses-col">
|
<div className="doses-col">
|
||||||
@@ -856,7 +970,10 @@ export function SharedSchedule() {
|
|||||||
const isTaken = takenDoses.has(dose.id);
|
const isTaken = takenDoses.has(dose.id);
|
||||||
const isOverdue = dose.when < Date.now() && !isTaken;
|
const isOverdue = dose.when < Date.now() && !isTaken;
|
||||||
return (
|
return (
|
||||||
<div key={dose.id} className={`dose-item ${isTaken ? "all-taken" : ""}`}>
|
<div
|
||||||
|
key={dose.id}
|
||||||
|
className={`dose-item ${isOverdue ? "overdue" : ""} ${isTaken ? "all-taken" : ""}`}
|
||||||
|
>
|
||||||
<span className="dose-time">{dose.timeStr}</span>
|
<span className="dose-time">{dose.timeStr}</span>
|
||||||
<span className="dose-usage">
|
<span className="dose-usage">
|
||||||
{dose.usage} {dose.usage !== 1 ? t("common.pills") : t("common.pill")}
|
{dose.usage} {dose.usage !== 1 ? t("common.pills") : t("common.pill")}
|
||||||
@@ -899,43 +1016,55 @@ export function SharedSchedule() {
|
|||||||
);
|
);
|
||||||
})()}
|
})()}
|
||||||
|
|
||||||
{/* Future days toggle */}
|
{/* Future days toggle — identical to DashboardPage */}
|
||||||
{futureDays.length > 0 && (
|
{futureDays.length > 0 &&
|
||||||
<div
|
(() => {
|
||||||
className={`future-days-toggle ${showFutureDays ? "expanded" : ""}`}
|
const totalFutureDoses = futureDays.flatMap((d) =>
|
||||||
onClick={() => setShowFutureDays(!showFutureDays)}
|
d.meds.flatMap((m) => m.doses.map((dose) => dose.id))
|
||||||
>
|
);
|
||||||
<span className="future-days-icon">{showFutureDays ? "▼" : "▶"}</span>
|
const takenFutureDoses = totalFutureDoses.filter((id) => takenDoses.has(id)).length;
|
||||||
<span className="future-days-label">
|
return (
|
||||||
{showFutureDays ? t("dashboard.schedules.hideFutureDays") : t("dashboard.schedules.showFutureDays")}
|
<div className="future-days-header">
|
||||||
</span>
|
<div
|
||||||
<span className="future-days-count">
|
className={`future-days-toggle ${showFutureDays ? "expanded" : ""}`}
|
||||||
({t("dashboard.schedules.futureDaysCount", { count: futureDays.length })})
|
onClick={() => setShowFutureDays(!showFutureDays)}
|
||||||
</span>
|
>
|
||||||
</div>
|
<span className="future-days-icon">{showFutureDays ? "▼" : "▶"}</span>
|
||||||
)}
|
<span className="future-days-label">
|
||||||
|
{showFutureDays
|
||||||
|
? t("dashboard.schedules.hideFutureDays")
|
||||||
|
: t("dashboard.schedules.showFutureDays")}
|
||||||
|
</span>
|
||||||
|
<span className="future-days-count">
|
||||||
|
({t("dashboard.schedules.futureDaysCount", { count: futureDays.length })})
|
||||||
|
</span>
|
||||||
|
{takenFutureDoses > 0 && totalFutureDoses.length > 0 && (
|
||||||
|
<span className="future-days-progress">
|
||||||
|
{takenFutureDoses}/{totalFutureDoses.length}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})()}
|
||||||
|
|
||||||
{/* Future days (when expanded) */}
|
{/* Future days (when expanded) — identical to DashboardPage */}
|
||||||
{showFutureDays &&
|
{showFutureDays &&
|
||||||
futureDays.map((day) => {
|
futureDays.map((day) => {
|
||||||
// Check if all doses in this day are taken (auto-collapse)
|
|
||||||
const allDoseIds = day.meds.flatMap((item) => item.doses.map((d) => d.id));
|
const allDoseIds = day.meds.flatMap((item) => item.doses.map((d) => d.id));
|
||||||
const allDayTaken = allDoseIds.length > 0 && allDoseIds.every((id) => takenDoses.has(id));
|
const allDayTaken = allDoseIds.length > 0 && allDoseIds.every((id) => takenDoses.has(id));
|
||||||
const takenCount = allDoseIds.filter((id) => takenDoses.has(id)).length;
|
const takenCount = allDoseIds.filter((id) => takenDoses.has(id)).length;
|
||||||
|
|
||||||
// Calculate stock status for this day
|
|
||||||
const worstStatus = getDayStockStatus(day.meds);
|
const worstStatus = getDayStockStatus(day.meds);
|
||||||
|
|
||||||
// Determine if day should be collapsed (auto-collapsed by default, manual override)
|
// Future days: collapsed by default, manual override to expand
|
||||||
const isAutoCollapsed = allDayTaken;
|
const isAutoCollapsed = true;
|
||||||
const isManuallyExpanded = manuallyExpandedDays.has(day.dateStr);
|
const isManuallyExpanded = manuallyExpandedDays.has(day.dateStr);
|
||||||
const isManuallyCollapsed = manuallyCollapsedDays.has(day.dateStr);
|
const isCollapsed = !isManuallyExpanded;
|
||||||
const isCollapsed = isAutoCollapsed ? !isManuallyExpanded : isManuallyCollapsed;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={day.dateStr}
|
key={day.dateStr}
|
||||||
className={`day-block ${isCollapsed ? "collapsed" : ""} ${allDayTaken ? "all-taken" : ""} stock-${worstStatus}`}
|
className={`day-block ${isCollapsed ? "collapsed" : ""} ${allDayTaken ? "all-taken" : ""} stock-${showStock ? worstStatus : "success"}`}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
className="day-divider clickable"
|
className="day-divider clickable"
|
||||||
@@ -958,24 +1087,15 @@ export function SharedSchedule() {
|
|||||||
day.meds.map((item) => {
|
day.meds.map((item) => {
|
||||||
const med = data.medications.find((m) => m.name === item.medName);
|
const med = data.medications.find((m) => m.name === item.medName);
|
||||||
const medCoverage = coverageByMed[item.medName];
|
const medCoverage = coverageByMed[item.medName];
|
||||||
const isEmpty = medCoverage ? medCoverage.medsLeft <= 0 : false;
|
|
||||||
const depletionTime = depletionByMed[item.medName];
|
const depletionTime = depletionByMed[item.medName];
|
||||||
const willBeOutOfStock = typeof depletionTime === "number" && item.lastWhen > depletionTime;
|
const willBeOutOfStock = typeof depletionTime === "number" && item.lastWhen > depletionTime;
|
||||||
|
const status = showStock
|
||||||
// Calculate status for this medication on this day
|
? willBeOutOfStock
|
||||||
let status: { className: string; label: string } | null = null;
|
? { className: "danger", label: "status.outOfStock" }
|
||||||
if (willBeOutOfStock) {
|
: medCoverage
|
||||||
status = { className: "danger", label: "status.outOfStock" };
|
? getStockStatus(medCoverage.daysLeft, medCoverage.medsLeft, stockThresholds)
|
||||||
} else if (medCoverage) {
|
: null
|
||||||
const { daysLeft, medsLeft } = medCoverage;
|
: null;
|
||||||
if (medsLeft <= 0 || daysLeft === 0) {
|
|
||||||
status = { className: "danger", label: "status.outOfStock" };
|
|
||||||
} else if (daysLeft !== null && daysLeft < lowStockDays) {
|
|
||||||
status = { className: "warning", label: "status.lowStock" };
|
|
||||||
} else {
|
|
||||||
status = { className: "success", label: "status.normal" };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const itemDoseIds = item.doses.map((d) => d.id);
|
const itemDoseIds = item.doses.map((d) => d.id);
|
||||||
const allTaken = itemDoseIds.every((id) => takenDoses.has(id));
|
const allTaken = itemDoseIds.every((id) => takenDoses.has(id));
|
||||||
@@ -986,37 +1106,27 @@ export function SharedSchedule() {
|
|||||||
>
|
>
|
||||||
<div className="time-main">
|
<div className="time-main">
|
||||||
<div className="med-name">
|
<div className="med-name">
|
||||||
<span
|
<div
|
||||||
className={med?.imageUrl ? "clickable" : ""}
|
className={med?.imageUrl ? "med-avatar clickable" : ""}
|
||||||
onClick={() => med?.imageUrl && openLightbox(med.imageUrl, med.name)}
|
onClick={() => med?.imageUrl && openLightbox(med.imageUrl, med.name)}
|
||||||
>
|
>
|
||||||
<MedicationAvatar name={item.medName} imageUrl={med?.imageUrl} size="sm" />
|
<MedicationAvatar name={item.medName} imageUrl={med?.imageUrl} size="sm" />
|
||||||
</span>
|
</div>
|
||||||
<span className="med-name-text">{item.medName}</span>
|
<span className="med-name-text">{item.medName}</span>
|
||||||
{med?.genericName && <span className="med-generic-inline">({med.genericName})</span>}
|
{med?.genericName && <span className="med-generic-inline">({med.genericName})</span>}
|
||||||
</div>
|
</div>
|
||||||
<div className="tag-row">
|
<div className="tag-row">
|
||||||
<span className="tag subtle">
|
<span className="tag subtle">{t("common.pillsTotal", { count: item.total })}</span>
|
||||||
{item.total} {t("common.pills")} {t("common.total")}
|
{status && (
|
||||||
</span>
|
<span className={`status-chip small ${status.className}`}>{t(status.label)}</span>
|
||||||
{status && <span className={`tag ${status.className}`}>{t(status.label)}</span>}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="doses-col">
|
<div className="doses-col">
|
||||||
{item.doses.map((dose) => {
|
{item.doses.map((dose) => {
|
||||||
const isTaken = takenDoses.has(dose.id);
|
const isTaken = takenDoses.has(dose.id);
|
||||||
// Only disable doses on future DAYS, not later today
|
|
||||||
const doseDate = new Date(dose.when);
|
|
||||||
doseDate.setHours(0, 0, 0, 0);
|
|
||||||
const todayMidnight = new Date();
|
|
||||||
todayMidnight.setHours(0, 0, 0, 0);
|
|
||||||
const isFutureDose = doseDate.getTime() > todayMidnight.getTime();
|
|
||||||
const isOverdue = dose.when < Date.now() && !isTaken && !isFutureDose;
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div key={dose.id} className={`dose-item future ${isTaken ? "all-taken" : ""}`}>
|
||||||
key={dose.id}
|
|
||||||
className={`dose-item ${isFutureDose ? "future" : ""} ${isTaken ? "all-taken" : ""}`}
|
|
||||||
>
|
|
||||||
<span className="dose-time">{dose.timeStr}</span>
|
<span className="dose-time">{dose.timeStr}</span>
|
||||||
<span className="dose-usage">
|
<span className="dose-usage">
|
||||||
{dose.usage} {dose.usage !== 1 ? t("common.pills") : t("common.pill")}
|
{dose.usage} {dose.usage !== 1 ? t("common.pills") : t("common.pill")}
|
||||||
@@ -1024,9 +1134,7 @@ export function SharedSchedule() {
|
|||||||
` (${dose.usage * med.pillWeightMg} ${med.doseUnit ?? "mg"})`}
|
` (${dose.usage * med.pillWeightMg} ${med.doseUnit ?? "mg"})`}
|
||||||
</span>
|
</span>
|
||||||
<div className="dose-checks">
|
<div className="dose-checks">
|
||||||
<div
|
<div className={`dose-person ${isTaken ? "taken" : ""}`}>
|
||||||
className={`dose-person ${isTaken ? "taken" : ""} ${isOverdue ? "overdue" : ""}`}
|
|
||||||
>
|
|
||||||
{dose.takenBy && <span className="person-name">{dose.takenBy}</span>}
|
{dose.takenBy && <span className="person-name">{dose.takenBy}</span>}
|
||||||
{isTaken ? (
|
{isTaken ? (
|
||||||
<button
|
<button
|
||||||
@@ -1041,7 +1149,7 @@ export function SharedSchedule() {
|
|||||||
className="dose-btn take"
|
className="dose-btn take"
|
||||||
onClick={() => markDoseTaken(dose.id)}
|
onClick={() => markDoseTaken(dose.id)}
|
||||||
title={t("dose.markAsTaken")}
|
title={t("dose.markAsTaken")}
|
||||||
disabled={isFutureDose || isEmpty}
|
disabled={true}
|
||||||
>
|
>
|
||||||
✓
|
✓
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -66,7 +66,8 @@ export function UserFilterModal({
|
|||||||
</div>
|
</div>
|
||||||
<div className="user-med-stats">
|
<div className="user-med-stats">
|
||||||
<span className="user-med-pills">
|
<span className="user-med-pills">
|
||||||
{currentStock}/{formatNumber(packageSize)} {t("common.pills")}
|
{currentStock}/{formatNumber(packageSize)}{" "}
|
||||||
|
{packageSize === 1 ? t("common.pill") : t("common.pills")}
|
||||||
</span>
|
</span>
|
||||||
{status && <span className={`status-chip ${status.className}`}>{t(status.label)}</span>}
|
{status && <span className={`status-chip ${status.className}`}>{t(status.label)}</span>}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -278,7 +278,8 @@ export function AppProvider({ children }: { children: React.ReactNode }) {
|
|||||||
systemLocale,
|
systemLocale,
|
||||||
settingsHook.settings.reminderDaysBefore,
|
settingsHook.settings.reminderDaysBefore,
|
||||||
settingsHook.settings.stockCalculationMode,
|
settingsHook.settings.stockCalculationMode,
|
||||||
doses.takenDoses
|
doses.takenDoses,
|
||||||
|
doses.takenDoseTimestamps
|
||||||
),
|
),
|
||||||
[
|
[
|
||||||
medications.meds,
|
medications.meds,
|
||||||
@@ -287,6 +288,7 @@ export function AppProvider({ children }: { children: React.ReactNode }) {
|
|||||||
settingsHook.settings.reminderDaysBefore,
|
settingsHook.settings.reminderDaysBefore,
|
||||||
settingsHook.settings.stockCalculationMode,
|
settingsHook.settings.stockCalculationMode,
|
||||||
doses.takenDoses,
|
doses.takenDoses,
|
||||||
|
doses.takenDoseTimestamps,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -613,7 +615,9 @@ export function AppProvider({ children }: { children: React.ReactNode }) {
|
|||||||
settings.repeatRemindersEnabled !== savedSettings.repeatRemindersEnabled ||
|
settings.repeatRemindersEnabled !== savedSettings.repeatRemindersEnabled ||
|
||||||
settings.reminderRepeatIntervalMinutes !== savedSettings.reminderRepeatIntervalMinutes ||
|
settings.reminderRepeatIntervalMinutes !== savedSettings.reminderRepeatIntervalMinutes ||
|
||||||
settings.maxNaggingReminders !== savedSettings.maxNaggingReminders ||
|
settings.maxNaggingReminders !== savedSettings.maxNaggingReminders ||
|
||||||
settings.stockCalculationMode !== savedSettings.stockCalculationMode
|
settings.stockCalculationMode !== savedSettings.stockCalculationMode ||
|
||||||
|
settings.shareStockStatus !== savedSettings.shareStockStatus ||
|
||||||
|
settings.expiryWarningDays !== savedSettings.expiryWarningDays
|
||||||
);
|
);
|
||||||
}, [settingsHook.settings, settingsHook.savedSettings]);
|
}, [settingsHook.settings, settingsHook.savedSettings]);
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export type { Settings, UseSettingsReturn } from "./useSettings";
|
|||||||
export { useSettings } from "./useSettings";
|
export { useSettings } from "./useSettings";
|
||||||
export type { UseShareReturn } from "./useShare";
|
export type { UseShareReturn } from "./useShare";
|
||||||
export { useShare } from "./useShare";
|
export { useShare } from "./useShare";
|
||||||
export type { Theme, UseThemeReturn } from "./useTheme";
|
export type { Theme, ThemePreference, UseThemeReturn } from "./useTheme";
|
||||||
export { useTheme } from "./useTheme";
|
export { useTheme } from "./useTheme";
|
||||||
export type { UseUnsavedChangesWarningReturn } from "./useUnsavedChangesWarning";
|
export type { UseUnsavedChangesWarningReturn } from "./useUnsavedChangesWarning";
|
||||||
export { useUnsavedChangesWarning } from "./useUnsavedChangesWarning";
|
export { useUnsavedChangesWarning } from "./useUnsavedChangesWarning";
|
||||||
|
|||||||
@@ -2,11 +2,12 @@
|
|||||||
// useDoses Hook - Dose tracking state and operations
|
// useDoses Hook - Dose tracking state and operations
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
|
|
||||||
export interface UseDosesReturn {
|
export interface UseDosesReturn {
|
||||||
takenDoses: Set<string>;
|
takenDoses: Set<string>;
|
||||||
setTakenDoses: React.Dispatch<React.SetStateAction<Set<string>>>;
|
setTakenDoses: React.Dispatch<React.SetStateAction<Set<string>>>;
|
||||||
|
takenDoseTimestamps: Map<string, number>;
|
||||||
dismissedDoses: Set<string>;
|
dismissedDoses: Set<string>;
|
||||||
showClearMissedConfirm: boolean;
|
showClearMissedConfirm: boolean;
|
||||||
setShowClearMissedConfirm: (show: boolean) => void;
|
setShowClearMissedConfirm: (show: boolean) => void;
|
||||||
@@ -19,25 +20,39 @@ export interface UseDosesReturn {
|
|||||||
|
|
||||||
export function useDoses(): UseDosesReturn {
|
export function useDoses(): UseDosesReturn {
|
||||||
const [takenDoses, setTakenDoses] = useState<Set<string>>(new Set());
|
const [takenDoses, setTakenDoses] = useState<Set<string>>(new Set());
|
||||||
|
const [takenDoseTimestamps, setTakenDoseTimestamps] = useState<Map<string, number>>(new Map());
|
||||||
const [dismissedDoses, setDismissedDoses] = useState<Set<string>>(new Set());
|
const [dismissedDoses, setDismissedDoses] = useState<Set<string>>(new Set());
|
||||||
const [showClearMissedConfirm, setShowClearMissedConfirm] = useState(false);
|
const [showClearMissedConfirm, setShowClearMissedConfirm] = useState(false);
|
||||||
|
|
||||||
|
// Track in-flight mutations to prevent polling from overwriting optimistic updates
|
||||||
|
const mutationInFlightRef = useRef(0);
|
||||||
|
|
||||||
// Load taken doses from server
|
// Load taken doses from server
|
||||||
const loadTakenDoses = useCallback(async () => {
|
const loadTakenDoses = useCallback(async () => {
|
||||||
|
// Skip polling while mutations are in-flight to prevent race conditions
|
||||||
|
// where a poll response with stale data overwrites optimistic updates
|
||||||
|
if (mutationInFlightRef.current > 0) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch("/api/doses/taken", { credentials: "include" });
|
const res = await fetch("/api/doses/taken", { credentials: "include" });
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
|
// Double-check no mutation started while we were fetching
|
||||||
|
if (mutationInFlightRef.current > 0) return;
|
||||||
|
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
const taken = new Set<string>();
|
const taken = new Set<string>();
|
||||||
|
const timestamps = new Map<string, number>();
|
||||||
const dismissed = new Set<string>();
|
const dismissed = new Set<string>();
|
||||||
for (const d of data.doses) {
|
for (const d of data.doses) {
|
||||||
if (d.dismissed) {
|
if (d.dismissed) {
|
||||||
dismissed.add(d.doseId);
|
dismissed.add(d.doseId);
|
||||||
} else {
|
} else {
|
||||||
taken.add(d.doseId);
|
taken.add(d.doseId);
|
||||||
|
timestamps.set(d.doseId, d.takenAt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setTakenDoses(taken);
|
setTakenDoses(taken);
|
||||||
|
setTakenDoseTimestamps(timestamps);
|
||||||
setDismissedDoses(dismissed);
|
setDismissedDoses(dismissed);
|
||||||
}
|
}
|
||||||
// Don't reset on error - keep current state
|
// Don't reset on error - keep current state
|
||||||
@@ -77,59 +92,91 @@ export function useDoses(): UseDosesReturn {
|
|||||||
[takenDoses, getDoseId]
|
[takenDoses, getDoseId]
|
||||||
);
|
);
|
||||||
|
|
||||||
const markDoseTaken = useCallback(async (doseId: string) => {
|
const markDoseTaken = useCallback(
|
||||||
// Optimistic update
|
async (doseId: string) => {
|
||||||
setTakenDoses((prev) => {
|
// Optimistic update
|
||||||
const next = new Set(prev);
|
mutationInFlightRef.current++;
|
||||||
next.add(doseId);
|
|
||||||
return next;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Send to server
|
|
||||||
try {
|
|
||||||
await fetch("/api/doses/taken", {
|
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
credentials: "include",
|
|
||||||
body: JSON.stringify({ doseId }),
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// Revert on error
|
|
||||||
setTakenDoses((prev) => {
|
|
||||||
const next = new Set(prev);
|
|
||||||
next.delete(doseId);
|
|
||||||
return next;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const undoDoseTaken = useCallback(async (doseId: string) => {
|
|
||||||
// Optimistic update
|
|
||||||
setTakenDoses((prev) => {
|
|
||||||
const next = new Set(prev);
|
|
||||||
next.delete(doseId);
|
|
||||||
return next;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Send to server
|
|
||||||
try {
|
|
||||||
await fetch(`/api/doses/taken/${encodeURIComponent(doseId)}`, {
|
|
||||||
method: "DELETE",
|
|
||||||
credentials: "include",
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
// Revert on error
|
|
||||||
setTakenDoses((prev) => {
|
setTakenDoses((prev) => {
|
||||||
const next = new Set(prev);
|
const next = new Set(prev);
|
||||||
next.add(doseId);
|
next.add(doseId);
|
||||||
return next;
|
return next;
|
||||||
});
|
});
|
||||||
}
|
setTakenDoseTimestamps((prev) => {
|
||||||
}, []);
|
const next = new Map(prev);
|
||||||
|
next.set(doseId, Date.now());
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Send to server
|
||||||
|
try {
|
||||||
|
await fetch("/api/doses/taken", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
credentials: "include",
|
||||||
|
body: JSON.stringify({ doseId }),
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
// Revert on error
|
||||||
|
setTakenDoses((prev) => {
|
||||||
|
const next = new Set(prev);
|
||||||
|
next.delete(doseId);
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
setTakenDoseTimestamps((prev) => {
|
||||||
|
const next = new Map(prev);
|
||||||
|
next.delete(doseId);
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
mutationInFlightRef.current--;
|
||||||
|
// Re-sync with server after mutation completes
|
||||||
|
loadTakenDoses();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[loadTakenDoses]
|
||||||
|
);
|
||||||
|
|
||||||
|
const undoDoseTaken = useCallback(
|
||||||
|
async (doseId: string) => {
|
||||||
|
// Optimistic update
|
||||||
|
mutationInFlightRef.current++;
|
||||||
|
setTakenDoses((prev) => {
|
||||||
|
const next = new Set(prev);
|
||||||
|
next.delete(doseId);
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
setTakenDoseTimestamps((prev) => {
|
||||||
|
const next = new Map(prev);
|
||||||
|
next.delete(doseId);
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Send to server
|
||||||
|
try {
|
||||||
|
await fetch(`/api/doses/taken/${encodeURIComponent(doseId)}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
credentials: "include",
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
// Revert on error
|
||||||
|
setTakenDoses((prev) => {
|
||||||
|
const next = new Set(prev);
|
||||||
|
next.add(doseId);
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
mutationInFlightRef.current--;
|
||||||
|
// Re-sync with server after mutation completes
|
||||||
|
loadTakenDoses();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[loadTakenDoses]
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
takenDoses,
|
takenDoses,
|
||||||
setTakenDoses,
|
setTakenDoses,
|
||||||
|
takenDoseTimestamps,
|
||||||
dismissedDoses,
|
dismissedDoses,
|
||||||
showClearMissedConfirm,
|
showClearMissedConfirm,
|
||||||
setShowClearMissedConfirm,
|
setShowClearMissedConfirm,
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ export interface Settings {
|
|||||||
lastNotificationChannel: "email" | "push" | "both" | null;
|
lastNotificationChannel: "email" | "push" | "both" | null;
|
||||||
lastReminderMedName: string | null;
|
lastReminderMedName: string | null;
|
||||||
lastReminderTakenBy: string | null;
|
lastReminderTakenBy: string | null;
|
||||||
|
lastStockReminderSent: string | null;
|
||||||
|
lastStockReminderChannel: "email" | "push" | "both" | null;
|
||||||
|
lastStockReminderMedNames: string | null;
|
||||||
shoutrrrEnabled: boolean;
|
shoutrrrEnabled: boolean;
|
||||||
shoutrrrUrl: string;
|
shoutrrrUrl: string;
|
||||||
emailStockReminders: boolean;
|
emailStockReminders: boolean;
|
||||||
@@ -37,6 +40,7 @@ export interface Settings {
|
|||||||
shoutrrrStockReminders: boolean;
|
shoutrrrStockReminders: boolean;
|
||||||
shoutrrrIntakeReminders: boolean;
|
shoutrrrIntakeReminders: boolean;
|
||||||
stockCalculationMode: "automatic" | "manual";
|
stockCalculationMode: "automatic" | "manual";
|
||||||
|
shareStockStatus: boolean;
|
||||||
expiryWarningDays: number;
|
expiryWarningDays: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +69,9 @@ const defaultSettings: Settings = {
|
|||||||
lastNotificationChannel: null,
|
lastNotificationChannel: null,
|
||||||
lastReminderMedName: null,
|
lastReminderMedName: null,
|
||||||
lastReminderTakenBy: null,
|
lastReminderTakenBy: null,
|
||||||
|
lastStockReminderSent: null,
|
||||||
|
lastStockReminderChannel: null,
|
||||||
|
lastStockReminderMedNames: null,
|
||||||
shoutrrrEnabled: false,
|
shoutrrrEnabled: false,
|
||||||
shoutrrrUrl: "",
|
shoutrrrUrl: "",
|
||||||
emailStockReminders: true,
|
emailStockReminders: true,
|
||||||
@@ -72,6 +79,7 @@ const defaultSettings: Settings = {
|
|||||||
shoutrrrStockReminders: true,
|
shoutrrrStockReminders: true,
|
||||||
shoutrrrIntakeReminders: true,
|
shoutrrrIntakeReminders: true,
|
||||||
stockCalculationMode: "automatic",
|
stockCalculationMode: "automatic",
|
||||||
|
shareStockStatus: true,
|
||||||
expiryWarningDays: 30,
|
expiryWarningDays: 30,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -141,6 +149,9 @@ export function useSettings(): UseSettingsReturn {
|
|||||||
lastNotificationChannel: data.lastNotificationChannel ?? prev.lastNotificationChannel,
|
lastNotificationChannel: data.lastNotificationChannel ?? prev.lastNotificationChannel,
|
||||||
lastReminderMedName: data.lastReminderMedName ?? prev.lastReminderMedName,
|
lastReminderMedName: data.lastReminderMedName ?? prev.lastReminderMedName,
|
||||||
lastReminderTakenBy: data.lastReminderTakenBy ?? prev.lastReminderTakenBy,
|
lastReminderTakenBy: data.lastReminderTakenBy ?? prev.lastReminderTakenBy,
|
||||||
|
lastStockReminderSent: data.lastStockReminderSent ?? prev.lastStockReminderSent,
|
||||||
|
lastStockReminderChannel: data.lastStockReminderChannel ?? prev.lastStockReminderChannel,
|
||||||
|
lastStockReminderMedNames: data.lastStockReminderMedNames ?? prev.lastStockReminderMedNames,
|
||||||
}));
|
}));
|
||||||
setSavedSettings((prev) => ({
|
setSavedSettings((prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
@@ -149,6 +160,9 @@ export function useSettings(): UseSettingsReturn {
|
|||||||
lastNotificationChannel: data.lastNotificationChannel ?? prev.lastNotificationChannel,
|
lastNotificationChannel: data.lastNotificationChannel ?? prev.lastNotificationChannel,
|
||||||
lastReminderMedName: data.lastReminderMedName ?? prev.lastReminderMedName,
|
lastReminderMedName: data.lastReminderMedName ?? prev.lastReminderMedName,
|
||||||
lastReminderTakenBy: data.lastReminderTakenBy ?? prev.lastReminderTakenBy,
|
lastReminderTakenBy: data.lastReminderTakenBy ?? prev.lastReminderTakenBy,
|
||||||
|
lastStockReminderSent: data.lastStockReminderSent ?? prev.lastStockReminderSent,
|
||||||
|
lastStockReminderChannel: data.lastStockReminderChannel ?? prev.lastStockReminderChannel,
|
||||||
|
lastStockReminderMedNames: data.lastStockReminderMedNames ?? prev.lastStockReminderMedNames,
|
||||||
}));
|
}));
|
||||||
})
|
})
|
||||||
.catch(() => {});
|
.catch(() => {});
|
||||||
@@ -198,6 +212,7 @@ export function useSettings(): UseSettingsReturn {
|
|||||||
shoutrrrStockReminders: settings.shoutrrrStockReminders,
|
shoutrrrStockReminders: settings.shoutrrrStockReminders,
|
||||||
shoutrrrIntakeReminders: settings.shoutrrrIntakeReminders,
|
shoutrrrIntakeReminders: settings.shoutrrrIntakeReminders,
|
||||||
stockCalculationMode: settings.stockCalculationMode,
|
stockCalculationMode: settings.stockCalculationMode,
|
||||||
|
shareStockStatus: settings.shareStockStatus,
|
||||||
language: i18n.language,
|
language: i18n.language,
|
||||||
smtpHost: settings.smtpHost,
|
smtpHost: settings.smtpHost,
|
||||||
smtpPort: settings.smtpPort,
|
smtpPort: settings.smtpPort,
|
||||||
|
|||||||
@@ -1,32 +1,80 @@
|
|||||||
// =============================================================================
|
// =============================================================================
|
||||||
// useTheme Hook - Theme (dark/light mode) state management
|
// useTheme Hook - Theme (dark/light/system mode) state management
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
|
|
||||||
export type Theme = "light" | "dark";
|
export type Theme = "light" | "dark";
|
||||||
|
export type ThemePreference = "light" | "dark" | "system";
|
||||||
|
|
||||||
export interface UseThemeReturn {
|
export interface UseThemeReturn {
|
||||||
|
/** The resolved theme applied to the DOM ("light" | "dark") */
|
||||||
theme: Theme;
|
theme: Theme;
|
||||||
|
/** The user's preference ("light" | "dark" | "system") */
|
||||||
|
themePreference: ThemePreference;
|
||||||
|
/** Set the theme preference */
|
||||||
|
setThemePreference: (pref: ThemePreference) => void;
|
||||||
|
/** Legacy toggle: cycles light → dark → system → light */
|
||||||
toggleTheme: () => void;
|
toggleTheme: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSystemTheme(): Theme {
|
||||||
|
if (typeof window !== "undefined" && window.matchMedia?.("(prefers-color-scheme: light)").matches) {
|
||||||
|
return "light";
|
||||||
|
}
|
||||||
|
return "dark";
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveTheme(pref: ThemePreference): Theme {
|
||||||
|
return pref === "system" ? getSystemTheme() : pref;
|
||||||
|
}
|
||||||
|
|
||||||
export function useTheme(): UseThemeReturn {
|
export function useTheme(): UseThemeReturn {
|
||||||
const [theme, setTheme] = useState<Theme>(() => {
|
const [themePreference, setThemePreferenceState] = useState<ThemePreference>(() => {
|
||||||
if (typeof window !== "undefined") {
|
if (typeof window !== "undefined") {
|
||||||
return (localStorage.getItem("theme") as Theme) || "dark";
|
const stored = localStorage.getItem("theme") as ThemePreference | null;
|
||||||
|
if (stored === "light" || stored === "dark" || stored === "system") return stored;
|
||||||
|
return "dark";
|
||||||
}
|
}
|
||||||
return "dark";
|
return "dark";
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
const [theme, setTheme] = useState<Theme>(() => resolveTheme(themePreference));
|
||||||
document.documentElement.setAttribute("data-theme", theme);
|
|
||||||
localStorage.setItem("theme", theme);
|
|
||||||
}, [theme]);
|
|
||||||
|
|
||||||
const toggleTheme = useCallback(() => {
|
// Apply resolved theme to DOM whenever preference or system theme changes
|
||||||
setTheme((prev) => (prev === "dark" ? "light" : "dark"));
|
useEffect(() => {
|
||||||
|
const resolved = resolveTheme(themePreference);
|
||||||
|
setTheme(resolved);
|
||||||
|
document.documentElement.setAttribute("data-theme", resolved);
|
||||||
|
localStorage.setItem("theme", themePreference);
|
||||||
|
}, [themePreference]);
|
||||||
|
|
||||||
|
// Listen for system theme changes when preference is "system"
|
||||||
|
useEffect(() => {
|
||||||
|
if (themePreference !== "system") return;
|
||||||
|
const mq = window.matchMedia?.("(prefers-color-scheme: light)");
|
||||||
|
if (!mq) return;
|
||||||
|
|
||||||
|
const handler = () => {
|
||||||
|
const resolved = resolveTheme("system");
|
||||||
|
setTheme(resolved);
|
||||||
|
document.documentElement.setAttribute("data-theme", resolved);
|
||||||
|
};
|
||||||
|
mq.addEventListener("change", handler);
|
||||||
|
return () => mq.removeEventListener("change", handler);
|
||||||
|
}, [themePreference]);
|
||||||
|
|
||||||
|
const setThemePreference = useCallback((pref: ThemePreference) => {
|
||||||
|
setThemePreferenceState(pref);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return { theme, toggleTheme };
|
const toggleTheme = useCallback(() => {
|
||||||
|
setThemePreferenceState((prev) => {
|
||||||
|
if (prev === "light") return "dark";
|
||||||
|
if (prev === "dark") return "system";
|
||||||
|
return "light";
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return { theme, themePreference, setThemePreference, toggleTheme };
|
||||||
}
|
}
|
||||||
|
|||||||
+51
-23
@@ -21,11 +21,11 @@
|
|||||||
"badge": "Bestandsüberwachung",
|
"badge": "Bestandsüberwachung",
|
||||||
"noMeds": "Noch keine Medikamente konfiguriert.",
|
"noMeds": "Noch keine Medikamente konfiguriert.",
|
||||||
"allGood": "Alles in Ordnung, genug Vorrat.",
|
"allGood": "Alles in Ordnung, genug Vorrat.",
|
||||||
"lowWarning": "Genug Vorrat, aber {{meds}} wird knapp.",
|
"lowWarning": "Genug Vorrat, aber {{meds}} ist kritisch niedrig.",
|
||||||
"lowWarning_other": "Genug Vorrat, aber {{meds}} werden knapp.",
|
"lowWarning_other": "Genug Vorrat, aber {{meds}} sind kritisch niedrig.",
|
||||||
"lowWarningPrefix": "Genug Vorrat, aber",
|
"lowWarningPrefix": "Genug Vorrat, aber",
|
||||||
"lowWarningSuffix": "wird knapp.",
|
"lowWarningSuffix": "ist kritisch niedrig.",
|
||||||
"lowWarningSuffix_other": "werden knapp.",
|
"lowWarningSuffix_other": "sind kritisch niedrig.",
|
||||||
"sendReminder": "🔔 Erinnerung jetzt senden"
|
"sendReminder": "🔔 Erinnerung jetzt senden"
|
||||||
},
|
},
|
||||||
"overview": {
|
"overview": {
|
||||||
@@ -59,10 +59,11 @@
|
|||||||
"reminders": {
|
"reminders": {
|
||||||
"active": "Automatische Erinnerungen aktiv",
|
"active": "Automatische Erinnerungen aktiv",
|
||||||
"status": "Status",
|
"status": "Status",
|
||||||
"allStockOk": "Bestand OK",
|
"allStockOk": "Bestand gut",
|
||||||
"allOk": "Alles OK",
|
"allOk": "Alles gut",
|
||||||
"lastReminder": "Letzte Einnahme-Erinnerung",
|
"lastReminder": "Letzte Einnahme-Erinnerung",
|
||||||
"lastSent": "Letzte Einnahme-Erinnerung",
|
"lastSent": "Letzte Einnahme-Erinnerung",
|
||||||
|
"lastStockSent": "Letzte Bestands-Erinnerung",
|
||||||
"next": "Nachbestell-Erinnerung",
|
"next": "Nachbestell-Erinnerung",
|
||||||
"nextIn": "Nachbestell-Erinnerung",
|
"nextIn": "Nachbestell-Erinnerung",
|
||||||
"inDays": "in {{days}} Tagen",
|
"inDays": "in {{days}} Tagen",
|
||||||
@@ -73,8 +74,8 @@
|
|||||||
"needRefill_other": "{{count}} Medikamente nachfüllen",
|
"needRefill_other": "{{count}} Medikamente nachfüllen",
|
||||||
"emptyStock": "{{count}} Medikament leer",
|
"emptyStock": "{{count}} Medikament leer",
|
||||||
"emptyStock_other": "{{count}} Medikamente leer",
|
"emptyStock_other": "{{count}} Medikamente leer",
|
||||||
"lowWarning": "{{count}} Medikament wird knapp",
|
"lowWarning": "{{count}} Medikament kritisch niedrig",
|
||||||
"lowWarning_other": "{{count}} Medikamente werden knapp",
|
"lowWarning_other": "{{count}} Medikamente kritisch niedrig",
|
||||||
"waitingFirstCheck": "Warte auf erste Prüfung",
|
"waitingFirstCheck": "Warte auf erste Prüfung",
|
||||||
"type": "Typ",
|
"type": "Typ",
|
||||||
"typeStock": "Bestand",
|
"typeStock": "Bestand",
|
||||||
@@ -123,7 +124,9 @@
|
|||||||
"pillsPerBlister": "Tabletten pro Blister",
|
"pillsPerBlister": "Tabletten pro Blister",
|
||||||
"loose": "Lose",
|
"loose": "Lose",
|
||||||
"total": "Gesamt",
|
"total": "Gesamt",
|
||||||
"stock": "Bestand"
|
"stock": "Bestand",
|
||||||
|
"totalCapacity": "Kapazität",
|
||||||
|
"type": "Typ"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"form": {
|
"form": {
|
||||||
@@ -135,7 +138,7 @@
|
|||||||
"takenBy": "Eingenommen von",
|
"takenBy": "Eingenommen von",
|
||||||
"packageType": "Verpackungsart",
|
"packageType": "Verpackungsart",
|
||||||
"packageTypeBlister": "Blisterpackung",
|
"packageTypeBlister": "Blisterpackung",
|
||||||
"packageTypeBottle": "Pillendose / Behälter",
|
"packageTypeBottle": "Pillendose",
|
||||||
"packs": "Packungen",
|
"packs": "Packungen",
|
||||||
"blistersPerPack": "Blister pro Packung",
|
"blistersPerPack": "Blister pro Packung",
|
||||||
"pillsPerBlister": "Tabletten pro Blister",
|
"pillsPerBlister": "Tabletten pro Blister",
|
||||||
@@ -176,10 +179,12 @@
|
|||||||
"badge": "Vorrat planen",
|
"badge": "Vorrat planen",
|
||||||
"from": "Von",
|
"from": "Von",
|
||||||
"until": "Bis",
|
"until": "Bis",
|
||||||
"includeUntilStart": "Verbrauch von heute bis Startdatum einrechnen",
|
"includeUntilStart": "Aktuellen Verbrauch einrechnen",
|
||||||
|
"includeUntilStartTooltip": "Wenn aktiviert, werden die Pillen, die zwischen heute und dem gewählten Startdatum verbraucht werden, vom aktuellen Bestand abgezogen. So erhältst du ein genaueres Bild davon, wie viel du zu Beginn des Planungszeitraums tatsächlich noch übrig hast.",
|
||||||
"calculate": "Berechnen",
|
"calculate": "Berechnen",
|
||||||
"calculating": "Wird berechnet...",
|
"calculating": "Wird berechnet...",
|
||||||
"sendEmail": "📧 Per E-Mail senden",
|
"sendEmail": "📧 Per E-Mail senden",
|
||||||
|
"sendNotification": "🔔 Bedarf senden",
|
||||||
"table": {
|
"table": {
|
||||||
"medication": "Medikament",
|
"medication": "Medikament",
|
||||||
"usage": "Verbrauch",
|
"usage": "Verbrauch",
|
||||||
@@ -228,25 +233,34 @@
|
|||||||
"intakeCheck": "Einnahmeprüfung",
|
"intakeCheck": "Einnahmeprüfung",
|
||||||
"15minBefore": "15 Min. vor geplanter Zeit",
|
"15minBefore": "15 Min. vor geplanter Zeit",
|
||||||
"nextCheck": "Nächste Bestandsprüfung",
|
"nextCheck": "Nächste Bestandsprüfung",
|
||||||
"lastSent": "Zuletzt gesendet",
|
"lastSent": "Letzte Benachrichtigung",
|
||||||
|
"lastStockSent": "Letzte Bestands-Erinnerung",
|
||||||
|
"lastIntakeSent": "Letzte Einnahme-Erinnerung",
|
||||||
"envHint": "Diese Werte können über REMINDER_HOUR und REMINDER_MINUTES_BEFORE in .env konfiguriert werden"
|
"envHint": "Diese Werte können über REMINDER_HOUR und REMINDER_MINUTES_BEFORE in .env konfiguriert werden"
|
||||||
},
|
},
|
||||||
"stock": {
|
"stock": {
|
||||||
"title": "Bestand",
|
"title": "Bestand",
|
||||||
"threshold": "Erinnerungsschwelle",
|
|
||||||
"remindWhen": "Erinnern wenn Vorrat unter",
|
|
||||||
"repeatDaily": "Täglich wiederholen",
|
|
||||||
"repeatTooltip": "Wenn aktiviert, wird täglich eine Erinnerung gesendet solange der Bestand niedrig ist. Andernfalls nur einmal pro Medikament bis zum Auffüllen.",
|
|
||||||
"calculationMode": "Bestandsberechnung",
|
"calculationMode": "Bestandsberechnung",
|
||||||
"automatic": "Automatisch",
|
"automatic": "Automatisch",
|
||||||
"automaticDesc": "Bestand wird automatisch anhand des Einnahmeplans reduziert",
|
"automaticDesc": "Bestand wird automatisch anhand des Einnahmeplans reduziert",
|
||||||
"manual": "Manuell",
|
"manual": "Manuell",
|
||||||
"manualDesc": "Bestand wird nur reduziert wenn Dosen als genommen markiert werden",
|
"manualDesc": "Bestand wird nur reduziert wenn Dosen als genommen markiert werden",
|
||||||
"display": "Anzeige",
|
"thresholds": "Schwellenwerte",
|
||||||
"lowStockDays": "Niedriger Bestand (Tage)",
|
"criticalStockDays": "Kritisch (Tage)",
|
||||||
"lowStockTooltip": "Gelbe Warnung ab diesem Schwellenwert",
|
"criticalStockTooltip": "Bestand unter diesem Wert ist kritisch und erfordert sofortige Aufmerksamkeit",
|
||||||
"highStockDays": "Hoher Bestand (Tage)",
|
"lowStockDays": "Niedrig (Tage)",
|
||||||
"highStockTooltip": "Grün mit Stern ab diesem Schwellenwert"
|
"lowStockTooltip": "Bestand unter diesem Wert bedeutet, dass bald nachbestellt werden sollte",
|
||||||
|
"highStockDays": "Hoch (Tage)",
|
||||||
|
"highStockTooltip": "Bestand über diesem Wert bedeutet, dass du gut versorgt bist",
|
||||||
|
"thresholdValidation": "Werte müssen sein: Kritisch < Niedrig < Hoch",
|
||||||
|
"shareStockStatus": "Bestand auf geteilten Links anzeigen",
|
||||||
|
"shareStockStatusDesc": "Bestandsstatus (Normal/Niedrig/Kritisch) und farbige Rahmen auf geteilten Zeitplan-Links für Einnahme-Nutzer anzeigen"
|
||||||
|
},
|
||||||
|
"stockReminder": {
|
||||||
|
"title": "Bestands-Erinnerung",
|
||||||
|
"description": "Benachrichtigung wenn Medikamentenbestand erreicht",
|
||||||
|
"repeatDaily": "Täglich wiederholen",
|
||||||
|
"repeatTooltip": "Wenn aktiviert, wird täglich eine Erinnerung gesendet solange der Bestand kritisch ist. Andernfalls nur einmal pro Medikament bis zum Auffüllen."
|
||||||
},
|
},
|
||||||
"saveSettings": "Einstellungen speichern"
|
"saveSettings": "Einstellungen speichern"
|
||||||
},
|
},
|
||||||
@@ -287,9 +301,16 @@
|
|||||||
"tooltips": {
|
"tooltips": {
|
||||||
"intakeReminders": "Einnahme-Erinnerungen aktiviert",
|
"intakeReminders": "Einnahme-Erinnerungen aktiviert",
|
||||||
"hasNotes": "Hat Notizen",
|
"hasNotes": "Hat Notizen",
|
||||||
|
"stockExceedsCapacity": "Bestand überschreitet Packungskapazität — Packungsanzahl anpassen",
|
||||||
"lightMode": "Zum hellen Modus wechseln",
|
"lightMode": "Zum hellen Modus wechseln",
|
||||||
"darkMode": "Zum dunklen Modus wechseln"
|
"darkMode": "Zum dunklen Modus wechseln"
|
||||||
},
|
},
|
||||||
|
"theme": {
|
||||||
|
"title": "Design",
|
||||||
|
"light": "Hell",
|
||||||
|
"dark": "Dunkel",
|
||||||
|
"system": "System"
|
||||||
|
},
|
||||||
"dose": {
|
"dose": {
|
||||||
"takenBy": "eingenommen von",
|
"takenBy": "eingenommen von",
|
||||||
"markAsTaken": "Als eingenommen markieren"
|
"markAsTaken": "Als eingenommen markieren"
|
||||||
@@ -341,6 +362,9 @@
|
|||||||
"common": {
|
"common": {
|
||||||
"loading": "Wird geladen...",
|
"loading": "Wird geladen...",
|
||||||
"sending": "Wird gesendet...",
|
"sending": "Wird gesendet...",
|
||||||
|
"sent": "Gesendet!",
|
||||||
|
"sendFailed": "Senden fehlgeschlagen",
|
||||||
|
"networkError": "Netzwerkfehler",
|
||||||
"saving": "Wird gespeichert...",
|
"saving": "Wird gespeichert...",
|
||||||
"unsavedChanges": {
|
"unsavedChanges": {
|
||||||
"title": "Ungespeicherte Änderungen",
|
"title": "Ungespeicherte Änderungen",
|
||||||
@@ -379,6 +403,9 @@
|
|||||||
"fullBlisters": "volle Blister",
|
"fullBlisters": "volle Blister",
|
||||||
"inBlister": "in 1 Blister",
|
"inBlister": "in 1 Blister",
|
||||||
"total": "gesamt",
|
"total": "gesamt",
|
||||||
|
"pillsTotal": "{{count}} Tabletten gesamt",
|
||||||
|
"pillsTotal_one": "{{count}} Tablette gesamt",
|
||||||
|
"pillsTotal_other": "{{count}} Tabletten gesamt",
|
||||||
"max": "max"
|
"max": "max"
|
||||||
},
|
},
|
||||||
"share": {
|
"share": {
|
||||||
@@ -443,6 +470,7 @@
|
|||||||
"refill": {
|
"refill": {
|
||||||
"title": "Nachfüllen",
|
"title": "Nachfüllen",
|
||||||
"packs": "Packungen hinzufügen",
|
"packs": "Packungen hinzufügen",
|
||||||
|
"pillsToAdd": "Tabletten hinzufügen",
|
||||||
"loosePills": "Lose Tabletten hinzufügen",
|
"loosePills": "Lose Tabletten hinzufügen",
|
||||||
"pillsPerPack": "1 Packung = {{count}} Tabletten",
|
"pillsPerPack": "1 Packung = {{count}} Tabletten",
|
||||||
"addToStock": "Zum Bestand hinzufügen",
|
"addToStock": "Zum Bestand hinzufügen",
|
||||||
@@ -459,6 +487,7 @@
|
|||||||
"editStock": {
|
"editStock": {
|
||||||
"title": "Bestand korrigieren",
|
"title": "Bestand korrigieren",
|
||||||
"hint": "Dies ist für die Korrektur von Bestandsabweichungen. Für normale Bestandsänderungen nutze 'Nachfüllen'.",
|
"hint": "Dies ist für die Korrektur von Bestandsabweichungen. Für normale Bestandsänderungen nutze 'Nachfüllen'.",
|
||||||
|
"totalPills": "Gesamte Tabletten",
|
||||||
"fullBlisters": "Volle Blister",
|
"fullBlisters": "Volle Blister",
|
||||||
"partialBlisterPills": "Angebrochener Blister",
|
"partialBlisterPills": "Angebrochener Blister",
|
||||||
"pillsPerBlister": "(je {{count}} Tabletten)",
|
"pillsPerBlister": "(je {{count}} Tabletten)",
|
||||||
@@ -478,10 +507,9 @@
|
|||||||
"checking": "Prüfe...",
|
"checking": "Prüfe...",
|
||||||
"upToDate": "Du bist auf dem neuesten Stand!",
|
"upToDate": "Du bist auf dem neuesten Stand!",
|
||||||
"updateAvailable": "Update verfügbar",
|
"updateAvailable": "Update verfügbar",
|
||||||
"viewOnGitHub": "Auf GitHub ansehen",
|
|
||||||
"downloadUpdate": "Update herunterladen",
|
"downloadUpdate": "Update herunterladen",
|
||||||
"checkFailed": "Update-Prüfung fehlgeschlagen",
|
"checkFailed": "Update-Prüfung fehlgeschlagen",
|
||||||
"lastChecked": "Zuletzt geprüft",
|
"viewOnGitHub": "Auf GitHub ansehen",
|
||||||
"github": "GitHub",
|
"github": "GitHub",
|
||||||
"license": "MIT-Lizenz",
|
"license": "MIT-Lizenz",
|
||||||
"copyright": "© {{year}} Daniel Volz",
|
"copyright": "© {{year}} Daniel Volz",
|
||||||
|
|||||||
+51
-23
@@ -21,11 +21,11 @@
|
|||||||
"badge": "Stock watch",
|
"badge": "Stock watch",
|
||||||
"noMeds": "No medications configured yet.",
|
"noMeds": "No medications configured yet.",
|
||||||
"allGood": "All good, enough stock.",
|
"allGood": "All good, enough stock.",
|
||||||
"lowWarning": "Enough stock for now, but {{meds}} is running low.",
|
"lowWarning": "Enough stock for now, but {{meds}} is running critically low.",
|
||||||
"lowWarning_other": "Enough stock for now, but {{meds}} are running low.",
|
"lowWarning_other": "Enough stock for now, but {{meds}} are running critically low.",
|
||||||
"lowWarningPrefix": "Enough stock for now, but",
|
"lowWarningPrefix": "Enough stock for now, but",
|
||||||
"lowWarningSuffix": "is running low.",
|
"lowWarningSuffix": "is running critically low.",
|
||||||
"lowWarningSuffix_other": "are running low.",
|
"lowWarningSuffix_other": "are running critically low.",
|
||||||
"sendReminder": "🔔 Send Reminder Now"
|
"sendReminder": "🔔 Send Reminder Now"
|
||||||
},
|
},
|
||||||
"overview": {
|
"overview": {
|
||||||
@@ -59,10 +59,11 @@
|
|||||||
"reminders": {
|
"reminders": {
|
||||||
"active": "Automatic reminders active",
|
"active": "Automatic reminders active",
|
||||||
"status": "Status",
|
"status": "Status",
|
||||||
"allStockOk": "All stock OK",
|
"allStockOk": "All stock good",
|
||||||
"allOk": "All OK",
|
"allOk": "All good",
|
||||||
"lastReminder": "Last intake reminder",
|
"lastReminder": "Last intake reminder",
|
||||||
"lastSent": "Last intake reminder",
|
"lastSent": "Last intake reminder",
|
||||||
|
"lastStockSent": "Last stock reminder",
|
||||||
"next": "Refill reminder",
|
"next": "Refill reminder",
|
||||||
"nextIn": "Refill reminder",
|
"nextIn": "Refill reminder",
|
||||||
"inDays": "in {{days}} days",
|
"inDays": "in {{days}} days",
|
||||||
@@ -73,8 +74,8 @@
|
|||||||
"needRefill_other": "{{count}} meds need refill",
|
"needRefill_other": "{{count}} meds need refill",
|
||||||
"emptyStock": "{{count}} med is empty",
|
"emptyStock": "{{count}} med is empty",
|
||||||
"emptyStock_other": "{{count}} meds are empty",
|
"emptyStock_other": "{{count}} meds are empty",
|
||||||
"lowWarning": "{{count}} medication running low",
|
"lowWarning": "{{count}} medication running critically low",
|
||||||
"lowWarning_other": "{{count}} medications running low",
|
"lowWarning_other": "{{count}} medications running critically low",
|
||||||
"waitingFirstCheck": "Waiting for first check",
|
"waitingFirstCheck": "Waiting for first check",
|
||||||
"type": "Type",
|
"type": "Type",
|
||||||
"typeStock": "Stock",
|
"typeStock": "Stock",
|
||||||
@@ -123,7 +124,9 @@
|
|||||||
"pillsPerBlister": "Pills per blister",
|
"pillsPerBlister": "Pills per blister",
|
||||||
"loose": "Loose",
|
"loose": "Loose",
|
||||||
"total": "Total",
|
"total": "Total",
|
||||||
"stock": "Stock"
|
"stock": "Stock",
|
||||||
|
"totalCapacity": "Capacity",
|
||||||
|
"type": "Type"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"form": {
|
"form": {
|
||||||
@@ -135,7 +138,7 @@
|
|||||||
"takenBy": "Taken by",
|
"takenBy": "Taken by",
|
||||||
"packageType": "Package Type",
|
"packageType": "Package Type",
|
||||||
"packageTypeBlister": "Blister Pack",
|
"packageTypeBlister": "Blister Pack",
|
||||||
"packageTypeBottle": "Pill Bottle / Container",
|
"packageTypeBottle": "Pill Bottle",
|
||||||
"packs": "Packs",
|
"packs": "Packs",
|
||||||
"blistersPerPack": "Blisters per pack",
|
"blistersPerPack": "Blisters per pack",
|
||||||
"pillsPerBlister": "Pills per blister",
|
"pillsPerBlister": "Pills per blister",
|
||||||
@@ -176,10 +179,12 @@
|
|||||||
"badge": "Plan your supply",
|
"badge": "Plan your supply",
|
||||||
"from": "From",
|
"from": "From",
|
||||||
"until": "Until",
|
"until": "Until",
|
||||||
"includeUntilStart": "Include consumption from today until start date",
|
"includeUntilStart": "Include current consumption",
|
||||||
|
"includeUntilStartTooltip": "When enabled, pills consumed between today and the selected start date are subtracted from your current stock. This gives a more accurate picture of how much you'll actually have left when the planning period begins.",
|
||||||
"calculate": "Calculate",
|
"calculate": "Calculate",
|
||||||
"calculating": "Calculating...",
|
"calculating": "Calculating...",
|
||||||
"sendEmail": "📧 Send via Email",
|
"sendEmail": "📧 Send via Email",
|
||||||
|
"sendNotification": "🔔 Send Demand",
|
||||||
"table": {
|
"table": {
|
||||||
"medication": "Medication",
|
"medication": "Medication",
|
||||||
"usage": "Usage",
|
"usage": "Usage",
|
||||||
@@ -228,25 +233,34 @@
|
|||||||
"intakeCheck": "Intake check",
|
"intakeCheck": "Intake check",
|
||||||
"15minBefore": "15 min before scheduled time",
|
"15minBefore": "15 min before scheduled time",
|
||||||
"nextCheck": "Next stock check",
|
"nextCheck": "Next stock check",
|
||||||
"lastSent": "Last sent",
|
"lastSent": "Last notification sent",
|
||||||
|
"lastStockSent": "Last stock reminder",
|
||||||
|
"lastIntakeSent": "Last intake reminder",
|
||||||
"envHint": "These values can be configured via REMINDER_HOUR and REMINDER_MINUTES_BEFORE in .env"
|
"envHint": "These values can be configured via REMINDER_HOUR and REMINDER_MINUTES_BEFORE in .env"
|
||||||
},
|
},
|
||||||
"stock": {
|
"stock": {
|
||||||
"title": "Stock",
|
"title": "Stock",
|
||||||
"threshold": "Reminder Threshold",
|
|
||||||
"remindWhen": "Remind when supply drops below",
|
|
||||||
"repeatDaily": "Repeat daily",
|
|
||||||
"repeatTooltip": "When enabled, sends reminders every day while stock is low. Otherwise, only notifies once per medication until restocked.",
|
|
||||||
"calculationMode": "Stock Calculation",
|
"calculationMode": "Stock Calculation",
|
||||||
"automatic": "Automatic",
|
"automatic": "Automatic",
|
||||||
"automaticDesc": "Stock automatically decreases based on schedule",
|
"automaticDesc": "Stock automatically decreases based on schedule",
|
||||||
"manual": "Manual",
|
"manual": "Manual",
|
||||||
"manualDesc": "Stock only decreases when doses are marked as taken",
|
"manualDesc": "Stock only decreases when doses are marked as taken",
|
||||||
"display": "Display",
|
"thresholds": "Thresholds",
|
||||||
"lowStockDays": "Low Stock (days)",
|
"criticalStockDays": "Critical (days)",
|
||||||
"lowStockTooltip": "Yellow warning color threshold",
|
"criticalStockTooltip": "Stock below this value is critical and needs immediate attention",
|
||||||
"highStockDays": "High Stock (days)",
|
"lowStockDays": "Low (days)",
|
||||||
"highStockTooltip": "Green with star threshold"
|
"lowStockTooltip": "Stock below this value means you should reorder soon",
|
||||||
|
"highStockDays": "High (days)",
|
||||||
|
"highStockTooltip": "Stock above this value means you are well supplied",
|
||||||
|
"thresholdValidation": "Values must be: Critical < Low < High",
|
||||||
|
"shareStockStatus": "Show Stock on Shared Links",
|
||||||
|
"shareStockStatusDesc": "Show stock status (Normal/Low/Critical) and colored borders on shared schedule links for intake users"
|
||||||
|
},
|
||||||
|
"stockReminder": {
|
||||||
|
"title": "Stock Reminder",
|
||||||
|
"description": "Sends notification when medication stock reaches",
|
||||||
|
"repeatDaily": "Repeat daily",
|
||||||
|
"repeatTooltip": "When enabled, sends reminders every day while stock is critical. Otherwise, only notifies once per medication until restocked."
|
||||||
},
|
},
|
||||||
"saveSettings": "Save Settings"
|
"saveSettings": "Save Settings"
|
||||||
},
|
},
|
||||||
@@ -287,9 +301,16 @@
|
|||||||
"tooltips": {
|
"tooltips": {
|
||||||
"intakeReminders": "Intake reminders enabled",
|
"intakeReminders": "Intake reminders enabled",
|
||||||
"hasNotes": "Has notes",
|
"hasNotes": "Has notes",
|
||||||
|
"stockExceedsCapacity": "Stock exceeds package capacity — consider updating pack count",
|
||||||
"lightMode": "Switch to light mode",
|
"lightMode": "Switch to light mode",
|
||||||
"darkMode": "Switch to dark mode"
|
"darkMode": "Switch to dark mode"
|
||||||
},
|
},
|
||||||
|
"theme": {
|
||||||
|
"title": "Theme",
|
||||||
|
"light": "Light",
|
||||||
|
"dark": "Dark",
|
||||||
|
"system": "System"
|
||||||
|
},
|
||||||
"dose": {
|
"dose": {
|
||||||
"takenBy": "taken by",
|
"takenBy": "taken by",
|
||||||
"markAsTaken": "Mark as taken"
|
"markAsTaken": "Mark as taken"
|
||||||
@@ -341,6 +362,9 @@
|
|||||||
"common": {
|
"common": {
|
||||||
"loading": "Loading...",
|
"loading": "Loading...",
|
||||||
"sending": "Sending...",
|
"sending": "Sending...",
|
||||||
|
"sent": "Sent!",
|
||||||
|
"sendFailed": "Failed to send",
|
||||||
|
"networkError": "Network error",
|
||||||
"saving": "Saving...",
|
"saving": "Saving...",
|
||||||
"unsavedChanges": {
|
"unsavedChanges": {
|
||||||
"title": "Unsaved Changes",
|
"title": "Unsaved Changes",
|
||||||
@@ -379,6 +403,9 @@
|
|||||||
"fullBlisters": "full blisters",
|
"fullBlisters": "full blisters",
|
||||||
"inBlister": "in 1 blister",
|
"inBlister": "in 1 blister",
|
||||||
"total": "total",
|
"total": "total",
|
||||||
|
"pillsTotal": "{{count}} pills total",
|
||||||
|
"pillsTotal_one": "{{count}} pill total",
|
||||||
|
"pillsTotal_other": "{{count}} pills total",
|
||||||
"max": "max"
|
"max": "max"
|
||||||
},
|
},
|
||||||
"share": {
|
"share": {
|
||||||
@@ -443,6 +470,7 @@
|
|||||||
"refill": {
|
"refill": {
|
||||||
"title": "Refill",
|
"title": "Refill",
|
||||||
"packs": "Packs to add",
|
"packs": "Packs to add",
|
||||||
|
"pillsToAdd": "Pills to add",
|
||||||
"loosePills": "Loose pills to add",
|
"loosePills": "Loose pills to add",
|
||||||
"pillsPerPack": "1 pack = {{count}} pills",
|
"pillsPerPack": "1 pack = {{count}} pills",
|
||||||
"addToStock": "Add to Stock",
|
"addToStock": "Add to Stock",
|
||||||
@@ -459,6 +487,7 @@
|
|||||||
"editStock": {
|
"editStock": {
|
||||||
"title": "Correct Stock",
|
"title": "Correct Stock",
|
||||||
"hint": "This is for correcting stock discrepancies. For regular stock changes, use 'Refill'.",
|
"hint": "This is for correcting stock discrepancies. For regular stock changes, use 'Refill'.",
|
||||||
|
"totalPills": "Total pills",
|
||||||
"fullBlisters": "Full blisters",
|
"fullBlisters": "Full blisters",
|
||||||
"partialBlisterPills": "Partial blister",
|
"partialBlisterPills": "Partial blister",
|
||||||
"pillsPerBlister": "({{count}} pills each)",
|
"pillsPerBlister": "({{count}} pills each)",
|
||||||
@@ -478,10 +507,9 @@
|
|||||||
"checking": "Checking...",
|
"checking": "Checking...",
|
||||||
"upToDate": "You're up to date!",
|
"upToDate": "You're up to date!",
|
||||||
"updateAvailable": "Update available",
|
"updateAvailable": "Update available",
|
||||||
"viewOnGitHub": "View on GitHub",
|
|
||||||
"downloadUpdate": "Download Update",
|
"downloadUpdate": "Download Update",
|
||||||
"checkFailed": "Could not check for updates",
|
"checkFailed": "Could not check for updates",
|
||||||
"lastChecked": "Last checked",
|
"viewOnGitHub": "View on GitHub",
|
||||||
"github": "GitHub",
|
"github": "GitHub",
|
||||||
"license": "MIT License",
|
"license": "MIT License",
|
||||||
"copyright": "© {{year}} Daniel Volz",
|
"copyright": "© {{year}} Daniel Volz",
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { ConfirmModal, MedicationAvatar } from "../components";
|
import { ConfirmModal, MedicationAvatar } from "../components";
|
||||||
import { useAuth } from "../components/Auth";
|
import { useAuth } from "../components/Auth";
|
||||||
@@ -34,14 +35,18 @@ function formatOpenBlisterAndLoose(
|
|||||||
return `${openBlisterPills} ${t("common.of")} ${pillsPerBlister} ${t("common.pills")}`;
|
return `${openBlisterPills} ${t("common.of")} ${pillsPerBlister} ${t("common.pills")}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get total pills for a medication
|
// Get total pills for a medication (packageType-aware)
|
||||||
function getMedTotal(med: {
|
function getMedTotal(med: {
|
||||||
packCount: number;
|
packCount: number;
|
||||||
blistersPerPack: number;
|
blistersPerPack: number;
|
||||||
pillsPerBlister: number;
|
pillsPerBlister: number;
|
||||||
looseTablets: number;
|
looseTablets: number;
|
||||||
stockAdjustment?: number | null;
|
stockAdjustment?: number | null;
|
||||||
|
packageType?: string;
|
||||||
}): number {
|
}): number {
|
||||||
|
if (med.packageType === "bottle") {
|
||||||
|
return med.looseTablets + (med.stockAdjustment ?? 0);
|
||||||
|
}
|
||||||
return med.packCount * med.blistersPerPack * med.pillsPerBlister + med.looseTablets + (med.stockAdjustment ?? 0);
|
return med.packCount * med.blistersPerPack * med.pillsPerBlister + med.looseTablets + (med.stockAdjustment ?? 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,12 +81,16 @@ function getReminderStatusData(
|
|||||||
_lastNotificationChannel: string | null,
|
_lastNotificationChannel: string | null,
|
||||||
lastReminderMedName: string | null,
|
lastReminderMedName: string | null,
|
||||||
lastReminderTakenBy: string | null,
|
lastReminderTakenBy: string | null,
|
||||||
|
lastStockReminderSent: string | null,
|
||||||
|
_lastStockReminderChannel: string | null,
|
||||||
|
lastStockReminderMedNames: string | null,
|
||||||
t: (key: string, options?: Record<string, unknown>) => string,
|
t: (key: string, options?: Record<string, unknown>) => string,
|
||||||
locale: string
|
locale: string
|
||||||
): {
|
): {
|
||||||
status: { text: string; className: string };
|
status: { text: string; className: string };
|
||||||
lowStockMeds: { name: string; daysLeft: number; isCritical: boolean }[];
|
lowStockMeds: { name: string; daysLeft: number; isCritical: boolean }[];
|
||||||
lastSent: { date: string; medName: string | null; takenBy: string | null } | null;
|
lastStockSent: { date: string; medNames: string | null } | null;
|
||||||
|
lastIntakeSent: { date: string; medName: string | null; takenBy: string | null } | null;
|
||||||
} {
|
} {
|
||||||
const criticalCount = lowCoverage.length;
|
const criticalCount = lowCoverage.length;
|
||||||
const lowCount = allCoverage.filter((c) => {
|
const lowCount = allCoverage.filter((c) => {
|
||||||
@@ -137,25 +146,40 @@ function getReminderStatusData(
|
|||||||
// Convert to array and sort by days left (most urgent first)
|
// Convert to array and sort by days left (most urgent first)
|
||||||
const lowStockMeds = Array.from(lowStockMap.values()).sort((a, b) => a.daysLeft - b.daysLeft);
|
const lowStockMeds = Array.from(lowStockMap.values()).sort((a, b) => a.daysLeft - b.daysLeft);
|
||||||
|
|
||||||
// Parse last sent info
|
// Parse last stock reminder sent info (from dedicated stock tracking columns)
|
||||||
let lastSent: { date: string; medName: string | null; takenBy: string | null } | null = null;
|
let lastStockSent: { date: string; medNames: string | null } | null = null;
|
||||||
if (lastAutoEmailSent) {
|
if (lastStockReminderSent) {
|
||||||
const lastSentDate = new Date(lastAutoEmailSent);
|
const sentDate = new Date(lastStockReminderSent);
|
||||||
const formattedDate = lastSentDate.toLocaleDateString(locale, {
|
const formattedDate = sentDate.toLocaleDateString(locale, {
|
||||||
day: "2-digit",
|
day: "2-digit",
|
||||||
month: "short",
|
month: "short",
|
||||||
hour: "2-digit",
|
hour: "2-digit",
|
||||||
minute: "2-digit",
|
minute: "2-digit",
|
||||||
});
|
});
|
||||||
|
lastStockSent = {
|
||||||
|
date: formattedDate,
|
||||||
|
medNames: lastStockReminderMedNames,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
lastSent = {
|
// Parse last intake reminder sent info (from intake tracking columns)
|
||||||
|
let lastIntakeSent: { date: string; medName: string | null; takenBy: string | null } | null = null;
|
||||||
|
if (lastAutoEmailSent) {
|
||||||
|
const sentDate = new Date(lastAutoEmailSent);
|
||||||
|
const formattedDate = sentDate.toLocaleDateString(locale, {
|
||||||
|
day: "2-digit",
|
||||||
|
month: "short",
|
||||||
|
hour: "2-digit",
|
||||||
|
minute: "2-digit",
|
||||||
|
});
|
||||||
|
lastIntakeSent = {
|
||||||
date: formattedDate,
|
date: formattedDate,
|
||||||
medName: lastReminderMedName,
|
medName: lastReminderMedName,
|
||||||
takenBy: lastReminderTakenBy,
|
takenBy: lastReminderTakenBy,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return { status, lowStockMeds, lastSent };
|
return { status, lowStockMeds, lastStockSent, lastIntakeSent };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function DashboardPage() {
|
export function DashboardPage() {
|
||||||
@@ -195,6 +219,7 @@ export function DashboardPage() {
|
|||||||
openShareDialog,
|
openShareDialog,
|
||||||
openScheduleLightbox,
|
openScheduleLightbox,
|
||||||
stockThresholds,
|
stockThresholds,
|
||||||
|
loadSettings,
|
||||||
} = useAppContext();
|
} = useAppContext();
|
||||||
|
|
||||||
// Get structured reminder data
|
// Get structured reminder data
|
||||||
@@ -208,6 +233,9 @@ export function DashboardPage() {
|
|||||||
settings.lastNotificationChannel,
|
settings.lastNotificationChannel,
|
||||||
settings.lastReminderMedName,
|
settings.lastReminderMedName,
|
||||||
settings.lastReminderTakenBy,
|
settings.lastReminderTakenBy,
|
||||||
|
settings.lastStockReminderSent,
|
||||||
|
settings.lastStockReminderChannel,
|
||||||
|
settings.lastStockReminderMedNames,
|
||||||
t,
|
t,
|
||||||
getSystemLocale(i18n.language)
|
getSystemLocale(i18n.language)
|
||||||
);
|
);
|
||||||
@@ -221,6 +249,50 @@ export function DashboardPage() {
|
|||||||
(settings.shoutrrrEnabled && settings.shoutrrrIntakeReminders);
|
(settings.shoutrrrEnabled && settings.shoutrrrIntakeReminders);
|
||||||
const anyRemindersEnabled = stockRemindersEnabled || intakeRemindersEnabled;
|
const anyRemindersEnabled = stockRemindersEnabled || intakeRemindersEnabled;
|
||||||
|
|
||||||
|
// Manual reminder send state
|
||||||
|
const [sendingReminder, setSendingReminder] = useState(false);
|
||||||
|
const [reminderResult, setReminderResult] = useState<{ success: boolean; message: string } | null>(null);
|
||||||
|
|
||||||
|
async function sendManualReminder() {
|
||||||
|
if (!stockRemindersEnabled || reminderData.lowStockMeds.length === 0) return;
|
||||||
|
setSendingReminder(true);
|
||||||
|
setReminderResult(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const lowStock = reminderData.lowStockMeds.map((m) => {
|
||||||
|
const cov = coverage.all.find((c) => c.name === m.name);
|
||||||
|
return {
|
||||||
|
name: m.name,
|
||||||
|
medsLeft: cov?.medsLeft ?? 0,
|
||||||
|
daysLeft: m.daysLeft,
|
||||||
|
depletionDate: cov?.depletionDate ?? null,
|
||||||
|
isCritical: m.isCritical,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await fetch("/api/reminder/send-email", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
credentials: "include",
|
||||||
|
body: JSON.stringify({
|
||||||
|
email: settings.notificationEmail,
|
||||||
|
lowStock,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
if (res.ok) {
|
||||||
|
setReminderResult({ success: true, message: data.message || t("common.sent") });
|
||||||
|
// Refresh settings so "Last stock reminder" row appears immediately
|
||||||
|
loadSettings();
|
||||||
|
} else {
|
||||||
|
setReminderResult({ success: false, message: data.error || t("common.sendFailed") });
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
setReminderResult({ success: false, message: t("common.networkError") });
|
||||||
|
}
|
||||||
|
setSendingReminder(false);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{anyRemindersEnabled && (
|
{anyRemindersEnabled && (
|
||||||
@@ -230,14 +302,11 @@ export function DashboardPage() {
|
|||||||
<NotificationBellIcon />
|
<NotificationBellIcon />
|
||||||
</span>
|
</span>
|
||||||
<span className="reminder-status-title">{t("dashboard.reminders.active")}</span>
|
<span className="reminder-status-title">{t("dashboard.reminders.active")}</span>
|
||||||
{reminderData.lowStockMeds.length === 0 && (
|
<span className={`status-chip small ${reminderData.status.className}`}>{reminderData.status.text}</span>
|
||||||
<span className={`reminder-status-badge ${reminderData.status.className}`}>
|
|
||||||
{reminderData.status.className === "success" && "✓ "}
|
|
||||||
{reminderData.status.text}
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
{(reminderData.lowStockMeds.length > 0 || (intakeRemindersEnabled && reminderData.lastSent)) && (
|
{(reminderData.lowStockMeds.length > 0 ||
|
||||||
|
(stockRemindersEnabled && reminderData.lastStockSent) ||
|
||||||
|
(intakeRemindersEnabled && reminderData.lastIntakeSent)) && (
|
||||||
<div className="reminder-status-details">
|
<div className="reminder-status-details">
|
||||||
{stockRemindersEnabled && reminderData.lowStockMeds.length > 0 && (
|
{stockRemindersEnabled && reminderData.lowStockMeds.length > 0 && (
|
||||||
<div className="reminder-status-row">
|
<div className="reminder-status-row">
|
||||||
@@ -272,22 +341,68 @@ export function DashboardPage() {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{intakeRemindersEnabled && reminderData.lastSent && (
|
{stockRemindersEnabled && reminderData.lastStockSent && (
|
||||||
|
<div className="reminder-status-row">
|
||||||
|
<span className="reminder-status-label">{t("dashboard.reminders.lastStockSent")}:</span>
|
||||||
|
<span className="reminder-status-value">
|
||||||
|
{reminderData.lastStockSent.medNames &&
|
||||||
|
(() => {
|
||||||
|
// Extract first med name (medNames may be "Name (+N)")
|
||||||
|
const rawName = reminderData.lastStockSent!.medNames!;
|
||||||
|
const firstName = rawName.replace(/\s*\(\+\d+\)$/, "");
|
||||||
|
const suffix = rawName.includes("(+") ? rawName.slice(firstName.length) : "";
|
||||||
|
const medication = meds.find((m) => m.name === firstName);
|
||||||
|
return medication ? (
|
||||||
|
<>
|
||||||
|
<span className="med-link clickable" onClick={() => openMedDetail(medication)}>
|
||||||
|
{firstName}
|
||||||
|
</span>
|
||||||
|
{suffix && <span className="reminder-med-name">{suffix}</span>}
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<span className="reminder-med-name">{rawName}</span>
|
||||||
|
);
|
||||||
|
})()}
|
||||||
|
<span className="reminder-date"> {reminderData.lastStockSent.date}</span>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{intakeRemindersEnabled && reminderData.lastIntakeSent && (
|
||||||
<div className="reminder-status-row">
|
<div className="reminder-status-row">
|
||||||
<span className="reminder-status-label">{t("dashboard.reminders.lastSent")}:</span>
|
<span className="reminder-status-label">{t("dashboard.reminders.lastSent")}:</span>
|
||||||
<span className="reminder-status-value">
|
<span className="reminder-status-value">
|
||||||
{reminderData.lastSent.medName && (
|
{reminderData.lastIntakeSent.medName &&
|
||||||
<span className="reminder-med-name">{reminderData.lastSent.medName}</span>
|
(() => {
|
||||||
|
const medication = meds.find((m) => m.name === reminderData.lastIntakeSent!.medName);
|
||||||
|
return medication ? (
|
||||||
|
<span className="med-link clickable" onClick={() => openMedDetail(medication)}>
|
||||||
|
{reminderData.lastIntakeSent!.medName}
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
<span className="reminder-med-name">{reminderData.lastIntakeSent!.medName}</span>
|
||||||
|
);
|
||||||
|
})()}
|
||||||
|
{reminderData.lastIntakeSent.takenBy && (
|
||||||
|
<span className="reminder-taken-by"> ({reminderData.lastIntakeSent.takenBy})</span>
|
||||||
)}
|
)}
|
||||||
{reminderData.lastSent.takenBy && (
|
<span className="reminder-date"> {reminderData.lastIntakeSent.date}</span>
|
||||||
<span className="reminder-taken-by"> ({reminderData.lastSent.takenBy})</span>
|
|
||||||
)}
|
|
||||||
<span className="reminder-date"> {reminderData.lastSent.date}</span>
|
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{stockRemindersEnabled && reminderData.lowStockMeds.length > 0 && (
|
||||||
|
<div className="reminder-send-row">
|
||||||
|
<button type="button" className="ghost" onClick={sendManualReminder} disabled={sendingReminder}>
|
||||||
|
{sendingReminder ? t("common.sending") : t("dashboard.reorder.sendReminder")}
|
||||||
|
</button>
|
||||||
|
{reminderResult && (
|
||||||
|
<span className={`reminder-send-result ${reminderResult.success ? "success" : "error"}`}>
|
||||||
|
{reminderResult.message}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</section>
|
</section>
|
||||||
)}
|
)}
|
||||||
{/* Reorder Reminder card: Only show when reminders are NOT enabled (otherwise Reminder Bar shows the same info) */}
|
{/* Reorder Reminder card: Only show when reminders are NOT enabled (otherwise Reminder Bar shows the same info) */}
|
||||||
@@ -427,9 +542,12 @@ export function DashboardPage() {
|
|||||||
? t("table.pillsCount", { count: Math.round(row.medsLeft) })
|
? t("table.pillsCount", { count: Math.round(row.medsLeft) })
|
||||||
: formatFullBlisters(stock.fullBlisters, t)}
|
: formatFullBlisters(stock.fullBlisters, t)}
|
||||||
</span>
|
</span>
|
||||||
<span data-label={t("table.stockDetails")} className={textClass}>
|
<span
|
||||||
|
data-label={t("table.stockDetails")}
|
||||||
|
className={`${textClass}${med?.packageType === "bottle" ? " hide-on-card" : ""}`}
|
||||||
|
>
|
||||||
{med?.packageType === "bottle"
|
{med?.packageType === "bottle"
|
||||||
? "-"
|
? "—"
|
||||||
: formatOpenBlisterAndLoose(
|
: formatOpenBlisterAndLoose(
|
||||||
stock.openBlisterPills,
|
stock.openBlisterPills,
|
||||||
stock.loosePills,
|
stock.loosePills,
|
||||||
@@ -553,7 +671,7 @@ export function DashboardPage() {
|
|||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={day.dateStr}
|
key={day.dateStr}
|
||||||
className={`day-block past ${isCollapsed ? "collapsed" : ""} ${allDayTaken ? "all-taken" : ""} stock-${worstStatus}`}
|
className={`day-block past ${isCollapsed ? "collapsed" : ""} ${allDayTaken ? "all-taken" : allDoseIds.length > 0 ? "past-missed" : ""}`}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
className="day-divider clickable"
|
className="day-divider clickable"
|
||||||
@@ -585,6 +703,9 @@ export function DashboardPage() {
|
|||||||
const med = meds.find((m) => m.name === item.medName);
|
const med = meds.find((m) => m.name === item.medName);
|
||||||
const medCov = coverageByMed[item.medName];
|
const medCov = coverageByMed[item.medName];
|
||||||
const isEmpty = medCov ? medCov.medsLeft <= 0 : false;
|
const isEmpty = medCov ? medCov.medsLeft <= 0 : false;
|
||||||
|
const status = medCov
|
||||||
|
? getStockStatus(medCov.daysLeft, medCov.medsLeft, stockThresholds)
|
||||||
|
: null;
|
||||||
const itemDoseIds = expandDoseIds(item.doses);
|
const itemDoseIds = expandDoseIds(item.doses);
|
||||||
const allTaken = itemDoseIds.every((id) => takenDoses.has(id));
|
const allTaken = itemDoseIds.every((id) => takenDoses.has(id));
|
||||||
return (
|
return (
|
||||||
@@ -608,9 +729,10 @@ export function DashboardPage() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div className="tag-row">
|
<div className="tag-row">
|
||||||
<span className="tag subtle">
|
<span className="tag subtle">{t("common.pillsTotal", { count: item.total })}</span>
|
||||||
{item.total} {t("common.pills")} {t("common.total")}
|
{status && (
|
||||||
</span>
|
<span className={`status-chip small ${status.className}`}>{t(status.label)}</span>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="doses-col">
|
<div className="doses-col">
|
||||||
@@ -757,10 +879,10 @@ export function DashboardPage() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div className="tag-row">
|
<div className="tag-row">
|
||||||
<span className="tag subtle">
|
<span className="tag subtle">{t("common.pillsTotal", { count: item.total })}</span>
|
||||||
{item.total} {t("common.pills")} {t("common.total")}
|
{status && (
|
||||||
</span>
|
<span className={`status-chip small ${status.className}`}>{t(status.label)}</span>
|
||||||
{status && <span className={`tag ${status.className}`}>{t(status.label)}</span>}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="doses-col">
|
<div className="doses-col">
|
||||||
@@ -944,10 +1066,10 @@ export function DashboardPage() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div className="tag-row">
|
<div className="tag-row">
|
||||||
<span className="tag subtle">
|
<span className="tag subtle">{t("common.pillsTotal", { count: item.total })}</span>
|
||||||
{item.total} {t("common.pills")} {t("common.total")}
|
{status && (
|
||||||
</span>
|
<span className={`status-chip small ${status.className}`}>{t(status.label)}</span>
|
||||||
{status && <span className={`tag ${status.className}`}>{t(status.label)}</span>}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="doses-col">
|
<div className="doses-col">
|
||||||
|
|||||||
@@ -340,22 +340,46 @@ export function MedicationsPage() {
|
|||||||
</div>
|
</div>
|
||||||
<div className="med-details">
|
<div className="med-details">
|
||||||
<span>
|
<span>
|
||||||
{t("medications.details.packs")}: <strong>{med.packCount}</strong>
|
{t("medications.details.type")}:{" "}
|
||||||
</span>
|
<strong>
|
||||||
<span>
|
{med.packageType === "bottle" ? t("form.packageTypeBottle") : t("form.packageTypeBlister")}
|
||||||
{t("medications.details.blisters")}: <strong>{med.blistersPerPack}</strong>
|
</strong>
|
||||||
</span>
|
|
||||||
<span>
|
|
||||||
{t("medications.details.pillsPerBlister")}: <strong>{med.pillsPerBlister}</strong>
|
|
||||||
</span>
|
|
||||||
<span>
|
|
||||||
{t("medications.details.loose")}: <strong>{med.looseTablets}</strong>
|
|
||||||
</span>
|
</span>
|
||||||
|
{med.packageType === "blister" ? (
|
||||||
|
<>
|
||||||
|
<span>
|
||||||
|
{t("medications.details.packs")}: <strong>{med.packCount}</strong>
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
{t("medications.details.blisters")}: <strong>{med.blistersPerPack}</strong>
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
{t("medications.details.pillsPerBlister")}: <strong>{med.pillsPerBlister}</strong>
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
{t("medications.details.loose")}: <strong>{med.looseTablets}</strong>
|
||||||
|
</span>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<span>
|
||||||
|
{t("medications.details.totalCapacity")}: <strong>{med.totalPills ?? med.looseTablets}</strong>
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div className="med-total">
|
<div className="med-total">
|
||||||
{t("medications.details.stock")}:{" "}
|
{t("medications.details.stock")}:{" "}
|
||||||
{coverageByMed[med.name] ? Math.round(coverageByMed[med.name].medsLeft) : getPackageSize(med)} /{" "}
|
{coverageByMed[med.name] ? Math.round(coverageByMed[med.name].medsLeft) : getPackageSize(med)} /{" "}
|
||||||
{getPackageSize(med)} {t("common.pills")}
|
{getPackageSize(med)} {getPackageSize(med) === 1 ? t("common.pill") : t("common.pills")}
|
||||||
|
{(coverageByMed[med.name] ? Math.round(coverageByMed[med.name].medsLeft) : getPackageSize(med)) >
|
||||||
|
getPackageSize(med) && (
|
||||||
|
<span
|
||||||
|
className="info-tooltip tooltip-align-left warning-text"
|
||||||
|
data-tooltip={t("tooltips.stockExceedsCapacity")}
|
||||||
|
>
|
||||||
|
{" "}
|
||||||
|
⚠️
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="med-actions">
|
<div className="med-actions">
|
||||||
@@ -526,7 +550,7 @@ export function MedicationsPage() {
|
|||||||
</label>
|
</label>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<label>
|
<label className="full">
|
||||||
{t("form.pillWeight")} ({form.doseUnit})
|
{t("form.pillWeight")} ({form.doseUnit})
|
||||||
<div className="dose-input-group">
|
<div className="dose-input-group">
|
||||||
<input
|
<input
|
||||||
@@ -569,24 +593,38 @@ export function MedicationsPage() {
|
|||||||
<div className="full refill-section">
|
<div className="full refill-section">
|
||||||
<h4 className="refill-title">{t("refill.title")}</h4>
|
<h4 className="refill-title">{t("refill.title")}</h4>
|
||||||
<div className="refill-form-inline">
|
<div className="refill-form-inline">
|
||||||
<label>
|
{form.packageType === "blister" ? (
|
||||||
{t("refill.packs")}
|
<>
|
||||||
<input
|
<label>
|
||||||
type="number"
|
{t("refill.packs")}
|
||||||
min="0"
|
<input
|
||||||
value={refillPacks}
|
type="number"
|
||||||
onChange={(e) => setRefillPacks(parseInt(e.target.value, 10) || 0)}
|
min="0"
|
||||||
/>
|
value={refillPacks}
|
||||||
</label>
|
onChange={(e) => setRefillPacks(parseInt(e.target.value, 10) || 0)}
|
||||||
<label>
|
/>
|
||||||
{t("refill.loosePills")}
|
</label>
|
||||||
<input
|
<label>
|
||||||
type="number"
|
{t("refill.loosePills")}
|
||||||
min="0"
|
<input
|
||||||
value={refillLoose}
|
type="number"
|
||||||
onChange={(e) => setRefillLoose(parseInt(e.target.value, 10) || 0)}
|
min="0"
|
||||||
/>
|
value={refillLoose}
|
||||||
</label>
|
onChange={(e) => setRefillLoose(parseInt(e.target.value, 10) || 0)}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<label>
|
||||||
|
{t("refill.pillsToAdd")}
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
min="0"
|
||||||
|
value={refillLoose}
|
||||||
|
onChange={(e) => setRefillLoose(parseInt(e.target.value, 10) || 0)}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="success"
|
className="success"
|
||||||
@@ -595,12 +633,18 @@ export function MedicationsPage() {
|
|||||||
>
|
>
|
||||||
{refillSaving ? t("refill.adding") : t("refill.button")}
|
{refillSaving ? t("refill.adding") : t("refill.button")}
|
||||||
</button>
|
</button>
|
||||||
{(refillPacks > 0 || refillLoose > 0) && (
|
{(() => {
|
||||||
<span className="refill-preview">
|
const totalRefill =
|
||||||
+{refillPacks * Number(form.blistersPerPack || 0) * Number(form.pillsPerBlister || 1) + refillLoose}{" "}
|
form.packageType === "blister"
|
||||||
{t("common.pills")}
|
? refillPacks * Number(form.blistersPerPack || 0) * Number(form.pillsPerBlister || 1) +
|
||||||
</span>
|
refillLoose
|
||||||
)}
|
: refillLoose;
|
||||||
|
return totalRefill > 0 ? (
|
||||||
|
<span className="refill-preview">
|
||||||
|
+{totalRefill} {totalRefill === 1 ? t("common.pill") : t("common.pills")}
|
||||||
|
</span>
|
||||||
|
) : null;
|
||||||
|
})()}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -632,7 +676,11 @@ export function MedicationsPage() {
|
|||||||
<div className="card-head">
|
<div className="card-head">
|
||||||
<h3>{t("form.blisters.title")}</h3>
|
<h3>{t("form.blisters.title")}</h3>
|
||||||
<div className="blisters-actions">
|
<div className="blisters-actions">
|
||||||
<button type="button" className="primary" onClick={() => addIntake()}>
|
<button
|
||||||
|
type="button"
|
||||||
|
className="primary"
|
||||||
|
onClick={() => addIntake(form.takenBy.length === 1 ? form.takenBy[0] : undefined)}
|
||||||
|
>
|
||||||
+ {t("form.blisters.addIntake")}
|
+ {t("form.blisters.addIntake")}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -675,25 +723,29 @@ export function MedicationsPage() {
|
|||||||
onChange={(e) => setIntakeValue(idx, "startTime", e.target.value)}
|
onChange={(e) => setIntakeValue(idx, "startTime", e.target.value)}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
<label title={t("form.blisters.takenByTooltip")}>
|
{form.takenBy.length === 0 ? null : (
|
||||||
{t("form.blisters.takenByIntake")}
|
<label title={t("form.blisters.takenByTooltip")}>
|
||||||
<select value={intake.takenBy} onChange={(e) => setIntakeValue(idx, "takenBy", e.target.value)}>
|
{t("form.blisters.takenByIntake")}
|
||||||
<option value="">{t("form.blisters.takenByEveryone")}</option>
|
<select value={intake.takenBy} onChange={(e) => setIntakeValue(idx, "takenBy", e.target.value)}>
|
||||||
{existingPeople.map((person) => (
|
{form.takenBy.map((person) => (
|
||||||
<option key={person} value={person}>
|
<option key={person} value={person}>
|
||||||
{person}
|
{person}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
<label className="inline-checkbox" title={t("form.blisters.remindTooltip")}>
|
)}
|
||||||
<input
|
<div className="remind-toggle-row" title={t("form.blisters.remindTooltip")}>
|
||||||
type="checkbox"
|
|
||||||
checked={intake.intakeRemindersEnabled}
|
|
||||||
onChange={(e) => setIntakeValue(idx, "intakeRemindersEnabled", e.target.checked)}
|
|
||||||
/>
|
|
||||||
<span>🔔</span>
|
<span>🔔</span>
|
||||||
</label>
|
<label className="toggle-switch small">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={intake.intakeRemindersEnabled}
|
||||||
|
onChange={(e) => setIntakeValue(idx, "intakeRemindersEnabled", e.target.checked)}
|
||||||
|
/>
|
||||||
|
<span className="toggle-slider"></span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{form.intakes.length > 1 && (
|
{form.intakes.length > 1 && (
|
||||||
<button type="button" className="danger" onClick={() => removeIntake(idx)}>
|
<button type="button" className="danger" onClick={() => removeIntake(idx)}>
|
||||||
|
|||||||
@@ -117,8 +117,11 @@ export function PlannerPage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sendPlannerEmail() {
|
const canSendNotification =
|
||||||
if (!settings.notificationEmail || plannerRows.length === 0) return;
|
(settings.emailEnabled && settings.notificationEmail) || (settings.shoutrrrEnabled && settings.shoutrrrUrl);
|
||||||
|
|
||||||
|
async function sendPlannerNotification() {
|
||||||
|
if (!canSendNotification || plannerRows.length === 0) return;
|
||||||
setSendingPlannerEmail(true);
|
setSendingPlannerEmail(true);
|
||||||
setPlannerEmailResult(null);
|
setPlannerEmailResult(null);
|
||||||
|
|
||||||
@@ -136,12 +139,12 @@ export function PlannerPage() {
|
|||||||
});
|
});
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
setPlannerEmailResult({ success: true, message: data.message || "Email sent!" });
|
setPlannerEmailResult({ success: true, message: data.message || t("common.sent") });
|
||||||
} else {
|
} else {
|
||||||
setPlannerEmailResult({ success: false, message: data.error || "Failed to send" });
|
setPlannerEmailResult({ success: false, message: data.error || t("common.sendFailed") });
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
setPlannerEmailResult({ success: false, message: "Network error" });
|
setPlannerEmailResult({ success: false, message: t("common.networkError") });
|
||||||
}
|
}
|
||||||
setSendingPlannerEmail(false);
|
setSendingPlannerEmail(false);
|
||||||
}
|
}
|
||||||
@@ -178,6 +181,9 @@ export function PlannerPage() {
|
|||||||
onChange={(e) => setIncludeUntilStart(e.target.checked)}
|
onChange={(e) => setIncludeUntilStart(e.target.checked)}
|
||||||
/>
|
/>
|
||||||
{t("planner.includeUntilStart")}
|
{t("planner.includeUntilStart")}
|
||||||
|
<span className="info-tooltip small" data-tooltip={t("planner.includeUntilStartTooltip")}>
|
||||||
|
ⓘ
|
||||||
|
</span>
|
||||||
</label>
|
</label>
|
||||||
<div className="planner-actions">
|
<div className="planner-actions">
|
||||||
<button type="button" className="ghost" onClick={resetRange}>
|
<button type="button" className="ghost" onClick={resetRange}>
|
||||||
@@ -207,14 +213,22 @@ export function PlannerPage() {
|
|||||||
{row.medicationName}
|
{row.medicationName}
|
||||||
</span>
|
</span>
|
||||||
<span data-label={t("planner.table.usage")}>
|
<span data-label={t("planner.table.usage")}>
|
||||||
<strong>{row.plannerUsage}</strong> {t("common.pills")}
|
<strong>{row.plannerUsage}</strong>
|
||||||
|
{row.plannerUsage === 1 ? t("common.pill") : t("common.pills")}
|
||||||
</span>
|
</span>
|
||||||
<span data-label={t("planner.table.blisters")}>
|
<span data-label={t("planner.table.blisters")}>
|
||||||
{row.blistersNeeded} × {row.blisterSize}
|
{row.packageType === "bottle" ? "–" : `${row.blistersNeeded} × ${row.blisterSize}`}
|
||||||
</span>
|
</span>
|
||||||
<span data-label={t("planner.table.available")}>
|
<span data-label={t("planner.table.available")}>
|
||||||
{row.fullBlisters} {t("common.blisters")}
|
{row.packageType === "bottle" ? (
|
||||||
{row.loosePills > 0 && ` + ${Math.round(row.loosePills * 10) / 10} ${t("common.pills")}`}
|
`${Math.round(row.loosePills * 10) / 10} ${Math.round(row.loosePills * 10) / 10 === 1 ? t("common.pill") : t("common.pills")}`
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
{row.fullBlisters} {t("common.blisters")}
|
||||||
|
{row.loosePills > 0 &&
|
||||||
|
` + ${Math.round(row.loosePills * 10) / 10} ${Math.round(row.loosePills * 10) / 10 === 1 ? t("common.pill") : t("common.pills")}`}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</span>
|
</span>
|
||||||
<span
|
<span
|
||||||
data-label={t("table.status")}
|
data-label={t("table.status")}
|
||||||
@@ -226,10 +240,15 @@ export function PlannerPage() {
|
|||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
{settings.emailEnabled && settings.notificationEmail && (
|
{canSendNotification && (
|
||||||
<div className="planner-email-action">
|
<div className="planner-email-action">
|
||||||
<button type="button" className="ghost" onClick={sendPlannerEmail} disabled={sendingPlannerEmail}>
|
<button
|
||||||
{sendingPlannerEmail ? t("common.sending") : t("planner.sendEmail")}
|
type="button"
|
||||||
|
className="ghost"
|
||||||
|
onClick={sendPlannerNotification}
|
||||||
|
disabled={sendingPlannerEmail}
|
||||||
|
>
|
||||||
|
{sendingPlannerEmail ? t("common.sending") : t("planner.sendNotification")}
|
||||||
</button>
|
</button>
|
||||||
{plannerEmailResult && (
|
{plannerEmailResult && (
|
||||||
<span className={plannerEmailResult.success ? "success-text" : "danger-text"}>
|
<span className={plannerEmailResult.success ? "success-text" : "danger-text"}>
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ export function SchedulePage() {
|
|||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={day.dateStr}
|
key={day.dateStr}
|
||||||
className={`day-block past ${isCollapsed ? "collapsed" : ""} ${allDayTaken ? "all-taken" : ""} stock-${worstStatus}`}
|
className={`day-block past ${isCollapsed ? "collapsed" : ""} ${allDayTaken ? "all-taken" : allDoseIds.length > 0 ? "past-missed" : ""}`}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
className="day-divider clickable"
|
className="day-divider clickable"
|
||||||
@@ -186,9 +186,7 @@ export function SchedulePage() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div className="tag-row">
|
<div className="tag-row">
|
||||||
<span className="tag subtle">
|
<span className="tag subtle">{t("common.pillsTotal", { count: item.total })}</span>
|
||||||
{item.total} {t("common.pills")} {t("common.total")}
|
|
||||||
</span>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="doses-col">
|
<div className="doses-col">
|
||||||
@@ -285,9 +283,7 @@ export function SchedulePage() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div className="tag-row">
|
<div className="tag-row">
|
||||||
<span className="tag subtle">
|
<span className="tag subtle">{t("common.pillsTotal", { count: item.total })}</span>
|
||||||
{item.total} {t("common.pills")} {t("common.total")}
|
|
||||||
</span>
|
|
||||||
{status && <span className={`tag ${status.className}`}>{t(status.label)}</span>}
|
{status && <span className={`tag ${status.className}`}>{t(status.label)}</span>}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -236,6 +236,75 @@ export function SettingsPage() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div className="setting-section">
|
||||||
|
<div className="section-header">
|
||||||
|
<h3>{t("settings.stockReminder.title")}</h3>
|
||||||
|
</div>
|
||||||
|
<div className="setting-row compact">
|
||||||
|
<label className="setting-label">
|
||||||
|
{t("settings.stockReminder.description")}{" "}
|
||||||
|
<span className="status-chip small danger">{t("status.criticalStock")}</span>
|
||||||
|
</label>
|
||||||
|
<label
|
||||||
|
className={`toggle-switch small${!settings.emailEnabled && !settings.shoutrrrEnabled ? " disabled" : ""}`}
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={
|
||||||
|
(settings.emailEnabled && settings.emailStockReminders) ||
|
||||||
|
(settings.shoutrrrEnabled && settings.shoutrrrStockReminders)
|
||||||
|
}
|
||||||
|
onChange={(e) => {
|
||||||
|
const newVal = e.target.checked;
|
||||||
|
if (newVal) {
|
||||||
|
setSettings({
|
||||||
|
...settings,
|
||||||
|
emailStockReminders: settings.emailEnabled ? true : settings.emailStockReminders,
|
||||||
|
shoutrrrStockReminders: settings.shoutrrrEnabled ? true : settings.shoutrrrStockReminders,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setSettings({
|
||||||
|
...settings,
|
||||||
|
emailStockReminders: false,
|
||||||
|
shoutrrrStockReminders: false,
|
||||||
|
repeatDailyReminders: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
disabled={!settings.emailEnabled && !settings.shoutrrrEnabled}
|
||||||
|
/>
|
||||||
|
<span className="toggle-slider"></span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<div className="setting-row compact" style={{ marginTop: "4px" }}>
|
||||||
|
<label className="setting-label">
|
||||||
|
{t("settings.stockReminder.repeatDaily")}
|
||||||
|
<span
|
||||||
|
className="info-tooltip small tooltip-align-left"
|
||||||
|
data-tooltip={t("settings.stockReminder.repeatTooltip")}
|
||||||
|
>
|
||||||
|
ⓘ
|
||||||
|
</span>
|
||||||
|
</label>
|
||||||
|
<label
|
||||||
|
className={`toggle-switch small${!((settings.emailEnabled && settings.emailStockReminders) || (settings.shoutrrrEnabled && settings.shoutrrrStockReminders)) ? " disabled" : ""}`}
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={settings.repeatDailyReminders}
|
||||||
|
onChange={(e) => setSettings({ ...settings, repeatDailyReminders: e.target.checked })}
|
||||||
|
disabled={
|
||||||
|
!(
|
||||||
|
(settings.emailEnabled && settings.emailStockReminders) ||
|
||||||
|
(settings.shoutrrrEnabled && settings.shoutrrrStockReminders)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<span className="toggle-slider"></span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="setting-section">
|
<div className="setting-section">
|
||||||
<div className="section-header">
|
<div className="section-header">
|
||||||
<h3>{t("settings.notifications.email")}</h3>
|
<h3>{t("settings.notifications.email")}</h3>
|
||||||
@@ -400,9 +469,23 @@ export function SettingsPage() {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{settings.lastStockReminderSent && (
|
||||||
|
<div className="schedule-row">
|
||||||
|
<span className="schedule-label">{t("settings.schedule.lastStockSent")}</span>
|
||||||
|
<span className="schedule-value">
|
||||||
|
{new Date(settings.lastStockReminderSent).toLocaleString(getSystemLocale(i18n.language), {
|
||||||
|
day: "2-digit",
|
||||||
|
month: "2-digit",
|
||||||
|
year: "numeric",
|
||||||
|
hour: "2-digit",
|
||||||
|
minute: "2-digit",
|
||||||
|
})}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
{settings.lastAutoEmailSent && (
|
{settings.lastAutoEmailSent && (
|
||||||
<div className="schedule-row">
|
<div className="schedule-row">
|
||||||
<span className="schedule-label">{t("settings.schedule.lastSent")}</span>
|
<span className="schedule-label">{t("settings.schedule.lastIntakeSent")}</span>
|
||||||
<span className="schedule-value">
|
<span className="schedule-value">
|
||||||
{new Date(settings.lastAutoEmailSent).toLocaleString(getSystemLocale(i18n.language), {
|
{new Date(settings.lastAutoEmailSent).toLocaleString(getSystemLocale(i18n.language), {
|
||||||
day: "2-digit",
|
day: "2-digit",
|
||||||
@@ -423,51 +506,6 @@ export function SettingsPage() {
|
|||||||
<h2>{t("settings.stock.title")}</h2>
|
<h2>{t("settings.stock.title")}</h2>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="setting-section">
|
|
||||||
<div className="section-header">
|
|
||||||
<h3>{t("settings.stock.threshold")}</h3>
|
|
||||||
</div>
|
|
||||||
<div className="threshold-input">
|
|
||||||
<label>
|
|
||||||
<span className="threshold-label">{t("settings.stock.remindWhen")}</span>
|
|
||||||
<div className="threshold-field">
|
|
||||||
<input
|
|
||||||
type="number"
|
|
||||||
min="1"
|
|
||||||
max="90"
|
|
||||||
value={settings.reminderDaysBefore}
|
|
||||||
onChange={(e) => setSettings({ ...settings, reminderDaysBefore: Number(e.target.value) || 7 })}
|
|
||||||
/>
|
|
||||||
<span className="threshold-unit">{t("common.days")}</span>
|
|
||||||
</div>
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
<div className="setting-row compact">
|
|
||||||
<label className="setting-label">
|
|
||||||
{t("settings.stock.repeatDaily")}
|
|
||||||
<span className="info-tooltip small" data-tooltip={t("settings.stock.repeatTooltip")}>
|
|
||||||
ⓘ
|
|
||||||
</span>
|
|
||||||
</label>
|
|
||||||
<label
|
|
||||||
className={`toggle-switch small${!((settings.emailEnabled && settings.emailStockReminders && settings.notificationEmail) || (settings.shoutrrrEnabled && settings.shoutrrrStockReminders && settings.shoutrrrUrl)) ? " disabled" : ""}`}
|
|
||||||
>
|
|
||||||
<input
|
|
||||||
type="checkbox"
|
|
||||||
checked={settings.repeatDailyReminders}
|
|
||||||
onChange={(e) => setSettings({ ...settings, repeatDailyReminders: e.target.checked })}
|
|
||||||
disabled={
|
|
||||||
!(
|
|
||||||
(settings.emailEnabled && settings.emailStockReminders && settings.notificationEmail) ||
|
|
||||||
(settings.shoutrrrEnabled && settings.shoutrrrStockReminders && settings.shoutrrrUrl)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
<span className="toggle-slider"></span>
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="setting-section">
|
<div className="setting-section">
|
||||||
<div className="section-header">
|
<div className="section-header">
|
||||||
<h3>{t("settings.stock.calculationMode")}</h3>
|
<h3>{t("settings.stock.calculationMode")}</h3>
|
||||||
@@ -512,40 +550,100 @@ export function SettingsPage() {
|
|||||||
|
|
||||||
<div className="setting-section">
|
<div className="setting-section">
|
||||||
<div className="section-header">
|
<div className="section-header">
|
||||||
<h3>{t("settings.stock.display")}</h3>
|
<h3>{t("settings.stock.thresholds")}</h3>
|
||||||
</div>
|
</div>
|
||||||
<div className="setting-group">
|
<div className="setting-group threshold-chips-group">
|
||||||
<label>
|
<label className={settings.reminderDaysBefore >= settings.lowStockDays ? "threshold-invalid" : ""}>
|
||||||
<span className="field-label">{t("settings.stock.lowStockDays")}</span>
|
<span className="field-label threshold-chip-label">
|
||||||
|
<span className="status-chip small danger">{t("status.criticalStock")}</span>
|
||||||
|
<span
|
||||||
|
className="info-tooltip small tooltip-align-left"
|
||||||
|
data-tooltip={t("settings.stock.criticalStockTooltip")}
|
||||||
|
>
|
||||||
|
ⓘ
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
<div className="input-with-tooltip">
|
<div className="input-with-tooltip">
|
||||||
<input
|
<input
|
||||||
type="number"
|
type="number"
|
||||||
min="1"
|
min="1"
|
||||||
|
max="364"
|
||||||
|
value={settings.reminderDaysBefore}
|
||||||
|
onChange={(e) => setSettings({ ...settings, reminderDaysBefore: Number(e.target.value) || 7 })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
<label
|
||||||
|
className={
|
||||||
|
settings.lowStockDays <= settings.reminderDaysBefore ||
|
||||||
|
settings.lowStockDays >= settings.highStockDays
|
||||||
|
? "threshold-invalid"
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<span className="field-label threshold-chip-label">
|
||||||
|
<span className="status-chip small warning">{t("status.lowStock")}</span>
|
||||||
|
<span
|
||||||
|
className="info-tooltip small tooltip-align-left"
|
||||||
|
data-tooltip={t("settings.stock.lowStockTooltip")}
|
||||||
|
>
|
||||||
|
ⓘ
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
<div className="input-with-tooltip">
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
min="2"
|
||||||
max="365"
|
max="365"
|
||||||
value={settings.lowStockDays}
|
value={settings.lowStockDays}
|
||||||
onChange={(e) => setSettings({ ...settings, lowStockDays: Number(e.target.value) || 30 })}
|
onChange={(e) => setSettings({ ...settings, lowStockDays: Number(e.target.value) || 30 })}
|
||||||
/>
|
/>
|
||||||
<span className="info-tooltip" data-tooltip={t("settings.stock.lowStockTooltip")}>
|
|
||||||
ⓘ
|
|
||||||
</span>
|
|
||||||
</div>
|
</div>
|
||||||
</label>
|
</label>
|
||||||
<label>
|
<label className={settings.highStockDays <= settings.lowStockDays ? "threshold-invalid" : ""}>
|
||||||
<span className="field-label">{t("settings.stock.highStockDays")}</span>
|
<span className="field-label threshold-chip-label">
|
||||||
|
<span className="status-chip small high">{t("status.highStock")}</span>
|
||||||
|
<span
|
||||||
|
className="info-tooltip small tooltip-align-left"
|
||||||
|
data-tooltip={t("settings.stock.highStockTooltip")}
|
||||||
|
>
|
||||||
|
ⓘ
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
<div className="input-with-tooltip">
|
<div className="input-with-tooltip">
|
||||||
<input
|
<input
|
||||||
type="number"
|
type="number"
|
||||||
min="1"
|
min="3"
|
||||||
max="730"
|
max="730"
|
||||||
value={settings.highStockDays}
|
value={settings.highStockDays}
|
||||||
onChange={(e) => setSettings({ ...settings, highStockDays: Number(e.target.value) || 180 })}
|
onChange={(e) => setSettings({ ...settings, highStockDays: Number(e.target.value) || 180 })}
|
||||||
/>
|
/>
|
||||||
<span className="info-tooltip" data-tooltip={t("settings.stock.highStockTooltip")}>
|
|
||||||
ⓘ
|
|
||||||
</span>
|
|
||||||
</div>
|
</div>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
{(settings.reminderDaysBefore >= settings.lowStockDays ||
|
||||||
|
settings.lowStockDays >= settings.highStockDays) && (
|
||||||
|
<p className="threshold-validation-error">{t("settings.stock.thresholdValidation")}</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="setting-section">
|
||||||
|
<div className="setting-row compact">
|
||||||
|
<div className="setting-label">
|
||||||
|
<span>{t("settings.stock.shareStockStatus")}</span>
|
||||||
|
<span className="info-tooltip small" data-tooltip={t("settings.stock.shareStockStatusDesc")}>
|
||||||
|
ⓘ
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<label className="toggle-switch small">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={settings.shareStockStatus}
|
||||||
|
onChange={(e) => setSettings({ ...settings, shareStockStatus: e.target.checked })}
|
||||||
|
/>
|
||||||
|
<span className="toggle-slider"></span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
@@ -651,7 +749,15 @@ export function SettingsPage() {
|
|||||||
</article>
|
</article>
|
||||||
|
|
||||||
<div className="form-footer">
|
<div className="form-footer">
|
||||||
<button type="submit" disabled={settingsSaving || (!settingsChanged && settingsSaved)}>
|
<button
|
||||||
|
type="submit"
|
||||||
|
disabled={
|
||||||
|
settingsSaving ||
|
||||||
|
(!settingsChanged && settingsSaved) ||
|
||||||
|
settings.reminderDaysBefore >= settings.lowStockDays ||
|
||||||
|
settings.lowStockDays >= settings.highStockDays
|
||||||
|
}
|
||||||
|
>
|
||||||
{settingsSaving
|
{settingsSaving
|
||||||
? t("common.saving")
|
? t("common.saving")
|
||||||
: settingsSaved && !settingsChanged
|
: settingsSaved && !settingsChanged
|
||||||
|
|||||||
+226
-14
@@ -152,6 +152,90 @@ body.modal-open {
|
|||||||
background: rgba(47, 134, 246, 0.12);
|
background: rgba(47, 134, 246, 0.12);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* =============================================================================
|
||||||
|
Theme Dropdown Menu
|
||||||
|
============================================================================= */
|
||||||
|
.theme-menu {
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-dropdown {
|
||||||
|
position: absolute;
|
||||||
|
top: calc(100% + 0.5rem);
|
||||||
|
right: 0;
|
||||||
|
min-width: 160px;
|
||||||
|
background: rgba(15, 23, 42, 0.85);
|
||||||
|
backdrop-filter: blur(12px);
|
||||||
|
-webkit-backdrop-filter: blur(12px);
|
||||||
|
border: 1px solid rgba(255, 255, 255, 0.12);
|
||||||
|
border-radius: 12px;
|
||||||
|
box-shadow:
|
||||||
|
0 12px 32px rgba(0, 0, 0, 0.4),
|
||||||
|
inset 0 0 0 1px rgba(255, 255, 255, 0.05);
|
||||||
|
opacity: 0;
|
||||||
|
visibility: hidden;
|
||||||
|
transform: translateY(-6px) scale(0.95);
|
||||||
|
transition: all 0.2s ease;
|
||||||
|
z-index: 1000;
|
||||||
|
overflow: hidden;
|
||||||
|
padding: 0.375rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
[data-theme="light"] .theme-dropdown {
|
||||||
|
background: rgba(255, 255, 255, 0.85);
|
||||||
|
border: 1px solid rgba(0, 0, 0, 0.08);
|
||||||
|
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.12);
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-menu.open .theme-dropdown {
|
||||||
|
opacity: 1;
|
||||||
|
visibility: visible;
|
||||||
|
transform: translateY(0) scale(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-dropdown-item {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.75rem;
|
||||||
|
width: 100%;
|
||||||
|
padding: 0.625rem 0.75rem;
|
||||||
|
border: none;
|
||||||
|
background: none;
|
||||||
|
color: var(--text-primary);
|
||||||
|
font-size: 0.875rem;
|
||||||
|
cursor: pointer;
|
||||||
|
border-radius: 8px;
|
||||||
|
transition: background 0.15s ease;
|
||||||
|
text-align: left;
|
||||||
|
box-shadow: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-dropdown-item:hover {
|
||||||
|
background: rgba(59, 130, 246, 0.15);
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-dropdown-item.active {
|
||||||
|
color: var(--accent-primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-dropdown-item svg {
|
||||||
|
width: 18px;
|
||||||
|
height: 18px;
|
||||||
|
flex-shrink: 0;
|
||||||
|
opacity: 0.7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-dropdown-item.active svg {
|
||||||
|
opacity: 1;
|
||||||
|
color: var(--accent-primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-check {
|
||||||
|
margin-left: auto;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
|
||||||
.hero-title {
|
.hero-title {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
@@ -290,6 +374,31 @@ body.modal-open {
|
|||||||
color: var(--danger);
|
color: var(--danger);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.reminder-send-row {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.75rem;
|
||||||
|
padding-left: 1.75rem;
|
||||||
|
padding-top: 0.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.reminder-send-row .ghost {
|
||||||
|
font-size: 0.8rem;
|
||||||
|
padding: 0.25rem 0.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.reminder-send-result {
|
||||||
|
font-size: 0.8rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.reminder-send-result.success {
|
||||||
|
color: var(--success);
|
||||||
|
}
|
||||||
|
|
||||||
|
.reminder-send-result.error {
|
||||||
|
color: var(--danger);
|
||||||
|
}
|
||||||
|
|
||||||
.med-link {
|
.med-link {
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
text-decoration: underline;
|
text-decoration: underline;
|
||||||
@@ -389,7 +498,6 @@ body.modal-open {
|
|||||||
border-radius: 14px;
|
border-radius: 14px;
|
||||||
padding: 1.25rem;
|
padding: 1.25rem;
|
||||||
box-shadow: 0 14px 36px var(--shadow);
|
box-shadow: 0 14px 36px var(--shadow);
|
||||||
overflow: hidden;
|
|
||||||
transition:
|
transition:
|
||||||
background 200ms ease,
|
background 200ms ease,
|
||||||
border-color 200ms ease;
|
border-color 200ms ease;
|
||||||
@@ -1024,12 +1132,14 @@ textarea.auto-resize {
|
|||||||
|
|
||||||
.dose-input-group input {
|
.dose-input-group input {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
min-width: 0;
|
min-width: 100px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.dose-unit-select {
|
.dose-unit-select {
|
||||||
width: auto;
|
width: auto;
|
||||||
min-width: 80px;
|
min-width: unset;
|
||||||
|
max-width: 120px;
|
||||||
|
flex-shrink: 0;
|
||||||
padding: 0.5rem 0.75rem;
|
padding: 0.5rem 0.75rem;
|
||||||
background: var(--bg-primary);
|
background: var(--bg-primary);
|
||||||
border: 1px solid var(--border-primary);
|
border: 1px solid var(--border-primary);
|
||||||
@@ -1330,6 +1440,20 @@ textarea.auto-resize {
|
|||||||
}
|
}
|
||||||
.day-block.all-taken {
|
.day-block.all-taken {
|
||||||
border-color: rgba(57, 217, 138, 0.3);
|
border-color: rgba(57, 217, 138, 0.3);
|
||||||
|
background: linear-gradient(135deg, rgba(57, 217, 138, 0.06) 0%, rgba(57, 217, 138, 0.015) 100%);
|
||||||
|
}
|
||||||
|
.day-block.all-taken .day-divider,
|
||||||
|
.day-block.all-taken.stock-warning .day-divider,
|
||||||
|
.day-block.all-taken.stock-danger .day-divider {
|
||||||
|
color: var(--success);
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
.day-block.past-missed {
|
||||||
|
border-color: rgba(252, 211, 77, 0.35);
|
||||||
|
}
|
||||||
|
.day-block.past-missed .day-divider {
|
||||||
|
color: var(--warning);
|
||||||
|
opacity: 0.8;
|
||||||
}
|
}
|
||||||
.day-block.today.all-taken {
|
.day-block.today.all-taken {
|
||||||
border-color: var(--success);
|
border-color: var(--success);
|
||||||
@@ -1800,6 +1924,10 @@ textarea.auto-resize {
|
|||||||
.status-chip.high::before {
|
.status-chip.high::before {
|
||||||
content: "★";
|
content: "★";
|
||||||
}
|
}
|
||||||
|
.status-chip.small {
|
||||||
|
font-size: 0.7rem;
|
||||||
|
padding: 0.2rem 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
@media (max-width: 760px) {
|
@media (max-width: 760px) {
|
||||||
.table-head,
|
.table-head,
|
||||||
@@ -1810,6 +1938,9 @@ textarea.auto-resize {
|
|||||||
.table-head {
|
.table-head {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
.table-row .hide-on-card {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
.table-row {
|
.table-row {
|
||||||
padding: 0.75rem;
|
padding: 0.75rem;
|
||||||
display: flex;
|
display: flex;
|
||||||
@@ -1836,8 +1967,8 @@ textarea.auto-resize {
|
|||||||
/* First span (name cell) - centered horizontal layout */
|
/* First span (name cell) - centered horizontal layout */
|
||||||
.table-row span:first-child {
|
.table-row span:first-child {
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
padding-bottom: 0.5rem;
|
padding-bottom: 0.15rem;
|
||||||
margin-bottom: 0.25rem;
|
margin-bottom: 0;
|
||||||
}
|
}
|
||||||
.table-row span:first-child::before {
|
.table-row span:first-child::before {
|
||||||
display: none; /* Hide "NAME" label on mobile */
|
display: none; /* Hide "NAME" label on mobile */
|
||||||
@@ -1845,6 +1976,11 @@ textarea.auto-resize {
|
|||||||
/* Status chip in table row - left aligned */
|
/* Status chip in table row - left aligned */
|
||||||
.table-row span.status-chip {
|
.table-row span.status-chip {
|
||||||
align-self: flex-start;
|
align-self: flex-start;
|
||||||
|
justify-content: flex-start;
|
||||||
|
gap: 0.4rem;
|
||||||
|
}
|
||||||
|
.table-row span.status-chip::before {
|
||||||
|
margin-right: 0;
|
||||||
}
|
}
|
||||||
/* Avatar + name layout - centered */
|
/* Avatar + name layout - centered */
|
||||||
.table-row .cell-with-avatar {
|
.table-row .cell-with-avatar {
|
||||||
@@ -2019,11 +2155,12 @@ textarea.auto-resize {
|
|||||||
text-transform: uppercase;
|
text-transform: uppercase;
|
||||||
letter-spacing: 0.04em;
|
letter-spacing: 0.04em;
|
||||||
}
|
}
|
||||||
.planner-checkbox {
|
.planner label.planner-checkbox {
|
||||||
grid-column: 1 / -1;
|
grid-column: 1 / -1;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
flex-wrap: nowrap;
|
||||||
gap: 0.75rem;
|
gap: 0.75rem;
|
||||||
color: var(--text-secondary);
|
color: var(--text-secondary);
|
||||||
font-size: 0.875rem;
|
font-size: 0.875rem;
|
||||||
@@ -2394,6 +2531,16 @@ textarea.auto-resize {
|
|||||||
z-index: 101;
|
z-index: 101;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Tooltip aligned to left edge of icon (prevents clipping inside modals) */
|
||||||
|
.info-tooltip.tooltip-align-left::after {
|
||||||
|
left: 0;
|
||||||
|
transform: none;
|
||||||
|
}
|
||||||
|
.info-tooltip.tooltip-align-left::before {
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
}
|
||||||
|
|
||||||
.info-tooltip:hover::after,
|
.info-tooltip:hover::after,
|
||||||
.info-tooltip:hover::before,
|
.info-tooltip:hover::before,
|
||||||
.info-tooltip:focus::after,
|
.info-tooltip:focus::after,
|
||||||
@@ -2870,6 +3017,62 @@ textarea.auto-resize {
|
|||||||
font-size: 0.9rem;
|
font-size: 0.9rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Threshold Chips Group - 3-column grid for Critical/Low/High */
|
||||||
|
.threshold-chips-group {
|
||||||
|
grid-template-columns: 1fr 1fr 1fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
.threshold-chips-group label {
|
||||||
|
text-transform: none;
|
||||||
|
letter-spacing: normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
.threshold-chip-label {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
margin-bottom: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.threshold-chip-label .status-chip {
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.threshold-invalid input {
|
||||||
|
border-color: var(--danger) !important;
|
||||||
|
box-shadow: 0 0 0 1px var(--danger);
|
||||||
|
}
|
||||||
|
|
||||||
|
.threshold-validation-error {
|
||||||
|
margin: 0.75rem 0 0;
|
||||||
|
padding: 0.5rem 0.75rem;
|
||||||
|
background: rgba(255, 94, 94, 0.1);
|
||||||
|
border: 1px solid rgba(255, 94, 94, 0.3);
|
||||||
|
border-radius: 6px;
|
||||||
|
color: #fca5a5;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Stock Reminder Trigger in Notifications */
|
||||||
|
.stock-reminder-trigger {
|
||||||
|
margin-bottom: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stock-reminder-trigger .setting-desc {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
margin: 0;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.stock-reminder-trigger .status-chip {
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
/* Compact Setting Row - for inline toggles without card styling */
|
/* Compact Setting Row - for inline toggles without card styling */
|
||||||
.setting-row.compact {
|
.setting-row.compact {
|
||||||
padding: 0.75rem 0;
|
padding: 0.75rem 0;
|
||||||
@@ -3045,6 +3248,9 @@ textarea.auto-resize {
|
|||||||
.setting-group {
|
.setting-group {
|
||||||
grid-template-columns: 1fr;
|
grid-template-columns: 1fr;
|
||||||
}
|
}
|
||||||
|
.threshold-chips-group {
|
||||||
|
grid-template-columns: 1fr 1fr 1fr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Medication Avatar */
|
/* Medication Avatar */
|
||||||
@@ -4614,7 +4820,6 @@ a.about-version-link:hover {
|
|||||||
.about-update-section {
|
.about-update-section {
|
||||||
padding: 1.25rem 1.5rem;
|
padding: 1.25rem 1.5rem;
|
||||||
border-bottom: 1px solid var(--border-primary);
|
border-bottom: 1px solid var(--border-primary);
|
||||||
min-height: 148px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.about-update-btn {
|
.about-update-btn {
|
||||||
@@ -4713,13 +4918,6 @@ a.about-version-link:hover {
|
|||||||
background: var(--accent-hover);
|
background: var(--accent-hover);
|
||||||
}
|
}
|
||||||
|
|
||||||
.update-last-checked {
|
|
||||||
display: block;
|
|
||||||
margin-top: 0.5rem;
|
|
||||||
font-size: 0.75rem;
|
|
||||||
opacity: 0.8;
|
|
||||||
}
|
|
||||||
|
|
||||||
.about-links {
|
.about-links {
|
||||||
padding: 1.25rem 1.5rem;
|
padding: 1.25rem 1.5rem;
|
||||||
border-bottom: 1px solid var(--border-primary);
|
border-bottom: 1px solid var(--border-primary);
|
||||||
@@ -5444,6 +5642,20 @@ a.about-version-link:hover {
|
|||||||
justify-self: start;
|
justify-self: start;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.mobile-edit-form .blister-row .remind-toggle-row {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.blister-inputs .remind-toggle-row {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
align-self: end;
|
||||||
|
padding-bottom: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
.mobile-edit-form .blister-row .datetime-inputs {
|
.mobile-edit-form .blister-row .datetime-inputs {
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: 0.5rem;
|
gap: 0.5rem;
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ describe("AppHeader", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("renders theme toggle button", async () => {
|
it("renders theme menu button", async () => {
|
||||||
const mockOnOpenProfile = vi.fn();
|
const mockOnOpenProfile = vi.fn();
|
||||||
const mockOnOpenAbout = vi.fn();
|
const mockOnOpenAbout = vi.fn();
|
||||||
|
|
||||||
@@ -95,12 +95,33 @@ describe("AppHeader", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
const buttons = screen.getAllByRole("button");
|
const themeBtn = screen.getByTitle(/theme\.title/i);
|
||||||
const themeBtn = buttons.find((btn) => btn.textContent?.includes("🌙") || btn.textContent?.includes("☀️"));
|
|
||||||
expect(themeBtn).toBeInTheDocument();
|
expect(themeBtn).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("opens theme dropdown and shows Light/Dark/System options", async () => {
|
||||||
|
const mockOnOpenProfile = vi.fn();
|
||||||
|
const mockOnOpenAbout = vi.fn();
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter initialEntries={["/dashboard"]}>
|
||||||
|
<AuthProvider>
|
||||||
|
<AppHeader onOpenProfile={mockOnOpenProfile} onOpenAbout={mockOnOpenAbout} />
|
||||||
|
</AuthProvider>
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
const themeBtn = screen.getByTitle(/theme\.title/i);
|
||||||
|
fireEvent.click(themeBtn);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(screen.getByText(/theme\.light/i)).toBeInTheDocument();
|
||||||
|
expect(screen.getByText(/theme\.dark/i)).toBeInTheDocument();
|
||||||
|
expect(screen.getByText(/theme\.system/i)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
it("renders settings button when auth is disabled", async () => {
|
it("renders settings button when auth is disabled", async () => {
|
||||||
const mockOnOpenProfile = vi.fn();
|
const mockOnOpenProfile = vi.fn();
|
||||||
const mockOnOpenAbout = vi.fn();
|
const mockOnOpenAbout = vi.fn();
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ const mockMedication: Medication = {
|
|||||||
id: 1,
|
id: 1,
|
||||||
name: "Test Med",
|
name: "Test Med",
|
||||||
genericName: "Generic Name",
|
genericName: "Generic Name",
|
||||||
|
packageType: "blister",
|
||||||
packCount: 1,
|
packCount: 1,
|
||||||
blistersPerPack: 1,
|
blistersPerPack: 1,
|
||||||
pillsPerBlister: 30,
|
pillsPerBlister: 30,
|
||||||
@@ -385,3 +386,197 @@ describe("MedDetailModal with refill history", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("MedDetailModal intake schedule usage display", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not multiply usage by personCount when intakes have per-intake takenBy", () => {
|
||||||
|
// Two people at medication level, but each intake has its own takenBy
|
||||||
|
const med: Medication = {
|
||||||
|
...mockMedication,
|
||||||
|
takenBy: ["Alice", "Bob"],
|
||||||
|
blisters: [
|
||||||
|
{ usage: 1, every: 1, start: "2024-01-01T09:00:00" },
|
||||||
|
{ usage: 1, every: 1, start: "2024-01-01T21:00:00" },
|
||||||
|
],
|
||||||
|
intakes: [
|
||||||
|
{ usage: 1, every: 1, start: "2024-01-01T09:00:00", takenBy: "Alice", intakeRemindersEnabled: false },
|
||||||
|
{ usage: 1, every: 1, start: "2024-01-01T21:00:00", takenBy: "Bob", intakeRemindersEnabled: false },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
render(<MedDetailModal {...defaultProps} selectedMed={med} />);
|
||||||
|
|
||||||
|
const usageElements = document.querySelectorAll(".med-schedule-usage");
|
||||||
|
// Each intake should show "1 pill" (not "2 pills")
|
||||||
|
usageElements.forEach((el) => {
|
||||||
|
expect(el.textContent).toContain("1");
|
||||||
|
expect(el.textContent).not.toMatch(/^2\b/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("multiplies usage by personCount for legacy blisters without per-intake takenBy", () => {
|
||||||
|
// Two people at medication level, legacy blisters without intakes
|
||||||
|
const med: Medication = {
|
||||||
|
...mockMedication,
|
||||||
|
takenBy: ["Alice", "Bob"],
|
||||||
|
blisters: [{ usage: 1, every: 1, start: "2024-01-01T09:00:00" }],
|
||||||
|
// No intakes array - legacy format
|
||||||
|
};
|
||||||
|
render(<MedDetailModal {...defaultProps} selectedMed={med} />);
|
||||||
|
|
||||||
|
const usageElements = document.querySelectorAll(".med-schedule-usage");
|
||||||
|
// Legacy: 1 pill * 2 people = "2 pills"
|
||||||
|
expect(usageElements.length).toBe(1);
|
||||||
|
expect(usageElements[0].textContent).toContain("2");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows correct usage for single person with per-intake takenBy", () => {
|
||||||
|
const med: Medication = {
|
||||||
|
...mockMedication,
|
||||||
|
takenBy: ["Alice"],
|
||||||
|
pillWeightMg: 500,
|
||||||
|
blisters: [{ usage: 2, every: 1, start: "2024-01-01T09:00:00" }],
|
||||||
|
intakes: [{ usage: 2, every: 1, start: "2024-01-01T09:00:00", takenBy: "Alice", intakeRemindersEnabled: false }],
|
||||||
|
};
|
||||||
|
render(<MedDetailModal {...defaultProps} selectedMed={med} />);
|
||||||
|
|
||||||
|
const usageElements = document.querySelectorAll(".med-schedule-usage");
|
||||||
|
expect(usageElements.length).toBe(1);
|
||||||
|
// Should show "2 pills (1000 mg)" - usage=2, not multiplied
|
||||||
|
expect(usageElements[0].textContent).toContain("2");
|
||||||
|
expect(usageElements[0].textContent).toContain("1000");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("MedDetailModal stock overflow warning", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows warning icon when stock exceeds package capacity", () => {
|
||||||
|
const overflowCoverage: Coverage = {
|
||||||
|
name: "Test Med",
|
||||||
|
medsLeft: 49,
|
||||||
|
daysLeft: 49,
|
||||||
|
depletionDate: "2024-03-01",
|
||||||
|
depletionTime: Date.now() + 49 * 86400000,
|
||||||
|
nextDose: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
render(<MedDetailModal {...defaultProps} coverage={{ all: [overflowCoverage] }} />);
|
||||||
|
|
||||||
|
// packageSize = 1 * 1 * 30 + 0 = 30, currentStock = 49 > 30
|
||||||
|
const warningIcon = document.querySelector(".info-tooltip.tooltip-align-left.warning-text");
|
||||||
|
expect(warningIcon).toBeInTheDocument();
|
||||||
|
expect(warningIcon?.getAttribute("data-tooltip")).toBe("tooltips.stockExceedsCapacity");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not show warning icon when stock is within package capacity", () => {
|
||||||
|
render(<MedDetailModal {...defaultProps} />);
|
||||||
|
|
||||||
|
// packageSize = 30, currentStock = 25 < 30
|
||||||
|
const warningIcon = document.querySelector(".info-tooltip.tooltip-align-left.warning-text");
|
||||||
|
expect(warningIcon).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not show warning icon when stock equals package capacity", () => {
|
||||||
|
const exactCoverage: Coverage = {
|
||||||
|
name: "Test Med",
|
||||||
|
medsLeft: 30,
|
||||||
|
daysLeft: 30,
|
||||||
|
depletionDate: "2024-02-01",
|
||||||
|
depletionTime: Date.now() + 30 * 86400000,
|
||||||
|
nextDose: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
render(<MedDetailModal {...defaultProps} coverage={{ all: [exactCoverage] }} />);
|
||||||
|
|
||||||
|
// packageSize = 30, currentStock = 30 — equal, no warning
|
||||||
|
const warningIcon = document.querySelector(".info-tooltip.tooltip-align-left.warning-text");
|
||||||
|
expect(warningIcon).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("MedDetailModal bottle package type", () => {
|
||||||
|
const bottleMed: Medication = {
|
||||||
|
id: 2,
|
||||||
|
name: "Bottle Med",
|
||||||
|
genericName: null,
|
||||||
|
packageType: "bottle",
|
||||||
|
packCount: 0,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 1,
|
||||||
|
looseTablets: 80,
|
||||||
|
totalPills: 100,
|
||||||
|
takenBy: [],
|
||||||
|
blisters: [{ usage: 1, every: 1, start: "2024-01-01T09:00:00" }],
|
||||||
|
updatedAt: null,
|
||||||
|
expiryDate: null,
|
||||||
|
notes: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
const bottleCoverage: Coverage = {
|
||||||
|
name: "Bottle Med",
|
||||||
|
medsLeft: 80,
|
||||||
|
daysLeft: 80,
|
||||||
|
depletionDate: "2024-06-01",
|
||||||
|
depletionTime: Date.now() + 80 * 86400000,
|
||||||
|
nextDose: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
const bottleProps = {
|
||||||
|
...defaultProps,
|
||||||
|
selectedMed: bottleMed,
|
||||||
|
coverage: { all: [bottleCoverage] },
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not show blister fields in stock info section", () => {
|
||||||
|
render(<MedDetailModal {...bottleProps} />);
|
||||||
|
|
||||||
|
// Should show current stock
|
||||||
|
expect(screen.getByText(/modal\.currentStock/i)).toBeInTheDocument();
|
||||||
|
|
||||||
|
// Should NOT show full blisters or open blister labels
|
||||||
|
expect(screen.queryByText(/table\.fullBlisters/i)).not.toBeInTheDocument();
|
||||||
|
expect(screen.queryByText(/table\.openBlister/i)).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows bottle type in package details section", () => {
|
||||||
|
render(<MedDetailModal {...bottleProps} />);
|
||||||
|
|
||||||
|
// Should show package type as bottle
|
||||||
|
expect(screen.getByText(/form\.packageTypeBottle/i)).toBeInTheDocument();
|
||||||
|
|
||||||
|
// Should show total capacity
|
||||||
|
expect(screen.getByText(/form\.totalCapacity/i)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows pills-only refill modal for bottle type", () => {
|
||||||
|
render(<MedDetailModal {...bottleProps} showRefillModal={true} />);
|
||||||
|
|
||||||
|
// Should show pills to add label
|
||||||
|
expect(screen.getByText(/refill\.pillsToAdd/i)).toBeInTheDocument();
|
||||||
|
|
||||||
|
// Should NOT show packs label in refill
|
||||||
|
const refillModal = document.querySelector(".refill-modal");
|
||||||
|
// Packs label should not be present for bottle type
|
||||||
|
expect(screen.queryByText("refill.packs")).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows total pills input in edit stock modal for bottle type", () => {
|
||||||
|
render(<MedDetailModal {...bottleProps} showEditStockModal={true} />);
|
||||||
|
|
||||||
|
// Should show total pills label
|
||||||
|
expect(screen.getByText(/editStock\.totalPills/i)).toBeInTheDocument();
|
||||||
|
|
||||||
|
// Should NOT show full blisters or partial blister labels
|
||||||
|
expect(screen.queryByText(/editStock\.fullBlisters/i)).not.toBeInTheDocument();
|
||||||
|
expect(screen.queryByText(/editStock\.partialBlisterPills/i)).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -424,8 +424,14 @@ describe("MobileEditModal takenBy", () => {
|
|||||||
|
|
||||||
render(<MobileEditModal {...defaultProps} form={form} />);
|
render(<MobileEditModal {...defaultProps} form={form} />);
|
||||||
|
|
||||||
expect(screen.getByText("John")).toBeInTheDocument();
|
// Check tags are rendered (use getAllByText since names also appear in intake dropdowns)
|
||||||
expect(screen.getByText("Jane")).toBeInTheDocument();
|
const johnElements = screen.getAllByText("John");
|
||||||
|
const janeElements = screen.getAllByText("Jane");
|
||||||
|
expect(johnElements.length).toBeGreaterThanOrEqual(1);
|
||||||
|
expect(janeElements.length).toBeGreaterThanOrEqual(1);
|
||||||
|
// Verify the tag elements specifically exist
|
||||||
|
expect(johnElements.some((el) => el.closest(".tag"))).toBe(true);
|
||||||
|
expect(janeElements.some((el) => el.closest(".tag"))).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("calls onRemoveTakenByPerson when tag removed", () => {
|
it("calls onRemoveTakenByPerson when tag removed", () => {
|
||||||
@@ -535,3 +541,52 @@ describe("MobileEditModal optional fields", () => {
|
|||||||
expect(toggle).toBeInTheDocument();
|
expect(toggle).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("MobileEditModal bottle package type", () => {
|
||||||
|
const bottleForm: FormState = {
|
||||||
|
...defaultForm,
|
||||||
|
packageType: "bottle",
|
||||||
|
packCount: "0",
|
||||||
|
blistersPerPack: "1",
|
||||||
|
pillsPerBlister: "1",
|
||||||
|
looseTablets: "80",
|
||||||
|
totalPills: "100",
|
||||||
|
};
|
||||||
|
|
||||||
|
it("shows pills-only refill form for bottle type when editing", () => {
|
||||||
|
render(<MobileEditModal {...defaultProps} form={bottleForm} editingId={1} />);
|
||||||
|
|
||||||
|
// Should show "pillsToAdd" label for bottle
|
||||||
|
expect(screen.getByText(/refill\.pillsToAdd/i)).toBeInTheDocument();
|
||||||
|
|
||||||
|
// Should NOT show "packs" label in refill section
|
||||||
|
const refillSection = document.querySelector(".refill-section");
|
||||||
|
expect(refillSection).toBeInTheDocument();
|
||||||
|
expect(refillSection!.textContent).not.toContain("refill.packs");
|
||||||
|
expect(refillSection!.textContent).not.toContain("refill.loosePills");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows packs and loose refill form for blister type when editing", () => {
|
||||||
|
render(<MobileEditModal {...defaultProps} form={defaultForm} editingId={1} />);
|
||||||
|
|
||||||
|
// Should show "packs" and "loosePills" labels for blister
|
||||||
|
const refillSection = document.querySelector(".refill-section");
|
||||||
|
expect(refillSection).toBeInTheDocument();
|
||||||
|
expect(refillSection!.textContent).toContain("refill.packs");
|
||||||
|
expect(refillSection!.textContent).toContain("refill.loosePills");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows totalCapacity and currentPills fields for bottle form", () => {
|
||||||
|
render(<MobileEditModal {...defaultProps} form={bottleForm} />);
|
||||||
|
|
||||||
|
// Should show total capacity field
|
||||||
|
expect(screen.getByText(/form\.totalCapacity/i)).toBeInTheDocument();
|
||||||
|
// Should show current pills field
|
||||||
|
expect(screen.getByText(/form\.currentPills/i)).toBeInTheDocument();
|
||||||
|
|
||||||
|
// Should NOT show blister-specific fields
|
||||||
|
expect(screen.queryByText("form.packs")).not.toBeInTheDocument();
|
||||||
|
expect(screen.queryByText("form.blistersPerPack")).not.toBeInTheDocument();
|
||||||
|
expect(screen.queryByText("form.pillsPerBlister")).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -103,10 +103,14 @@ describe("useDoses", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("marks dose as taken optimistically", async () => {
|
it("marks dose as taken optimistically", async () => {
|
||||||
// First call for initial load, subsequent calls for marking dose
|
// First call for initial load, second for marking dose, third for re-sync
|
||||||
(global.fetch as ReturnType<typeof vi.fn>)
|
(global.fetch as ReturnType<typeof vi.fn>)
|
||||||
.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({ doses: [] }) })
|
.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({ doses: [] }) })
|
||||||
.mockResolvedValueOnce({ ok: true });
|
.mockResolvedValueOnce({ ok: true })
|
||||||
|
.mockResolvedValueOnce({
|
||||||
|
ok: true,
|
||||||
|
json: () => Promise.resolve({ doses: [{ doseId: "new-dose", takenAt: Date.now(), dismissed: false }] }),
|
||||||
|
});
|
||||||
|
|
||||||
const { result } = renderHook(() => useDoses());
|
const { result } = renderHook(() => useDoses());
|
||||||
|
|
||||||
@@ -119,7 +123,9 @@ describe("useDoses", () => {
|
|||||||
await result.current.markDoseTaken("new-dose");
|
await result.current.markDoseTaken("new-dose");
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result.current.takenDoses.has("new-dose")).toBe(true);
|
await waitFor(() => {
|
||||||
|
expect(result.current.takenDoses.has("new-dose")).toBe(true);
|
||||||
|
});
|
||||||
expect(fetch).toHaveBeenCalledWith(
|
expect(fetch).toHaveBeenCalledWith(
|
||||||
"/api/doses/taken",
|
"/api/doses/taken",
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
@@ -130,10 +136,11 @@ describe("useDoses", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("reverts optimistic update on error", async () => {
|
it("reverts optimistic update on error", async () => {
|
||||||
// First call for initial load, second for marking dose fails
|
// First call for initial load, second for marking dose fails, third for re-sync
|
||||||
(global.fetch as ReturnType<typeof vi.fn>)
|
(global.fetch as ReturnType<typeof vi.fn>)
|
||||||
.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({ doses: [] }) })
|
.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({ doses: [] }) })
|
||||||
.mockRejectedValueOnce(new Error("Network error"));
|
.mockRejectedValueOnce(new Error("Network error"))
|
||||||
|
.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({ doses: [] }) });
|
||||||
|
|
||||||
const { result } = renderHook(() => useDoses());
|
const { result } = renderHook(() => useDoses());
|
||||||
|
|
||||||
@@ -153,12 +160,13 @@ describe("useDoses", () => {
|
|||||||
|
|
||||||
it("undoes dose taken optimistically", async () => {
|
it("undoes dose taken optimistically", async () => {
|
||||||
const mockDoses = {
|
const mockDoses = {
|
||||||
doses: [{ doseId: "taken-dose", dismissed: false }],
|
doses: [{ doseId: "taken-dose", takenAt: Date.now(), dismissed: false }],
|
||||||
};
|
};
|
||||||
|
|
||||||
(global.fetch as ReturnType<typeof vi.fn>)
|
(global.fetch as ReturnType<typeof vi.fn>)
|
||||||
.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(mockDoses) })
|
.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(mockDoses) })
|
||||||
.mockResolvedValueOnce({ ok: true });
|
.mockResolvedValueOnce({ ok: true })
|
||||||
|
.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({ doses: [] }) });
|
||||||
|
|
||||||
const { result } = renderHook(() => useDoses());
|
const { result } = renderHook(() => useDoses());
|
||||||
|
|
||||||
@@ -170,10 +178,101 @@ describe("useDoses", () => {
|
|||||||
await result.current.undoDoseTaken("taken-dose");
|
await result.current.undoDoseTaken("taken-dose");
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result.current.takenDoses.has("taken-dose")).toBe(false);
|
await waitFor(() => {
|
||||||
|
expect(result.current.takenDoses.has("taken-dose")).toBe(false);
|
||||||
|
});
|
||||||
expect(fetch).toHaveBeenCalledWith("/api/doses/taken/taken-dose", expect.objectContaining({ method: "DELETE" }));
|
expect(fetch).toHaveBeenCalledWith("/api/doses/taken/taken-dose", expect.objectContaining({ method: "DELETE" }));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("reverts undo on error by re-adding the dose", async () => {
|
||||||
|
const mockDoses = {
|
||||||
|
doses: [{ doseId: "taken-dose", takenAt: 1710500000000, dismissed: false }],
|
||||||
|
};
|
||||||
|
|
||||||
|
// Initial load returns taken-dose, DELETE fails, re-sync returns taken-dose still there
|
||||||
|
(global.fetch as ReturnType<typeof vi.fn>)
|
||||||
|
.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(mockDoses) })
|
||||||
|
.mockRejectedValueOnce(new Error("Network error"))
|
||||||
|
.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(mockDoses) });
|
||||||
|
|
||||||
|
const { result } = renderHook(() => useDoses());
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(result.current.takenDoses.has("taken-dose")).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
await result.current.undoDoseTaken("taken-dose");
|
||||||
|
});
|
||||||
|
|
||||||
|
// After error, the dose should be re-added (reverted)
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(result.current.takenDoses.has("taken-dose")).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("populates takenDoseTimestamps from API response", async () => {
|
||||||
|
const takenAt = 1710500000000;
|
||||||
|
const mockDoses = {
|
||||||
|
doses: [{ doseId: "dose-1", takenAt, dismissed: false }],
|
||||||
|
};
|
||||||
|
|
||||||
|
(global.fetch as ReturnType<typeof vi.fn>).mockResolvedValueOnce({
|
||||||
|
ok: true,
|
||||||
|
json: () => Promise.resolve(mockDoses),
|
||||||
|
});
|
||||||
|
|
||||||
|
const { result } = renderHook(() => useDoses());
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(result.current.takenDoseTimestamps.get("dose-1")).toBe(takenAt);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("markDoseTaken sets takenDoseTimestamp optimistically", async () => {
|
||||||
|
const now = Date.now();
|
||||||
|
vi.setSystemTime(now);
|
||||||
|
|
||||||
|
// Initial load, POST success, re-sync
|
||||||
|
(global.fetch as ReturnType<typeof vi.fn>)
|
||||||
|
.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({ doses: [] }) })
|
||||||
|
.mockResolvedValueOnce({ ok: true })
|
||||||
|
.mockResolvedValueOnce({
|
||||||
|
ok: true,
|
||||||
|
json: () => Promise.resolve({ doses: [{ doseId: "new-dose", takenAt: now, dismissed: false }] }),
|
||||||
|
});
|
||||||
|
|
||||||
|
const { result } = renderHook(() => useDoses());
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(result.current.takenDoses.size).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
await result.current.markDoseTaken("new-dose");
|
||||||
|
});
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(result.current.takenDoseTimestamps.has("new-dose")).toBe(true);
|
||||||
|
expect(result.current.takenDoseTimestamps.get("new-dose")).toBe(now);
|
||||||
|
});
|
||||||
|
|
||||||
|
vi.useRealTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps state on fetch error during initial load", async () => {
|
||||||
|
// Initial load fails
|
||||||
|
(global.fetch as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error("Network error"));
|
||||||
|
|
||||||
|
const { result } = renderHook(() => useDoses());
|
||||||
|
|
||||||
|
// Should keep empty state, not crash
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(result.current.takenDoses.size).toBe(0);
|
||||||
|
expect(result.current.dismissedDoses.size).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("setShowClearMissedConfirm works", () => {
|
it("setShowClearMissedConfirm works", () => {
|
||||||
const { result } = renderHook(() => useDoses());
|
const { result } = renderHook(() => useDoses());
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,20 @@ describe("useTheme", () => {
|
|||||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockReturnValue(null);
|
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockReturnValue(null);
|
||||||
// Reset mock to default behavior
|
// Reset mock to default behavior
|
||||||
(window.localStorage.setItem as ReturnType<typeof vi.fn>).mockImplementation(() => {});
|
(window.localStorage.setItem as ReturnType<typeof vi.fn>).mockImplementation(() => {});
|
||||||
|
// Mock matchMedia to return dark system theme by default
|
||||||
|
Object.defineProperty(window, "matchMedia", {
|
||||||
|
writable: true,
|
||||||
|
value: vi.fn().mockImplementation((query: string) => ({
|
||||||
|
matches: false,
|
||||||
|
media: query,
|
||||||
|
onchange: null,
|
||||||
|
addEventListener: vi.fn(),
|
||||||
|
removeEventListener: vi.fn(),
|
||||||
|
dispatchEvent: vi.fn(),
|
||||||
|
addListener: vi.fn(),
|
||||||
|
removeListener: vi.fn(),
|
||||||
|
})),
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -17,58 +31,76 @@ describe("useTheme", () => {
|
|||||||
it("returns dark as default theme", () => {
|
it("returns dark as default theme", () => {
|
||||||
const { result } = renderHook(() => useTheme());
|
const { result } = renderHook(() => useTheme());
|
||||||
expect(result.current.theme).toBe("dark");
|
expect(result.current.theme).toBe("dark");
|
||||||
|
expect(result.current.themePreference).toBe("dark");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("reads theme from localStorage", () => {
|
it("reads theme preference from localStorage", () => {
|
||||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockReturnValue("light");
|
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockReturnValue("light");
|
||||||
const { result } = renderHook(() => useTheme());
|
const { result } = renderHook(() => useTheme());
|
||||||
expect(result.current.theme).toBe("light");
|
expect(result.current.theme).toBe("light");
|
||||||
|
expect(result.current.themePreference).toBe("light");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("toggles theme from dark to light", () => {
|
it("toggles theme through light → dark → system → light", () => {
|
||||||
const { result } = renderHook(() => useTheme());
|
|
||||||
|
|
||||||
expect(result.current.theme).toBe("dark");
|
|
||||||
|
|
||||||
act(() => {
|
|
||||||
result.current.toggleTheme();
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(result.current.theme).toBe("light");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("toggles theme from light to dark", () => {
|
|
||||||
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockReturnValue("light");
|
(window.localStorage.getItem as ReturnType<typeof vi.fn>).mockReturnValue("light");
|
||||||
const { result } = renderHook(() => useTheme());
|
const { result } = renderHook(() => useTheme());
|
||||||
|
expect(result.current.themePreference).toBe("light");
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.toggleTheme();
|
||||||
|
});
|
||||||
|
expect(result.current.themePreference).toBe("dark");
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.toggleTheme();
|
||||||
|
});
|
||||||
|
expect(result.current.themePreference).toBe("system");
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.toggleTheme();
|
||||||
|
});
|
||||||
|
expect(result.current.themePreference).toBe("light");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("sets theme preference directly", () => {
|
||||||
|
const { result } = renderHook(() => useTheme());
|
||||||
|
expect(result.current.themePreference).toBe("dark");
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.setThemePreference("light");
|
||||||
|
});
|
||||||
|
expect(result.current.themePreference).toBe("light");
|
||||||
expect(result.current.theme).toBe("light");
|
expect(result.current.theme).toBe("light");
|
||||||
|
|
||||||
act(() => {
|
act(() => {
|
||||||
result.current.toggleTheme();
|
result.current.setThemePreference("system");
|
||||||
});
|
});
|
||||||
|
expect(result.current.themePreference).toBe("system");
|
||||||
|
// System resolves to dark (matchMedia returns false for light)
|
||||||
expect(result.current.theme).toBe("dark");
|
expect(result.current.theme).toBe("dark");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("saves theme to localStorage on change", () => {
|
it("saves theme preference to localStorage on change", () => {
|
||||||
const { result } = renderHook(() => useTheme());
|
const { result } = renderHook(() => useTheme());
|
||||||
|
|
||||||
act(() => {
|
act(() => {
|
||||||
result.current.toggleTheme();
|
result.current.setThemePreference("light");
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(window.localStorage.setItem).toHaveBeenCalledWith("theme", "light");
|
expect(window.localStorage.setItem).toHaveBeenCalledWith("theme", "light");
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.setThemePreference("system");
|
||||||
|
});
|
||||||
|
expect(window.localStorage.setItem).toHaveBeenCalledWith("theme", "system");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sets data-theme attribute on document", () => {
|
it("sets data-theme attribute on document", () => {
|
||||||
const { result } = renderHook(() => useTheme());
|
const { result } = renderHook(() => useTheme());
|
||||||
|
|
||||||
expect(document.documentElement.getAttribute("data-theme")).toBe("dark");
|
expect(document.documentElement.getAttribute("data-theme")).toBe("dark");
|
||||||
|
|
||||||
act(() => {
|
act(() => {
|
||||||
result.current.toggleTheme();
|
result.current.setThemePreference("light");
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(document.documentElement.getAttribute("data-theme")).toBe("light");
|
expect(document.documentElement.getAttribute("data-theme")).toBe("light");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { fireEvent, render, screen } from "@testing-library/react";
|
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||||
import { MemoryRouter } from "react-router-dom";
|
import { MemoryRouter } from "react-router-dom";
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { DashboardPage } from "../../pages/DashboardPage";
|
import { DashboardPage } from "../../pages/DashboardPage";
|
||||||
@@ -160,6 +160,7 @@ const createMockAppContext = (overrides = {}) => ({
|
|||||||
setShowClearMissedConfirm: vi.fn(),
|
setShowClearMissedConfirm: vi.fn(),
|
||||||
clearingMissed: false,
|
clearingMissed: false,
|
||||||
dismissMissedDoses: vi.fn(),
|
dismissMissedDoses: vi.fn(),
|
||||||
|
loadSettings: vi.fn(),
|
||||||
...overrides,
|
...overrides,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -592,7 +593,9 @@ describe("DashboardPage with email notifications", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Reorder card should NOT be shown when reminders are active (Reminder Bar shows the info instead)
|
// Reorder card should NOT be shown when reminders are active (Reminder Bar shows the info instead)
|
||||||
expect(screen.queryByText(/dashboard\.reorder\.sendReminder/i)).not.toBeInTheDocument();
|
// The send reminder button IS shown in the reminder status bar (not the reorder card)
|
||||||
|
expect(document.querySelector(".reminder-status-bar")).toBeInTheDocument();
|
||||||
|
expect(screen.queryByText(/dashboard\.reorder\.title/i)).not.toBeInTheDocument();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -622,6 +625,76 @@ describe("DashboardPage with shoutrrr notifications", () => {
|
|||||||
const statusBar = document.querySelector(".reminder-status-bar");
|
const statusBar = document.querySelector(".reminder-status-bar");
|
||||||
expect(statusBar).toBeInTheDocument();
|
expect(statusBar).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("shows send reminder button when stock reminders are enabled and low stock exists", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<DashboardPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByText("dashboard.reorder.sendReminder")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("sends manual reminder notification on button click", async () => {
|
||||||
|
global.fetch = vi.fn().mockImplementation((url: string) => {
|
||||||
|
if (url === "/api/reminder/send-email") {
|
||||||
|
return Promise.resolve({
|
||||||
|
ok: true,
|
||||||
|
json: () => Promise.resolve({ success: true, message: "Notification sent via push" }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Settings refresh after successful send
|
||||||
|
return Promise.resolve({
|
||||||
|
ok: true,
|
||||||
|
json: () => Promise.resolve({}),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<DashboardPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
const sendButton = screen.getByText("dashboard.reorder.sendReminder");
|
||||||
|
fireEvent.click(sendButton);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(global.fetch).toHaveBeenCalledWith(
|
||||||
|
"/api/reminder/send-email",
|
||||||
|
expect.objectContaining({
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
credentials: "include",
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(screen.getByText("Notification sent via push")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows error message when manual reminder fails", async () => {
|
||||||
|
global.fetch = vi.fn().mockResolvedValue({
|
||||||
|
ok: false,
|
||||||
|
json: () => Promise.resolve({ error: "No notification channels configured" }),
|
||||||
|
});
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<DashboardPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
const sendButton = screen.getByText("dashboard.reorder.sendReminder");
|
||||||
|
fireEvent.click(sendButton);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(screen.getByText("No notification channels configured")).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("DashboardPage with past days", () => {
|
describe("DashboardPage with past days", () => {
|
||||||
@@ -819,3 +892,69 @@ describe("DashboardPage good stock state", () => {
|
|||||||
expect(screen.getByText(/dashboard\.reorder\.allGood/i)).toBeInTheDocument();
|
expect(screen.getByText(/dashboard\.reorder\.allGood/i)).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("DashboardPage bottle package type", () => {
|
||||||
|
const bottleMed = {
|
||||||
|
id: 3,
|
||||||
|
name: "Ibuprofen",
|
||||||
|
packageType: "bottle" as const,
|
||||||
|
packCount: 0,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 1,
|
||||||
|
looseTablets: 100,
|
||||||
|
totalPills: 200,
|
||||||
|
takenBy: [],
|
||||||
|
blisters: [{ usage: 2, every: 1, start: "2024-01-01T09:00:00Z" }],
|
||||||
|
intakeRemindersEnabled: false,
|
||||||
|
notes: null,
|
||||||
|
expiryDate: null,
|
||||||
|
imageUrl: null,
|
||||||
|
updatedAt: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
const bottleCoverage = {
|
||||||
|
name: "Ibuprofen",
|
||||||
|
medsLeft: 100,
|
||||||
|
daysLeft: 50,
|
||||||
|
depletionDate: "2025-04-01",
|
||||||
|
depletionTime: Date.now() + 50 * 86400000,
|
||||||
|
nextDose: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
localStorage.clear();
|
||||||
|
mockContextValue = createMockAppContext({
|
||||||
|
meds: [bottleMed],
|
||||||
|
coverage: { all: [bottleCoverage], low: [] },
|
||||||
|
coverageByMed: { Ibuprofen: bottleCoverage },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders pill count instead of blisters for bottle type", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<DashboardPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
// Should show medication name
|
||||||
|
expect(screen.getByText("Ibuprofen")).toBeInTheDocument();
|
||||||
|
|
||||||
|
// Should show pills count (bottle shows pillsCount, not blisters)
|
||||||
|
expect(screen.getByText(/table\.pillsCount/i)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows dash for stock details column for bottle type", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<DashboardPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
// For bottle type, the stock details column shows "—"
|
||||||
|
const dashElements = document.querySelectorAll('[data-label="table.stockDetails"]');
|
||||||
|
const bottleDetails = Array.from(dashElements).find((el) => el.textContent === "—");
|
||||||
|
expect(bottleDetails).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ const mockMeds = [
|
|||||||
id: 1,
|
id: 1,
|
||||||
name: "Aspirin",
|
name: "Aspirin",
|
||||||
genericName: "Acetylsalicylic acid",
|
genericName: "Acetylsalicylic acid",
|
||||||
|
packageType: "blister" as const,
|
||||||
packCount: 1,
|
packCount: 1,
|
||||||
blistersPerPack: 2,
|
blistersPerPack: 2,
|
||||||
pillsPerBlister: 10,
|
pillsPerBlister: 10,
|
||||||
@@ -25,6 +26,7 @@ const mockMeds = [
|
|||||||
id: 2,
|
id: 2,
|
||||||
name: "Vitamin D",
|
name: "Vitamin D",
|
||||||
genericName: null,
|
genericName: null,
|
||||||
|
packageType: "blister" as const,
|
||||||
packCount: 0,
|
packCount: 0,
|
||||||
blistersPerPack: 1,
|
blistersPerPack: 1,
|
||||||
pillsPerBlister: 30,
|
pillsPerBlister: 30,
|
||||||
@@ -755,9 +757,9 @@ describe("MedicationsPage intake reminders toggle", () => {
|
|||||||
// Desktop form uses class "full blisters" container
|
// Desktop form uses class "full blisters" container
|
||||||
const blistersContainer = document.querySelector(".blisters");
|
const blistersContainer = document.querySelector(".blisters");
|
||||||
expect(blistersContainer).toBeInTheDocument();
|
expect(blistersContainer).toBeInTheDocument();
|
||||||
// Check for the inline-checkbox that controls intake reminders in each blister row
|
// Check for the remind-toggle-row that controls intake reminders in each blister row
|
||||||
const intakeCheckbox = document.querySelector(".blister-row .inline-checkbox");
|
const intakeToggle = document.querySelector(".blister-row .remind-toggle-row");
|
||||||
expect(intakeCheckbox).toBeInTheDocument();
|
expect(intakeToggle).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("can toggle intake reminders per intake", () => {
|
it("can toggle intake reminders per intake", () => {
|
||||||
@@ -770,8 +772,8 @@ describe("MedicationsPage intake reminders toggle", () => {
|
|||||||
</MemoryRouter>
|
</MemoryRouter>
|
||||||
);
|
);
|
||||||
|
|
||||||
// Each blister row has inline-checkbox for intake reminders
|
// Each blister row has remind-toggle-row for intake reminders
|
||||||
const checkbox = document.querySelector('.blister-row .inline-checkbox input[type="checkbox"]');
|
const checkbox = document.querySelector('.blister-row .remind-toggle-row input[type="checkbox"]');
|
||||||
if (checkbox) {
|
if (checkbox) {
|
||||||
fireEvent.click(checkbox);
|
fireEvent.click(checkbox);
|
||||||
expect(setIntakeValue).toHaveBeenCalled();
|
expect(setIntakeValue).toHaveBeenCalled();
|
||||||
@@ -1442,4 +1444,177 @@ describe("MedicationsPage form saved state", () => {
|
|||||||
|
|
||||||
expect(screen.getByText(/common\.saved/i)).toBeInTheDocument();
|
expect(screen.getByText(/common\.saved/i)).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("shows stock overflow warning when medsLeft exceeds package size", () => {
|
||||||
|
const overflowMed = {
|
||||||
|
...mockMeds[0],
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 10,
|
||||||
|
looseTablets: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
mockContextValue = createMockContext({
|
||||||
|
meds: [overflowMed],
|
||||||
|
coverageByMed: {
|
||||||
|
[overflowMed.name]: {
|
||||||
|
name: overflowMed.name,
|
||||||
|
medsLeft: 25,
|
||||||
|
daysLeft: 25,
|
||||||
|
depletionDate: "2024-02-01",
|
||||||
|
depletionTime: Date.now() + 25 * 86400000,
|
||||||
|
nextDose: null,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<MedicationsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
// packageSize = 1*1*10 + 0 = 10, medsLeft = 25 > 10 → warning shown
|
||||||
|
const warningIcon = document.querySelector(".med-total .info-tooltip.tooltip-align-left.warning-text");
|
||||||
|
expect(warningIcon).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not show stock overflow warning when stock is within capacity", () => {
|
||||||
|
const normalMed = {
|
||||||
|
...mockMeds[0],
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 30,
|
||||||
|
looseTablets: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
mockContextValue = createMockContext({
|
||||||
|
meds: [normalMed],
|
||||||
|
coverageByMed: {
|
||||||
|
[normalMed.name]: {
|
||||||
|
name: normalMed.name,
|
||||||
|
medsLeft: 20,
|
||||||
|
daysLeft: 20,
|
||||||
|
depletionDate: "2024-02-01",
|
||||||
|
depletionTime: Date.now() + 20 * 86400000,
|
||||||
|
nextDose: null,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<MedicationsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
// packageSize = 30, medsLeft = 20 < 30 → no warning
|
||||||
|
const warningIcon = document.querySelector(".med-total .info-tooltip.tooltip-align-left.warning-text");
|
||||||
|
expect(warningIcon).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("MedicationsPage bottle package type", () => {
|
||||||
|
const bottleMed = {
|
||||||
|
id: 3,
|
||||||
|
name: "Ibuprofen",
|
||||||
|
genericName: null,
|
||||||
|
packageType: "bottle" as const,
|
||||||
|
packCount: 0,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 1,
|
||||||
|
looseTablets: 150,
|
||||||
|
totalPills: 200,
|
||||||
|
takenBy: [],
|
||||||
|
blisters: [{ usage: 1, every: 1, start: "2024-01-01T09:00:00Z" }],
|
||||||
|
intakeRemindersEnabled: false,
|
||||||
|
notes: null,
|
||||||
|
expiryDate: null,
|
||||||
|
imageUrl: null,
|
||||||
|
updatedAt: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
localStorage.clear();
|
||||||
|
mockContextValue = createMockContext({ meds: [bottleMed] });
|
||||||
|
mockFormHookValue = createMockFormHook();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows bottle type and capacity instead of blister fields in med-details", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<MedicationsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
const medDetails = document.querySelector(".med-details");
|
||||||
|
expect(medDetails).toBeInTheDocument();
|
||||||
|
|
||||||
|
// Should show type and capacity for bottle
|
||||||
|
expect(medDetails!.textContent).toContain("form.packageTypeBottle");
|
||||||
|
expect(medDetails!.textContent).toContain("medications.details.totalCapacity");
|
||||||
|
|
||||||
|
// Should NOT show blister-specific fields
|
||||||
|
expect(medDetails!.textContent).not.toContain("medications.details.blisters");
|
||||||
|
expect(medDetails!.textContent).not.toContain("medications.details.pillsPerBlister");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows pills-only refill form for bottle type when editing", () => {
|
||||||
|
mockFormHookValue = createMockFormHook({
|
||||||
|
editingId: 3,
|
||||||
|
form: {
|
||||||
|
...createMockFormHook().form,
|
||||||
|
packageType: "bottle" as const,
|
||||||
|
totalPills: "200",
|
||||||
|
looseTablets: "150",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
mockContextValue = createMockContext({ meds: [bottleMed] });
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<MedicationsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
// Should show "pillsToAdd" label for bottle
|
||||||
|
expect(screen.getByText(/refill\.pillsToAdd/i)).toBeInTheDocument();
|
||||||
|
|
||||||
|
// Should NOT show "packs" label in refill
|
||||||
|
const refillSection = document.querySelector(".refill-section");
|
||||||
|
expect(refillSection).toBeInTheDocument();
|
||||||
|
expect(refillSection!.textContent).not.toContain("refill.packs");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("MedicationsPage blister refill shows packs", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
localStorage.clear();
|
||||||
|
mockContextValue = createMockContext({ meds: mockMeds });
|
||||||
|
mockFormHookValue = createMockFormHook({
|
||||||
|
editingId: 1,
|
||||||
|
form: {
|
||||||
|
...createMockFormHook().form,
|
||||||
|
packageType: "blister" as const,
|
||||||
|
packCount: "1",
|
||||||
|
blistersPerPack: "2",
|
||||||
|
pillsPerBlister: "10",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows packs and loose pills refill fields for blister type", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<MedicationsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
const refillSection = document.querySelector(".refill-section");
|
||||||
|
expect(refillSection).toBeInTheDocument();
|
||||||
|
expect(refillSection!.textContent).toContain("refill.packs");
|
||||||
|
expect(refillSection!.textContent).toContain("refill.loosePills");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { fireEvent, render, screen } from "@testing-library/react";
|
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||||
import { MemoryRouter } from "react-router-dom";
|
import { MemoryRouter } from "react-router-dom";
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { PlannerPage } from "../../pages/PlannerPage";
|
import { PlannerPage } from "../../pages/PlannerPage";
|
||||||
@@ -481,3 +481,101 @@ describe("PlannerPage medication detail", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("PlannerPage bottle package type", () => {
|
||||||
|
const bottlePlannerRows = [
|
||||||
|
{
|
||||||
|
medicationId: 3,
|
||||||
|
medicationName: "Ibuprofen",
|
||||||
|
totalPills: 60,
|
||||||
|
plannerUsage: 20,
|
||||||
|
blisterSize: 1,
|
||||||
|
blistersNeeded: 0,
|
||||||
|
fullBlisters: 0,
|
||||||
|
loosePills: 20,
|
||||||
|
enough: true,
|
||||||
|
packageType: "bottle" as const,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const blisterPlannerRows = [
|
||||||
|
{
|
||||||
|
medicationId: 1,
|
||||||
|
medicationName: "Aspirin",
|
||||||
|
totalPills: 60,
|
||||||
|
plannerUsage: 20,
|
||||||
|
blisterSize: 10,
|
||||||
|
blistersNeeded: 2,
|
||||||
|
fullBlisters: 2,
|
||||||
|
loosePills: 0,
|
||||||
|
enough: true,
|
||||||
|
packageType: "blister" as const,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
localStorage.clear();
|
||||||
|
mockContextValue = createMockContext({ meds: mockMeds });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows dash for blisters column when bottle type", async () => {
|
||||||
|
global.fetch = vi.fn().mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: () => Promise.resolve(bottlePlannerRows),
|
||||||
|
});
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<PlannerPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
// Submit the form to trigger the planner calculation
|
||||||
|
const form = document.querySelector("form.planner");
|
||||||
|
expect(form).toBeInTheDocument();
|
||||||
|
await act(async () => {
|
||||||
|
fireEvent.submit(form!);
|
||||||
|
});
|
||||||
|
|
||||||
|
// For bottle type, blisters column should show "–"
|
||||||
|
await waitFor(() => {
|
||||||
|
const tableRows = document.querySelectorAll(".table-row");
|
||||||
|
expect(tableRows.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
const tableRows = document.querySelectorAll(".table-row");
|
||||||
|
const bottleRow = Array.from(tableRows).find((row) => row.textContent?.includes("Ibuprofen"));
|
||||||
|
expect(bottleRow).toBeTruthy();
|
||||||
|
expect(bottleRow!.textContent).toContain("–");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows blisters calculation for blister type", async () => {
|
||||||
|
global.fetch = vi.fn().mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: () => Promise.resolve(blisterPlannerRows),
|
||||||
|
});
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<PlannerPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
// Submit the form to trigger the planner calculation
|
||||||
|
const form = document.querySelector("form.planner");
|
||||||
|
expect(form).toBeInTheDocument();
|
||||||
|
await act(async () => {
|
||||||
|
fireEvent.submit(form!);
|
||||||
|
});
|
||||||
|
|
||||||
|
// For blister type, should show "2 × 10"
|
||||||
|
await waitFor(() => {
|
||||||
|
const tableRows = document.querySelectorAll(".table-row");
|
||||||
|
expect(tableRows.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
const tableRows = document.querySelectorAll(".table-row");
|
||||||
|
const blisterRow = Array.from(tableRows).find((row) => row.textContent?.includes("Aspirin"));
|
||||||
|
expect(blisterRow).toBeTruthy();
|
||||||
|
expect(blisterRow!.textContent).toContain("2 × 10");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ const createMockContext = (overrides = {}) => ({
|
|||||||
skipReminderIfTaken: true,
|
skipReminderIfTaken: true,
|
||||||
skipRemindersForTakenDoses: false,
|
skipRemindersForTakenDoses: false,
|
||||||
stockCalculationMode: "automatic",
|
stockCalculationMode: "automatic",
|
||||||
|
shareStockStatus: true,
|
||||||
stockCheckTime: "08:00",
|
stockCheckTime: "08:00",
|
||||||
intakeReminderTime: "09:00",
|
intakeReminderTime: "09:00",
|
||||||
},
|
},
|
||||||
@@ -635,6 +636,58 @@ describe("SettingsPage stock calculation mode", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("SettingsPage share stock status", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
mockContextValue = createMockContext({
|
||||||
|
settings: {
|
||||||
|
...createMockContext().settings,
|
||||||
|
shareStockStatus: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders share stock status toggle", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByText(/settings\.stock\.shareStockStatus$/)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("toggles share stock status setting", () => {
|
||||||
|
const setSettings = vi.fn();
|
||||||
|
mockContextValue = createMockContext({
|
||||||
|
setSettings,
|
||||||
|
settings: {
|
||||||
|
...createMockContext().settings,
|
||||||
|
shareStockStatus: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
// Find the toggle by its associated label text
|
||||||
|
const label = screen.getByText(/settings\.stock\.shareStockStatus$/);
|
||||||
|
const settingRow = label.closest(".setting-row");
|
||||||
|
const checkbox = settingRow?.querySelector('input[type="checkbox"]') as HTMLInputElement;
|
||||||
|
|
||||||
|
expect(checkbox).toBeTruthy();
|
||||||
|
expect(checkbox.checked).toBe(true);
|
||||||
|
|
||||||
|
// Toggle it off
|
||||||
|
fireEvent.click(checkbox);
|
||||||
|
|
||||||
|
expect(setSettings).toHaveBeenCalledWith(expect.objectContaining({ shareStockStatus: false }));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("SettingsPage repeat reminders", () => {
|
describe("SettingsPage repeat reminders", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
@@ -898,7 +951,7 @@ describe("SettingsPage schedule overview", () => {
|
|||||||
</MemoryRouter>
|
</MemoryRouter>
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(screen.getByText(/settings\.schedule\.lastSent/i)).toBeInTheDocument();
|
expect(screen.getByText(/settings\.schedule\.lastIntakeSent/i)).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -964,7 +1017,8 @@ describe("SettingsPage stock display thresholds", () => {
|
|||||||
</MemoryRouter>
|
</MemoryRouter>
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(screen.getByText(/settings\.stock\.lowStockDays/i)).toBeInTheDocument();
|
// Low stock is now shown as a chip label, not plain text
|
||||||
|
expect(screen.getByText(/status\.lowStock/i)).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("shows high stock days input", () => {
|
it("shows high stock days input", () => {
|
||||||
@@ -974,7 +1028,8 @@ describe("SettingsPage stock display thresholds", () => {
|
|||||||
</MemoryRouter>
|
</MemoryRouter>
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(screen.getByText(/settings\.stock\.highStockDays/i)).toBeInTheDocument();
|
// High stock is now shown as a chip label, not plain text
|
||||||
|
expect(screen.getByText(/status\.highStock/i)).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("allows changing high stock days", () => {
|
it("allows changing high stock days", () => {
|
||||||
@@ -1011,14 +1066,14 @@ describe("SettingsPage repeat daily reminders", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("shows repeat daily reminders toggle", () => {
|
it("shows repeat daily reminders toggle in notifications", () => {
|
||||||
render(
|
render(
|
||||||
<MemoryRouter>
|
<MemoryRouter>
|
||||||
<SettingsPage />
|
<SettingsPage />
|
||||||
</MemoryRouter>
|
</MemoryRouter>
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(screen.getByText(/settings\.stock\.repeatDaily/i)).toBeInTheDocument();
|
expect(screen.getByText(/settings\.stockReminder\.repeatDaily/i)).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1154,6 +1209,237 @@ describe("SettingsPage importing state", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("SettingsPage stock threshold chips", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
mockContextValue = createMockContext();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders Critical stock chip", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
// Critical chip appears in both Stock Thresholds and Notification trigger
|
||||||
|
const criticalChips = screen.getAllByText(/status\.criticalStock/i);
|
||||||
|
expect(criticalChips.length).toBeGreaterThanOrEqual(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders Low stock chip", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByText(/status\.lowStock/i)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders High stock chip", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByText(/status\.highStock/i)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders stock calculation mode first in stock card", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByText(/settings\.stock\.calculationMode/i)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders thresholds section header", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByText(/settings\.stock\.thresholds/i)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders three threshold inputs (Critical, Low, High)", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
// Should have a threshold-chips-group with 3 labels
|
||||||
|
const chipGroup = document.querySelector(".threshold-chips-group");
|
||||||
|
expect(chipGroup).toBeInTheDocument();
|
||||||
|
const inputs = chipGroup?.querySelectorAll('input[type="number"]');
|
||||||
|
expect(inputs?.length).toBe(3);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("SettingsPage stock threshold validation", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows validation error when Critical >= Low", () => {
|
||||||
|
mockContextValue = createMockContext({
|
||||||
|
settings: {
|
||||||
|
...createMockContext().settings,
|
||||||
|
reminderDaysBefore: 30,
|
||||||
|
lowStockDays: 30,
|
||||||
|
highStockDays: 180,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByText(/settings\.stock\.thresholdValidation/i)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("shows validation error when Low >= High", () => {
|
||||||
|
mockContextValue = createMockContext({
|
||||||
|
settings: {
|
||||||
|
...createMockContext().settings,
|
||||||
|
reminderDaysBefore: 7,
|
||||||
|
lowStockDays: 200,
|
||||||
|
highStockDays: 180,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByText(/settings\.stock\.thresholdValidation/i)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not show validation error when thresholds are valid", () => {
|
||||||
|
mockContextValue = createMockContext({
|
||||||
|
settings: {
|
||||||
|
...createMockContext().settings,
|
||||||
|
reminderDaysBefore: 7,
|
||||||
|
lowStockDays: 30,
|
||||||
|
highStockDays: 180,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.queryByText(/settings\.stock\.thresholdValidation/i)).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("disables save button when thresholds are invalid", () => {
|
||||||
|
mockContextValue = createMockContext({
|
||||||
|
settings: {
|
||||||
|
...createMockContext().settings,
|
||||||
|
reminderDaysBefore: 30,
|
||||||
|
lowStockDays: 30,
|
||||||
|
highStockDays: 180,
|
||||||
|
},
|
||||||
|
settingsChanged: true,
|
||||||
|
settingsSaved: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
const submitBtn = document.querySelector('button[type="submit"]');
|
||||||
|
expect(submitBtn).toBeDisabled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("enables save button when thresholds are valid and changes exist", () => {
|
||||||
|
mockContextValue = createMockContext({
|
||||||
|
settings: {
|
||||||
|
...createMockContext().settings,
|
||||||
|
reminderDaysBefore: 7,
|
||||||
|
lowStockDays: 30,
|
||||||
|
highStockDays: 180,
|
||||||
|
},
|
||||||
|
settingsChanged: true,
|
||||||
|
settingsSaved: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
const submitBtn = document.querySelector('button[type="submit"]');
|
||||||
|
expect(submitBtn).not.toBeDisabled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("marks invalid threshold input with error styling", () => {
|
||||||
|
mockContextValue = createMockContext({
|
||||||
|
settings: {
|
||||||
|
...createMockContext().settings,
|
||||||
|
reminderDaysBefore: 30,
|
||||||
|
lowStockDays: 30,
|
||||||
|
highStockDays: 180,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
const invalidLabels = document.querySelectorAll(".threshold-invalid");
|
||||||
|
expect(invalidLabels.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("SettingsPage stock reminder in notifications", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
mockContextValue = createMockContext();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders stock reminder section in notifications card", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByText(/settings\.stockReminder\.title/i)).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders stock reminder description with Critical chip", () => {
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<SettingsPage />
|
||||||
|
</MemoryRouter>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByText(/settings\.stockReminder\.description/i)).toBeInTheDocument();
|
||||||
|
// Critical chip should appear next to the description text
|
||||||
|
const descLabel = screen.getByText(/settings\.stockReminder\.description/i);
|
||||||
|
const criticalChip = descLabel.querySelector(".status-chip.danger");
|
||||||
|
expect(criticalChip).toBeInTheDocument();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("SettingsPage no SMTP configured", () => {
|
describe("SettingsPage no SMTP configured", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
|
|||||||
@@ -59,6 +59,44 @@ describe("getMedTotal", () => {
|
|||||||
|
|
||||||
expect(getMedTotal(med)).toBe(0);
|
expect(getMedTotal(med)).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("calculates bottle type from looseTablets only", () => {
|
||||||
|
const med = {
|
||||||
|
packageType: "bottle" as const,
|
||||||
|
packCount: 0,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 1,
|
||||||
|
looseTablets: 150,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(getMedTotal(med)).toBe(150);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calculates bottle type with stock adjustment", () => {
|
||||||
|
const med = {
|
||||||
|
packageType: "bottle" as const,
|
||||||
|
packCount: 0,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 1,
|
||||||
|
looseTablets: 150,
|
||||||
|
stockAdjustment: -10,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(getMedTotal(med)).toBe(140); // 150 + (-10) = 140
|
||||||
|
});
|
||||||
|
|
||||||
|
it("ignores blister fields for bottle type", () => {
|
||||||
|
const med = {
|
||||||
|
packageType: "bottle" as const,
|
||||||
|
packCount: 5,
|
||||||
|
blistersPerPack: 10,
|
||||||
|
pillsPerBlister: 20,
|
||||||
|
looseTablets: 80,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Should use looseTablets only, NOT 5*10*20 + 80 = 1080
|
||||||
|
expect(getMedTotal(med)).toBe(80);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getPackageSize", () => {
|
describe("getPackageSize", () => {
|
||||||
@@ -84,6 +122,32 @@ describe("getPackageSize", () => {
|
|||||||
|
|
||||||
expect(getPackageSize(med)).toBe(10);
|
expect(getPackageSize(med)).toBe(10);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("returns looseTablets for bottle type", () => {
|
||||||
|
const med = {
|
||||||
|
packageType: "bottle" as const,
|
||||||
|
packCount: 0,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 1,
|
||||||
|
looseTablets: 200,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(getPackageSize(med)).toBe(200);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("ignores blister fields for bottle type", () => {
|
||||||
|
const med = {
|
||||||
|
packageType: "bottle" as const,
|
||||||
|
packCount: 5,
|
||||||
|
blistersPerPack: 10,
|
||||||
|
pillsPerBlister: 20,
|
||||||
|
looseTablets: 80,
|
||||||
|
stockAdjustment: 50,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Should use looseTablets only, ignore stockAdjustment and blister math
|
||||||
|
expect(getPackageSize(med)).toBe(80);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("FIELD_LIMITS", () => {
|
describe("FIELD_LIMITS", () => {
|
||||||
|
|||||||
@@ -289,6 +289,113 @@ describe("calculateCoverage", () => {
|
|||||||
expect(result.all[0].daysLeft).toBeNull();
|
expect(result.all[0].daysLeft).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("uses intakes format when available instead of blisters", () => {
|
||||||
|
// The new intakes format should be used for coverage calculation
|
||||||
|
// when med.intakes is present, falling through getBlistersForMed
|
||||||
|
const meds: Medication[] = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: "IntakesMed",
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 30,
|
||||||
|
looseTablets: 0,
|
||||||
|
takenBy: [],
|
||||||
|
blisters: [], // Empty blisters — intakes should be used instead
|
||||||
|
intakes: [
|
||||||
|
{
|
||||||
|
usage: 2,
|
||||||
|
every: 1,
|
||||||
|
start: "2024-03-10T09:00:00",
|
||||||
|
takenBy: null,
|
||||||
|
intakeRemindersEnabled: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
updatedAt: null,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = calculateCoverage(meds, [], "en", 7, "automatic", new Set());
|
||||||
|
|
||||||
|
expect(result.all).toHaveLength(1);
|
||||||
|
// 30 pills, 2 per day consumed. March 10 09:00 to March 15 12:00 = 6 occurrences × 2 = 12 consumed
|
||||||
|
expect(result.all[0].medsLeft).toBe(18);
|
||||||
|
expect(result.all[0].daysLeft).toBe(9); // 18 pills / 2 per day = 9 days
|
||||||
|
});
|
||||||
|
|
||||||
|
it("per-intake takenBy counts person correctly in automatic mode", () => {
|
||||||
|
// When intakes have per-intake takenBy, each person-intake pair is counted
|
||||||
|
const meds: Medication[] = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: "PersonMed",
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 60,
|
||||||
|
looseTablets: 0,
|
||||||
|
takenBy: ["Alice", "Bob"],
|
||||||
|
blisters: [],
|
||||||
|
intakes: [
|
||||||
|
{
|
||||||
|
usage: 1,
|
||||||
|
every: 1,
|
||||||
|
start: "2024-03-10T09:00:00",
|
||||||
|
takenBy: "Alice",
|
||||||
|
intakeRemindersEnabled: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
usage: 1,
|
||||||
|
every: 1,
|
||||||
|
start: "2024-03-10T09:00:00",
|
||||||
|
takenBy: "Bob",
|
||||||
|
intakeRemindersEnabled: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
updatedAt: null,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = calculateCoverage(meds, [], "en", 7, "automatic", new Set());
|
||||||
|
|
||||||
|
expect(result.all).toHaveLength(1);
|
||||||
|
// 2 intakes × 1 pill/day × 6 occurrences = 12 consumed
|
||||||
|
// dailyRate = 2 (1/day × 2 people)
|
||||||
|
// medsLeft = 60 - 12 = 48, daysLeft = 48 / 2 = 24
|
||||||
|
expect(result.all[0].medsLeft).toBe(48);
|
||||||
|
expect(result.all[0].daysLeft).toBe(24);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("automatic mode without stock correction counts from blister start", () => {
|
||||||
|
// Without stock correction, effectiveStart should be the blisterStart itself.
|
||||||
|
// This tests the `else` branch where effectiveStart = blisterStart.
|
||||||
|
const meds: Medication[] = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: "TestMed",
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 30,
|
||||||
|
looseTablets: 0,
|
||||||
|
takenBy: [],
|
||||||
|
blisters: [
|
||||||
|
{
|
||||||
|
usage: 1,
|
||||||
|
every: 1,
|
||||||
|
start: "2024-03-13T09:00:00", // 2 days ago + today = 3 occurrences
|
||||||
|
},
|
||||||
|
],
|
||||||
|
updatedAt: null,
|
||||||
|
// No lastStockCorrectionAt
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = calculateCoverage(meds, [], "en", 7, "automatic", new Set());
|
||||||
|
|
||||||
|
expect(result.all).toHaveLength(1);
|
||||||
|
// March 13, 14, 15 at 09:00 — all past (it's 12:00 on March 15) = 3 consumed
|
||||||
|
expect(result.all[0].medsLeft).toBe(27);
|
||||||
|
});
|
||||||
|
|
||||||
it("filters low stock medications", () => {
|
it("filters low stock medications", () => {
|
||||||
const meds: Medication[] = [
|
const meds: Medication[] = [
|
||||||
{
|
{
|
||||||
@@ -409,8 +516,9 @@ describe("calculateCoverage", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("stock correction with dose tracking data also reflects correctly", () => {
|
it("stock correction with dose tracking data also reflects correctly", () => {
|
||||||
// When the user has dose tracking data, the actualConsumed path is used.
|
// In automatic mode, dose tracking data is ignored — stock is always
|
||||||
// Verify that no phantom dose is generated right after a stock correction.
|
// reduced based on the schedule. Verify that tracked doses don't affect
|
||||||
|
// the calculation and that stock correction still resets the baseline.
|
||||||
const correctionTime = new Date("2024-03-15T12:00:00Z");
|
const correctionTime = new Date("2024-03-15T12:00:00Z");
|
||||||
const march14 = new Date("2024-03-14T00:00:00").getTime();
|
const march14 = new Date("2024-03-14T00:00:00").getTime();
|
||||||
|
|
||||||
@@ -437,20 +545,23 @@ describe("calculateCoverage", () => {
|
|||||||
];
|
];
|
||||||
|
|
||||||
// User has tracked a dose yesterday (before the correction)
|
// User has tracked a dose yesterday (before the correction)
|
||||||
|
// In automatic mode, this should be ignored — only the schedule matters.
|
||||||
const takenDoses = new Set([`1-0-${march14}`]);
|
const takenDoses = new Set([`1-0-${march14}`]);
|
||||||
|
|
||||||
const result = calculateCoverage(meds, [], "en", 7, "automatic", takenDoses);
|
const result = calculateCoverage(meds, [], "en", 7, "automatic", takenDoses);
|
||||||
|
|
||||||
expect(result.all).toHaveLength(1);
|
expect(result.all).toHaveLength(1);
|
||||||
// getMedTotal = 30 - 7 = 23.
|
// getMedTotal = 30 - 7 = 23.
|
||||||
// The taken dose from yesterday should NOT be counted (it's before the correction).
|
// Automatic mode ignores tracking data. After correction, consumption
|
||||||
// No new doses should exist since the correction just happened.
|
// restarts from correctionTime + period, which is in the future.
|
||||||
expect(result.all[0].medsLeft).toBe(23);
|
expect(result.all[0].medsLeft).toBe(23);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("stock correction consumption resumes after one full period", () => {
|
it("stock correction consumption resumes after one full period", () => {
|
||||||
// After 1 day (for daily medication), the next dose should be consumed.
|
// After correction, the next scheduled dose on the blister's grid should
|
||||||
// Set system time to 1 day + 1 hour after correction.
|
// be counted once its time arrives.
|
||||||
|
// Correction at March 14 12:00, blister start 08:00 daily →
|
||||||
|
// next dose after correction = March 15 08:00. Now is 13:00 on March 15 → 1 dose.
|
||||||
const correctionTime = new Date("2024-03-14T12:00:00Z");
|
const correctionTime = new Date("2024-03-14T12:00:00Z");
|
||||||
vi.setSystemTime(new Date("2024-03-15T13:00:00Z")); // 25 hours after correction
|
vi.setSystemTime(new Date("2024-03-15T13:00:00Z")); // 25 hours after correction
|
||||||
|
|
||||||
@@ -484,14 +595,205 @@ describe("calculateCoverage", () => {
|
|||||||
expect(result.all[0].medsLeft).toBe(22);
|
expect(result.all[0].medsLeft).toBe(22);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("manual mode: stock correction excludes same-day taken doses", () => {
|
it("stock correction aligns to schedule grid, not correction timestamp", () => {
|
||||||
// BUG FIX: In manual mode, doses taken on the same day as a stock correction
|
// BUG: When correction happened just before a scheduled dose (e.g. 15:40
|
||||||
// were counted as consumed (>= comparison with date-only timestamps).
|
// correction, 15:42 dose), the old code added 1 full period to the correction
|
||||||
// The user already accounted for today's consumption when setting the stock count.
|
// time (15:40 + 24h = tomorrow 15:40), missing today's 15:42 dose entirely.
|
||||||
//
|
// FIX: Align effectiveStart to the blister's schedule grid so that the first
|
||||||
// Scenario: User has 110 pills, took 1 dose today, corrects to 111.
|
// dose counted is the next one on the schedule after the correction.
|
||||||
// Bug: medsLeft = 111 - 1 = 110 (today's dose counted)
|
const correctionTime = new Date("2024-03-14T15:40:00Z"); // 2 min before dose
|
||||||
// Fix: medsLeft = 111 - 0 = 111 (today's dose excluded)
|
vi.setSystemTime(new Date("2024-03-14T15:45:00Z")); // 5 min after correction, 3 min after dose
|
||||||
|
|
||||||
|
const meds: Medication[] = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: "TestMed",
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 30,
|
||||||
|
looseTablets: 0,
|
||||||
|
stockAdjustment: -5, // 30 - 5 = 25 pills
|
||||||
|
lastStockCorrectionAt: correctionTime.toISOString(),
|
||||||
|
takenBy: [],
|
||||||
|
blisters: [
|
||||||
|
{
|
||||||
|
usage: 1,
|
||||||
|
every: 1,
|
||||||
|
start: "2024-03-01T15:42:00Z", // Daily at 15:42
|
||||||
|
},
|
||||||
|
],
|
||||||
|
updatedAt: correctionTime.toISOString(),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = calculateCoverage(meds, [], "en", 7, "automatic", new Set());
|
||||||
|
|
||||||
|
expect(result.all).toHaveLength(1);
|
||||||
|
// Correction at 15:40, dose at 15:42, now at 15:45.
|
||||||
|
// The 15:42 dose is AFTER the correction → should be counted.
|
||||||
|
// medsLeft = 25 - 1 = 24
|
||||||
|
expect(result.all[0].medsLeft).toBe(24);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("stock correction shortly after a dose does not count that dose again", () => {
|
||||||
|
// If correction happens shortly AFTER a dose, that dose is already reflected
|
||||||
|
// in the stock count and should NOT be counted again.
|
||||||
|
const correctionTime = new Date("2024-03-14T15:45:00Z"); // 3 min AFTER the 15:42 dose
|
||||||
|
vi.setSystemTime(new Date("2024-03-14T16:00:00Z")); // 15 min after correction
|
||||||
|
|
||||||
|
const meds: Medication[] = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: "TestMed",
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 30,
|
||||||
|
looseTablets: 0,
|
||||||
|
stockAdjustment: -5,
|
||||||
|
lastStockCorrectionAt: correctionTime.toISOString(),
|
||||||
|
takenBy: [],
|
||||||
|
blisters: [
|
||||||
|
{
|
||||||
|
usage: 1,
|
||||||
|
every: 1,
|
||||||
|
start: "2024-03-01T15:42:00Z", // Daily at 15:42
|
||||||
|
},
|
||||||
|
],
|
||||||
|
updatedAt: correctionTime.toISOString(),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = calculateCoverage(meds, [], "en", 7, "automatic", new Set());
|
||||||
|
|
||||||
|
expect(result.all).toHaveLength(1);
|
||||||
|
// Correction at 15:45, after today's 15:42 dose → next dose is TOMORROW 15:42.
|
||||||
|
// Now is 16:00 today → next dose hasn't arrived yet → 0 consumed.
|
||||||
|
// medsLeft = 25
|
||||||
|
expect(result.all[0].medsLeft).toBe(25);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("automatic mode ignores past dose tracking data", () => {
|
||||||
|
// Automatic mode uses time-based expected consumption for past doses.
|
||||||
|
// Even if a user marks only some past doses as taken, the stock should still
|
||||||
|
// decrease for ALL scheduled doses whose time has passed.
|
||||||
|
const meds: Medication[] = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: "TestMed",
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 30,
|
||||||
|
looseTablets: 0,
|
||||||
|
takenBy: [],
|
||||||
|
blisters: [
|
||||||
|
{
|
||||||
|
usage: 1,
|
||||||
|
every: 1,
|
||||||
|
start: "2024-03-10T09:00:00",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
updatedAt: null,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// System time is March 15 12:00, start is 09:00 → 6 occurrences (March 10-15)
|
||||||
|
const march10 = new Date("2024-03-10T00:00:00").getTime();
|
||||||
|
const march11 = new Date("2024-03-11T00:00:00").getTime();
|
||||||
|
|
||||||
|
// User only marked 2 out of 6 past doses as taken
|
||||||
|
const takenDoses = new Set([`1-0-${march10}`, `1-0-${march11}`]);
|
||||||
|
|
||||||
|
const resultWithTracking = calculateCoverage(meds, [], "en", 7, "automatic", takenDoses);
|
||||||
|
const resultWithoutTracking = calculateCoverage(meds, [], "en", 7, "automatic", new Set());
|
||||||
|
|
||||||
|
// Both should have the same medsLeft — past tracking data doesn't reduce extra
|
||||||
|
expect(resultWithTracking.all[0].medsLeft).toBe(resultWithoutTracking.all[0].medsLeft);
|
||||||
|
// 30 pills - 6 consumed = 24
|
||||||
|
expect(resultWithTracking.all[0].medsLeft).toBe(24);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("automatic mode counts early-taken future doses", () => {
|
||||||
|
// If a user marks a dose as taken BEFORE the scheduled time,
|
||||||
|
// it should count as consumed immediately (early intake).
|
||||||
|
// System time is March 15 12:00, intake at 21:00 → today's dose not yet auto-consumed
|
||||||
|
vi.setSystemTime(new Date("2024-03-15T12:00:00Z"));
|
||||||
|
|
||||||
|
const meds: Medication[] = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: "TestMed",
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 30,
|
||||||
|
looseTablets: 0,
|
||||||
|
takenBy: [],
|
||||||
|
blisters: [
|
||||||
|
{
|
||||||
|
usage: 1,
|
||||||
|
every: 1,
|
||||||
|
start: "2024-03-10T21:00:00", // 21:00 = after current time 12:00
|
||||||
|
},
|
||||||
|
],
|
||||||
|
updatedAt: null,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// 5 occurrences auto-consumed: March 10-14 (all at 21:00, which is past)
|
||||||
|
// March 15 at 21:00 hasn't passed yet (it's only 12:00)
|
||||||
|
const resultNoTracking = calculateCoverage(meds, [], "en", 7, "automatic", new Set());
|
||||||
|
expect(resultNoTracking.all[0].medsLeft).toBe(25); // 30 - 5 = 25
|
||||||
|
|
||||||
|
// User marks today's (March 15) dose as taken early at 12:00
|
||||||
|
const march15 = new Date("2024-03-15T00:00:00").getTime();
|
||||||
|
const takenDoses = new Set([`1-0-${march15}`]);
|
||||||
|
|
||||||
|
const resultEarlyTaken = calculateCoverage(meds, [], "en", 7, "automatic", takenDoses);
|
||||||
|
// 5 auto + 1 early = 6 consumed → 30 - 6 = 24
|
||||||
|
expect(resultEarlyTaken.all[0].medsLeft).toBe(24);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("automatic mode does not double-count after intake time passes", () => {
|
||||||
|
// After the scheduled time, the dose is auto-consumed.
|
||||||
|
// If it was also marked as taken (earlier), it should NOT be counted twice.
|
||||||
|
vi.setSystemTime(new Date("2024-03-15T22:00:00Z")); // After 21:00
|
||||||
|
|
||||||
|
const meds: Medication[] = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: "TestMed",
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 30,
|
||||||
|
looseTablets: 0,
|
||||||
|
takenBy: [],
|
||||||
|
blisters: [
|
||||||
|
{
|
||||||
|
usage: 1,
|
||||||
|
every: 1,
|
||||||
|
start: "2024-03-10T21:00:00",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
updatedAt: null,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// 6 occurrences auto-consumed: March 10-15 (all at 21:00, now it's 22:00)
|
||||||
|
const march15 = new Date("2024-03-15T00:00:00").getTime();
|
||||||
|
const march14 = new Date("2024-03-14T00:00:00").getTime();
|
||||||
|
// User marked March 14 and 15 as taken (both already auto-consumed by now)
|
||||||
|
const takenDoses = new Set([`1-0-${march14}`, `1-0-${march15}`]);
|
||||||
|
|
||||||
|
const resultTracked = calculateCoverage(meds, [], "en", 7, "automatic", takenDoses);
|
||||||
|
const resultNoTracking = calculateCoverage(meds, [], "en", 7, "automatic", new Set());
|
||||||
|
|
||||||
|
// Both should be 24 (30 - 6). No double counting!
|
||||||
|
expect(resultTracked.all[0].medsLeft).toBe(24);
|
||||||
|
expect(resultNoTracking.all[0].medsLeft).toBe(24);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("manual mode: dose taken BEFORE stock correction is excluded", () => {
|
||||||
|
// When a user corrects stock, any dose marked BEFORE the correction
|
||||||
|
// is already reflected in the corrected count and should NOT be counted.
|
||||||
const correctionTime = new Date("2024-03-15T12:00:00Z");
|
const correctionTime = new Date("2024-03-15T12:00:00Z");
|
||||||
const todayMidnight = new Date("2024-03-15T00:00:00").getTime();
|
const todayMidnight = new Date("2024-03-15T00:00:00").getTime();
|
||||||
|
|
||||||
@@ -517,17 +819,59 @@ describe("calculateCoverage", () => {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
// User took a dose today (before the correction)
|
// User took a dose today at 10am (BEFORE the correction at 12pm)
|
||||||
const takenDoses = new Set([`1-0-${todayMidnight}`]);
|
const doseId = `1-0-${todayMidnight}`;
|
||||||
|
const takenDoses = new Set([doseId]);
|
||||||
|
const takenDoseTimestamps = new Map([[doseId, new Date("2024-03-15T10:00:00Z").getTime()]]);
|
||||||
|
|
||||||
const result = calculateCoverage(meds, [], "en", 7, "manual", takenDoses);
|
const result = calculateCoverage(meds, [], "en", 7, "manual", takenDoses, takenDoseTimestamps);
|
||||||
|
|
||||||
expect(result.all).toHaveLength(1);
|
expect(result.all).toHaveLength(1);
|
||||||
// getMedTotal = 196 - 85 = 111
|
// getMedTotal = 196 - 85 = 111
|
||||||
// Today's taken dose should NOT be counted (same day as correction)
|
// Dose was taken BEFORE correction → NOT counted
|
||||||
expect(result.all[0].medsLeft).toBe(111);
|
expect(result.all[0].medsLeft).toBe(111);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("manual mode: dose taken AFTER stock correction is counted", () => {
|
||||||
|
// When a user corrects stock and then takes a dose, that dose SHOULD be counted.
|
||||||
|
const correctionTime = new Date("2024-03-15T12:00:00Z");
|
||||||
|
const todayMidnight = new Date("2024-03-15T00:00:00").getTime();
|
||||||
|
|
||||||
|
const meds: Medication[] = [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: "DailyMed",
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 14,
|
||||||
|
pillsPerBlister: 14,
|
||||||
|
looseTablets: 0,
|
||||||
|
stockAdjustment: -85, // 196 - 85 = 111 pills
|
||||||
|
lastStockCorrectionAt: correctionTime.toISOString(),
|
||||||
|
takenBy: [],
|
||||||
|
blisters: [
|
||||||
|
{
|
||||||
|
usage: 1,
|
||||||
|
every: 1,
|
||||||
|
start: "2024-01-01T08:00:00",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
updatedAt: correctionTime.toISOString(),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// User took a dose today at 2pm (AFTER the correction at 12pm)
|
||||||
|
const doseId = `1-0-${todayMidnight}`;
|
||||||
|
const takenDoses = new Set([doseId]);
|
||||||
|
const takenDoseTimestamps = new Map([[doseId, new Date("2024-03-15T14:00:00Z").getTime()]]);
|
||||||
|
|
||||||
|
const result = calculateCoverage(meds, [], "en", 7, "manual", takenDoses, takenDoseTimestamps);
|
||||||
|
|
||||||
|
expect(result.all).toHaveLength(1);
|
||||||
|
// getMedTotal = 196 - 85 = 111
|
||||||
|
// Dose was taken AFTER correction → counted → 111 - 1 = 110
|
||||||
|
expect(result.all[0].medsLeft).toBe(110);
|
||||||
|
});
|
||||||
|
|
||||||
it("manual mode: stock correction counts next-day taken doses", () => {
|
it("manual mode: stock correction counts next-day taken doses", () => {
|
||||||
// After a stock correction, doses taken the next day SHOULD be counted.
|
// After a stock correction, doses taken the next day SHOULD be counted.
|
||||||
const correctionTime = new Date("2024-03-14T12:00:00Z");
|
const correctionTime = new Date("2024-03-14T12:00:00Z");
|
||||||
@@ -555,14 +899,16 @@ describe("calculateCoverage", () => {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
// User takes dose on March 15 (day after correction on March 14)
|
// User takes dose on March 15 at 8am (day after correction on March 14)
|
||||||
const takenDoses = new Set([`1-0-${march15Midnight}`]);
|
const doseId = `1-0-${march15Midnight}`;
|
||||||
|
const takenDoses = new Set([doseId]);
|
||||||
|
const takenDoseTimestamps = new Map([[doseId, new Date("2024-03-15T08:00:00Z").getTime()]]);
|
||||||
|
|
||||||
const result = calculateCoverage(meds, [], "en", 7, "manual", takenDoses);
|
const result = calculateCoverage(meds, [], "en", 7, "manual", takenDoses, takenDoseTimestamps);
|
||||||
|
|
||||||
expect(result.all).toHaveLength(1);
|
expect(result.all).toHaveLength(1);
|
||||||
// getMedTotal = 30 - 7 = 23
|
// getMedTotal = 30 - 7 = 23
|
||||||
// March 15 dose should be counted (day after correction)
|
// March 15 dose should be counted (taken after correction)
|
||||||
expect(result.all[0].medsLeft).toBe(22);
|
expect(result.all[0].medsLeft).toBe(22);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -592,9 +938,16 @@ describe("calculateCoverage", () => {
|
|||||||
];
|
];
|
||||||
|
|
||||||
// User took doses on March 14 and 15
|
// User took doses on March 14 and 15
|
||||||
const takenDoses = new Set([`1-0-${march14Midnight}`, `1-0-${march15Midnight}`]);
|
const doseId1 = `1-0-${march14Midnight}`;
|
||||||
|
const doseId2 = `1-0-${march15Midnight}`;
|
||||||
|
const takenDoses = new Set([doseId1, doseId2]);
|
||||||
|
// No stock correction → takenAt doesn't matter, but provide for completeness
|
||||||
|
const takenDoseTimestamps = new Map([
|
||||||
|
[doseId1, new Date("2024-03-14T08:00:00Z").getTime()],
|
||||||
|
[doseId2, new Date("2024-03-15T08:00:00Z").getTime()],
|
||||||
|
]);
|
||||||
|
|
||||||
const result = calculateCoverage(meds, [], "en", 7, "manual", takenDoses);
|
const result = calculateCoverage(meds, [], "en", 7, "manual", takenDoses, takenDoseTimestamps);
|
||||||
|
|
||||||
expect(result.all).toHaveLength(1);
|
expect(result.all).toHaveLength(1);
|
||||||
// Both doses should be counted: medsLeft = 30 - 2 = 28
|
// Both doses should be counted: medsLeft = 30 - 2 = 28
|
||||||
@@ -603,7 +956,7 @@ describe("calculateCoverage", () => {
|
|||||||
|
|
||||||
it("manual mode: stock correction with multiple medications", () => {
|
it("manual mode: stock correction with multiple medications", () => {
|
||||||
// Regression test: 3 medications (daily, daily, weekly).
|
// Regression test: 3 medications (daily, daily, weekly).
|
||||||
// Stock correction on all 3. The daily ones have same-day taken doses.
|
// Stock correction on all 3. Daily meds have doses taken BEFORE correction.
|
||||||
const correctionTime = new Date("2024-03-15T12:00:00Z");
|
const correctionTime = new Date("2024-03-15T12:00:00Z");
|
||||||
const todayMidnight = new Date("2024-03-15T00:00:00").getTime();
|
const todayMidnight = new Date("2024-03-15T00:00:00").getTime();
|
||||||
|
|
||||||
@@ -649,21 +1002,113 @@ describe("calculateCoverage", () => {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
// Daily meds have same-day taken doses, weekly med does not
|
// Daily meds have same-day doses taken BEFORE correction (at 8am, correction at 12pm)
|
||||||
const takenDoses = new Set([`1-0-${todayMidnight}`, `2-0-${todayMidnight}`]);
|
const doseId1 = `1-0-${todayMidnight}`;
|
||||||
|
const doseId2 = `2-0-${todayMidnight}`;
|
||||||
|
const takenDoses = new Set([doseId1, doseId2]);
|
||||||
|
const takenDoseTimestamps = new Map([
|
||||||
|
[doseId1, new Date("2024-03-15T08:00:00Z").getTime()], // Before correction
|
||||||
|
[doseId2, new Date("2024-03-15T09:00:00Z").getTime()], // Before correction
|
||||||
|
]);
|
||||||
|
|
||||||
const result = calculateCoverage(meds, [], "en", 7, "manual", takenDoses);
|
const result = calculateCoverage(meds, [], "en", 7, "manual", takenDoses, takenDoseTimestamps);
|
||||||
|
|
||||||
expect(result.all).toHaveLength(3);
|
expect(result.all).toHaveLength(3);
|
||||||
const daily1 = result.all.find((c) => c.name === "DailyMed1")!;
|
const daily1 = result.all.find((c) => c.name === "DailyMed1")!;
|
||||||
const daily2 = result.all.find((c) => c.name === "DailyMed2")!;
|
const daily2 = result.all.find((c) => c.name === "DailyMed2")!;
|
||||||
const weekly = result.all.find((c) => c.name === "WeeklyMed")!;
|
const weekly = result.all.find((c) => c.name === "WeeklyMed")!;
|
||||||
|
|
||||||
// All three should reflect full stock (same-day doses excluded)
|
// All three should reflect full stock (doses taken before correction → excluded)
|
||||||
expect(daily1.medsLeft).toBe(111);
|
expect(daily1.medsLeft).toBe(111);
|
||||||
expect(daily2.medsLeft).toBe(20);
|
expect(daily2.medsLeft).toBe(20);
|
||||||
expect(weekly.medsLeft).toBe(8);
|
expect(weekly.medsLeft).toBe(8);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("manual mode: person-suffix dose IDs are counted correctly", () => {
|
||||||
|
// BUG HUNT: In prod (manual mode), dose IDs have a person suffix like
|
||||||
|
// "31-0-1770505200000-Daniel". Does the manual mode code correctly parse
|
||||||
|
// and count these?
|
||||||
|
const march14 = new Date("2024-03-14T00:00:00").getTime();
|
||||||
|
const march15 = new Date("2024-03-15T00:00:00").getTime();
|
||||||
|
|
||||||
|
const meds: Medication[] = [
|
||||||
|
{
|
||||||
|
id: 31,
|
||||||
|
name: "ProdMed",
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 30,
|
||||||
|
looseTablets: 0,
|
||||||
|
takenBy: ["Daniel"],
|
||||||
|
blisters: [
|
||||||
|
{
|
||||||
|
usage: 1,
|
||||||
|
every: 1,
|
||||||
|
start: "2024-03-01T08:00:00",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
updatedAt: null,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// Dose IDs with person suffix (as prod generates them)
|
||||||
|
const doseId1 = `31-0-${march14}-Daniel`;
|
||||||
|
const doseId2 = `31-0-${march15}-Daniel`;
|
||||||
|
const takenDoses = new Set([doseId1, doseId2]);
|
||||||
|
// No stock correction → all counted
|
||||||
|
const takenDoseTimestamps = new Map([
|
||||||
|
[doseId1, new Date("2024-03-14T08:00:00Z").getTime()],
|
||||||
|
[doseId2, new Date("2024-03-15T08:00:00Z").getTime()],
|
||||||
|
]);
|
||||||
|
|
||||||
|
const result = calculateCoverage(meds, [], "en", 7, "manual", takenDoses, takenDoseTimestamps);
|
||||||
|
|
||||||
|
expect(result.all).toHaveLength(1);
|
||||||
|
// Both doses should be counted: medsLeft = 30 - 2 = 28
|
||||||
|
expect(result.all[0].medsLeft).toBe(28);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("manual mode: future dose taken today counts immediately", () => {
|
||||||
|
// User marks a future dose (later today) as taken.
|
||||||
|
// It should be counted in manual mode immediately.
|
||||||
|
vi.setSystemTime(new Date("2024-03-15T12:00:00Z"));
|
||||||
|
const march15 = new Date("2024-03-15T00:00:00").getTime();
|
||||||
|
|
||||||
|
const meds: Medication[] = [
|
||||||
|
{
|
||||||
|
id: 31,
|
||||||
|
name: "ProdMed",
|
||||||
|
packCount: 1,
|
||||||
|
blistersPerPack: 1,
|
||||||
|
pillsPerBlister: 30,
|
||||||
|
looseTablets: 0,
|
||||||
|
takenBy: ["Daniel"],
|
||||||
|
blisters: [
|
||||||
|
{
|
||||||
|
usage: 1,
|
||||||
|
every: 1,
|
||||||
|
start: "2024-03-01T21:00:00", // 21:00, still in future at 12:00
|
||||||
|
},
|
||||||
|
],
|
||||||
|
updatedAt: null,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
// No doses taken → 30 pills
|
||||||
|
const resultBefore = calculateCoverage(meds, [], "en", 7, "manual", new Set());
|
||||||
|
expect(resultBefore.all[0].medsLeft).toBe(30);
|
||||||
|
|
||||||
|
// Take today's dose (future time) → 29 pills
|
||||||
|
const doseId = `31-0-${march15}-Daniel`;
|
||||||
|
const takenDoses = new Set([doseId]);
|
||||||
|
const takenDoseTimestamps = new Map([[doseId, Date.now()]]);
|
||||||
|
const resultAfter = calculateCoverage(meds, [], "en", 7, "manual", takenDoses, takenDoseTimestamps);
|
||||||
|
expect(resultAfter.all[0].medsLeft).toBe(29);
|
||||||
|
|
||||||
|
// Undo → back to 30 pills
|
||||||
|
const resultUndo = calculateCoverage(meds, [], "en", 7, "manual", new Set());
|
||||||
|
expect(resultUndo.all[0].medsLeft).toBe(30);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getStockStatus", () => {
|
describe("getStockStatus", () => {
|
||||||
@@ -708,6 +1153,19 @@ describe("getStockStatus", () => {
|
|||||||
expect(result.level).toBe("normal");
|
expect(result.level).toBe("normal");
|
||||||
expect(result.label).toBe("status.noSchedule");
|
expect(result.label).toBe("status.noSchedule");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("returns critical when daysLeft is at or below criticalStockDays", () => {
|
||||||
|
const thresholdsWithCritical: StockThresholds = {
|
||||||
|
lowStockDays: 30,
|
||||||
|
criticalStockDays: 7,
|
||||||
|
normalStockDays: 90,
|
||||||
|
highStockDays: 180,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = getStockStatus(5, 10, thresholdsWithCritical);
|
||||||
|
expect(result.level).toBe("critical");
|
||||||
|
expect(result.className).toBe("danger");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("getNextReminderForMed", () => {
|
describe("getNextReminderForMed", () => {
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ export type PlannerRow = {
|
|||||||
fullBlisters: number;
|
fullBlisters: number;
|
||||||
loosePills: number;
|
loosePills: number;
|
||||||
enough: boolean;
|
enough: boolean;
|
||||||
|
packageType?: PackageType;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type RefillEntry = {
|
export type RefillEntry = {
|
||||||
@@ -170,6 +171,7 @@ export type SharedMedication = {
|
|||||||
doseUnit?: DoseUnit | null;
|
doseUnit?: DoseUnit | null;
|
||||||
imageUrl?: string | null;
|
imageUrl?: string | null;
|
||||||
totalPills: number;
|
totalPills: number;
|
||||||
|
packageType?: PackageType;
|
||||||
packCount: number;
|
packCount: number;
|
||||||
blistersPerPack: number;
|
blistersPerPack: number;
|
||||||
looseTablets: number;
|
looseTablets: number;
|
||||||
@@ -179,6 +181,8 @@ export type SharedMedication = {
|
|||||||
intakes?: Intake[]; // New intake format with per-intake takenBy
|
intakes?: Intake[]; // New intake format with per-intake takenBy
|
||||||
dismissedUntil?: string | null;
|
dismissedUntil?: string | null;
|
||||||
updatedAt?: string | number | null; // For filtering out doses from previous schedule configurations
|
updatedAt?: string | number | null; // For filtering out doses from previous schedule configurations
|
||||||
|
lastStockCorrectionAt?: number | null; // Timestamp in ms for stock correction cutoff
|
||||||
|
stockAdjustment?: number; // Manual stock adjustment
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SharedScheduleData = {
|
export type SharedScheduleData = {
|
||||||
@@ -188,7 +192,13 @@ export type SharedScheduleData = {
|
|||||||
medications: SharedMedication[];
|
medications: SharedMedication[];
|
||||||
stockThresholds?: {
|
stockThresholds?: {
|
||||||
lowStockDays: number;
|
lowStockDays: number;
|
||||||
|
normalStockDays?: number;
|
||||||
|
highStockDays?: number;
|
||||||
|
reminderDaysBefore?: number;
|
||||||
|
expiryWarningDays?: number;
|
||||||
};
|
};
|
||||||
|
stockCalculationMode?: "automatic" | "manual";
|
||||||
|
shareStockStatus?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ExpiredLinkData = {
|
export type ExpiredLinkData = {
|
||||||
|
|||||||
@@ -100,7 +100,8 @@ export function calculateCoverage(
|
|||||||
locale: string,
|
locale: string,
|
||||||
reminderDaysBefore: number,
|
reminderDaysBefore: number,
|
||||||
stockCalculationMode: "automatic" | "manual",
|
stockCalculationMode: "automatic" | "manual",
|
||||||
takenDoses: Set<string>
|
takenDoses: Set<string>,
|
||||||
|
takenDoseTimestamps?: Map<string, number>
|
||||||
): { low: Coverage[]; all: Coverage[] } {
|
): { low: Coverage[]; all: Coverage[] } {
|
||||||
const MS_PER_DAY = 86_400_000;
|
const MS_PER_DAY = 86_400_000;
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
@@ -116,66 +117,112 @@ export function calculateCoverage(
|
|||||||
// Also add medication-level takenBy for backward compatibility
|
// Also add medication-level takenBy for backward compatibility
|
||||||
m.takenBy?.forEach((person) => uniquePeople.add(person));
|
m.takenBy?.forEach((person) => uniquePeople.add(person));
|
||||||
const personCount = Math.max(1, uniquePeople.size || m.takenBy?.length || 1);
|
const personCount = Math.max(1, uniquePeople.size || m.takenBy?.length || 1);
|
||||||
const dailyRate = blisters.reduce((sum, s) => sum + (s.every > 0 ? s.usage / s.every : 0), 0) * personCount;
|
|
||||||
|
// Calculate daily consumption rate per intake, accounting for per-intake takenBy.
|
||||||
|
// When an intake has a per-intake takenBy (new format), it represents exactly
|
||||||
|
// one person's dose — do NOT multiply by personCount again.
|
||||||
|
// For legacy intakes (no takenBy), the intake applies to ALL people.
|
||||||
|
let dailyRate = 0;
|
||||||
|
blisters.forEach((s, idx) => {
|
||||||
|
const baseRate = s.every > 0 ? s.usage / s.every : 0;
|
||||||
|
const intake = intakes[idx];
|
||||||
|
if (intake?.takenBy) {
|
||||||
|
// Per-intake takenBy: this intake is for exactly 1 person
|
||||||
|
dailyRate += baseRate;
|
||||||
|
} else {
|
||||||
|
// Legacy: this intake applies to all people
|
||||||
|
dailyRate += baseRate * personCount;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let consumed = 0;
|
let consumed = 0;
|
||||||
const stockCorrectionCutoff = m.lastStockCorrectionAt ? new Date(m.lastStockCorrectionAt).getTime() : 0;
|
const stockCorrectionCutoff = m.lastStockCorrectionAt ? new Date(m.lastStockCorrectionAt).getTime() : 0;
|
||||||
|
|
||||||
if (stockCalculationMode === "automatic") {
|
if (stockCalculationMode === "automatic") {
|
||||||
// In automatic mode, calculate expected consumption based on time
|
// In automatic mode, stock is reduced automatically based on the schedule.
|
||||||
// but also account for manual corrections (doses marked as not taken)
|
// Every scheduled dose counts as consumed once its time has passed.
|
||||||
|
// Additionally, if a user marks a future dose as taken BEFORE the scheduled
|
||||||
|
// time (early intake), that dose is also counted as consumed immediately.
|
||||||
|
// This prevents double-counting: once the scheduled time arrives, the dose
|
||||||
|
// was already counted via the early-taken path, not again via time.
|
||||||
blisters.forEach((s, blisterIdx) => {
|
blisters.forEach((s, blisterIdx) => {
|
||||||
const blisterStart = new Date(s.start).getTime();
|
const blisterStart = new Date(s.start).getTime();
|
||||||
const period = Math.max(1, s.every) * MS_PER_DAY;
|
const period = Math.max(1, s.every) * MS_PER_DAY;
|
||||||
|
|
||||||
// After a stock correction, start counting consumption from the NEXT
|
// After a stock correction, start counting consumption from the NEXT
|
||||||
// scheduled dose, because the user's pill count already reflects all
|
// scheduled dose on this blister's grid, because the user's pill count
|
||||||
// consumption up to the correction time.
|
// already reflects all consumption up to the correction time.
|
||||||
|
// We align to the schedule grid so that e.g. correction at 15:40 with
|
||||||
|
// a daily 15:42 dose counts today's 15:42 dose (2 min later), not
|
||||||
|
// tomorrow's dose (24h later as the old code did).
|
||||||
let effectiveStart: number;
|
let effectiveStart: number;
|
||||||
if (stockCorrectionCutoff > 0 && stockCorrectionCutoff >= blisterStart) {
|
if (stockCorrectionCutoff > 0 && stockCorrectionCutoff >= blisterStart) {
|
||||||
effectiveStart = stockCorrectionCutoff + period;
|
const elapsedSinceStart = stockCorrectionCutoff - blisterStart;
|
||||||
|
const periodsElapsed = Math.floor(elapsedSinceStart / period);
|
||||||
|
effectiveStart = blisterStart + (periodsElapsed + 1) * period;
|
||||||
} else {
|
} else {
|
||||||
effectiveStart = blisterStart;
|
effectiveStart = blisterStart;
|
||||||
}
|
}
|
||||||
if (Number.isNaN(effectiveStart) || effectiveStart > now) return;
|
if (Number.isNaN(effectiveStart)) return;
|
||||||
const occurrences = Math.floor((now - effectiveStart) / period) + 1;
|
|
||||||
const intake = intakes[blisterIdx];
|
const intake = intakes[blisterIdx];
|
||||||
const intakePerson = intake?.takenBy;
|
const intakePerson = intake?.takenBy;
|
||||||
|
|
||||||
// For per-intake takenBy, only count for that person
|
// For per-intake takenBy, only count for that person
|
||||||
// For legacy (no takenBy), count for all people in medication takenBy
|
// For legacy (no takenBy), count for all people in medication takenBy
|
||||||
const peopleForThisIntake = intakePerson ? [intakePerson] : m.takenBy?.length > 0 ? m.takenBy : [null];
|
const peopleForThisIntake = intakePerson ? [intakePerson] : m.takenBy?.length > 0 ? m.takenBy : [null];
|
||||||
const expectedConsumed = occurrences * s.usage * peopleForThisIntake.length;
|
|
||||||
|
|
||||||
// Count how many doses were actually marked as taken for this blister
|
// Time-based: count doses where the scheduled time has already passed
|
||||||
let actualConsumed = 0;
|
let timeBasedConsumed = 0;
|
||||||
|
let lastAutoConsumedDateMs = 0;
|
||||||
|
|
||||||
// Generate all expected dose IDs for this blister up to now
|
if (effectiveStart <= now) {
|
||||||
for (let i = 0; i < occurrences; i++) {
|
const occurrences = Math.floor((now - effectiveStart) / period) + 1;
|
||||||
const doseDate = new Date(effectiveStart + i * period);
|
timeBasedConsumed = occurrences * s.usage * peopleForThisIntake.length;
|
||||||
const dateOnlyMs = new Date(doseDate.getFullYear(), doseDate.getMonth(), doseDate.getDate()).getTime();
|
|
||||||
const baseDoseId = `${m.id}-${blisterIdx}-${dateOnlyMs}`;
|
|
||||||
|
|
||||||
// Check if each person has taken this dose
|
// Date-only timestamp of the last auto-consumed dose
|
||||||
for (const person of peopleForThisIntake) {
|
const lastDoseTime = new Date(effectiveStart + (occurrences - 1) * period);
|
||||||
const doseId = person ? `${baseDoseId}-${person}` : baseDoseId;
|
lastAutoConsumedDateMs = new Date(
|
||||||
if (takenDoses.has(doseId)) {
|
lastDoseTime.getFullYear(),
|
||||||
actualConsumed += s.usage;
|
lastDoseTime.getMonth(),
|
||||||
|
lastDoseTime.getDate()
|
||||||
|
).getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Early intakes: count future doses already marked as taken.
|
||||||
|
// The cutoff is the later of: last auto-consumed date or stock correction date.
|
||||||
|
// This prevents double-counting (time-based + early-taken) and respects corrections.
|
||||||
|
const stockCorrectionDateOnly =
|
||||||
|
stockCorrectionCutoff > 0
|
||||||
|
? new Date(
|
||||||
|
new Date(stockCorrectionCutoff).getFullYear(),
|
||||||
|
new Date(stockCorrectionCutoff).getMonth(),
|
||||||
|
new Date(stockCorrectionCutoff).getDate()
|
||||||
|
).getTime()
|
||||||
|
: 0;
|
||||||
|
const earlyCutoff = Math.max(lastAutoConsumedDateMs, stockCorrectionDateOnly);
|
||||||
|
|
||||||
|
let earlyTakenConsumed = 0;
|
||||||
|
for (const doseId of takenDoses) {
|
||||||
|
const parts = doseId.split("-");
|
||||||
|
if (parts.length >= 3) {
|
||||||
|
const medId = parseInt(parts[0], 10);
|
||||||
|
const bIdx = parseInt(parts[1], 10);
|
||||||
|
const timestamp = parseInt(parts[2], 10);
|
||||||
|
if (medId === m.id && bIdx === blisterIdx && timestamp > earlyCutoff) {
|
||||||
|
earlyTakenConsumed += s.usage;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we have tracking data (any doses marked), use actual consumed
|
consumed += timeBasedConsumed + earlyTakenConsumed;
|
||||||
// Otherwise fall back to expected (for backwards compatibility)
|
|
||||||
const hasTrackingData = Array.from(takenDoses).some((id) => {
|
|
||||||
const parts = id.split("-");
|
|
||||||
return parts.length >= 3 && parseInt(parts[0], 10) === m.id && parseInt(parts[1], 10) === blisterIdx;
|
|
||||||
});
|
|
||||||
|
|
||||||
consumed += hasTrackingData ? actualConsumed : expectedConsumed;
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// In manual mode, only count doses that are explicitly marked as taken
|
// In manual mode, only count doses that are explicitly marked as taken.
|
||||||
|
// For stock correction filtering, we use the actual time the dose was marked
|
||||||
|
// as taken (takenAt), not the scheduled date. This correctly handles same-day
|
||||||
|
// scenarios: if a user corrects stock at 3pm, then takes a dose at 4pm,
|
||||||
|
// the dose counts because takenAt (4pm) > correctionTime (3pm).
|
||||||
takenDoses.forEach((doseId) => {
|
takenDoses.forEach((doseId) => {
|
||||||
const parts = doseId.split("-");
|
const parts = doseId.split("-");
|
||||||
if (parts.length >= 3) {
|
if (parts.length >= 3) {
|
||||||
@@ -190,19 +237,17 @@ export function calculateCoverage(
|
|||||||
blisterStartDate.getMonth(),
|
blisterStartDate.getMonth(),
|
||||||
blisterStartDate.getDate()
|
blisterStartDate.getDate()
|
||||||
).getTime();
|
).getTime();
|
||||||
// Convert stock correction cutoff to date-only as well
|
|
||||||
const stockCorrectionDateOnly =
|
// Use actual takenAt timestamp for stock correction comparison.
|
||||||
stockCorrectionCutoff > 0
|
// A dose counts only if it was MARKED after the stock correction,
|
||||||
? new Date(
|
// regardless of what day it was scheduled for.
|
||||||
new Date(stockCorrectionCutoff).getFullYear(),
|
const takenAt = takenDoseTimestamps?.get(doseId) ?? 0;
|
||||||
new Date(stockCorrectionCutoff).getMonth(),
|
const afterCorrectionOrNoCorrectionMs = stockCorrectionCutoff === 0 || takenAt > stockCorrectionCutoff;
|
||||||
new Date(stockCorrectionCutoff).getDate()
|
|
||||||
).getTime()
|
|
||||||
: 0;
|
|
||||||
if (
|
if (
|
||||||
!Number.isNaN(blisterStartDateOnly) &&
|
!Number.isNaN(blisterStartDateOnly) &&
|
||||||
doseTimestamp >= blisterStartDateOnly &&
|
doseTimestamp >= blisterStartDateOnly &&
|
||||||
doseTimestamp > stockCorrectionDateOnly
|
afterCorrectionOrNoCorrectionMs
|
||||||
) {
|
) {
|
||||||
consumed += blisters[blisterIdx].usage;
|
consumed += blisters[blisterIdx].usage;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user