Merge branch 'feature/refactor-model-fields-and-relationships-06-11-2025' into develop

This commit is contained in:
2025-11-06 16:00:10 +01:00
13 changed files with 139 additions and 91 deletions

View File

@@ -13,10 +13,10 @@ class CompanyController extends Controller
{
public function all_simple()
{
$companies = Company::all()->makeHidden(['created_at', 'updated_at']);
$companies = Company::all();
$companies->each(function ($company) {
$company->contact = User::find($company->contact)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
$company->contact = User::find($company->contact);
});
return response()->json($companies);
@@ -41,7 +41,7 @@ class CompanyController extends Controller
], 400);
}
$company->contact = User::find($company->contact)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
$company->contact = User::find($company->contact);
return response()->json($company);
}

View File

@@ -19,12 +19,7 @@ class InternshipController extends Controller
abort(403, 'Unauthorized');
}
$internships = Internship::all()->makeHidden(['created_at', 'updated_at']);
$internships->each(function ($internship) {
$this->expandInternship($internship, true, true);
});
$internships = Internship::all();
return response()->json($internships);
}
@@ -33,21 +28,17 @@ class InternshipController extends Controller
$user = auth()->user();
if ($user->role === 'STUDENT') {
$internships = Internship::whereUserId($user->id)->get()->makeHidden(['created_at', 'updated_at']);
$internships = Internship::whereUserId($user->id)->get();
} elseif ($user->role === 'EMPLOYER') {
$company = Company::whereContact($user->id)->first();
if (!$company) {
return response()->json(['message' => 'No company associated with this user.'], 404);
}
$internships = Internship::whereCompanyId($company->id)->get()->makeHidden(['created_at', 'updated_at']);
$internships = Internship::whereCompanyId($company->id)->get();
} else {
abort(403, 'Unauthorized');
}
$internships->each(function ($internship) use ($user) {
$this->expandInternship($internship, $user->role === "EMPLOYER", true);
});
return response()->json($internships);
}
@@ -63,8 +54,6 @@ class InternshipController extends Controller
], 400);
}
$this->expandInternship($internship, false, false);
if ($user->role !== 'ADMIN' && $internship->user_id !== $user->id && $user->id !== $internship->company->contact) {
abort(403, 'Unauthorized');
}
@@ -310,29 +299,4 @@ class InternshipController extends Controller
], 400));
}
}
private function expandInternship(Internship $internship, bool $expand_user, bool $expand_dates)
{
if ($expand_user) {
$internship->user = User::find($internship->user_id)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
unset($internship->user_id);
}
$internship->company = Company::find($internship->company_id)->makeHidden(['created_at', 'updated_at']);
unset($internship->company_id);
$internship->contact = User::find($internship->company->contact)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
unset($internship->company->contact);
$internship->status = InternshipStatus::whereInternshipId($internship->id)->orderByDesc('changed')->get()->first()->makeHidden(['created_at', 'updated_at', 'id']);
$internship->status->modified_by = User::find($internship->status->modified_by)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
if ($expand_dates) {
$internship->start = Carbon::parse($internship->start)->format('d.m.Y');
$internship->end = Carbon::parse($internship->end)->format('d.m.Y');
}
$internship->agreement = $internship->agreement !== null;
$internship->report = $internship->report !== null;
}
}

View File

@@ -14,7 +14,7 @@ class InternshipStatusController extends Controller
public function get(int $id)
{
$user = auth()->user();
$internship_statuses = InternshipStatus::whereInternshipId($id)->orderByDesc('changed')->get()->makeHidden(['created_at', 'updated_at', 'id']);
$internship_statuses = InternshipStatus::whereInternshipId($id)->orderByDesc('changed')->get();
if (!$internship_statuses) {
return response()->json([
@@ -27,10 +27,6 @@ class InternshipStatusController extends Controller
abort(403, 'Unauthorized');
}
$internship_statuses->each(function ($internship_status) {
$internship_status->modified_by = User::find($internship_status->modified_by)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
});
return response()->json($internship_statuses);
}
@@ -49,7 +45,7 @@ class InternshipStatusController extends Controller
abort(403, 'Unauthorized');
}
$currentStatus = $this->currentInternshipStatus($internship);
$currentStatus = $internship->status;
$nextPossibleStatuses = $this->possibleNewStatuses($currentStatus->status, $user->role, $internship->report_confirmed);
return response()->json($nextPossibleStatuses);
@@ -113,7 +109,7 @@ class InternshipStatusController extends Controller
abort(403, 'Unauthorized');
}
$internshipStatus = $this->currentInternshipStatus($internship);
$internshipStatus = $internship->status;
$newStatusValidator = 'in:' . implode(',', $this->possibleNewStatuses($internshipStatus->status, $user->role, $internship->report_confirmed));
$request->validate([
@@ -172,9 +168,4 @@ class InternshipStatusController extends Controller
throw new \InvalidArgumentException('Unknown status');
}
}
private function currentInternshipStatus(Internship $internship)
{
return InternshipStatus::whereInternshipId($internship->id)->orderByDesc('changed')->firstOrFail();
}
}

