Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending transaction model #201

Closed
smtlab opened this issue Aug 19, 2020 · 6 comments
Closed

Extending transaction model #201

smtlab opened this issue Aug 19, 2020 · 6 comments
Assignees
Labels
good issue Good for newcomers question Further information is requested

Comments

@smtlab
Copy link

smtlab commented Aug 19, 2020

Hey @rez1dent3

Can you suggest to me how should I go about the below use case?

I am working on the logistics app, where I have shipments from multiple customers.

Every customer has cod-wallet
I have to deposit Cash on delivery amount from shipments to cod-wallet.

The problem here is there is a background service which picks up unpaid shipments from DB and processes deposit, also I cannot maintain any flag in shipment model for paid status as it would not be reliable, I directly want to join with transactions table and check if the entry exists with type deposit for a shipment.

Unfortunately, the transaction only takes metadata from the user side, using the JSON column I cannot take advantage of joins.

My thoughts on giving developers the flexibility to customize model

Any suggestions would really help.

Thanks in advance

@rez1dent3
Copy link
Member

Hello @smtlab.

You can add the required columns to the database yourself. Someone asked me already like this..
Here's an example.

Migrating a table:

Schema::table((new Transaction())->getTable(), function (Blueprint $table) {
$table->string('bank_method')->nullable();
});

Adding data on insertion:

namespace Bavix\Wallet\Test\Objects;
class Operation extends \Bavix\Wallet\Objects\Operation
{
/**
* @return array
*/
public function toArray(): array
{
return \array_merge(parent::toArray(), [
'bank_method' => $this->meta['bank_method'] ?? null,
]);
}
}

Overriding the transaction record object:

$this->app->bind(Operation::class, Objects\Operation::class);

And now we write data to the database:

self::assertFalse($buyer->relationLoaded('wallet'));
$transaction = $buyer->deposit(1000, ['bank_method' => 'VietComBank']);
self::assertEquals($transaction->amount, $buyer->balance);
self::assertInstanceOf(Transaction::class, $transaction);
self::assertEquals('VietComBank', $transaction->bank_method);

For convenience, you can override the Transaction model:

$app['config']->set('wallet.transaction.model', Transaction::class);

Nothing else is needed. The complete code is available here, here and here)))

@rez1dent3 rez1dent3 self-assigned this Aug 19, 2020
@rez1dent3 rez1dent3 added good issue Good for newcomers question Further information is requested labels Aug 19, 2020
@smtlab
Copy link
Author

smtlab commented Aug 20, 2020

@rez1dent3 Thanks for giving a detailed explanation, really appreciate your time :)

I completely missed the config file, I see I can override all services and models.

@atapatel
Copy link

@smtlab can you please share your logic for expiry logic here?

@nagsamayam
Copy link

Hi @rez1dent3

Thank you soo much for providing such a great package.
In the above comment you have suggested use Bavix\Wallet\Objects\Operation class to store additional 'trasactions' table columns data. But I didn't see the Bavix\Wallet\Objects\Operation class in the codebase. Can you please suggest how to add additional columns for 'transactions' table?

@rez1dent3
Copy link
Member

@nagsamayam Hello. I don't check old issue, I found your question by accident. If you want to discuss in more detail, then create a new issue.

Yes, Operation no longer exists, but it is still possible to implement this functionality. An example is in unit tests:

public function extract(TransactionDtoInterface $dto): array
{
$bankMethod = null;
if ($dto->getMeta() !== null) {
$bankMethod = $dto->getMeta()['bank_method'] ?? null;
}
return array_merge($this->transactionDtoTransformer->extract($dto), [
'bank_method' => $bankMethod,
]);
}

@nagsamayam
Copy link

@nagsamayam Hello. I don't check old issue, I found your question by accident. If you want to discuss in more detail, then create a new issue.

Yes, Operation no longer exists, but it is still possible to implement this functionality. An example is in unit tests:

public function extract(TransactionDtoInterface $dto): array
{
$bankMethod = null;
if ($dto->getMeta() !== null) {
$bankMethod = $dto->getMeta()['bank_method'] ?? null;
}
return array_merge($this->transactionDtoTransformer->extract($dto), [
'bank_method' => $bankMethod,
]);
}

Hi @rez1dent3
Thank you soo much for the solution provided and also noted your suggestion to create new issue. Once again thank you so much for providing package.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants