Merge branch 'fronkdev' into 'master'
Added API endpoint PUT /preorder/{id} to update contact data
See merge request fronk/thetool!1781
This commit is contained in:
@@ -53,6 +53,7 @@ class PreorderApicontroller extends mfBaseApicontroller {
|
||||
$this->addRoute("/preorder/:code/clientInstallationFinished", [$modules["Cif"], "providerSetCif"], "POST");
|
||||
$this->addRoute("/preorder/:code/serviceActivated", [$modules["Activation"], "setServiceActive"], "POST");
|
||||
$this->addRoute("/preorder/:code", "getPreorder", "GET");
|
||||
$this->addRoute("/preorder/:code", "updatePreorder", "PUT");
|
||||
$this->addRoute("/preorder/:code", "cancelPreorder", "DELETE");
|
||||
|
||||
|
||||
@@ -331,6 +332,67 @@ class PreorderApicontroller extends mfBaseApicontroller {
|
||||
|
||||
}
|
||||
|
||||
protected function updatePreorder($code) {
|
||||
$code = trim($code);
|
||||
if(!$code) {
|
||||
return mfResponse::NotFound(["message" => "Preorder not found"]);
|
||||
}
|
||||
|
||||
$preorder = PreorderModel::getFirst(['ucode' => strtoupper($code), 'partner_id' => $this->me->address_id]);
|
||||
if(!$preorder) {
|
||||
// try as extref
|
||||
$preorder = PreorderModel::getFirst(['extref' => $code, 'partner_id' => $this->me->address_id]);
|
||||
}
|
||||
if(!$preorder) {
|
||||
// try oan id
|
||||
$preorder = PreorderModel::getFirst(['oaid' => strtolower($code), 'partner_id' => $this->me->address_id], "`create` DESC");
|
||||
}
|
||||
|
||||
if(!$preorder) {
|
||||
return mfResponse::NotFound(["message" => "Preorder not found"]);
|
||||
}
|
||||
|
||||
|
||||
if($preorder->partner_id != $this->me->address_id) {
|
||||
return mfResponse::NotFound(["message" => "Preorder not found"]);
|
||||
}
|
||||
|
||||
$updates = [];
|
||||
foreach(["contact_type","company","uid","firstname","lastname","street","housenumber","block","stock","stiege","tuer","unit_string","zip","city","phone","email"] as $type) {
|
||||
if(array_key_exists($type, $this->post)) {
|
||||
$updates[$type] = trim($this->post[$type]);
|
||||
}
|
||||
}
|
||||
|
||||
if(!count($updates)) {
|
||||
return mfResponse::BadRequest(["message" => "No updates provided"]);
|
||||
}
|
||||
|
||||
if(array_key_exists("contact_type", $updates)) {
|
||||
if($updates["contact_type"] != "tenant" && $updates["contact_type"] != "owner") {
|
||||
return mfResponse::BadRequest(["message" => "Invalid contact type. Must be 'owner' or 'tenant'"]);
|
||||
}
|
||||
}
|
||||
|
||||
$updates["edit_by"] = $this->me->id;
|
||||
$preorder->update($updates);
|
||||
|
||||
// sanity checks
|
||||
if(!$preorder->company && (!$preorder->firstname || !$preorder->lastname)) {
|
||||
return mfResponse::BadRequest(["message" => "Cannot leave company or first- and lastname empty"]);
|
||||
}
|
||||
if(!$preorder->email) {
|
||||
return mfResponse::BadRequest(["message" => "Cannot leave email empty"]);
|
||||
}
|
||||
|
||||
if(!$preorder->save()) {
|
||||
return mfResponse::InternalServerError();
|
||||
}
|
||||
|
||||
return mfResponse::Ok(["message" => "Contact successfully updated"]);
|
||||
|
||||
}
|
||||
|
||||
protected function cancelPreorder($code) {
|
||||
if($this->me->is("Preorderreadonly")) return \mfResponse::Forbidden();
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user