bulk client creation
Some checks failed
PHPUnit / test (push) Has been cancelled

This commit is contained in:
2026-07-07 16:53:35 +02:00
parent 181ca2b7d4
commit 4afab336ee
7 changed files with 566 additions and 29 deletions

View File

@ -62,18 +62,17 @@ case 'GET':
break;
case 'POST':
$body = read_json_body();
$clientCode = trim($body['clientCode'] ?? '');
$coachID = trim($body['coachID'] ?? '');
$body = read_json_body();
$coachID = trim($body['coachID'] ?? '');
$bulk = !empty($body['bulk']);
if ($clientCode === '' || $coachID === '') {
json_error('MISSING_FIELDS', 'clientCode and coachID are required', 400);
if ($coachID === '') {
json_error('MISSING_FIELDS', 'coachID is required', 400);
}
try {
[$pdo, $tmpDb, $lockFp] = qdb_open_write_or_fail();
// Validate coach exists and caller is allowed to assign to them
if ($callerRole === 'supervisor') {
$chkCoach = $pdo->prepare("SELECT coachID FROM coach WHERE coachID = :cid AND supervisorID = :sid");
$chkCoach->execute([':cid' => $coachID, ':sid' => $callerEntityID]);
@ -86,6 +85,57 @@ case 'POST':
json_error('NOT_FOUND', 'Counselor not found or not authorized', 404);
}
if ($bulk) {
$fromCode = trim($body['fromCode'] ?? '');
$toCode = trim($body['toCode'] ?? '');
if ($fromCode === '' || $toCode === '') {
qdb_discard($tmpDb, $lockFp);
json_error('MISSING_FIELDS', 'fromCode and toCode are required for bulk creation', 400);
}
require_once __DIR__ . '/../lib/client_code_range.php';
try {
$codes = qdb_expand_client_code_range($fromCode, $toCode);
} catch (InvalidArgumentException $e) {
qdb_discard($tmpDb, $lockFp);
json_error('INVALID_RANGE', $e->getMessage(), 400);
}
$chk = $pdo->prepare("SELECT clientCode FROM client WHERE clientCode = :cc");
$insert = $pdo->prepare(
"INSERT INTO client (clientCode, coachID, archived, archivedAt) VALUES (:cc, :cid, 0, 0)"
);
$created = [];
$skipped = [];
foreach ($codes as $code) {
$chk->execute([':cc' => $code]);
if ($chk->fetch()) {
$skipped[] = ['clientCode' => $code, 'reason' => 'duplicate'];
continue;
}
$insert->execute([':cc' => $code, ':cid' => $coachID]);
$created[] = $code;
}
qdb_save($tmpDb, $lockFp);
json_success([
'coachID' => $coachID,
'fromCode' => $fromCode,
'toCode' => $toCode,
'created' => $created,
'skipped' => $skipped,
'createdCount' => count($created),
'skippedCount' => count($skipped),
]);
}
$clientCode = trim($body['clientCode'] ?? '');
if ($clientCode === '') {
qdb_discard($tmpDb, $lockFp);
json_error('MISSING_FIELDS', 'clientCode is required', 400);
}
$chk = $pdo->prepare("SELECT clientCode FROM client WHERE clientCode = :cc");
$chk->execute([':cc' => $clientCode]);
if ($chk->fetch()) {