Files
casadoc/public/userportal/api/me-save.php
T
2026-07-27 21:04:07 +03:00

47 lines
1.6 KiB
PHP

<?php
require __DIR__ . '/_bootstrap.php';
/**
* @OA\Post(
* path="/me-save.php",
* tags={"Auth"},
* summary="Update the current user's profile",
* description="Partial update: only the keys present in the body are changed. E-mail is read-only here.",
* security={{"bearerAuth":{}}},
* @OA\RequestBody(required=true, @OA\JsonContent(
* @OA\Property(property="first_name", type="string", nullable=true),
* @OA\Property(property="last_name", type="string", nullable=true),
* @OA\Property(property="phone", type="string", nullable=true),
* @OA\Property(property="address", type="string", nullable=true)
* )),
* @OA\Response(response=200, description="OK", @OA\JsonContent(
* @OA\Property(property="data", ref="#/components/schemas/User"))),
* @OA\Response(response=401, ref="#/components/responses/Unauthorized"),
* @OA\Response(response=422, ref="#/components/responses/ValidationError")
* )
*/
require_method('POST');
$user = require_auth($pdo);
$in = body();
$data = [];
foreach (['first_name', 'last_name', 'phone', 'address'] as $col) {
if (array_key_exists($col, $in)) {
$value = $in[$col];
$data[$col] = ($value === null || trim((string) $value) === '') ? null : trim((string) $value);
}
}
if (!$data) {
json_error(422, 'Nothing to update');
}
$set = implode(', ', array_map(fn ($c) => "$c = ?", array_keys($data)));
$pdo->prepare("UPDATE auth_users SET $set WHERE id = ?")
->execute([...array_values($data), $user['id']]);
$stmt = $pdo->prepare('SELECT * FROM auth_users WHERE id = ? LIMIT 1');
$stmt->execute([$user['id']]);
json_data(present_user($stmt->fetch()));