bavix / laravel-wallet

It's easy to work with a virtual wallet
https://bavix.github.io/laravel-wallet/
MIT License
1.11k stars 222 forks source link

Request before creating event on transfer, wallet, trasnaction #922

Closed tereaksmey123 closed 5 months ago

tereaksmey123 commented 5 months ago

Is your feature request related to a problem? Please describe. we face an issue where

Describe the solution you'd like

Describe alternatives you've considered By the way, it can be done by create a database trigger but it really messy. this solution only when i have no choice.

Additional context Thank in advance.

rez1dent3 commented 5 months ago

Hello.

Cannot add custom value to a custom column without writing database trigger.

Can I have a specific case? Now you can paste any data into any columns via meta.

we do need a way to add or modify value before create

Override the assembler class of each method. It is executed before creation. What goes into the database depends on his data.

As for 3 table is using the same uuid generate class, we cannot make difference uuid for each purpose like Wallet uuid format into 000 000 000 Transaction|Transfer uuid format into: [code 2 length][date 4 length]-[ulid]

The package creates a uuid automatically only if it does not come from the client. That is, you can always pass the uuid you need and use it as you wish.

Provide a (Wallet|Transaction|Transfer)CreatingEvent to allow add or modify the field similar to Eloquent|Observer creating event

If you add it, and I don’t see the need for this yet, then the event will be unlinked from the model, you will not be able to change the model before recording it in the database. Models are created at the last stages, because the main thing is performance.


If you need examples then let me know. I will prepare examples for you.

tereaksmey123 commented 5 months ago

Hello.

Cannot add custom value to a custom column without writing database trigger.

Can I have a specific case? Now you can paste any data into any columns via meta.

Example: has new table Order. so going to add 2 column on transactions

we do need a way to add or modify value before create

Override the assembler class of each method. It is executed before creation. What goes into the database depends on his data.

  • Override package class is risky and painful when upgrade version
  • As the class define as final class so has to do full copy even i want to adjust only 1 of it method etc.
  • Override with ability to fully extends the class is safer when upgrade
    class MyClass extends TransactionDtoTransformer
    {
    public function extract(TransactionDtoInterface $dto): array
    {
    return [...parent::extract(TransactionDtoInterface $dto),  ...['uuid' => 'new uuid format', 'any column' => 'value']];
    }
    }

As for 3 table is using the same uuid generate class, we cannot make difference uuid for each purpose like Wallet uuid format into 000 000 000 Transaction|Transfer uuid format into: [code 2 length][date 4 length]-[ulid]

The package creates a uuid automatically only if it does not come from the client. That is, you can always pass the uuid you need and use it as you wish.

  • My case uuid are not from client, and server should automatic do it
    • yes i could pass the uuid when i call the method.
    • but if i have many place call it i has to manually pass it down all the time
    • Concept of model event creating, it allow developer to mutate the data into a new form before pass to create function
      from observer code
      public function creating(Order $order)
      {
      $order->uuid = UniqueId::make($order->getTable())->ulid();
      }

      Provide a (Wallet|Transaction|Transfer)CreatingEvent to allow add or modify the field similar to Eloquent|Observer creating event

If you add it, and I don’t see the need for this yet, then the event will be unlinked from the model, you will not be able to change the model before recording it in the database. Models are created at the last stages, because the main thing is performance.

Didn't mean to change to Eloquent event, but if we could have a similar solution of their concept to intercept the data before pass it to create function If you need examples then let me know. I will prepare examples for you.

rez1dent3 commented 5 months ago

Override package class is risky and painful when upgrade version As the class define as final class so has to do full copy even i want to adjust only 1 of it method etc. Override with ability to fully extends the class is safer when upgrade

Pattern Decorator: https://refactoring.guru/design-patterns/decorator

Example: has new table Order. so going to add 2 column on transactions Add custom column mostly for better search and relation than search in json order_id > set order_id without waiting transaction created to select transaction to update order_id from meta ref_id from payment system: 2 case 1 transaction can have same ref_id but difference type first time comfirmed: false first time comfirmed: true when confirmed true > set ref_id to keep 1 way of updating ref_id, i don't use TransactionCreatedEventInterface for ref_id but make a trigger for it ... Didn't mean to change to Eloquent event, but if we could have a similar solution of their concept to intercept the data before pass it to create function

Example using a pattern: https://github.com/bavix/laravel-wallet/commit/c72f6d0609efe573fb370a35563715650f277138


At this point, I still don't see a reason to add an extra event.

rez1dent3 commented 5 months ago

Have you tried to repeat the example? In my opinion, this is exactly what you want.

tereaksmey123 commented 5 months ago

Override package class is risky and painful when upgrade version As the class define as final class so has to do full copy even i want to adjust only 1 of it method etc. Override with ability to fully extends the class is safer when upgrade

Pattern Decorator: https://refactoring.guru/design-patterns/decorator

Example: has new table Order. so going to add 2 column on transactions Add custom column mostly for better search and relation than search in json order_id > set order_id without waiting transaction created to select transaction to update order_id from meta ref_id from payment system: 2 case 1 transaction can have same ref_id but difference type first time comfirmed: false first time comfirmed: true when confirmed true > set ref_id to keep 1 way of updating ref_id, i don't use TransactionCreatedEventInterface for ref_id but make a trigger for it ... Didn't mean to change to Eloquent event, but if we could have a similar solution of their concept to intercept the data before pass it to create function

Example using a pattern: c72f6d0

At this point, I still don't see a reason to add an extra event.

'transformers' => [
// 'transaction' => TransactionDtoTransformer::class,
'transaction' => TransactionODto::class,
'transfer' => TransferDtoTransformer::class,
],

it work when i add new field

public function extract(TransactionDtoInterface $dto): array
{
return [...$this->transactionDtoTransformer->extract($dto), ...[
// 'uuid' => UniqueId::make((new Transaction)->getTable())->ulid(),
'ref_code' => $dto->getType().($dto->getMeta()['ref_code'] ?? $dto->getUuid())
]];
}

but when add exists column of dto than error at Bavix\Wallet\Services\AtmService::makeTransfers() line 72 assert($items !== []);

public function extract(TransactionDtoInterface $dto): array
{
return [...$this->transactionDtoTransformer->extract($dto), ...[
'uuid' => UniqueId::make((new Transaction)->getTable())->ulid(),
'ref_code' => $dto->getType().($dto->getMeta()['ref_code'] ?? $dto->getUuid())
]];
}
rez1dent3 commented 5 months ago

but when add exists column of dto than error at Bavix\Wallet\Services\AtmService::makeTransfers() line 72 assert($items !== []);

I wrote to you about the assembler class:

Override the assembler class of each method. It is executed before creation. What goes into the database depends on his data.

Example: https://github.com/bavix/laravel-wallet/commit/c762423192abff1ef67fa8947dbc76766fc55d8f

PS, yes, I forgot to use a constant in the test )

rez1dent3 commented 5 months ago

Happened?

tereaksmey123 commented 5 months ago

Thank for the help.