Initial commit of testapi
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Http\Request;
|
||||
use Carbon\Carbon;
|
||||
|
||||
use App\Models\ApiKeys;
|
||||
|
||||
class SmartdokLiveController extends Controller
|
||||
{
|
||||
private string $serviceName = 'smartdok';
|
||||
private string $apiKey;
|
||||
private string $authToken;
|
||||
|
||||
private string $serviceUrl = 'https://api.smartdok.no';
|
||||
|
||||
public function __construct() {
|
||||
$record = ApiKeys::where('category', $this->serviceName)->first();
|
||||
if (! $record) {
|
||||
throw new HttpResponseException(
|
||||
response()->json([
|
||||
'feil' => "ingen tjenesteoppføring funnet for tjenesten '{$this->serviceName}', som betyr at du ikke har definert en API nøkkel for denne tjenesten enda. Logg inn og legg til en API nøkkel først"
|
||||
], 404)
|
||||
);
|
||||
}
|
||||
|
||||
$this->apiKey = $record->key;
|
||||
|
||||
$smartdokAuthorizeRequest = Http::withHeaders([
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json'
|
||||
])->post("{$this->serviceUrl}/Authorize/ApiToken", [
|
||||
'input' => $this->apiKey
|
||||
]);
|
||||
|
||||
$response = $smartdokAuthorizeRequest->json();
|
||||
|
||||
if (!isset($response['Token'])) {
|
||||
throw new HttpResponseException(
|
||||
response()->json([
|
||||
'feil' => "Kunne ikke hente Visma SmartDok autorisasjon ved bruk av den lagrede API nøkkelen i mellomvaren"
|
||||
], 403)
|
||||
);
|
||||
}
|
||||
|
||||
$this->authToken = $response['Token'];
|
||||
}
|
||||
|
||||
public function users() {
|
||||
$smartdokUsersRequest = Http::withHeaders([
|
||||
'Authorization' => "Bearer {$this->authToken}",
|
||||
'Accept' => 'application/json'
|
||||
])->get("{$this->serviceUrl}/Users");
|
||||
|
||||
return $smartdokUsersRequest->json();
|
||||
}
|
||||
|
||||
public function user(string $id) {
|
||||
$smartdokUsersRequest = Http::withHeaders([
|
||||
'Authorization' => "Bearer {$this->authToken}",
|
||||
'Accept' => 'application/json'
|
||||
])->get("{$this->serviceUrl}/Users/{$id}");
|
||||
|
||||
return $smartdokUsersRequest->json();
|
||||
}
|
||||
|
||||
public function departments() {
|
||||
$smartdokDepartmentsRequest = Http::withHeaders([
|
||||
'Authorization' => "Bearer {$this->authToken}",
|
||||
'Accept' => 'application/json'
|
||||
])->get("{$this->serviceUrl}/Departments");
|
||||
|
||||
return $smartdokDepartmentsRequest->json();
|
||||
}
|
||||
|
||||
public function department(string $id) {
|
||||
$smartdokDepartmentsRequest = Http::withHeaders([
|
||||
'Authorization' => "Bearer {$this->authToken}",
|
||||
'Accept' => 'application/json'
|
||||
])->get("{$this->serviceUrl}/Departments/{$id}");
|
||||
|
||||
return $smartdokDepartmentsRequest->json();
|
||||
}
|
||||
|
||||
public function projects() {
|
||||
$smartdokProjectsRequest = Http::withHeaders([
|
||||
'Authorization' => "Bearer {$this->authToken}",
|
||||
'Accept' => 'application/json'
|
||||
])->get("{$this->serviceUrl}/Projects");
|
||||
|
||||
return $smartdokProjectsRequest->json();
|
||||
}
|
||||
|
||||
public function project(string $id) {
|
||||
$smartdokProjectsRequest = Http::withHeaders([
|
||||
'Authorization' => "Bearer {$this->authToken}",
|
||||
'Accept' => 'application/json'
|
||||
])->get("{$this->serviceUrl}/Projects/{$id}");
|
||||
|
||||
return $smartdokProjectsRequest->json();
|
||||
}
|
||||
|
||||
public function WorkHours(Request $request) {
|
||||
$request->validate([
|
||||
'fromDate' => 'required|date',
|
||||
'toDate' => 'required|date',
|
||||
'projectId' => 'nullable|integer',
|
||||
]);
|
||||
|
||||
$from = Carbon::parse($request->query('fromDate'))
|
||||
->startOfDay()
|
||||
->toDateTimeString(); // “2025-05-12 00:00:00”
|
||||
|
||||
$to = Carbon::parse($request->query('toDate'))
|
||||
->endOfDay()
|
||||
->toDateTimeString(); // “2025-05-12 23:59:59”
|
||||
|
||||
$query = [
|
||||
'fromDate' => $from,
|
||||
'toDate' => $to,
|
||||
];
|
||||
|
||||
if (! is_null($request->input('projectId'))) {
|
||||
$query['projectId'] = $request->input('projectId');
|
||||
}
|
||||
|
||||
$smartdokWorkHourRequest = Http::withHeaders([
|
||||
'Authorization' => "Bearer {$this->authToken}",
|
||||
'Accept' => 'application/json'
|
||||
])->get("{$this->serviceUrl}/WorkHours", $query);
|
||||
|
||||
return $smartdokWorkHourRequest->json();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user