diff --git a/src/Appwrite/Platform/Workers/Hamster.php b/src/Appwrite/Platform/Workers/Hamster.php index 3bb443faea..c12754d134 100644 --- a/src/Appwrite/Platform/Workers/Hamster.php +++ b/src/Appwrite/Platform/Workers/Hamster.php @@ -218,6 +218,15 @@ class Hamster extends Action } } + /** Add billing information to the project */ + $organization = $dbForConsole->findOne('teams', [ + Query::equal('$internalId', [$teamInternalId]) + ]); + + $billing = $this->getBillingDetails($organization); + $statsPerProject['billing_plan'] = $billing['billing_plan'] ?? null; + $statsPerProject['billing_start_date'] = $billing['billing_start_date'] ?? null; + /** Get Domains */ $statsPerProject['custom_domains'] = $dbForConsole->count('rules', [ Query::equal('projectInternalId', [$project->getInternalId()]), @@ -300,7 +309,7 @@ class Hamster extends Action }); /** - * Workaround to combine network.inbound+network.outbound as network. + * Workaround to combine network.Inbound+network.outbound as bandwidth. */ $statsPerProject["usage_bandwidth_infinity"] = $statsPerProject["usage_inbound_infinity"] + $statsPerProject["usage_outbound_infinity"]; $statsPerProject["usage_bandwidth_24h"] = $statsPerProject["usage_inbound_24h"] + $statsPerProject["usage_outbound_24h"]; @@ -344,7 +353,6 @@ class Hamster extends Action /** * @param Document $organization * @param Database $dbForConsole - * @throws \Utopia\Database\Exception */ private function getStatsForOrganization(Document $organization, Database $dbForConsole): void { @@ -358,22 +366,26 @@ class Hamster extends Action /** Organization name */ $statsPerOrganization['name'] = $organization->getAttribute('name'); - /** Get Email and of the organization owner */ $membership = $dbForConsole->findOne('memberships', [ Query::equal('teamInternalId', [$organization->getInternalId()]), ]); - if (!$membership || $membership->isEmpty()) { throw new \Exception('Membership not found. Skipping organization : ' . $organization->getId()); } - $userId = $membership->getAttribute('userId', null); if ($userId) { $user = $dbForConsole->getDocument('users', $userId); $statsPerOrganization['email'] = $user->getAttribute('email', null); } + /** Add billing information */ + $billing = $this->getBillingDetails($organization); + $statsPerOrganization['billing_plan'] = $billing['billing_plan'] ?? null; + $statsPerOrganization['billing_start_date'] = $billing['billing_start_date'] ?? null; + $statsPerOrganization['marked_for_deletion'] = $billing['markedForDeletion'] ?? 0; + $statsPerOrganization['billing_plan_downgrade'] = $billing['billing_plan_downgrade'] ?? null; + /** Organization Creation Date */ $statsPerOrganization['created'] = $organization->getAttribute('$createdAt'); @@ -412,6 +424,15 @@ class Hamster extends Action $statsPerUser['time'] = $user->getAttribute('$time'); + /** Add billing information */ + $organization = $dbForConsole->findOne('teams', [ + Query::equal('userInternalId', [$user->getInternalId()]) + ]); + + $billing = $this->getBillingDetails($organization); + $statsPerUser['billing_plan'] = $billing['billing_plan'] ?? null; + $statsPerUser['billing_start_date'] = $billing['billing_start_date'] ?? null; + /** Organization name */ $statsPerUser['name'] = $user->getAttribute('name'); @@ -446,4 +467,28 @@ class Hamster extends Action Console::error($e->getMessage()); } } + + private function getBillingDetails(Document $team): array + { + $billing = []; + + if (!empty($team) && !$team->isEmpty()) { + $billingPlan = $team->getAttribute('billingPlan', null); + $billingPlanDowngrade = $team->getAttribute('billingPlanDowngrade', null); + + if (!empty($billingPlan) && empty($billingPlanDowngrade)) { + $billing['billing_plan'] = $billingPlan; + } + + if (in_array($billingPlan, ['tier-1', 'tier-2'])) { + $billingStartDate = $team->getAttribute('billingStartDate', null); + $billing['billing_start_date'] = $billingStartDate; + } + + $billing['marked_for_deletion'] = $team->getAttribute('markedForDeletion', 0); + $billing['billing_plan_downgrade'] = $billingPlanDowngrade; + } + + return $billing; + } }