Initial commit of testapi
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AuthenticationController extends Controller
|
||||
{
|
||||
/**
|
||||
* @unauthenticated
|
||||
*
|
||||
* @return "{'status': 'success','token': 'plaintexttoken'}"
|
||||
*
|
||||
*
|
||||
*/
|
||||
public function authorize(Request $request) {
|
||||
$request->validate([
|
||||
'email' => 'required|email',
|
||||
'password' => 'required|string'
|
||||
]);
|
||||
|
||||
if (!$request->has('email') || !$request->has('password')) {
|
||||
return json_encode([
|
||||
'status' => 'invalid',
|
||||
'message' => 'ingen brukernavn eller passord oppgitt'
|
||||
], 422);
|
||||
}
|
||||
|
||||
$credentials = $request->only('email', 'password');
|
||||
|
||||
if (Auth::attempt($credentials)) {
|
||||
$user = Auth::user();
|
||||
$token = $user->createToken($request->token_name ?? 'default');
|
||||
return json_encode([
|
||||
'status' => 'success',
|
||||
'token' => $token->plainTextToken
|
||||
], Response::HTTP_ACCEPTED);
|
||||
}
|
||||
|
||||
return json_encode([
|
||||
'status' => 'invalid',
|
||||
'message' => 'ugyldig bruker/passord'
|
||||
], Response::HTTP_UNAUTHORIZED);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Carbon\Carbon;
|
||||
|
||||
use App\Models\Department;
|
||||
|
||||
class DepartmentController extends Controller
|
||||
{
|
||||
/**
|
||||
* GET /Departments
|
||||
* Query params:
|
||||
* - departmentName (string)
|
||||
* - updatedSince (date-time)
|
||||
*/
|
||||
public function index(Request $request) {
|
||||
$q = Department::query();
|
||||
|
||||
if ($name = $request->query('departmentName')) {
|
||||
$q->where('name', $name);
|
||||
}
|
||||
|
||||
if ($since = $request->query('updatedSince')) {
|
||||
$q->where('updated_at', '>=', Carbon::parse($since));
|
||||
}
|
||||
|
||||
$out = $q->get()->map(function ($d) {
|
||||
return [
|
||||
'Id' => $d->id,
|
||||
'Name' => $d->name,
|
||||
'Updated' => $d->updated_at->toIso8601String().'Z',
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json($out);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /Departments/{id}
|
||||
*/
|
||||
public function show(int $id) {
|
||||
$d = Department::findOrFail($id);
|
||||
|
||||
return response()->json([
|
||||
'Id' => $d->id,
|
||||
'Name' => $d->name,
|
||||
'Updated' => $d->updated_at->toIso8601String().'Z',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Carbon\Carbon;
|
||||
|
||||
use App\Models\Project;
|
||||
|
||||
class ProjectController extends Controller
|
||||
{
|
||||
/**
|
||||
* GET /Projects
|
||||
* Query params:
|
||||
* - projectNumber (string)
|
||||
* - projectName (string)
|
||||
* - updatedSince (date-time)
|
||||
* - createdSince (date-time)
|
||||
*/
|
||||
public function index(Request $request) {
|
||||
$q = Project::with(['departments', 'profiles']);
|
||||
|
||||
if ($num = $request->query('projectNumber')) {
|
||||
$q->where('project_number', $num);
|
||||
}
|
||||
if ($name = $request->query('projectName')) {
|
||||
$q->where('project_name', $name);
|
||||
}
|
||||
if ($sinceUp = $request->query('updatedSince')) {
|
||||
$q->where('updated_at', '>=', Carbon::parse($sinceUp));
|
||||
}
|
||||
if ($sinceCr = $request->query('createdSince')) {
|
||||
$q->where('created_at', '>=', Carbon::parse($sinceCr));
|
||||
}
|
||||
|
||||
$out = $q->get()->map(function ($p) {
|
||||
return [
|
||||
'Id' => $p->id,
|
||||
'ProjectName' => $p->project_name,
|
||||
'ProjectNumber' => $p->project_number,
|
||||
'Departments' => $p->departments->pluck('id')->all(),
|
||||
'UserIds' => $p->profiles->pluck('id')->all(),
|
||||
'Updated' => $p->updated_at->toIso8601String().'Z',
|
||||
'Created' => $p->created_at->toIso8601String().'Z',
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json($out);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /Projects/{id}
|
||||
*/
|
||||
public function show(int $id) {
|
||||
$p = Project::with(['departments', 'profiles'])->findOrFail($id);
|
||||
|
||||
return response()->json([
|
||||
'Id' => $p->id,
|
||||
'ProjectName' => $p->project_name,
|
||||
'ProjectNumber' => $p->project_number,
|
||||
'Departments' => $p->departments->pluck('id')->all(),
|
||||
'UserIds' => $p->profiles->pluck('id')->all(),
|
||||
'Updated' => $p->updated_at->toIso8601String().'Z',
|
||||
'Created' => $p->created_at->toIso8601String().'Z',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use App\Models\SmartdokProfile;
|
||||
|
||||
class SmartdokProfileController extends Controller
|
||||
{
|
||||
/**
|
||||
* List all users
|
||||
*
|
||||
* Returns a JSON array of all the smartdok users registered on the system
|
||||
*
|
||||
* @response \App\Models\SmartdokProfile
|
||||
*/
|
||||
public function index() {
|
||||
$profiles = SmartdokProfile::all()->map(function ($p) {
|
||||
return [
|
||||
'Id' => $p->id,
|
||||
'UserName' => $p->username,
|
||||
'Name' => $p->name,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json($profiles);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user
|
||||
*
|
||||
* Returns a JSON array of all the smartdok users registered on the system
|
||||
*
|
||||
* @param $id
|
||||
*
|
||||
* Example response:
|
||||
* {
|
||||
* "Id": "00000000-0000-0000-0000-000000000000",
|
||||
* "UserName": "string",
|
||||
* "Name": "string"
|
||||
* }
|
||||
*/
|
||||
public function show(string $id) {
|
||||
$p = SmartdokProfile::findOrFail($id);
|
||||
|
||||
return response()->json([
|
||||
'Id' => $p->id,
|
||||
'UserName' => $p->username,
|
||||
'Name' => $p->name,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Carbon\Carbon;
|
||||
|
||||
use App\Models\WorkHour;
|
||||
|
||||
class WorkHourController extends Controller
|
||||
{
|
||||
/**
|
||||
* GET /WorkHours
|
||||
*
|
||||
* Required query params:
|
||||
* - fromDate : date-time (filters work_date >= fromDate)
|
||||
* - toDate : date-time (filters work_date <= toDate)
|
||||
* Optional query param:
|
||||
* - projectId : integer (filters by associated project)
|
||||
*
|
||||
* @response \App\Models\WorkHour
|
||||
*/
|
||||
public function index(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”
|
||||
|
||||
$q = WorkHour::with('project.departments')
|
||||
->whereBetween('work_date', [$from, $to]);
|
||||
|
||||
if ($pid = $request->query('projectId')) {
|
||||
// filter directly on the foreign key
|
||||
$q->where('project_id', $pid);
|
||||
}
|
||||
|
||||
$result = $q->get()->map(function (WorkHour $w) {
|
||||
$deptIds = $w->project
|
||||
->departments
|
||||
->pluck('id')
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
|
||||
return [
|
||||
'WorkHourID' => $w->id,
|
||||
'UserID' => $w->user_id,
|
||||
'ProjectID' => $w->project_id,
|
||||
'OrdinaryHours' => $w->ordinary_hours,
|
||||
'Date' => $w->work_date->toDateString(),
|
||||
'Comment' => $w->comment,
|
||||
'DepartmentIDsProject' => $deptIds,
|
||||
'Updated' => $w->updated_at->toIso8601String() . 'Z',
|
||||
'Created' => $w->created_at->toIso8601String() . 'Z',
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json($result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user