Skip to content

Commit

Permalink
feat : people test
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken committed Jul 2, 2023
1 parent 18f4f51 commit f61f5d3
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 3 deletions.
4 changes: 2 additions & 2 deletions graphql/schemas/Guild/lead.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ input LeadInput {
description: String
reason_lost: String
pipeline_stage_id: ID!
custom_fields: [CustomFieldEntityInput!]!
custom_fields: [CustomFieldEntityInput]!
}

input LeadUpdateInput {
Expand All @@ -131,7 +131,7 @@ input LeadUpdateInput {
description: String
reason_lost: String
pipeline_stage_id: ID
custom_fields: [CustomFieldEntityInput!]!
custom_fields: [CustomFieldEntityInput]!
}

extend type Mutation @guard {
Expand Down
17 changes: 17 additions & 0 deletions graphql/schemas/Guild/people.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ input PeopleInput {
dob: String
contacts: [ContactInput!]!
address: [AddressInput!]!
custom_fields: [CustomFieldEntityInput]!
}

input PeopleParticipantInput {
Expand All @@ -49,6 +50,22 @@ input PeopleParticipantInput {
}

extend type Query @guard {
peoples(
where: _
@whereConditions(
columns: [
"id"
"uuid"
"companies_id"
"dob"
]
)
): [People!]!
@paginate(
model: "Kanvas\\Guild\\Customers\\Models\\People"
scopes: ["fromCompany"]
defaultCount: 10
)
peopleRelationships(
where: _ @whereConditions(columns: ["name", "description", "id"])
): [PeopleRelationship!]!
Expand Down
3 changes: 3 additions & 0 deletions src/Guild/Customers/Actions/CreatePeopleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function execute(): People
$people = People::create($attributes);
}

$people->setCustomFields($this->peopleData->custom_fields);
$people->saveCustomFields();

if ($this->peopleData->contacts->count()) {
$contacts = [];
foreach ($this->peopleData->contacts as $contact) {
Expand Down
3 changes: 3 additions & 0 deletions src/Guild/Customers/Actions/UpdatePeopleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public function execute(): People
//@todo how to avoid duplicated? should it be use or frontend?
$this->people->update($attributes);

$this->people->setCustomFields($this->peopleData->custom_fields);
$this->people->saveCustomFields();

if ($this->peopleData->contacts->count()) {
$contacts = [];
$this->people->contacts()->delete();
Expand Down
3 changes: 2 additions & 1 deletion src/Guild/Customers/DataTransferObject/People.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use DateTime;
use Kanvas\Companies\Models\CompaniesBranches;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Attributes\WithCast;
use Spatie\LaravelData\Casts\DateTimeInterfaceCast;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\Attributes\WithCast;

class People extends Data
{
Expand All @@ -36,6 +36,7 @@ public function __construct(
public readonly ?string $google_contact_id = null,
public readonly ?string $apple_contact_id = null,
public readonly ?string $linkedin_contact_id = null,
public readonly array $custom_fields = []
) {
}
}
304 changes: 304 additions & 0 deletions tests/GraphQL/Guild/PeopleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
<?php

declare(strict_types=1);

namespace Tests\GraphQL\Guild;

use Tests\TestCase;

class PeopleTest extends TestCase
{
public function testGetCustomers(): void
{
$this->graphQL('
query {
peoples {
data {
uuid
name
}
}
}')->assertOk();
}

protected function createPeopleAndResponse(array $input = [])
{
$user = auth()->user();
$branch = $user->getCurrentBranch();
$title = fake()->title();

if (empty($input)) {
$input = [
'firstname' => fake()->firstName(),
'lastname' => fake()->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(),
'state' => fake()->state(),
'country' => fake()->country(),
'zip' => fake()->postcode(),
],
],
'custom_fields' => [],
];
}

return $this->graphQL('
mutation($input: PeopleInput!) {
createPeople(input: $input) {
id,
uuid,
name,
dob
}
}
', [
'input' => $input,
])->json();
}

public function testCreatePeople()
{
$user = auth()->user();
$branch = $user->getCurrentBranch();
$firstname = fake()->firstName();
$lastname = fake()->lastName();
$name = $firstname . ' ' . $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(),
'state' => fake()->state(),
'country' => fake()->country(),
'zip' => fake()->postcode(),
],
],
'custom_fields' => [],
];

$this->graphQL('
mutation($input: PeopleInput!) {
createPeople(input: $input) {
name
}
}
', [
'input' => $input,
])->assertJson([
'data' => [
'createPeople' => [
'name' => $name,
],
],
]);
}

public function testUpdatePeople()
{
$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(),
'state' => fake()->state(),
'country' => fake()->country(),
'zip' => fake()->postcode(),
],
],
'custom_fields' => [],
];

$response = $this->createPeopleAndResponse($input);

$peopleId = $response['data']['createPeople']['id'];
$firstname = fake()->firstName();
$lastname = fake()->lastName();
$name = $firstname . ' ' . $lastname;
$input = [
'firstname' => $firstname,
'lastname' => $lastname,
'contacts' => [],
'address' => [],
'custom_fields' => [],
];
$this->graphQL('
mutation($id: ID!, $input: PeopleInput!) {
updatePeople(id: $id, input: $input) {
id
name
}
}
', [
'id' => $peopleId,
'input' => $input,
])->assertJson([
'data' => [
'updatePeople' => [
'id' => $peopleId,
'name' => $name,

],
],
]);
}

public function testDeletePeople()
{
$user = auth()->user();
$branch = $user->getCurrentBranch();
$title = fake()->title();

$input = [
'firstname' => fake()->firstName(),
'lastname' => fake()->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(),
'state' => fake()->state(),
'country' => fake()->country(),
'zip' => fake()->postcode(),
],
],
'custom_fields' => [],
];

$response = $this->createPeopleAndResponse($input);

$peopleId = $response['data']['createPeople']['id'];

$this->graphQL('
mutation($id: ID!) {
deletePeople(id: $id)
}
', [
'id' => $peopleId,
])->assertJson([
'data' => [
'deletePeople' => true,
],
]);
}

public function testRestoreLead()
{
$user = auth()->user();
$branch = $user->getCurrentBranch();
$title = fake()->title();

$input = [
'firstname' => fake()->firstName(),
'lastname' => fake()->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(),
'state' => fake()->state(),
'country' => fake()->country(),
'zip' => fake()->postcode(),
],
],
'custom_fields' => [],
];

$response = $this->createPeopleAndResponse($input);

$peopleId = $response['data']['createPeople']['id'];

$this->graphQL('
mutation($id: ID!) {
deletePeople(id: $id)
}
', [
'id' => $peopleId,
])->assertJson([
'data' => [
'deletePeople' => true,
],
]);

$this->graphQL('
mutation($id: ID!) {
restorePeople(id: $id)
}
', [
'id' => $peopleId,
])->assertJson([
'data' => [
'restorePeople' => true,
],
]);
}
}

0 comments on commit f61f5d3

Please sign in to comment.