From ed32b03e5be35e39abd9e52ed79e0dec228fc51a Mon Sep 17 00:00:00 2001 From: JOSEPH LE Date: Wed, 7 Aug 2024 15:16:59 +0700 Subject: [PATCH] update API handle ticket --- .../app/Http/Controllers/TicketController.php | 133 +++++++++++++----- .../Controllers/TimekeepingController.php | 11 -- ...2024_08_06_135632_create_tickets_table.php | 1 + 3 files changed, 102 insertions(+), 43 deletions(-) diff --git a/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php b/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php index 884584f..275b891 100644 --- a/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php +++ b/BACKEND/Modules/Admin/app/Http/Controllers/TicketController.php @@ -50,12 +50,11 @@ class TicketController extends Controller ); $responseData = array_merge( - $tickets->where('user_id',auth('admins')->user()->id)->orderBy('created_at', 'desc')->paginate($request->get('per_page'))->toArray(), + $tickets->where('user_id', auth('admins')->user()->id)->orderBy('created_at', 'desc')->paginate($request->get('per_page'))->toArray(), ['status' => true] ); return response()->json($responseData); - } public function getAll(Request $request) @@ -90,17 +89,18 @@ class TicketController extends Controller ); return response()->json($responseData); - } - public function createTicket(Request $request){ + public function createTicket(Request $request) + { // Define validation rules $rules = [ 'start_date' => 'required|date', - 'start_period' => 'required|string', // Adjust the validation rule as per your requirements + 'start_period' => 'required|string', 'end_date' => 'required|date|after_or_equal:start_date', - 'end_period' => 'required|string', // Adjust the validation rule as per your requirements + 'end_period' => 'required|string', + 'type' => 'required|string', ]; // Validate the request @@ -112,8 +112,10 @@ class TicketController extends Controller $startPeriod = $request->input('start_period'); //The session begins $endDate = $request->input('end_date'); //End date $endPeriod = $request->input('end_period'); //Session ends + $type = $request->input('type'); + $reason = $request->input('reason'); $user = auth('admins')->user(); // user create ticket - + // return $user; $ticket = Ticket::create([ @@ -121,16 +123,20 @@ class TicketController extends Controller 'start_period' => $startPeriod, 'end_date' => Carbon::create($endDate)->setTimezone(env('TIME_ZONE')), 'end_period' => $endPeriod, + 'type' => $type, + 'status' => 'WAITING', + 'reason' => $reason, 'user_id' => $user->id ]); - + // Send notification email to admin (list) return response()->json(['data' => $ticket, 'status' => true]); } - public function deleteTicket(Request $request){ + public function deleteTicket(Request $request) + { $rules = [ 'ticket_id' => 'required' ]; @@ -141,12 +147,12 @@ class TicketController extends Controller $user = auth('admins')->user(); $ticket_id = $request->input('ticket_id'); $ticket = Ticket::find($ticket_id); - if($ticket){ + if ($ticket) { // $user->id == user_id of ticket ---> delete - if($ticket->user_id == $user->id){ + if ($ticket->user_id == $user->id) { $ticket->delete(); return response()->json(['message' => 'delete success', 'status' => true]); - }else{ + } else { return response()->json(['message' => 'You are committing an act of vandalism', 'status' => false]); } } @@ -154,11 +160,13 @@ class TicketController extends Controller return response()->json(['message' => 'Delete fail', 'status' => false]); } - public function handleTicket(Request $request){ + public function handleTicket(Request $request) + { $rules = [ 'ticket_id' => 'required', - 'action' => 'required' + 'action' => 'required', + 'admin_note' => 'required' ]; // Validate the request @@ -169,23 +177,56 @@ class TicketController extends Controller $action = $request->input('action'); // 'confirm' or 'refuse' $admin = auth('admins')->user(); $ticket = Ticket::find($ticket_id); + + if (!$ticket || $ticket->status !== "WAITING") { + return response()->json(['message' => "Ticket not found", 'status' => false]); + } $results = $this->getAllPeriod($ticket->start_date, $ticket->start_period, $ticket->end_date, $ticket->end_period); - - return $results; - // $admin->id != user_id of ticket ---> continue + + // $admin->id != user_id of ticket ---> continue // Confirm - // Add records to the notes table like function Timekeeping.addNoteForUser() based on the $results array + // Add records to the notes table like function Timekeeping.addNoteForUser() based on the $results array - // Update updated_by and admin_note in tickets table + // Update updated_by and admin_note in tickets table - // Send notification email to users + // Send notification email to users // Refuse - // Update updated_by and admin_note in tickets table + // Update updated_by and admin_note in tickets table - // Send notification email to users + // Send notification email to users - // false + if ($action == "confirm") { + foreach ($results as $result) { + list($year, $month, $day) = explode('-', $result['date']); + Notes::create([ + 'n_user_id' => $ticket->user_id, + 'n_day' => $day, + 'n_month' => $month, + 'n_year' => $year, + 'n_time_type' => $result['period'], + 'n_reason' => $ticket->type, + 'n_note' => $ticket->reason + ]); + } + + $ticket['updated_by'] = $admin->name; + $ticket['admin_note'] = $admin_note; + $ticket['status'] = 'CONFIRMED'; + + $ticket->save(); + $this->createOrUpdateRecordForCurrentMonth($month, $year); + return response()->json(['message' => "confirmed", 'status' => true]); + } + + if ($action == "refuse") { + $ticket['updated_by'] = $admin->name; + $ticket['admin_note'] = $admin_note; + $ticket['status'] = 'REFUSED'; + return response()->json(['message' => "refused", 'status' => true]); + } + + return response()->json(['message' => "failed", 'status' => false]); } private function getAllPeriod($startDate, $startPeriod, $endDate, $endPeriod) @@ -196,28 +237,56 @@ class TicketController extends Controller //Use CarbonPeriod to create a period from the start date to the end date $period = CarbonPeriod::create($startDate, $endDate); - $time_type = Category::where('c_type','TIME_TYPE'); + $time_type = Category::where('c_type', 'TIME_TYPE')->get(); + $morning = null; + $afternoon = null; + $all_day = null; + + foreach ($time_type as $item) { + switch ($item->c_code) { + case 'S': + $morning = $item; + break; + case 'C': + $afternoon = $item; + break; + case 'ALL': + $all_day = $item; + break; + } + } foreach ($period as $date) { //If it is the start date if ($date->isSameDay($startDate)) { //Add start session - $results[] = ['date' => $date->toDateString(), 'period' => $startPeriod]; //If the start date is morning, add afternoon - if ($startPeriod == 'morning') { - $results[] = ['date' => $date->toDateString(), 'period' => 'afternoon']; + if ($startDate == $endDate) { + if ($startPeriod == $endPeriod) { + $results[] = ['date' => $date->toDateString(), 'period' => $startPeriod]; + } else { + $results[] = ['date' => $date->toDateString(), 'period' => $all_day->c_code]; + } + } else { + if ($startPeriod == $morning->c_code) { + $results[] = ['date' => $date->toDateString(), 'period' => $all_day->c_code]; + } else { + $results[] = ['date' => $date->toDateString(), 'period' => $startPeriod]; + } } } elseif ($date->isSameDay($endDate)) { //If it is the end date //If the end session is afternoon, add morning first - if ($endPeriod == 'afternoon') { - $results[] = ['date' => $date->toDateString(), 'period' => 'morning']; + + if ($endPeriod == $afternoon->c_code) { + $results[] = ['date' => $date->toDateString(), 'period' => $all_day->c_code]; + } else { + $results[] = ['date' => $date->toDateString(), 'period' => $endPeriod]; } - $results[] = ['date' => $date->toDateString(), 'period' => $endPeriod]; } else { //Add both sessions for days between intervals - $results[] = ['date' => $date->toDateString(), 'period' => 'morning']; - $results[] = ['date' => $date->toDateString(), 'period' => 'afternoon']; + $results[] = ['date' => $date->toDateString(), 'period' => $all_day->c_code]; + // $results[] = ['date' => $date->toDateString(), 'period' => 'afternoon']; } } diff --git a/BACKEND/Modules/Admin/app/Http/Controllers/TimekeepingController.php b/BACKEND/Modules/Admin/app/Http/Controllers/TimekeepingController.php index be341e5..b9f3e53 100644 --- a/BACKEND/Modules/Admin/app/Http/Controllers/TimekeepingController.php +++ b/BACKEND/Modules/Admin/app/Http/Controllers/TimekeepingController.php @@ -114,17 +114,6 @@ class TimekeepingController extends Controller if ($user_id == "") { return response()->json(['status' => false, 'message' => 'User not found!']); } - // Notes::insert([ - // [ - // "n_user_id" => $user_id, - // "n_day" => $day, - // "n_month" => $month, - // "n_year" => $year, - // "n_time_type" => $time_type, - // "n_reason" => $reason, - // "n_note" => $note - // ] - // ]); $existingNote = Notes::where('n_user_id', $user_id) ->where('n_day', $day) diff --git a/BACKEND/database/migrations/2024_08_06_135632_create_tickets_table.php b/BACKEND/database/migrations/2024_08_06_135632_create_tickets_table.php index 409b648..356d41a 100644 --- a/BACKEND/database/migrations/2024_08_06_135632_create_tickets_table.php +++ b/BACKEND/database/migrations/2024_08_06_135632_create_tickets_table.php @@ -20,6 +20,7 @@ return new class extends Migration $table->string('end_period'); $table->string('type'); $table->string('reason'); + $table->string('status'); $table->string('admin_note')->nullable(); $table->string('updated_by'); $table->timestamps();