Trying to figure out if this is possible or not but I am working on a SaaS communication project that allows customers to send text messages out to contacts. Those contacts are able to reply if they want to in order to provide 2-way communication.
The problem is there is a specific callback url set in the phone api which will need to properly route incoming text request. Each customer has their own number or number(s) so I want to be able to take the incoming request in a route, search the phone settings table across all tenant databases and if found, continue the request properly with saving the text message, look at any auto reply settings that were created, forward the text to another number, etc. all of which is per tenant specific.
Is it possible to search across all tenant databases in a route/controller and then forward the response properly to that tenant's db?
If not, or if it is cleaner, is it possible to connect to the master/core tenant instead and search through a master table of the phone numbers and hostnames and redirect the request to the proper tenant that way? If so, I would need to be able to connect the the master database for storing and updating the phone numbers from each client when they make changes.
I was able to solve it by adding the phone number in the hostnames table, searching for a match there, and resetting the current connection to that tenant.
$hostname = DB::table('hostnames')->select('*')->where('phone_number', $request->To)->first();
if($hostname->fqdn != 'defaultdomain.com'){
$dbname = DB::table('websites')->select('uuid')->where('id', $hostname->website_id)->first();
Config::set("database.connections.tenant", [
"driver" => 'mysql',
"host" => 'localhost',
"database" => $dbname->uuid,
"username" => env('DB_USERNAME'),
"password" => env('DB_PASSWORD')
]);
Config::set('database.default', 'tenant');
DB::purge('tenant');
DB::reconnect('tenant');
}
//Continue with contact info and storing phone logs