Add /subusers/me route for public registration and extend OpenAPI schema
- Add `/subusers/me` as a public registration endpoint, including CVR validation, phone lookup, and optional SMS setup link generation. - Extend OpenAPI specification with `SubuserSelf` and `SubuserGrantSummary` schemas for returning authenticated subuser profiles and grants.
This commit is contained in:
@@ -587,5 +587,83 @@ class subusersRoute
|
||||
];
|
||||
$response->success($result);
|
||||
}, []);
|
||||
|
||||
// Public registration endpoint (alias of POST /subusers) matching OpenAPI: POST /subusers/me
|
||||
$this->post('/subusers/me', function () {
|
||||
global /** @var response $response */
|
||||
$response;
|
||||
// Required input
|
||||
self::requireParameters([
|
||||
'cvr',
|
||||
'phone_country_code',
|
||||
'phone'
|
||||
]);
|
||||
|
||||
$cvr = (int)self::getParameter('cvr');
|
||||
$phone_country_code = (int)self::getParameter('phone_country_code');
|
||||
$phone = (int)self::getParameter('phone');
|
||||
|
||||
// Validation
|
||||
self::requireType($cvr, self::type_int());
|
||||
self::requireMinLength('cvr', 8);
|
||||
self::requireMaxLength('cvr', 8);
|
||||
self::requireType($phone_country_code, self::type_int());
|
||||
self::requireMinLength('phone_country_code', 1);
|
||||
self::requireMaxLength('phone_country_code', 3);
|
||||
self::requireType($phone, self::type_int());
|
||||
self::requireMinLength('phone', 4);
|
||||
self::requireMaxLength('phone', 15);
|
||||
|
||||
// Look up company by CVR in e-conomic
|
||||
$economic = new economic();
|
||||
$results = $economic->customers->customers->search([
|
||||
'corporateIdentificationNumber' => (string)$cvr,
|
||||
], [
|
||||
'skipPages' => 0,
|
||||
'pageSize' => 1000,
|
||||
])->collection;
|
||||
if (count($results) === 0) {
|
||||
$response->error('Customer not found', 404);
|
||||
}
|
||||
|
||||
// Ensure company user exists (sanity) and phone is not already registered
|
||||
$company_user = (new users_o())->getUserByCustomerNumber((int)$results[0]->customerNumber);
|
||||
$company_user->requireSelected();
|
||||
|
||||
$existing = (new subusers_o())->getSubuserByPhone($phone_country_code, $phone);
|
||||
if ($existing !== null) {
|
||||
$response->error('Account already exists with this phone number', 400);
|
||||
}
|
||||
|
||||
// Create subuser skeleton
|
||||
$subuser = (new subusers_o())->add(
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
(int)$phone_country_code,
|
||||
(int)$phone
|
||||
);
|
||||
|
||||
// Optionally send SMS with setup link
|
||||
$gatewayAPI = new gatewayapi();
|
||||
if ($gatewayAPI->isEnabled()) {
|
||||
$token = $subuser->generateSetupToken();
|
||||
$link = 'https://truckwash.io/complete-registration?token=' . $token;
|
||||
$message = 'Tak for din oprettelse af chaufførkonto hos Truck Wash! Klik på linket for at fuldføre registreringen: ' . $link;
|
||||
$phone_number_array = [(string)$phone_country_code . (string)$phone];
|
||||
$gatewayAPI->send($phone_number_array, $message);
|
||||
}
|
||||
|
||||
// Create a pending grant request for the company
|
||||
$subuser_grants_o = new subuser_grants_o();
|
||||
try {
|
||||
$subuser_grants_o->add($results[0]->customerNumber, $subuser->id, false, null);
|
||||
} catch (Exception $e) {
|
||||
$response->error('Failed to add subuser grant', 500);
|
||||
}
|
||||
|
||||
$response->success(['cvr' => $cvr, 'customer_number' => $results[0]->customerNumber]);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user