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

use correct paid_for_type on payment #396 #400

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/backend/app/Console/Commands/DemoDataCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace App\Console\Commands;

use App\Helpers\TokenGenerator;
use App\Models\MainSettings;
use App\Models\MaintenanceUsers;
use App\Models\Meter\Meter;
use App\Models\Meter\MeterToken;
use App\Models\Person\Person;
use App\Models\Token;
use App\Models\Transaction\AgentTransaction;
Expand Down Expand Up @@ -47,6 +49,7 @@ public function __construct(
private AirtelTransaction $airtelTransaction,
private Meter $meter,
private Token $token,
private MeterToken $meterToken,
private CalinTransaction $calinTransaction,
private MainSettings $mainSettings,
private TicketCategory $ticketCategory,
Expand Down Expand Up @@ -254,7 +257,7 @@ private function generateTransaction(): void {
// generate random token
if ($transactionData->transaction->amount > 0) {
$tokenData = [
'token' => Str::random(30),
'token' => TokenGenerator::generate(),
'load' => round(
$transactionData->transaction->amount /
$randomMeter['tariff']['price'],
Expand All @@ -266,6 +269,22 @@ private function generateTransaction(): void {
$token->save();
$transactionData->token = $token;

// generate meter_token
$meterTokenData = [
'meter_id' => $randomMeter->id,
'token' => TokenGenerator::generate(),
'energy' => round(
$transactionData->transaction->amount /
$randomMeter['tariff']['price'],
2
),
'transaction_id' => $transaction->id,
];
$meterToken = $this->meterToken->newQuery()->make(['meter_id' => $meterTokenData['meter_id'],
'token' => $meterTokenData['token'], '' => $meterTokenData['energy'],
'transaction_id' => $meterTokenData['transaction_id']]);
$meterToken->save();

// payment event
event(
'payment.successful',
Expand Down
19 changes: 19 additions & 0 deletions src/backend/app/Helpers/TokenGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Helpers;

class TokenGenerator {
/**
* Generate a random 12-digit token.
*
* @return string
*/
public static function generate() {
$token = '';
for ($i = 0; $i < 12; ++$i) {
$token .= random_int(0, 9);
}

return $token;
}
}
Comment on lines +5 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll leave this for now, but one comment for a later improvement: This should probably be a Factory https://laravel.com/docs/11.x/eloquent-factories

11 changes: 11 additions & 0 deletions src/frontend/src/mixins/token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const token = {
methods: {
formatToken(token) {
// Ensure token is a string
const tokenStr = String(token)
// Format in the desired pattern
// return tokenStr.match(/.{1,4}/g).join('-'); // For "1234-1234-1234"
return tokenStr.match(/.{1,3}/g).join(" ") // For "123 412 341 234"
},
},
}
11 changes: 6 additions & 5 deletions src/frontend/src/modules/Meter/Transactions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
<md-table-cell
v-text="moneyFormat(token.transaction.amount)"
></md-table-cell>
<md-table-cell v-if="token.paid_for_type === 'token'">
Token {{ token.paid_for.token }}
<md-table-cell v-if="token.paid_for_type === 'App\\Models\\Token'">
Token ({{ formatToken(token.paid_for.token) }})
dmohns marked this conversation as resolved.
Show resolved Hide resolved
</md-table-cell>
<md-table-cell v-else>
{{ token.paid_for_type }}
</md-table-cell>
<md-table-cell
v-if="token.paid_for_type === 'token'"
v-text="readable(token.paid_for.energy) + 'kWh'"
v-if="token.paid_for_type === 'App\\Models\\Token'"
v-text="readable(token.paid_for.energy) + ' kWh'"
></md-table-cell>
<md-table-cell v-else>-</md-table-cell>
<md-table-cell
Expand All @@ -49,10 +49,11 @@ import Widget from "../../shared/widget"
import { EventBus } from "@/shared/eventbus"
import { currency } from "@/mixins/currency"
import { timing } from "@/mixins/timing"
import { token } from "@/mixins/token"

export default {
name: "Transactions.vue",
mixins: [currency, timing],
mixins: [currency, timing, token],
components: { Widget },
props: {
transactions: {
Expand Down
Loading