added subscriber profile

This commit is contained in:
Kwesi Banson
2026-09-22 22:01:37 +00:00
parent c7f93a7dc1
commit 3ece39efe3
9 changed files with 846 additions and 5 deletions

View File

@@ -78,6 +78,8 @@ class SyncSubscriptionsServices extends Command
// The next run will safely pick up from the previous successful mark.
$this->error("Failed syncing {$service->name}: " . $e->getMessage());
}
// Inside your service loop in SyncSubscriptionsServices.php
$this->syncBillingTransactions($dynamicDb, $service->id, $lastSync);
}
$this->newLine();
@@ -129,7 +131,8 @@ class SyncSubscriptionsServices extends Command
$normalizedData[] = [
'service_id' => $serviceId,
'phone_number' => $sub->cell_number,
'status' => strtolower($sub->sub_state), // standardizes to 'active' or 'inactive'
'content_name' => $sub->content ?? 'Standard Bundle', // Added to match composite key
'status' => strtolower($sub->sub_state),
'join_date' => $sub->date_joined,
'updated_at' => now(),
'created_at' => now(),
@@ -138,9 +141,40 @@ class SyncSubscriptionsServices extends Command
DB::table('unified_subscribers')->upsert(
$normalizedData,
['service_id', 'phone_number'],
['service_id', 'phone_number', 'content_name'], // Updated to match table's unique index
['status', 'updated_at']
);
});
}
private function syncBillingTransactions($db, $serviceId, $lastSync)
{
// Check if the remote database actually has the billing_transactions table
if (!$db->getSchemaBuilder()->hasTable('billing_transactions')) {
return;
}
$db->table('billing_transactions')
->where('date', '>=', $lastSync) // Only pull transactions from the high-water mark forward
->orderBy('id')
->chunk(1000, function ($transactions) use ($serviceId) {
$batchData = [];
foreach ($transactions as $tx) {
$batchData[] = [
'service_id' => $serviceId,
'msisdn' => $tx->msisdn,
'content' => $tx->content,
'cost' => $tx->cost,
'transaction_date' => $tx->date,
'created_at' => now(),
'updated_at' => now(),
];
}
// Insert log entries into the hub warehouse
if (!empty($batchData)) {
DB::table('unified_billing_transactions')->insert($batchData);
}
});
}
}

View File

@@ -77,5 +77,30 @@ class DashboardController extends Controller
return view('dashboard.details', compact('service', 'subscribers'));
}
// app/Http/Controllers/DashboardController.php
public function subscriberProfile(Request $request, $serviceId, $msisdn)
{
$service = DB::table('subscription_services')->where('id', $serviceId)->first();
if (!$service) {
abort(404, 'Service not found.');
}
// Aggregate content frequency for this specific subscriber
$contentFrequency = DB::table('unified_billing_transactions')
->where('service_id', $serviceId)
->where('msisdn', $msisdn)
->select(
'content',
DB::raw('COUNT(*) as frequency'),
DB::raw('SUM(CAST(cost AS DECIMAL(10,2))) as total_spent'),
DB::raw('MAX(transaction_date) as last_received')
)
->groupBy('content')
->orderBy('frequency', 'desc')
->paginate(10);
return view('dashboard.subscriber-profile', compact('service', 'msisdn', 'contentFrequency'));
}
}