Files
thetool/application/Api/v1/Modules/Preorder/Activation.php
2025-03-20 16:02:05 +01:00

95 lines
2.8 KiB
PHP

<?php
namespace application\Api\v1\Modules\Preorder;
use application\Api\v1\Modules;
require_once(APPDIR."/Api/v1/Modules/ApiControllerModule.php");
/*
* API Endpoints for Preorder Activation
*/
class Activation extends Modules\ApiControllerModule {
public function init() {
}
/*
* POST /preorder/:code/serviceActivated
*/
public function setServiceActive($code) {
if($this->me->is("Preorderreadonly")) return \mfResponse::Forbidden();
$code = trim($code);
$activation_date = false;
if(array_key_exists("activation_date", $this->get)) {
$activation_date = trim($this->get['activation_date']);
}
if(!$code) {
return \mfResponse::NotFound(["message" => "Preorder not found"]);
}
$preorder = \PreorderModel::getFirst(['ucode' => strtoupper($code), 'partner_id' => $this->me->address_id]);
if(!$preorder) {
// try oaid
$preorder = \PreorderModel::getFirst(['oaid' => strtolower($code), 'partner_id' => $this->me->address_id], "`create` DESC");
}
if(!$preorder) {
// try as extref
$preorder = \PreorderModel::getFirst(['extref' => $code, 'partner_id' => $this->me->address_id]);
}
if(!$preorder) {
return \mfResponse::NotFound(["message" => "Preorder not found"]);
}
if($preorder->partner_id != $this->me->address_id) {
return \mfResponse::NotFound(["message" => "Preorder not found"]);
}
if($activation_date) {
try {
$adate = new \DateTime($activation_date);
$adate->setTimezone(new \DateTimeZone("Europe/Vienna"));
$adate->setTime(2, 0, 0); // set time to 2:00:00
// activation date cannot be in the future
if($adate > new \DateTime()) {
return \mfResponse::BadRequest(["message" => "Activation date can only be in the past"]);
}
} catch(\Exception $e) {
return \mfResponse::BadRequest(["message" => "Invalid activation_date format"]);
}
}
// set status to 500
if($preorder->status->code < 500) {
if($preorder->status->code < 242) {
return \mfResponse::BadRequest(["message" => "Service cannot be activated, because Construction work is not yet finished"]);
}
$new_status = \PreorderstatusModel::getFirst(["code" => 500]);
if(!$new_status) {
return \mfResponse::InternalServerError();
}
$preorder->status_id = $new_status->id;
$preorder->save();
if($adate) {
$history = \PreorderHistoryModel::getLastStatusChangeTo($preorder->id, 500);
if($history) {
$history->changed = $adate->getTimestamp();
$history->save();
}
}
}
return \mfResponse::Ok(["message" => "Status successfully updated."]);
}
}