Files
thetool/application/Api/v1/InvestigatorApicontroller.php
2026-01-28 22:50:16 +01:00

177 lines
6.6 KiB
PHP

<?php
class InvestigatorApicontroller extends mfBaseApicontroller
{
protected function init() {}
protected function registerRoutes()
{
$this->addRoute("/investigator/customer/:id", "getCustomer", "GET");
$this->addRoute("/investigator/customer/:id/contract", "getCustomerContract", "GET");
$this->addRoute("/investigator/customer/:id/cpe", "getCustomerCpe", "GET");
$this->addRoute("/investigator/customer/:id/address", "getCustomerAddress", "GET");
$this->addRoute("/investigator/search/customer", "searchCustomer", "GET");
}
protected function authenticated()
{
$this->registerRoutes();
}
public function getCustomer($customerId)
{
if (!$customerId) return mfResponse::BadRequest(['message' => 'Customer ID is required']);
$addresses = AddressModel::search(['customer_number' => $customerId]);
if (empty($addresses)) return mfResponse::NotFound(['message' => 'Customer not found']);
$address = $addresses[0];
return mfResponse::Ok([
'customer' => [
'id' => $address->id,
'customerNumber' => $address->customer_number,
'company' => $address->company,
'firstName' => $address->firstname,
'lastName' => $address->lastname,
'email' => $address->email,
'phone' => $address->phone,
'street' => $address->street,
'zip' => $address->zip,
'city' => $address->city,
'country' => $address->country,
]
]);
}
public function getCustomerContract($customerId)
{
if (!$customerId) return mfResponse::BadRequest(['message' => 'Customer ID is required']);
$addresses = AddressModel::search(['customer_number' => $customerId]);
if (empty($addresses)) return mfResponse::NotFound(['message' => 'Customer not found']);
$contracts = ContractModel::search(['owner_id' => $addresses[0]->id]);
$contractData = [];
foreach ($contracts as $contract) {
$contractData[] = [
'contractId' => $contract->contract_id,
'productName' => $contract->product_name,
'productExternal' => $contract->product_external,
'price' => $contract->price,
'billingPeriod' => $contract->billing_period,
'orderDate' => $contract->order_date,
'finishDate' => $contract->finish_date,
'cancelDate' => $contract->cancel_date,
'slaId' => $contract->sla_id,
'status' => $contract->cancel_date ? 'Cancelled' : ($contract->finish_date ? 'Active' : 'Pending'),
];
}
return mfResponse::Ok([
'customerId' => $customerId,
'contractCount' => count($contractData),
'contracts' => $contractData,
]);
}
public function getCustomerCpe($customerId)
{
if (!$customerId) return mfResponse::BadRequest(['message' => 'Customer ID is required']);
$addresses = AddressModel::search(['customer_number' => $customerId]);
if (empty($addresses)) return mfResponse::NotFound(['message' => 'Customer not found']);
$db = $this->db();
$sql = "SELECT cp.* FROM Cpeprovisioning cp
INNER JOIN `Order` o ON cp.order_id = o.id
WHERE o.owner_id = " . intval($addresses[0]->id) . "
ORDER BY cp.id DESC LIMIT 10";
$res = $db->query($sql);
$cpeData = [];
while ($row = $db->fetch_object($res)) {
$cpeData[] = [
'orderId' => $row->order_id ?? null,
'routerType' => $row->routertype ?? null,
'routerConfigFinished' => (bool)($row->routerconfig_finished ?? false),
'mac' => $row->mac ?? null,
'ontSerial' => $row->ont_sn ?? null,
'wifiSsid' => $row->wifi_ssid ?? null,
'wifiPasswordSet' => !empty($row->wifi_pass),
'vlanPublic' => $row->vlan_public ?? null,
'vlanNat' => $row->vlan_nat ?? null,
'vlanIpv6' => $row->vlan_ipv6 ?? null,
'shipping' => (bool)($row->shipping ?? false),
];
}
return mfResponse::Ok([
'customerId' => $customerId,
'cpeCount' => count($cpeData),
'cpeProvisioning' => $cpeData,
]);
}
public function getCustomerAddress($customerId)
{
if (!$customerId) return mfResponse::BadRequest(['message' => 'Customer ID is required']);
$addresses = AddressModel::search(['customer_number' => $customerId]);
if (empty($addresses)) return mfResponse::NotFound(['message' => 'Customer not found']);
$address = $addresses[0];
return mfResponse::Ok([
'customerId' => $customerId,
'address' => [
'company' => $address->company,
'name' => trim(($address->firstname ?? '') . ' ' . ($address->lastname ?? '')),
'street' => $address->street,
'zip' => $address->zip,
'city' => $address->city,
'country' => $address->country,
'email' => $address->email,
'phone' => $address->phone,
]
]);
}
public function searchCustomer()
{
$searchTerm = $this->get['q'] ?? '';
$limit = intval($this->get['limit'] ?? 10);
if (empty($searchTerm)) return mfResponse::BadRequest(['message' => 'Search term required']);
$db = $this->db();
$searchTerm = $db->escape($searchTerm);
$sql = "SELECT * FROM Address
WHERE customer_number LIKE '%$searchTerm%'
OR CONCAT(firstname, ' ', lastname) LIKE '%$searchTerm%'
OR company LIKE '%$searchTerm%'
OR email LIKE '%$searchTerm%'
OR street LIKE '%$searchTerm%'
LIMIT $limit";
$results = $db->queryRows($sql);
$customers = [];
foreach ($results as $row) {
$customers[] = [
'customerId' => $row['customer_number'],
'name' => trim(($row['firstname'] ?? '') . ' ' . ($row['lastname'] ?? '')) ?: $row['company'],
'email' => $row['email'],
'city' => $row['city'],
];
}
return mfResponse::Ok([
'searchTerm' => $searchTerm,
'count' => count($customers),
'customers' => $customers,
]);
}
}