payfrit-api/api/setup/checkDuplicate.php
John Mizerek 1f81d98c52 Initial PHP API migration from CFML
Complete port of all 163 API endpoints from Lucee/CFML to PHP 8.3.
Shared helpers in api/helpers.php (DB, auth, request/response, security).
PDO prepared statements throughout. Same JSON response shapes as CFML.
2026-03-14 14:26:59 -07:00

73 lines
1.9 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
/**
* Check for duplicate businesses
*
* POST JSON:
* {
* "name": "Business Name",
* "addressLine1": "123 Main St",
* "city": "Los Angeles",
* "state": "CA",
* "zip": "90001"
* }
*/
$response = ['OK' => true, 'duplicates' => []];
try {
$data = readJsonBody();
$bizName = trim($data['name'] ?? '');
$addressLine1 = trim($data['addressLine1'] ?? '');
$city = trim($data['city'] ?? '');
$state = trim($data['state'] ?? '');
$zip = trim($data['zip'] ?? '');
// Clean up city - remove trailing punctuation
$city = preg_replace('/[,.\s]+$/', '', $city);
$qDuplicates = queryTimed("
SELECT DISTINCT
b.ID AS BusinessID,
b.Name,
a.Line1,
a.City,
s.Abbreviation AS AddressState,
a.ZIPCode
FROM Businesses b
LEFT JOIN Addresses a ON a.BusinessID = b.ID
LEFT JOIN tt_States s ON s.ID = a.StateID
WHERE
LOWER(b.Name) = LOWER(?)
OR (
LOWER(a.Line1) = LOWER(?)
AND LOWER(a.City) = LOWER(?)
AND a.Line1 != ''
AND a.City != ''
)
ORDER BY b.Name
", [$bizName, $addressLine1, $city]);
foreach ($qDuplicates as $row) {
$addressParts = [];
if (!empty($row['Line1'])) $addressParts[] = $row['Line1'];
if (!empty($row['City'])) $addressParts[] = $row['City'];
if (!empty($row['AddressState'])) $addressParts[] = $row['AddressState'];
if (!empty($row['ZIPCode'])) $addressParts[] = $row['ZIPCode'];
$response['duplicates'][] = [
'BusinessID' => (int)$row['BusinessID'],
'Name' => $row['Name'],
'Address' => implode(', ', $addressParts),
];
}
} catch (Exception $e) {
$response['OK'] = false;
$response['error'] = $e->getMessage();
}
jsonResponse($response);