Skip to content

Commit

Permalink
Update variants channels tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arfenis committed Jul 5, 2023
1 parent 67c190a commit 4947db8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 37 deletions.
27 changes: 17 additions & 10 deletions app/GraphQL/Inventory/Mutations/Variants/Variants.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ public function create(mixed $root, array $req): VariantModel
{
$variantDto = VariantDto::viaRequest($req['input']);
$action = new CreateVariantsAction($variantDto, auth()->user());
return $action->execute();
$variantModel = $action->execute();

WarehouseRepository::getById($variantDto->warehouse_id, $variantDto->product->company()->get()->first());
$variantModel->warehouses()->attach($variantDto->warehouse_id);

return $variantModel;
}

/**
Expand Down Expand Up @@ -62,7 +67,7 @@ public function delete(mixed $root, array $req): bool
{
$variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany());

return $variant->delete();
return $variant->softdelete();
}

/**
Expand Down Expand Up @@ -143,14 +148,13 @@ public function removeAttribute(mixed $root, array $req): VariantModel
*/
public function addToChannel(mixed $root, array $req): VariantModel
{
$variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany());
$warehouse = WarehouseRepository::getById($req['warehouses_id']);

$variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany());
$warehouse = WarehouseRepository::getById((int) $req['warehouses_id']);
$variantWarehouses = ModelsVariantsWarehouses::where('products_variants_id', $variant->getId())
->where('warehouses_id', $warehouse->getId())
->firstOrFail();

$channel = ChannelRepository::getById($req['channels_id']);
$channel = ChannelRepository::getById((int) $req['channels_id']);
$variantChannel = VariantChannel::from($req['input']);
(new AddVariantToChannel($variantWarehouses, $channel, $variantChannel))->execute();
return $variant;
Expand All @@ -166,10 +170,13 @@ public function addToChannel(mixed $root, array $req): VariantModel
*/
public function removeChannel(mixed $root, array $req): VariantModel
{
$variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany());

$channel = ChannelRepository::getById($req['channels_id']);
$variant->channels()->detach($channel->id);
$variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany());
$warehouse = WarehouseRepository::getById((int) $req['warehouses_id']);
$variantWarehouses = ModelsVariantsWarehouses::where('products_variants_id', $variant->getId())
->where('warehouses_id', $warehouse->getId())
->firstOrFail();
$channel = ChannelRepository::getById((int) $req['channels_id']);
$variantWarehouses->channels()->where('id', $channel->getId())->detach($channel->id);
return $variant;
}
}
12 changes: 8 additions & 4 deletions graphql/schemas/Inventory/variant.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,19 @@ extend type Mutation @guard {
resolver: "App\\GraphQL\\Inventory\\Mutations\\Variants\\Variants@removeAttribute"
)
addVariantToChannel(
id: Int!
channels_id: Int!
warehouses_id: Int!
id: ID!
channels_id: ID!
warehouses_id: ID!
input: VariantChannelInput!
): Variant
@field(
resolver: "App\\GraphQL\\Inventory\\Mutations\\Variants\\Variants@addToChannel"
)
removeVariantChannel(id: Int!, channels_id: Int!): Variant
removeVariantChannel(
id: ID!
channels_id: ID!
warehouses_id: ID!
): Variant
@field(
resolver: "App\\GraphQL\\Inventory\\Mutations\\Variants\\Variants@removeChannel"
)
Expand Down
4 changes: 2 additions & 2 deletions src/Inventory/Variants/Models/VariantsWarehouses.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public function channels(): BelongsToMany
);
}

public function variant(): HasMany
public function variant(): BelongsTo
{
return $this->hasMany(Variants::class, 'products_variants_id');
return $this->belongsTo(Variants::class, 'products_variants_id');
}

public function warehouse(): BelongsTo
Expand Down
38 changes: 17 additions & 21 deletions tests/GraphQL/Inventory/VariantsChannelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class VariantsChannelsTest extends TestCase
*/
public function testVariantToChannel(): void
{
$data = [
$dataRegion = [
'name' => 'Test Region',
'slug' => 'test-region',
'short_slug' => 'test-region',
Expand All @@ -33,17 +33,13 @@ public function testVariantToChannel(): void
currency_id
is_default
}
}
', [
'data' => $data
])->assertJson([
'data' => ['createRegion' => $data]
]);

$response = $response->decodeResponseJson();

}', ['data' => $dataRegion])
->assertJson([
'data' => ['createRegion' => $dataRegion]
]);
$idRegion = $response->json()['data']['createRegion']['id'];
$data = [
'regions_id' => $response['data']['createRegion']['id'],
'regions_id' => $idRegion,
'name' => 'Test Warehouse',
'location' => 'Test Location',
'is_default' => false,
Expand All @@ -64,7 +60,7 @@ public function testVariantToChannel(): void
}', ['data' => $data])->assertJson([
'data' => ['createWarehouse' => $data]
]);
$warehousesId = $response->json()['data']['createWarehouse']['id'];
$warehouseId = $response->json()['data']['createWarehouse']['id'];
$data = [
'name' => fake()->name,
'description' => fake()->text,
Expand All @@ -84,7 +80,8 @@ public function testVariantToChannel(): void
$data = [
'name' => fake()->name,
'description' => fake()->text,
'products_id' => $productId
'products_id' => $productId,
'warehouse_id' => $warehouseId
];
$response = $this->graphQL('
mutation($data: VariantsInput!) {
Expand All @@ -95,9 +92,7 @@ public function testVariantToChannel(): void
description
products_id
}
}', ['data' => $data])->assertJson([
'data' => ['createVariant' => $data]
]);
}', ['data' => $data]);
$variantId = $response->json()['data']['createVariant']['id'];

$dataChannel = [
Expand All @@ -122,7 +117,7 @@ public function testVariantToChannel(): void
$channelId = $response->json()['data']['createChannel']['id'];
$response = $this->graphQL(
'
mutation addVariantToChannel($id: Int! $channels_id: Int! $warehouses_id: Int! $input: VariantChannelInput!){
mutation addVariantToChannel($id: ID! $channels_id: ID! $warehouses_id: ID! $input: VariantChannelInput!){
addVariantToChannel(id: $id channels_id:$channels_id warehouses_id:$warehouses_id input:$input){
id
}
Expand All @@ -131,7 +126,7 @@ public function testVariantToChannel(): void
[
'id' => $variantId,
'channels_id' => $channelId,
'warehouses_id' => $warehousesId,
'warehouses_id' => $warehouseId,
'input' => [
'price' => 100,
'discounted_price' => 10,
Expand All @@ -145,15 +140,16 @@ public function testVariantToChannel(): void
]);

$response = $this->graphQL(
'mutation ($id: Int! $channels_id: Int!) {
removeVariantChannel(id: $id channels_id: $channels_id)
'mutation ($id: ID! $channels_id: ID! $warehouses_id: ID!) {
removeVariantChannel(id: $id channels_id: $channels_id warehouses_id: $warehouses_id)
{
id
}
}',
[
'id' => $variantId,
'channels_id' => $channelId
'channels_id' => $channelId,
'warehouses_id' => $warehouseId,
]
);
$response->assertJson([
Expand Down

0 comments on commit 4947db8

Please sign in to comment.