fix(admin): drop cached environment data when switching context

A doctor who also owns a clinic runs two environments, and a subscription
belongs to an environment, not to the user. The panel treated it as the
user's: the subscription query was keyed ['subscription-my'] with no
context, and switching environments never touched the react-query cache. So
upgrading the clinic left the personal practice showing the clinic's plan
with its feature-gated menu items unlocked, and vice versa.

The query key now carries the active dbUuid, and the context switch clears
the whole cache — every cached response belongs to the environment it was
fetched in, not just this one.

The API was already correct: DualEnvironmentSubscriptionTest pins that
granting one environment leaves the other on free.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-08-19 22:15:56 +03:30
co-authored by Claude Opus 5
parent 5548d6248c
commit 1ce9957538
5 changed files with 229 additions and 1 deletions
+5
View File
@@ -1,5 +1,6 @@
import { useNavigate } from 'react-router';
import { useState } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { useAuthStore, ContextItem } from '../stores/authStore';
const ROLE_LABELS: Record<string, string> = {
@@ -18,11 +19,15 @@ const TYPE_ICONS: Record<string, string> = {
export default function SelectContextPage() {
const { availableContexts, switchContext } = useAuthStore();
const navigate = useNavigate();
const qc = useQueryClient();
const [loading, setLoading] = useState<string | null>(null);
const handleSelect = async (ctx: ContextItem) => {
setLoading(ctx.db_uuid);
await switchContext(ctx.db_uuid);
// هر پاسخِ cache‌شده متعلق به محیط قبلی است — از اشتراک و پلن گرفته تا بیماران و
// نوبت‌ها. بدون پاک کردن، محیط تازه داده و دسترسی‌های محیط قبلی را نشان می‌دهد.
qc.clear();
navigate('/admin/dashboard', { replace: true });
};