Skip to content

Commit

Permalink
refactor: add count people by subscription type
Browse files Browse the repository at this point in the history
  • Loading branch information
FredPeal committed Jul 13, 2024
1 parent b4e0b77 commit 58bc007
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/GraphQL/Guild/Queries/PeopleManagementQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
16 changes: 16 additions & 0 deletions graphql/schemas/Guild/people.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!]!
Expand All @@ -29,6 +31,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
Expand Down Expand Up @@ -137,6 +149,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 {
Expand Down
9 changes: 9 additions & 0 deletions src/Domains/Guild/Customers/Models/People.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ public function phones(): HasMany
);
}

public function subscriptions(): HasMany
{
return $this->hasMany(
PeopleSubscription::class,
'peoples_id',
'id'
);
}

/**
* @psalm-suppress MixedReturnStatement
*/
Expand Down
52 changes: 52 additions & 0 deletions tests/GraphQL/Guild/PeopleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']['peopleCount']));
}
}

0 comments on commit 58bc007

Please sign in to comment.