To start with, I have this application developed using Laravel (It's an SMS application that anyone can signup for and send messages anywhere in the world (120+ countries literally).
Now for every country that I add from the admin section, I can manually assign a price for that country's network e.g. in the USA, we've various telcos like AT&T, Verizon, etc. now each of them may have a different cost for sending a single SMS and this is the same way it's implemented.
Assuming I want to send messages to 10k contacts (phone numbers) the current query I have would have to check through each country I have saved in my database, check the various countries network and get their respective prefix (the prefix is the country code, in the USA it's +1, Australia if +61 and it goes on like that).
After it does this check then it will get the respective prices set for those country's network and it will have to calculate by the number of contacts being sent to in this case 10k then it will add by the number of pages of SMS typed. (a 1 page SMS is made up of 160 characters).
All this calculation has to go on, then a modal pops up and informs the user of the total contacts they're sending to, the cost of sending it, and their current account balance.
This current query seems to work for small volumes maybe 500 (in terms of speed in processing and doing all these calculations before the modal pops up for the user to confirm before the messages are sent).
I need a way to refactor this and I'm open to all opinions and suggestions from the community.
Thanks in advance and below in the code in my helper file.
public static function get_mobile_number_network(User $user, $mobile_number): array {
$mobile_number = self::format_mobile_number($mobile_number);
foreach (Network::with('country')->get() as $network) {
$prefixes = explode(',', $network->prefix);
foreach ($prefixes as $prefix) {
$prefix = trim($prefix);
if (starts_with($mobile_number, $prefix)) {
$usr_price = UserNetwork::where([
['user_id', '=', $user->id],
['network_id', '=', $network->id],
['country_id', '=', $network->country_id],
])->first();
return [
'network_id' => $network->id,
'network' => $network->name,
'country_id' => $network->country_id,
'country' => $network->country->name,
'new_sms_price' => empty($usr_price) ? $network->new_sms_price : $usr_price->new_sms_price,
'hlr_price' => empty($usr_price) ? $network->hlr_price : $usr_price->hlr_price,
];
}
}
}
return [
'network_id' => null,
'network' => null,
'country_id' => null,
'country' => null,
'new_sms_price' => $user->credit->price,
'hlr_price' => $user->credit->hlr_price,
];
}