40 lines
1.1 KiB
PHP
40 lines
1.1 KiB
PHP
<?php
|
|
require __DIR__ . '/_bootstrap.php';
|
|
|
|
/**
|
|
* @OA\Post(
|
|
* path="/owner-delete.php",
|
|
* tags={"Owners"},
|
|
* summary="Delete owner",
|
|
* security={{"bearerAuth":{}}},
|
|
* @OA\RequestBody(required=true, @OA\JsonContent(
|
|
* required={"owner_id"},
|
|
* @OA\Property(property="owner_id", type="integer")
|
|
* )),
|
|
* @OA\Response(response=200, ref="#/components/responses/Success"),
|
|
* @OA\Response(response=403, ref="#/components/responses/Forbidden")
|
|
* )
|
|
*/
|
|
require_method('POST');
|
|
$user = require_auth($pdo);
|
|
$ownerId = (int) (body()['owner_id'] ?? 0);
|
|
|
|
if ($ownerId <= 0) {
|
|
json_error(422, 'owner_id is required');
|
|
}
|
|
if (!user_owns_owner($pdo, $user, $ownerId)) {
|
|
json_error(403, 'No access to this owner');
|
|
}
|
|
|
|
$pdo->beginTransaction();
|
|
try {
|
|
$pdo->prepare('DELETE FROM home_owners WHERE owner_id = ?')->execute([$ownerId]);
|
|
$pdo->prepare('DELETE FROM property_owners WHERE owner_id = ? AND user_id = ?')->execute([$ownerId, $user['id']]);
|
|
$pdo->commit();
|
|
} catch (Throwable $e) {
|
|
$pdo->rollBack();
|
|
json_error(500, 'Delete failed');
|
|
}
|
|
|
|
json_ok();
|