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() public function all_simple()
{ {
$companies = Company::all()->makeHidden(['created_at', 'updated_at']); $companies = Company::all();
$companies->each(function ($company) { $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); return response()->json($companies);
@@ -41,7 +41,7 @@ class CompanyController extends Controller
], 400); ], 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); return response()->json($company);
} }

View File

@@ -19,12 +19,7 @@ class InternshipController extends Controller
abort(403, 'Unauthorized'); abort(403, 'Unauthorized');
} }
$internships = Internship::all()->makeHidden(['created_at', 'updated_at']); $internships = Internship::all();
$internships->each(function ($internship) {
$this->expandInternship($internship, true, true);
});
return response()->json($internships); return response()->json($internships);
} }
@@ -33,21 +28,17 @@ class InternshipController extends Controller
$user = auth()->user(); $user = auth()->user();
if ($user->role === 'STUDENT') { 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') { } elseif ($user->role === 'EMPLOYER') {
$company = Company::whereContact($user->id)->first(); $company = Company::whereContact($user->id)->first();
if (!$company) { if (!$company) {
return response()->json(['message' => 'No company associated with this user.'], 404); 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 { } else {
abort(403, 'Unauthorized'); abort(403, 'Unauthorized');
} }
$internships->each(function ($internship) use ($user) {
$this->expandInternship($internship, $user->role === "EMPLOYER", true);
});
return response()->json($internships); return response()->json($internships);
} }
@@ -63,8 +54,6 @@ class InternshipController extends Controller
], 400); ], 400);
} }
$this->expandInternship($internship, false, false);
if ($user->role !== 'ADMIN' && $internship->user_id !== $user->id && $user->id !== $internship->company->contact) { if ($user->role !== 'ADMIN' && $internship->user_id !== $user->id && $user->id !== $internship->company->contact) {
abort(403, 'Unauthorized'); abort(403, 'Unauthorized');
} }
@@ -310,29 +299,4 @@ class InternshipController extends Controller
], 400)); ], 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) public function get(int $id)
{ {
$user = auth()->user(); $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) { if (!$internship_statuses) {
return response()->json([ return response()->json([
@@ -27,10 +27,6 @@ class InternshipStatusController extends Controller
abort(403, 'Unauthorized'); 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); return response()->json($internship_statuses);
} }
@@ -49,7 +45,7 @@ class InternshipStatusController extends Controller
abort(403, 'Unauthorized'); abort(403, 'Unauthorized');
} }
$currentStatus = $this->currentInternshipStatus($internship); $currentStatus = $internship->status;
$nextPossibleStatuses = $this->possibleNewStatuses($currentStatus->status, $user->role, $internship->report_confirmed); $nextPossibleStatuses = $this->possibleNewStatuses($currentStatus->status, $user->role, $internship->report_confirmed);
return response()->json($nextPossibleStatuses); return response()->json($nextPossibleStatuses);
@@ -113,7 +109,7 @@ class InternshipStatusController extends Controller
abort(403, 'Unauthorized'); abort(403, 'Unauthorized');
} }
$internshipStatus = $this->currentInternshipStatus($internship); $internshipStatus = $internship->status;
$newStatusValidator = 'in:' . implode(',', $this->possibleNewStatuses($internshipStatus->status, $user->role, $internship->report_confirmed)); $newStatusValidator = 'in:' . implode(',', $this->possibleNewStatuses($internshipStatus->status, $user->role, $internship->report_confirmed));
$request->validate([ $request->validate([
@@ -172,9 +168,4 @@ class InternshipStatusController extends Controller
throw new \InvalidArgumentException('Unknown status'); 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' 'hiring'
]; ];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'created_at',
'updated_at',
];
/** /**
* Get the internships for the company. * Get the internships for the company.
*/ */
@@ -34,7 +44,7 @@ class Company extends Model
/** /**
* Get the contact person (user) for the company. * Get the contact person (user) for the company.
*/ */
public function contactPerson() public function contact()
{ {
return $this->belongsTo(User::class, 'contact'); return $this->belongsTo(User::class, 'contact');
} }

View File

@@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
@@ -28,6 +29,16 @@ class Internship extends Model
'report_confirmed', '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. * Get the attributes that should be cast.
* *
@@ -49,4 +60,32 @@ class Internship extends Model
{ {
return $this->belongsTo(Company::class, 'company_id'); 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', 'note',
'modified_by' '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', 'personal_email',
'study_field', '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', 'password',
'remember_token', 'remember_token',
'activation_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> <tbody>
<tr v-for="item in data"> <tr v-for="item in data">
<td>{{ item.company.name }}</td> <td>{{ item.company.name }}</td>
<td>{{ item.user!.name }}</td> <td>{{ item.student.name }}</td>
<td>{{ item.start }}</td> <td>{{ item.start }}</td>
<td>{{ item.end }}</td> <td>{{ item.end }}</td>
<td>{{ item.year_of_study }}</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 action_error = ref(null as null | string);
const refreshKey = ref(0); 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) { async function handleUpdateOfBasicInfo(internship: NewInternship) {
action_error.value = null; action_error.value = null;
@@ -53,47 +53,53 @@ async function handleUpdateOfBasicInfo(internship: NewInternship) {
<div style="height: 40px;"></div> <div style="height: 40px;"></div>
<!-- Čakajúca hláška --> <!-- Čakajúca hláška -->
<LoadingAlert /> <LoadingAlert v-if="loading" />
<!-- Chybová hláška --> <!-- Chybová hláška -->
<ErrorAlert v-if="action_error" :error="action_error" /> <ErrorAlert v-if="action_error" :error="action_error" />
<div> <!-- Chybová hláška -->
<h2>Základné informácie</h2> <ErrorAlert v-if="load_error" :error="load_error.message" />
<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>
<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> <hr />
<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>
<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> <br />
<InternshipStatusHistoryView :internship="data!" />
<br /> <h4>História</h4>
<InternshipStatusHistoryView :internship="data!" />
<h4>Zmena stavu</h4> <br />
<InternshipStatusEditor :internship="data!" @successful-submit="() => { refresh(); refreshKey++; }" />
</div>
<hr /> <h4>Zmena stavu</h4>
<InternshipStatusEditor :internship="data!"
@successful-submit="() => { refresh(); refreshKey++; }" />
</div>
<div> <hr />
<h2>Nahratie dokumentov</h2>
<ErrorAlert v-if="data?.status.status !== InternshipStatus.CONFIRMED" title="Blokované" <div>
error='Vaša prax nie je v stave "Schválená" a teda nemôžete nahrať dokumenty.' /> <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> </div>
</v-card> </v-card>
</v-container> </v-container>

View File

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

View File

@@ -1,9 +1,6 @@
import { Role } from "./role";
import type { User } from "./user"; import type { User } from "./user";
export interface InternshipStatusData { export interface InternshipStatusData {
internship_id: number;
user_id: string;
status: InternshipStatus; status: InternshipStatus;
changed: string; changed: string;
note: string; note: string;
@@ -36,6 +33,6 @@ export function prettyInternshipStatus(status: InternshipStatus) {
case InternshipStatus.NOT_DEFENDED: case InternshipStatus.NOT_DEFENDED:
return "Neobhájené"; return "Neobhájené";
default: 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 { export interface Internship {
id: number; id: number;
user_id?: string; student: User;
user?: User;
company: CompanyData; company: CompanyData;
start: string; start: string;
end: string; end: string;