Skip to content

Commit e03e30c

Browse files
feat(Employee): implement employee registration logic with password generation and error handling
1 parent 253632d commit e03e30c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

app/Http/Controllers/Employee/EmployeeInfoController.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
use App\Common\Helpers;
66
use App\Http\Controllers\Controller;
7+
use App\Http\Requests\Employee\EmployeeRequest;
78
use App\Models\EmployeeInfo;
89
use Auth;
910
use DB;
11+
use Exception;
1012
use Illuminate\Http\Request;
1113
use Inertia\Inertia;
1214
use Log;
15+
use Mail;
16+
use Str;
1317

1418
class EmployeeInfoController extends Controller
1519
{
@@ -89,10 +93,53 @@ public function index(Request $request)
8993
return Inertia::render('employees/Index', ['employees' => $employees]);
9094
}
9195

96+
/**
97+
* Show the form for creating a new employee info.
98+
*/
9299
public function create()
93100
{
94101
Log::info('Employee Info: Accessed employee registration page', ['action_user_id' => Auth::id()]);
95102

96103
return Inertia::render('employees/Create');
97104
}
105+
106+
/**
107+
* Store a newly created employee info in storage.
108+
*/
109+
public function store(EmployeeRequest $request)
110+
{
111+
try {
112+
$data = $request->validated();
113+
114+
$genPassword = Str::random(12);
115+
$request->merge(['password' => $genPassword]);
116+
117+
$userData = [
118+
'name' => $data['name'],
119+
'email' => $data['email'],
120+
'password' => $request['password'],
121+
];
122+
123+
$employeeData = $request->except(['name', 'email', 'password']);
124+
125+
$employee = EmployeeInfo::create($employeeData);
126+
$employee->user()->create($userData);
127+
128+
Log::info('Employee Info: A new employee was registered in the system.', ['action_user_id' => Auth::id()]);
129+
130+
// TODO: Enable email sending after setting up mail server
131+
// Mail::to($employee->user->email)->send(new EmployeeInfoMail($employee, $genPassword));
132+
133+
// TODO: Redirect to employee details page after implementing the show page
134+
return to_route('employees.show', ['employee' => $employee->id])->with('success', 'Employee registered successfully.');
135+
} catch (Exception $e) {
136+
Log::error('Employee Info: Error while registering new employee', [
137+
'action_user_id' => Auth::id(),
138+
'error' => $e->getMessage(),
139+
]);
140+
141+
return back()->withInput()->with('error', 'An error occurred while registering the employee. Please try again.');
142+
}
143+
144+
}
98145
}

0 commit comments

Comments
 (0)