View File

@@ -23,6 +23,16 @@ class Company extends Model
'hiring'
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'created_at',
'updated_at',
];
/**
* Get the internships for the company.
*/
@@ -34,7 +44,7 @@ class Company extends Model
/**
* Get the contact person (user) for the company.
*/
public function contactPerson()
public function contact()
{
return $this->belongsTo(User::class, 'contact');
}

View File

@@ -2,6 +2,7 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -28,6 +29,16 @@ class Internship extends Model
'report_confirmed',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'created_at',
'updated_at',
];
/**
* Get the attributes that should be cast.
*
@@ -49,4 +60,32 @@ class Internship extends Model
{
return $this->belongsTo(Company::class, 'company_id');
}
public function status()
{
return $this->hasOne(InternshipStatus::class, 'internship_id')->latestOfMany();
}
/**
* Prepare the model for JSON serialization.
*
* @return array<string, mixed>
*/
public function toArray(): array
{
return [
'id' => $this->id,
'student' => $this->student,
'company' => $this->company,
'start' => Carbon::parse($this->start)->format('d.m.Y'),
'end' => Carbon::parse($this->end)->format('d.m.Y'),
'year_of_study' => $this->year_of_study,
'semester' => $this->semester,
'position_description' => $this->position_description,
'agreement' => $this->agreement !== null,
'report' => $this->report !== null,
'report_confirmed' => $this->report_confirmed,
'status' => $this->status,
];
}
}

View File

@@ -22,4 +22,31 @@ class InternshipStatus extends Model
'note',
'modified_by'
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'created_at',
'updated_at',
];
public function modifiedBy()
{
return $this->belongsTo(User::class, 'modified_by');
}
public function toArray()
{
return [
'id' => $this->id,
'internship_id' => $this->internship_id,
'status' => $this->status,
'changed' => $this->changed,
'note' => $this->note,
'modified_by' => $this->modifiedBy,
];
}
}

View File

@@ -21,4 +21,14 @@ class StudentData extends Model
'personal_email',
'study_field',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'created_at',
'updated_at',
];
}

View File

