From efeb9e896327a83554ba7dc51ff52ba710b42282 Mon Sep 17 00:00:00 2001 From: Obinna Ikeh Date: Wed, 11 Dec 2024 10:39:08 +0100 Subject: [PATCH 1/4] generate random test token --- .../app/Console/Commands/DemoDataCreator.php | 21 ++++++++++++++++++- src/backend/app/Helpers/TokenGenerator.php | 19 +++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/backend/app/Helpers/TokenGenerator.php diff --git a/src/backend/app/Console/Commands/DemoDataCreator.php b/src/backend/app/Console/Commands/DemoDataCreator.php index d90c74bd2..d29744e64 100644 --- a/src/backend/app/Console/Commands/DemoDataCreator.php +++ b/src/backend/app/Console/Commands/DemoDataCreator.php @@ -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; @@ -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, @@ -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'], @@ -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', diff --git a/src/backend/app/Helpers/TokenGenerator.php b/src/backend/app/Helpers/TokenGenerator.php new file mode 100644 index 000000000..73037d18f --- /dev/null +++ b/src/backend/app/Helpers/TokenGenerator.php @@ -0,0 +1,19 @@ + Date: Wed, 11 Dec 2024 10:39:26 +0100 Subject: [PATCH 2/4] format token display --- src/frontend/src/modules/Meter/Transactions.vue | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/frontend/src/modules/Meter/Transactions.vue b/src/frontend/src/modules/Meter/Transactions.vue index 54d687c44..be7375abb 100644 --- a/src/frontend/src/modules/Meter/Transactions.vue +++ b/src/frontend/src/modules/Meter/Transactions.vue @@ -23,8 +23,8 @@ - - Token {{ token.paid_for.token }} + + Token ({{ formatToken(token.paid_for.token) }}) {{ token.paid_for_type }} @@ -92,6 +92,13 @@ export default { this.transactions.tokens.length, ) }, + 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" + }, }, } From e088d44c1dbcae6a512fde5cb172bc073d17ab80 Mon Sep 17 00:00:00 2001 From: Obinna Ikeh Date: Wed, 11 Dec 2024 10:45:31 +0100 Subject: [PATCH 3/4] fix prettier lint warning --- src/frontend/src/modules/Meter/Transactions.vue | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/frontend/src/modules/Meter/Transactions.vue b/src/frontend/src/modules/Meter/Transactions.vue index be7375abb..f64d70225 100644 --- a/src/frontend/src/modules/Meter/Transactions.vue +++ b/src/frontend/src/modules/Meter/Transactions.vue @@ -94,10 +94,10 @@ export default { }, formatToken(token) { // Ensure token is a string - const tokenStr = String(token); + 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" + return tokenStr.match(/.{1,3}/g).join(" ") // For "123 412 341 234" }, }, } From 8c8af961edaf7e244df5ba2b46920f7e60969f0d Mon Sep 17 00:00:00 2001 From: Obinna Ikeh Date: Wed, 11 Dec 2024 19:22:22 +0100 Subject: [PATCH 4/4] refactor token formatting using mixin --- src/frontend/src/mixins/token.js | 11 +++++++++++ src/frontend/src/modules/Meter/Transactions.vue | 14 ++++---------- 2 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 src/frontend/src/mixins/token.js diff --git a/src/frontend/src/mixins/token.js b/src/frontend/src/mixins/token.js new file mode 100644 index 000000000..070baf960 --- /dev/null +++ b/src/frontend/src/mixins/token.js @@ -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" + }, + }, +} diff --git a/src/frontend/src/modules/Meter/Transactions.vue b/src/frontend/src/modules/Meter/Transactions.vue index f64d70225..1d426a256 100644 --- a/src/frontend/src/modules/Meter/Transactions.vue +++ b/src/frontend/src/modules/Meter/Transactions.vue @@ -30,8 +30,8 @@ {{ token.paid_for_type }} -