familyHub/api/chore_nfc_rotate.php
Louis Whittington f14de0b7e1 Add NFC chore submission feature and enhance family settings
- Introduced NFC support for chore submissions, allowing specific person credit after Head of Household approval.
- Updated family settings to include NFC base URL, scan cooldown, and confirmation page options.
- Enhanced chore management with options for anyone to complete chores and NFC link generation.
- Improved API endpoints for handling NFC tokens and chore submissions.
- Updated readme to reflect new NFC features and settings.
2026-03-31 10:28:27 -05:00

73 lines
2.5 KiB
PHP

<?php
require_once __DIR__ . '/../includes/api_bootstrap.php';
require_once __DIR__ . '/../includes/chore_helpers.php';
require_once __DIR__ . '/../includes/family_settings.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
sendJson(['success' => false, 'error' => 'Method not allowed'], 405);
}
$people = migrateAllPeople(normalizePeopleList(readJsonFile('people.json')));
$actor = requireActivePerson($people);
if (($actor['role'] ?? '') !== ROLE_HEAD || !isHohVerified()) {
sendJson(['success' => false, 'error' => 'Only a verified Head of household can manage NFC'], 403);
}
$body = readJsonBody();
$id = isset($body['id']) ? trim((string) $body['id']) : '';
if ($id === '') {
sendJson(['success' => false, 'error' => 'id is required'], 400);
}
$rawChores = normalizeChoresList(readJsonFile('chores.json'));
$chores = migrateAllChores($rawChores, $people);
$idx = findChoreIndexById($chores, $id);
if ($idx === null) {
sendJson(['success' => false, 'error' => 'Chore not found'], 404);
}
$choreToken = generateOpaqueToken();
$chore = $chores[$idx];
$nfcMeta = normalizeChoreNfcMeta($chore['nfc'] ?? null);
$nfcMeta['token_hash'] = hashOpaqueToken($choreToken);
$nfcMeta['enabled'] = true;
$nfcMeta['created_at'] = gmdate('c');
$chore['nfc'] = $nfcMeta;
$chores[$idx] = migrateLegacyChoreRow($chore, $people);
$updatedPeople = $people;
$links = [];
$familySettings = loadFamilySettings();
$base = familyHubAppUrl($familySettings) . '/api/chore_submit_nfc.php';
foreach ($updatedPeople as $pi => $person) {
$personId = (string) ($person['id'] ?? '');
if ($personId === '') {
continue;
}
$personName = (string) ($person['name'] ?? $personId);
$personToken = generateOpaqueToken();
$updatedPeople[$pi]['nfc_submit_token_hash'] = hashOpaqueToken($personToken);
$updatedPeople[$pi]['nfc_submit_token_updated_at'] = gmdate('c');
$links[] = [
'person_id' => $personId,
'person_name' => $personName,
'url' => $base . '?id=' . rawurlencode($id) . '&token=' . rawurlencode($choreToken) . '&person_token=' . rawurlencode($personToken),
];
}
if (!writeJsonFile('people.json', $updatedPeople)) {
sendJson(['success' => false, 'error' => 'Failed to save person NFC tokens'], 500);
}
if (!writeJsonFile('chores.json', $chores)) {
sendJson(['success' => false, 'error' => 'Failed to save chore NFC token'], 500);
}
sendJson([
'success' => true,
'enabled' => true,
'links' => $links,
'nfc' => $nfcMeta,
]);