diff --git a/backend/app/Http/Controllers/CompanyController.php b/backend/app/Http/Controllers/CompanyController.php index 7afc3d4..d37e2f6 100644 --- a/backend/app/Http/Controllers/CompanyController.php +++ b/backend/app/Http/Controllers/CompanyController.php @@ -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); } diff --git a/backend/app/Http/Controllers/InternshipController.php b/backend/app/Http/Controllers/InternshipController.php index 0d7475e..e31adf2 100644 --- a/backend/app/Http/Controllers/InternshipController.php +++ b/backend/app/Http/Controllers/InternshipController.php @@ -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; - } } diff --git a/backend/app/Http/Controllers/InternshipStatusController.php b/backend/app/Http/Controllers/InternshipStatusController.php index c3f30c8..5b0820e 100644 --- a/backend/app/Http/Controllers/InternshipStatusController.php +++ b/backend/app/Http/Controllers/InternshipStatusController.php @@ -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(); - } } diff --git a/backend/app/Models/Company.php b/backend/app/Models/Company.php index 6320e72..f98416b 100644 --- a/backend/app/Models/Company.php +++ b/backend/app/Models/Company.php @@ -23,6 +23,16 @@ class Company extends Model 'hiring' ]; + /** + * The attributes that should be hidden for serialization. + * + * @var list + */ + 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'); } diff --git a/backend/app/Models/Internship.php b/backend/app/Models/Internship.php index 82dcd51..4330fee 100644 --- a/backend/app/Models/Internship.php +++ b/backend/app/Models/Internship.php @@ -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 + */ + 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 + */ + 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, + ]; + } } diff --git a/backend/app/Models/InternshipStatus.php b/backend/app/Models/InternshipStatus.php index 4272c86..fc41517 100644 --- a/backend/app/Models/InternshipStatus.php +++ b/backend/app/Models/InternshipStatus.php @@ -22,4 +22,31 @@ class InternshipStatus extends Model 'note', 'modified_by' ]; + + /** + * The attributes that should be hidden for serialization. + * + * @var list + */ + 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, + ]; + } } diff --git a/backend/app/Models/StudentData.php b/backend/app/Models/StudentData.php index 8490416..d9fb90f 100644 --- a/backend/app/Models/StudentData.php +++ b/backend/app/Models/StudentData.php @@ -21,4 +21,14 @@ class StudentData extends Model 'personal_email', 'study_field', ]; + + /** + * The attributes that should be hidden for serialization. + * + * @var list + */ + protected $hidden = [ + 'created_at', + 'updated_at', + ]; } diff --git a/backend/app/Models/User.php b/backend/app/Models/User.php index c80bbdc..744275f 100644 --- a/backend/app/Models/User.php +++ b/backend/app/Models/User.php @@ -39,6 +39,11 @@ class User extends Authenticatable 'password', 'remember_token', 'activation_token', + 'created_at', + 'updated_at', + 'email_verified_at', + 'active', + 'needs_password_change' ]; /** diff --git a/frontend/app/pages/dashboard/admin/internships/index.vue b/frontend/app/pages/dashboard/admin/internships/index.vue index eba92a8..342c788 100644 --- a/frontend/app/pages/dashboard/admin/internships/index.vue +++ b/frontend/app/pages/dashboard/admin/internships/index.vue @@ -49,7 +49,7 @@ const { data, error } = await useSanctumFetch('/api/internships'); {{ item.company.name }} - {{ item.user!.name }} + {{ item.student.name }} {{ item.start }} {{ item.end }} {{ item.year_of_study }} diff --git a/frontend/app/pages/dashboard/company/internships/edit/[id].vue b/frontend/app/pages/dashboard/company/internships/edit/[id].vue index 89337f6..5a9fdab 100644 --- a/frontend/app/pages/dashboard/company/internships/edit/[id].vue +++ b/frontend/app/pages/dashboard/company/internships/edit/[id].vue @@ -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(`/api/internships/${route.params.id}`); +const { data, error: load_error, refresh } = await useSanctumFetch(`/api/internships/${route.params.id}`); async function handleUpdateOfBasicInfo(internship: NewInternship) { action_error.value = null; @@ -53,47 +53,53 @@ async function handleUpdateOfBasicInfo(internship: NewInternship) {
- + -
-

Základné informácie

- - -
+ + -
+
+
+

Základné informácie

+ + +
-
-

Stav

-

Aktuálny stav

-

{{ prettyInternshipStatus(data?.status.status!) }}

-

Poznámka: {{ data?.status.note }}

-

Posledná zmena: {{ data?.status.changed }}, {{ data?.status.modified_by.name }}

+
-
+
+

Stav

+

Aktuálny stav

+

{{ prettyInternshipStatus(data?.status.status!) }}

+

Poznámka: {{ data?.status.note }}

+

Posledná zmena: {{ data?.status.changed }}, {{ data?.status.modified_by.name }}

-

História

- +
-
+

História

+ -

Zmena stavu

- -
+
-
+

Zmena stavu

+ +
-
-

Nahratie dokumentov

+
- +
+

Nahratie dokumentov

- + + + +
diff --git a/frontend/app/pages/dashboard/company/internships/index.vue b/frontend/app/pages/dashboard/company/internships/index.vue index 4379328..aadbdd9 100644 --- a/frontend/app/pages/dashboard/company/internships/index.vue +++ b/frontend/app/pages/dashboard/company/internships/index.vue @@ -47,7 +47,7 @@ const { data, error } = await useSanctumFetch('/api/internships/my - {{ item.user!.name }} + {{ item.student.name }} {{ item.start }} {{ item.end }} {{ item.year_of_study }} diff --git a/frontend/app/types/internship_status.ts b/frontend/app/types/internship_status.ts index 6098439..33d027c 100644 --- a/frontend/app/types/internship_status.ts +++ b/frontend/app/types/internship_status.ts @@ -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}'`); } } \ No newline at end of file diff --git a/frontend/app/types/internships.ts b/frontend/app/types/internships.ts index 96e285a..5f9ce0f 100644 --- a/frontend/app/types/internships.ts +++ b/frontend/app/types/internships.ts @@ -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;