@@ -39,6 +39,11 @@ class User extends Authenticatable
'password',
'remember_token',
'activation_token',
'created_at',
'updated_at',
'email_verified_at',
'active',
'needs_password_change'
];
/**

View File

@@ -49,7 +49,7 @@ const { data, error } = await useSanctumFetch<Internship[]>('/api/internships');
<tbody>
<tr v-for="item in data">
<td>{{ item.company.name }}</td>
<td>{{ item.user!.name }}</td>
<td>{{ item.student.name }}</td>
<td>{{ item.start }}</td>
<td>{{ item.end }}</td>
<td>{{ item.year_of_study }}</td>

View File

@@ -21,7 +21,7 @@ const loading = ref(false);
const action_error = ref(null as null | string);
const refreshKey = ref(0);
const { data, refresh } = await useSanctumFetch<Internship>(`/api/internships/${route.params.id}`);
const { data, error: load_error, refresh } = await useSanctumFetch<Internship>(`/api/internships/${route.params.id}`);
async function handleUpdateOfBasicInfo(internship: NewInternship) {
action_error.value = null;
@@ -53,47 +53,53 @@ async function handleUpdateOfBasicInfo(internship: NewInternship) {
<div style="height: 40px;"></div>
<!-- Čakajúca hláška -->
<LoadingAlert />
<LoadingAlert v-if="loading" />
<!-- Chybová hláška -->
<ErrorAlert v-if="action_error" :error="action_error" />
<div>
<h2>Základné informácie</h2>
<ErrorAlert v-if="data?.status.status !== InternshipStatus.SUBMITTED" title="Blokované"
error='Vaša prax nie je v stave "Zadaná" a teda nemôžete meniť údaje' />
<InternshipEditor v-else :internship="data!" :submit="handleUpdateOfBasicInfo" />
</div>
<!-- Chybová hláška -->
<ErrorAlert v-if="load_error" :error="load_error.message" />
<hr />
<div v-else>
<div>
<h2>Základné informácie</h2>
<ErrorAlert v-if="data?.status.status !== InternshipStatus.SUBMITTED" title="Blokované"
error='Vaša prax nie je v stave "Zadaná" a teda nemôžete meniť údaje' />
<InternshipEditor v-else :internship="data!" :submit="handleUpdateOfBasicInfo" />
</div>
<div>
<h2>Stav</h2>
<h4>Aktuálny stav</h4>
<p>{{ prettyInternshipStatus(data?.status.status!) }}</p>
<p>Poznámka: <em>{{ data?.status.note }}</em></p>
<p>Posledná zmena: <em>{{ data?.status.changed }}, {{ data?.status.modified_by.name }}</em></p>
<hr />
<br />
<div>
<h2>Stav</h2>
<h4>Aktuálny stav</h4>
<p>{{ prettyInternshipStatus(data?.status.status!) }}</p>
<p>Poznámka: <em>{{ data?.status.note }}</em></p>
<p>Posledná zmena: <em>{{ data?.status.changed }}, {{ data?.status.modified_by.name }}</em></p>
<h4>História</h4>
<InternshipStatusHistoryView :internship="data!" />
<br />
<br />
<h4>História</h4>
<InternshipStatusHistoryView :internship="data!" />
<h4>Zmena stavu</h4>
<InternshipStatusEditor :internship="data!" @successful-submit="() => { refresh(); refreshKey++; }" />
</div>
<br />
<hr />
<h4>Zmena stavu</h4>
<InternshipStatusEditor :internship="data!"
@successful-submit="() => { refresh(); refreshKey++; }" />
</div>
<div>
<h2>Nahratie dokumentov</h2>
<hr />
<ErrorAlert v-if="data?.status.status !== InternshipStatus.CONFIRMED" title="Blokované"
error='Vaša prax nie je v stave "Schválená" a teda nemôžete nahrať dokumenty.' />
<div>
<h2>Nahratie dokumentov</h2>
<InternshipDocumentEditor v-else :internship="data!" @successful-submit="refresh" />
<ErrorAlert v-if="data?.status.status !== InternshipStatus.CONFIRMED" title="Blokované"
error='Vaša prax nie je v stave "Schválená" a teda nemôžete nahrať dokumenty.' />
<InternshipDocumentEditor v-else :internship="data!" @successful-submit="refresh" />
</div>
</div>
</v-card>
</v-container>

View File

@@ -47,7 +47,7 @@ const { data, error } = await useSanctumFetch<Internship[]>('/api/internships/my
</thead>
<tbody>
<tr v-for="item in data">
<td>{{ item.user!.name }}</td>
<td>{{ item.student.name }}</td>
<td>{{ item.start }}</td>
<td>{{ item.end }}</td>
<td>{{ item.year_of_study }}</td>

View File

@@ -1,9 +1,6 @@
import { Role } from "./role";
import type { User } from "./user";
export interface InternshipStatusData {
internship_id: number;
user_id: string;
status: InternshipStatus;
changed: string;
note: string;
@@ -36,6 +33,6 @@ export function prettyInternshipStatus(status: InternshipStatus) {
case InternshipStatus.NOT_DEFENDED:
return "Neobhájené";
default:
throw new Error("Unknown status");
throw new Error(`Unknown internship status: '${status}'`);
}
}

View File

@@ -4,8 +4,7 @@ import type { User } from "./user";
export interface Internship {
id: number;
user_id?: string;
user?: User;
student: User;
company: CompanyData;
start: string;
end: string;