You've already forked isop-mirror
refactor: consolidate internship expansion logic into a single method
This commit is contained in:
@@ -22,33 +22,7 @@ class InternshipController extends Controller
|
|||||||
$internships = Internship::all()->makeHidden(['created_at', 'updated_at']);
|
$internships = Internship::all()->makeHidden(['created_at', 'updated_at']);
|
||||||
|
|
||||||
$internships->each(function ($internship) {
|
$internships->each(function ($internship) {
|
||||||
$internship->user = User::find($internship->user_id)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
|
$this->expandInternship($internship, true, true);
|
||||||
unset($internship->user_id);
|
|
||||||
});
|
|
||||||
|
|
||||||
$internships->each(function ($internship) {
|
|
||||||
$internship->company = Company::find($internship->company_id)->makeHidden(['created_at', 'updated_at']);
|
|
||||||
unset($internship->company_id);
|
|
||||||
});
|
|
||||||
|
|
||||||
$internships->each(function ($internship) {
|
|
||||||
$internship->contact = User::find($internship->company->contact)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
|
|
||||||
unset($internship->company->contact);
|
|
||||||
});
|
|
||||||
|
|
||||||
$internships->each(function ($internship) {
|
|
||||||
$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']);
|
|
||||||
});
|
|
||||||
|
|
||||||
$internships->each(function ($internship) {
|
|
||||||
$internship->start = Carbon::parse($internship->start)->format('d.m.Y');
|
|
||||||
$internship->end = Carbon::parse($internship->end)->format('d.m.Y');
|
|
||||||
});
|
|
||||||
|
|
||||||
$internships->each(function ($internship) {
|
|
||||||
$internship->agreement = $internship->agreement !== null;
|
|
||||||
$internship->report = $internship->report !== null;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return response()->json($internships);
|
return response()->json($internships);
|
||||||
@@ -70,42 +44,15 @@ class InternshipController extends Controller
|
|||||||
abort(403, 'Unauthorized');
|
abort(403, 'Unauthorized');
|
||||||
}
|
}
|
||||||
|
|
||||||
if($user->role === "EMPLOYER") {
|
$internships->each(function ($internship) use ($user) {
|
||||||
$internships->each(function ($internship) {
|
$this->expandInternship($internship, $user->role === "EMPLOYER", true);
|
||||||
$internship->user = User::find($internship->user_id)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
|
|
||||||
unset($internship->user_id);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
$internships->each(function ($internship) {
|
|
||||||
$internship->company = Company::find($internship->company_id)->makeHidden(['created_at', 'updated_at']);
|
|
||||||
unset($internship->company_id);
|
|
||||||
});
|
|
||||||
|
|
||||||
$internships->each(function ($internship) {
|
|
||||||
$internship->contact = User::find($internship->company->contact)->makeHidden(['created_at', 'updated_at', 'email_verified_at']);
|
|
||||||
unset($internship->company->contact);
|
|
||||||
});
|
|
||||||
|
|
||||||
$internships->each(function ($internship) {
|
|
||||||
$internship->status = InternshipStatus::whereColumn('internship_id', '=', $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']);
|
|
||||||
});
|
|
||||||
|
|
||||||
$internships->each(function ($internship) {
|
|
||||||
$internship->start = Carbon::parse($internship->start)->format('d.m.Y');
|
|
||||||
$internship->end = Carbon::parse($internship->end)->format('d.m.Y');
|
|
||||||
});
|
|
||||||
|
|
||||||
$internships->each(function ($internship) {
|
|
||||||
$internship->agreement = $internship->agreement !== null;
|
|
||||||
$internship->report = $internship->report !== null;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return response()->json($internships);
|
return response()->json($internships);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(int $id) {
|
public function get(int $id)
|
||||||
|
{
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
|
|
||||||
$internship = Internship::find($id);
|
$internship = Internship::find($id);
|
||||||
@@ -116,26 +63,17 @@ class InternshipController extends Controller
|
|||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
$internship->company = Company::find($internship->company_id)->makeHidden(['created_at', 'updated_at']);
|
$this->expandInternship($internship, false, false);
|
||||||
unset($internship->company_id);
|
|
||||||
|
|
||||||
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');
|
||||||
}
|
}
|
||||||
|
|
||||||
$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']);
|
|
||||||
|
|
||||||
$internship->agreement = $internship->agreement !== null;
|
|
||||||
$internship->report = $internship->report !== null;
|
|
||||||
|
|
||||||
return response()->json($internship);
|
return response()->json($internship);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_agreement(int $id) {
|
public function get_agreement(int $id)
|
||||||
|
{
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$internship = Internship::find($id);
|
$internship = Internship::find($id);
|
||||||
|
|
||||||
@@ -160,7 +98,8 @@ class InternshipController extends Controller
|
|||||||
->header('Content-Disposition', 'attachment; filename="agreement_' . $id . '.pdf"');
|
->header('Content-Disposition', 'attachment; filename="agreement_' . $id . '.pdf"');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_report(int $id) {
|
public function get_report(int $id)
|
||||||
|
{
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$internship = Internship::find($id);
|
$internship = Internship::find($id);
|
||||||
|
|
||||||
@@ -271,7 +210,8 @@ class InternshipController extends Controller
|
|||||||
return response()->noContent();
|
return response()->noContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update_documents(int $id, Request $request) {
|
public function update_documents(int $id, Request $request)
|
||||||
|
{
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$internship = Internship::find($id);
|
$internship = Internship::find($id);
|
||||||
|
|
||||||
@@ -329,7 +269,8 @@ class InternshipController extends Controller
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
private function validateNewInternship(Request $request) {
|
private function validateNewInternship(Request $request)
|
||||||
|
{
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'company_id' => ['required', 'exists:companies,id'],
|
'company_id' => ['required', 'exists:companies,id'],
|
||||||
'start' => ['required', 'date'],
|
'start' => ['required', 'date'],
|
||||||
@@ -345,7 +286,8 @@ class InternshipController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function checkOverlap(int $user_id, string $start_date, string $end_date, ?int $current_id = null) {
|
private function checkOverlap(int $user_id, string $start_date, string $end_date, ?int $current_id = null)
|
||||||
|
{
|
||||||
$existingInternship = Internship::where('user_id', $user_id)
|
$existingInternship = Internship::where('user_id', $user_id)
|
||||||
// check if the two internships do not have the same ID
|
// check if the two internships do not have the same ID
|
||||||
->when($current_id, function ($query) use ($current_id) {
|
->when($current_id, function ($query) use ($current_id) {
|
||||||
@@ -368,4 +310,29 @@ 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user