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

Changes for mobile apps (agent & cus-regs) #13

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 39 additions & 15 deletions NginxProxy/conf.p/app.conf
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
server {
listen 80;
server_name staging.micropowermanager.com;
root /var/www/html/dist;
index index.php index.html index.htm;
include /etc/nginx/mime.types;

location / {
server {
listen 80;
server_tokens off;

location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}

server {
listen 443 ssl;
server_tokens off;

ssl_certificate /etc/letsencrypt/live/demo.micropowermanager.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/demo.micropowermanager.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/dist;
index index.php index.html index.htm;


location / {
try_files $uri /index.html;
}
}

location /api/ {
location /tickets/ {
try_files $uri /index.php?$args;
gzip_static on;
}
location /tickets/ {

location /api/ {
try_files $uri /index.php?$args;
gzip_static on;
}
location ~ \.php$ {
}

location ~ \.php$ {
root /var/www/html/mpmanager/public;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass laravel:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private function payApplianceInstallments(TransactionDataContainer $container):
$applianceInstallmentPayer = resolve('ApplianceInstallmentPayer');
$applianceInstallmentPayer->initialize($container);
$applianceInstallmentPayer->payInstallmentsForDevice($container);
InensusDev marked this conversation as resolved.
Show resolved Hide resolved
$container->paidRates = $applianceInstallmentPayer->paidRates;

return $container;
}
Expand Down
25 changes: 22 additions & 3 deletions Website/htdocs/mpmanager/app/Jobs/TokenProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Jobs;

use App\Misc\TransactionDataContainer;
use App\Models\AssetRate;
use App\Models\Token;
use Exception;
use Illuminate\Bus\Queueable;
Expand All @@ -24,7 +25,7 @@ class TokenProcessor extends AbstractJob
public function __construct(
TransactionDataContainer $container,
bool $reCreate = false,
int $counter = self::MAX_TRIES
int $counter = self::MAX_TRIES,
) {
$this->transactionContainer = $container;
$this->reCreate = $reCreate;
Expand All @@ -46,7 +47,7 @@ public function executeJob(): void
if ($token === null) {
$this->generateToken($api);
}
if ($token !== null){
if ($token !== null) {
$this->handlePaymentEvents($token);
}
alchalade marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -91,13 +92,14 @@ private function handleTokenGenerationFailure(Exception $e): void
$this->retryTokenGeneration();
return;
}

Log::critical(
$this->transactionContainer->manufacturer->name . ' Token listener failed after ' .
$this->counter . ' times ',
['message' => $e->getMessage()]
);

$this->handleRollbackInFailure();

event('transaction.failed', [
$this->transactionContainer->transaction,
'Manufacturer Api did not succeed after 3 times with the following error: ' . $e->getMessage()
Expand All @@ -119,6 +121,8 @@ private function saveToken(array $tokenData): void
$token = Token::query()->make(['token' => $tokenData['token'], 'load' => $tokenData['load']]);
$token->transaction()->associate($this->transactionContainer->transaction);
$token->save();

$this->handlePaymentEvents($token);
}

private function handlePaymentEvents($token): void
Expand All @@ -137,4 +141,19 @@ private function handlePaymentEvents($token): void

event('transaction.successful', [$this->transactionContainer->transaction]);
}

private function handleRollbackInFailure()
InensusDev marked this conversation as resolved.
Show resolved Hide resolved
{
$paidRates = $this->transactionContainer->paidRates;
collect($paidRates)->map(function ($paidRate) {
$assetRate = AssetRate::query()->find($paidRate['asset_rate_id']);
$assetRate->remaining += $paidRate['paid'];
$assetRate->update();
$assetRate->save();
});
$paymentHistories = $this->transactionContainer->transaction->paymentHistories()->get();
$paymentHistories->map(function ($paymentHistory) {
$paymentHistory->delete();
});
}
}
22 changes: 15 additions & 7 deletions Website/htdocs/mpmanager/app/Listeners/AccessRateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
namespace App\Listeners;

use App\Exceptions\AccessRates\NoAccessRateFound;
use App\Models\Meter\MeterParameter;
use App\PaymentHandler\AccessRate;
use App\Models\AccessRate\AccessRate;
use App\Models\AccessRate\AccessRatePayment;
use App\Models\Meter\Meter;
use Illuminate\Events\Dispatcher;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;

class AccessRateListener
{
public function initializeAccessRatePayment(MeterParameter $meterParameter): void
public function initializeAccessRatePayment(Meter $meter): void
{
try {
$accessRatePayment = AccessRate::withMeterParameters($meterParameter);
$accessRatePayment->initializeAccessRatePayment()->save();
$accessRate = $meter->tariff()->first()->accessRate;
if (!$accessRate) {
throw new NoAccessRateFound('Access Rate is not set');
}
$nextPaymentDate = Carbon::now()->addDays($accessRate->period)->toDateString();
$accessRatePayment = new AccessRatePayment();
$accessRatePayment->accessRate()->associate($accessRate);
$accessRatePayment->meter()->associate($meter);
$accessRatePayment->due_date = $nextPaymentDate;
$accessRatePayment->debt = 0;
} catch (NoAccessRateFound $exception) {
Log::error($exception->getMessage(), ['id' => 'fj3g98suiq3z89fdhfjlsa']);
}
Expand Down
9 changes: 4 additions & 5 deletions Website/htdocs/mpmanager/app/Listeners/PaymentListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\AssetRate;
use App\Models\Meter\MeterParameter;
use App\Models\Meter\MeterToken;
InensusDev marked this conversation as resolved.
Show resolved Hide resolved
use App\Models\Token;
use App\Services\AccessRatePaymentHistoryService;
use App\Services\ApplianceRatePaymentHistoryService;
use App\Services\MeterTokenPaymentHistoryService;
InensusDev marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -25,7 +26,6 @@ public function __construct(
private PersonPaymentHistoryService $personPaymentHistoryService,
private ApplianceRatePaymentHistoryService $applianceRatePaymentHistoryService,
private AccessRatePaymentHistoryService $accessRatePaymentHistoryService,
private MeterTokenPaymentHistoryService $meterTokenPaymentHistoryService,
private TransactionPaymentHistoryService $transactionPaymentHistoryService,
) {
}
Expand Down Expand Up @@ -99,10 +99,9 @@ public function onPaymentSuccess(
$paymentHistory->paid_for_type = Asset::class;
$paymentHistory->paid_for_id = $paidFor->id;
break;
case $paidFor instanceof MeterToken:
$this->meterTokenPaymentHistoryService->setAssignee($paidFor);
$this->meterTokenPaymentHistoryService->setAssigned($paymentHistory);
$this->meterTokenPaymentHistoryService->assign();
case $paidFor instanceof Token:
$paymentHistory->paid_for_type = Token::class;
$paymentHistory->paid_for_id = $paidFor->id;
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TransactionDataContainer
public float $amount;
public float $totalAmount;
public float $rawAmount;
public AssetPerson $appliancePerson;
public AssetPerson|null $appliancePerson;
InensusDev marked this conversation as resolved.
Show resolved Hide resolved
public float $installmentCost;
public string $dayDifferenceBetweenTwoInstallments;

Expand Down
6 changes: 6 additions & 0 deletions Website/htdocs/mpmanager/app/Models/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

use App\Models\Transaction\Transaction;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;

class Token extends BaseModel
{
public const RELATION_NAME = 'token';
public function transaction(): BelongsTo
{
return $this->belongsTo(Transaction::class);
}
public function paymentHistories(): MorphOne
{
return $this->morphOne(PaymentHistory::class, 'paid_for');
}
}
2 changes: 2 additions & 0 deletions Website/htdocs/mpmanager/app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use App\Models\MiniGrid;
use App\Models\Person\Person;
use App\Models\SolarHomeSystem;
use App\Models\Token;
use App\Models\Transaction\AgentTransaction;
use App\Models\Transaction\AirtelTransaction;
use App\Models\Transaction\CashTransaction;
Expand Down Expand Up @@ -89,6 +90,7 @@ public function boot()
City::RELATION_NAME => City::class,
Address::RELATION_NAME => Address::class,
SolarHomeSystem::RELATION_NAME => SolarHomeSystem::class,
Token::RELATION_NAME => Token::class,
]
);
}
Expand Down
66 changes: 12 additions & 54 deletions Website/htdocs/mpmanager/app/Services/AgentCustomerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,14 @@ public function list(Agent $agent): LengthAwarePaginator
{
$miniGridId = $agent->mini_grid_id;

return $this->person->newQuery()->with(
[
'addresses' => function ($q) {
return $q->where('is_primary', 1);
},
'addresses.city',
'meters.meter',
]
)
return $this->person->newQuery()->with([
'devices',
'addresses' => fn($q) => $q->where('is_primary', 1)->with('city'),
])
->where('is_customer', 1)
->whereHas(
'addresses',
function ($q) use ($miniGridId) {
$q->whereHas(
'city',
function ($q) use ($miniGridId) {
$q->where('mini_grid_id', $miniGridId);
}
);
}
)
fn($q) => $q->whereHas('city', fn($q) => $q->where('mini_grid_id', $miniGridId)))
->paginate(config('settings.paginate'));
}

Expand All @@ -48,42 +35,13 @@ public function search($searchTerm, $limit, $agent)
{
$miniGridId = $agent->mini_grid_id;
InensusDev marked this conversation as resolved.
Show resolved Hide resolved

return $this->person->newQuery()->with(
[
'addresses' => function ($q) {
return $q->where('is_primary', 1);
},
'addresses.city',
'meters.meter',

]
)->where('is_customer', 1)
->where('name', 'LIKE', '%' . $searchTerm . '%')
->whereHas(
'addresses.city',
function ($q) use ($searchTerm, $miniGridId) {
$q->where('mini_grid_id', $miniGridId);
}
)
return $this->person->newQuery()->with(['addresses.city', 'devices'])->whereHas(
'addresses', fn($q) => $q->where('phone', 'LIKE', '%' . $searchTerm . '%')
)->orWhereHas(
'devices',
fn($q) => $q->where('device_serial', 'LIKE', '%' . $searchTerm . '%')
)->orWhere('name', 'LIKE', '%' . $searchTerm . '%')
->orWhere('surname', 'LIKE', '%' . $searchTerm . '%')
->orWhereHas(
'addresses',
function ($q) use ($searchTerm) {
$q->where('email', 'LIKE', '%' . $searchTerm . '%');
$q->where('phone', 'LIKE', '%' . $searchTerm . '%');
}
)
->orWhereHas(
'addresses.city',
function ($q) use ($searchTerm) {
$q->where('name', 'LIKE', '%' . $searchTerm . '%');
}
)
->orWhereHas(
'meters.meter',
function ($q) use ($searchTerm) {
$q->where('serial_number', 'LIKE', '%' . $searchTerm . '%');
}
)->paginate($limit);
->paginate($limit);
}
}
Loading