Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Jan 24, 2024
1 parent 77f4cc0 commit 680dd07
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/Internal/Repository/TransferRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

use Bavix\Wallet\Internal\Dto\TransferDtoInterface;
use Bavix\Wallet\Internal\Query\TransferQueryInterface;
use Bavix\Wallet\Internal\Service\JsonServiceInterface;
use Bavix\Wallet\Internal\Transform\TransferDtoTransformerInterface;
use Bavix\Wallet\Models\Transfer;

final readonly class TransferRepository implements TransferRepositoryInterface
{
public function __construct(
private TransferDtoTransformerInterface $transformer,
private JsonServiceInterface $jsonService,
private Transfer $transfer
) {
}
Expand All @@ -22,7 +24,14 @@ public function __construct(
*/
public function insert(array $objects): void
{
$values = array_map(fn (TransferDtoInterface $dto): array => $this->transformer->extract($dto), $objects);
$values = [];
foreach ($objects as $object) {
$values[] = array_map(
fn ($value) => is_array($value) ? $this->jsonService->encode($value) : $value,
$this->transformer->extract($object)
);
}

$this->transfer->newQuery()
->insert($values)
;
Expand Down
1 change: 1 addition & 0 deletions src/Internal/Transform/TransferDtoTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function extract(TransferDtoInterface $dto): array
'to_id' => $dto->getToId(),
'discount' => $dto->getDiscount(),
'fee' => $dto->getFee(),
'extra' => $dto->getExtra(),
'created_at' => $dto->getCreatedAt(),
'updated_at' => $dto->getUpdatedAt(),
];
Expand Down
1 change: 1 addition & 0 deletions src/Internal/Transform/TransferDtoTransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface TransferDtoTransformerInterface
* to_id: int|string,
* discount: int,
* fee: string,
* extra: array<mixed>|null,
* created_at: DateTimeImmutable,
* updated_at: DateTimeImmutable,
* }
Expand Down
1 change: 1 addition & 0 deletions src/Models/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @property int $to_id
* @property string $uuid
* @property string $fee
* @property ?array<mixed> $extra
* @property Transaction $deposit
* @property Transaction $withdraw
* @property DateTimeInterface $created_at
Expand Down
33 changes: 30 additions & 3 deletions tests/Units/Api/TransferHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Bavix\Wallet\External\Api\TransferQuery;
use Bavix\Wallet\External\Api\TransferQueryHandlerInterface;
use Bavix\Wallet\External\Dto\Extra;
use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;
use Bavix\Wallet\Test\Infra\Models\Buyer;
use Bavix\Wallet\Test\Infra\TestCase;
Expand All @@ -31,13 +32,39 @@ public function testWalletNotExists(): void
self::assertFalse($to->wallet->exists);

$transfers = $transferQueryHandler->apply([
new TransferQuery($from, $to, 100, null),
new TransferQuery($from, $to, 100, null),
new TransferQuery($to, $from, 50, null),
new TransferQuery(
$from,
$to,
100,
new Extra(
null,
null,
'598c184c-e6d6-4fc2-9640-c1c7acb38093',
['type' => 'first'],
),
),
new TransferQuery($from, $to, 100,
new Extra(
null,
null,
'f303d60d-c2de-45d0-b9ed-e1487429709a',
['type' => 'second'],
)),
new TransferQuery($to, $from, 50,
new Extra(
null,
null,
'7f0175fe-99cc-4058-92c6-157f0da18243',
['type' => 'third'],
)),
]);

self::assertSame(-150, $from->balanceInt);
self::assertSame(150, $to->balanceInt);
self::assertCount(3, $transfers);

self::assertSame(['type' => 'first'], $transfers['598c184c-e6d6-4fc2-9640-c1c7acb38093']->extra);
self::assertSame(['type' => 'second'], $transfers['f303d60d-c2de-45d0-b9ed-e1487429709a']->extra);
self::assertSame(['type' => 'third'], $transfers['7f0175fe-99cc-4058-92c6-157f0da18243']->extra);
}
}

0 comments on commit 680dd07

Please sign in to comment.