fix(subscription): report the acting environment's plan for gating

Feature gates and the resource cap are enforced against the environment
the user is standing in, but /subscription/my only ever returned the
plan of the environment they own. A doctor working as a guest in another
clinic consumed the host clinic's resource quota while the panel showed
their own plan's cap, so the quota number and the menu locks disagreed
with what the server would allow.

/subscription/my now also returns context_plan — limits and features of
the acting environment, without the other environment's plan identity.
effective_plan, subscription and used_trial stay on the owned
environment so the purchase flow is unchanged, and useSubscription
reads its caps and hasFeature from context_plan.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-08-20 14:48:13 +03:30
co-authored by Claude Opus 5
parent 25a12e9a06
commit 601d211d6f
5 changed files with 99 additions and 12 deletions
@@ -5,6 +5,7 @@ namespace App\Tests\Subscription;
use App\Auth\Entity\User;
use App\Auth\Entity\UserActiveContext;
use App\Clinic\Entity\Clinic;
use App\Clinic\Entity\ClinicDoctorPermission;
use App\Doctor\Entity\Doctor;
use App\Shared\Context\EntityContext;
use App\Subscription\Entity\SubscriptionPeriod;
@@ -83,6 +84,41 @@ class DualEnvironmentSubscriptionTest extends ApiTestCase
self::assertNotNull($body['data']['subscription']);
}
/**
* پزشکِ مهمانِ کلینیکِ دیگری سقف و قابلیت‌هایش را از کلینیکِ میزبان می‌گیرد — چون
* سرور هم با همان محیط می‌سنجد — ولی اشتراکِ خودش دست‌نخورده می‌ماند.
*/
public function testContextPlanFollowsTheHostClinicForAGuestDoctor(): void
{
$guest = $this->createUser(['ROLE_DOCTOR']);
$doctor = new Doctor($guest, 'دکتر مهمان');
$this->em->persist($doctor);
$host = new Clinic($this->createUser(['ROLE_CLINIC']));
$host->setName('کلینیک میزبان');
$this->em->persist($host);
$this->em->flush();
$host->getDoctors()->add($doctor);
$this->em->persist(new ClinicDoctorPermission($host, $doctor));
$this->em->flush();
$period = $this->makePeriod();
$this->service()->grant('clinic', $host->getId(), $period->getUuid(), $host->getUser());
$this->setActiveContext($guest, $host->getUuid(), EntityContext::TYPE_CLINIC);
$body = $this->authJson('GET', '/api/v1/subscription/my', $guest);
// سقف‌ها از کلینیک میزبان
self::assertSame($period->getPlan()->getMaxResources(), $body['data']['context_plan']['max_resources']);
self::assertTrue($body['data']['context_plan']['features']['patient_records']);
// ولی اشتراکِ خودِ پزشک همچنان free و بدون اشتراک فعال
self::assertSame('free', $body['data']['effective_plan']['name']);
self::assertNull($body['data']['subscription']);
// هویتِ پلنِ محیطِ میزبان افشا نمی‌شود
self::assertArrayNotHasKey('name', $body['data']['context_plan']);
}
public function testMyFallsBackToFreeWhileStandingInThePersonalPractice(): void
{
[$user, $doctor, $clinic] = $this->makeUserWithBothEnvironments();