Files
thetool/application/Api/v1/PreorderselfserviceApicontroller.php
2024-12-18 16:35:16 +01:00

115 lines
3.6 KiB
PHP

<?php
class PreorderselfserviceApicontroller extends mfBaseApicontroller {
public function init() {
$this->addRoute("/preorderselfservice/preorder", "getPreorder", "GET");
$this->addRoute("/preorderselfservice/:oaid/status/:status", "setSelfserviceStatus", "POST");
}
protected function authenticated() {
if($this->me->username != "preorder-selfservice-gui-api") {
return \mfResponse::Forbidden();
}
}
private function authUser() {
return true;
//return false;
}
protected function getPreorder() {
if(!$this->authUser()) {
return mfResponse::Forbidden(["status" => "403", "message" => "User authentication failed"]);
}
$auth_email = trim($this->get["auth_email"]);
$auth_phone = trim($this->get["auth_phone"]);
$search = [];
if($auth_email) {
$search = ["email" => $auth_email];
} elseif($auth_phone) {
$phone_search = [];
$auth_phone = preg_replace('/[^0-9]+/', '', $auth_phone);
$phone_search[] = $auth_phone;
if(substr($auth_phone, 0, 2) == "43") {
$phone_search[] = "+$auth_phone";
$auth_phone = preg_replace('/^43/', '0', $auth_phone);
$phone_search[] = $auth_phone;
}
if(substr($auth_phone, 0, 1) != "0") {
$phone_search[] = "0$auth_phone";
}
$search = ["phone" => $phone_search];
}
if(!count($search)) {
return mfResponse::BadRequest(["message" => "auth method missing"]);
}
$preorders = PreorderModel::searchActive($search);
$result = [];
foreach($preorders as $preorder) {
if(!$preorder->adb_hausnummer || !$preorder->adb_wohneinheit) continue;
//$hausnummer = $preorder->adb_hausnummer;
//$wohneinheit = $preorder->adb_wohneinheit;
$item = [];
$item["preorder"] = $preorder->getApiArray(["full_home", "full_address"]);
//$item["building"] = $hausnummer->getApiArray();
//$item["unit"] = $wohneinheit->getApiArray();
$result[] = $item;
}
return mfResponse::Ok($result);
}
/*
* /preorderselfservice/:oaid/:status
*/
protected function setSelfserviceStatus($params = []) {
if(!$params["oaid"] || !$params["status"]) {
return mfResponse::BadRequest(["message" => "mandatory parameters missing"]);
}
$oaid = $params["oaid"];
$status_code = $params["status"];
$preorder = PreorderModel::getFirst(["oaid" => $oaid]);
if(!$preorder) {
return mfResponse::NotFound(["message" => "OAID not found"]);
}
$valid_stati = [200, 242];
if(!in_array($status_code, $valid_stati)) {
return mfResponse::BadRequest(["message" => "Invalid Status code"]);
}
//var_dump($preorder->statusflags);exit;
$sflag = PreorderStatusflagModel::getFirst(["code" => $status_code]);
$sflag_val = PreorderStatusflagValueModel::getFirst(["preorder_id" => $preorder->id, "flag_id" => $sflag->id]);
if(!$sflag_val) {
$sflag_val = PreorderStatusflagValueModel::create([
"preorder_id" => $preorder->id,
"flag_id" => $sflag->id
]);
}
if($sflag_val->value != 1) {
$sflag_val->value = 1;
if(!$sflag_val->save()) {
return mfResponse::InternalServerError();
}
}
return mfResponse::Ok(["message" => "Status saved successfully"]);
}
}