payfrit-api/api/portal/addTeamMember.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

39 lines
1.2 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
$data = readJsonBody();
$businessId = (int) ($data['BusinessID'] ?? 0);
$userId = (int) ($data['UserID'] ?? 0);
$roleId = (int) ($data['RoleID'] ?? 1);
if ($roleId < 1 || $roleId > 3) $roleId = 1;
if ($businessId <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_business_id']);
}
if ($userId <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_user_id']);
}
// Check if already exists
$qCheck = queryOne(
"SELECT ID, IsActive FROM Employees WHERE BusinessID = ? AND UserID = ?",
[$businessId, $userId]
);
if ($qCheck) {
// Reactivate with role
queryTimed(
"UPDATE Employees SET IsActive = 1, StatusID = 2, RoleID = ? WHERE BusinessID = ? AND UserID = ?",
[$roleId, $businessId, $userId]
);
jsonResponse(['OK' => true, 'MESSAGE' => 'Employee reactivated', 'EmployeeID' => (int) $qCheck['ID']]);
}
// Insert new
queryTimed(
"INSERT INTO Employees (BusinessID, UserID, StatusID, IsActive, RoleID) VALUES (?, ?, 2, 1, ?)",
[$businessId, $userId, $roleId]
);
jsonResponse(['OK' => true, 'MESSAGE' => 'Team member added', 'EmployeeID' => (int) lastInsertId()]);