diff --git a/app/GraphQL/Guild/Queries/PeopleManagementQueries.php b/app/GraphQL/Guild/Queries/PeopleManagementQueries.php index 2ff11d423..f141039f9 100644 --- a/app/GraphQL/Guild/Queries/PeopleManagementQueries.php +++ b/app/GraphQL/Guild/Queries/PeopleManagementQueries.php @@ -26,4 +26,17 @@ public function countByTag(mixed $root, array $request, GraphQLContext $context) return $builder->count(); } + + public function getBySubscriptionType(mixed $root, array $request, GraphQLContext $context): int + { + $builder = People::whereHas('subscriptions', function ($query) use ($request) { + $query->where('peoples_subscriptions.subscription_type', $request['type']); + }); + + $builder->where('is_deleted', StateEnums::NO->getValue()); + $builder = $this->scopeFromCompany($builder); + $builder = $this->scopeFromApp($builder); + + return $builder->count(); + } } diff --git a/graphql/schemas/Guild/people.graphql b/graphql/schemas/Guild/people.graphql index 6ef58fd80..88c2d1c13 100644 --- a/graphql/schemas/Guild/people.graphql +++ b/graphql/schemas/Guild/people.graphql @@ -7,10 +7,12 @@ type People { firstname: String! middlename: String lastname: String + subscriptions: [PeopleSubscription!]! @hasMany dob: Date organizations: [Organization!] @belongsToMany contacts: [Contact!]! @hasMany address: [Address!]! @hasMany + employment_history: [PeopleEmploymentHistory!] @hasMany(relation: "employmentHistory") files: [Filesystem!]! @@ -34,6 +36,16 @@ type People { builder: "App\\GraphQL\\Social\\Queries\\Tags\\TagsQueries@getTagsBuilder" ) } +type PeopleSubscription { + id: ID! + subscription_type: String! + status: String! + start_date: Date! + end_date: Date + first_date: Date + next_renewal: Date +} + type PeopleEmploymentHistory { id: ID! organization: Organization! @belongsTo @@ -142,6 +154,10 @@ extend type Query @guard { @field( resolver: "App\\GraphQL\\Guild\\Queries\\PeopleManagementQueries@countByTag" ) + peopleCountBySubscriptionType(type: String!): Int + @field( + resolver: "App\\GraphQL\\Guild\\Queries\\PeopleManagementQueries@getBySubscriptionType" + ) } extend type Mutation @guard { diff --git a/src/Domains/Guild/Customers/Models/People.php b/src/Domains/Guild/Customers/Models/People.php index 7409733a1..0f37e7fbf 100644 --- a/src/Domains/Guild/Customers/Models/People.php +++ b/src/Domains/Guild/Customers/Models/People.php @@ -117,6 +117,15 @@ public function phones(): HasMany ); } + public function subscriptions(): HasMany + { + return $this->hasMany( + PeopleSubscription::class, + 'peoples_id', + 'id' + ); + } + /** * @psalm-suppress MixedReturnStatement */ diff --git a/tests/GraphQL/Guild/PeopleTest.php b/tests/GraphQL/Guild/PeopleTest.php index b54434259..2650c70e2 100644 --- a/tests/GraphQL/Guild/PeopleTest.php +++ b/tests/GraphQL/Guild/PeopleTest.php @@ -457,4 +457,56 @@ public function testCountPeople() ]); $this->assertTrue(is_int($response['data']['peopleCount'])); } + + public function testPeopleCountBySubscriptionType() + { + $user = auth()->user(); + $branch = $user->getCurrentBranch(); + $firstname = fake()->firstName(); + $lastname = fake()->lastName(); + + $input = [ + 'firstname' => $firstname, + 'lastname' => $lastname, + 'contacts' => [ + [ + 'value' => fake()->email(), + 'contacts_types_id' => 1, + 'weight' => 0, + ], + [ + 'value' => fake()->phoneNumber(), + 'contacts_types_id' => 2, + 'weight' => 0, + ], + ], + 'address' => [ + [ + 'address' => fake()->address(), + 'city' => fake()->city(), + 'county' => fake()->city(), + 'state' => fake()->state(), + 'country' => fake()->country(), + 'zip' => fake()->postcode(), + ], + ], + 'custom_fields' => [], + ]; + + $this->createPeopleAndResponse($input); + + $response = $this->graphQL(' + query { + peopleCountBySubscriptionType( + type: "Free" + ) + } + '); + $response->assertJsonStructure([ + 'data' => [ + 'peopleCountBySubscriptionType', + ], + ]); + $this->assertTrue(is_int($response['data']['peopleCountBySubscriptionType'])); + } }