Browse Source

Merge pull request 'handle super admin login' (#64) from dev-wahyun into staging

Reviewed-on: ibnu/generic-ospro-backend#64
pull/1/head
farhantock 10 months ago
parent
commit
b06e33b9cc
  1. 2
      app/Http/Controllers/AuthController.php
  2. 2
      app/Http/Controllers/MenuCompanyController.php
  3. 9
      app/Http/Controllers/RoleMenuController.php
  4. 273
      app/Http/Controllers/UserRegisterController.php
  5. 2733
      composer.lock
  6. 3
      routes/web.php

2
app/Http/Controllers/AuthController.php

@ -46,7 +46,7 @@ class AuthController extends Controller
$user = User::where([['username', $username],['password', md5($password)]])->first();
$checkExpiredOspro = $this->setExpiredTimeOspro($user['company_id']);
if($checkExpiredOspro === false && $user['company_id'] != null) {
return response()->json(['status' => 'error', 'message' => 'Expired! Please update license!','$checkExpiredOspro'=>$checkExpiredOspro], 201);
return response()->json(['status' => 'error', 'message' => 'Expired! Please update license!'], 201);
}
if ($is_mobile) {
$fcm_token = $request->fcm_token;

2
app/Http/Controllers/MenuCompanyController.php

@ -17,7 +17,7 @@ class MenuCompanyController extends Controller
if($data){
$data->delete();
}
if (is_array($menus) && count($menus) > 0 && isset($menus[0]['menu_id'])) {
if (is_array($menus) && count($menus) > 0 && isset($menus)) {
$countRes = 0;
foreach ($menus as $menu) {

9
app/Http/Controllers/RoleMenuController.php

@ -32,7 +32,7 @@ class RoleMenuController extends Controller
{
$menus = $request->all();
if (is_array($menus) && count($menus) > 0 && isset($menus[0]['menu_id'])) {
if (is_array($menus) && count($menus) > 0 && isset($menus)) {
$countRes = 0;
foreach ($menus as $menu) {
@ -56,12 +56,7 @@ class RoleMenuController extends Controller
return response()->json(['status' => 'failed', 'message' => 'Add data menu role failed created!', 'code' => 500]);
}
} else {
if(isset($data))
{
return response()->json(['status' => 'success', 'message' => 'Add data menu role successfully!', 'code' => 200]);
}else{
return response()->json(['status' => 'failed', 'message' => 'Add data menu role failed created!', 'code' => 500]);
}
return response()->json(['status' => 'failed', 'message' => 'Add data menu role failed created!', 'code' => 500]);
}
}

273
app/Http/Controllers/UserRegisterController.php

@ -0,0 +1,273 @@
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\{DB,Validator};
use App\Models\{Company, HumanResource, Menu, ProductTransaction, RefferalCode, Role, RoleMenu, MenuCompany};
class UserRegisterController extends Controller
{
public function add(Request $request)
{
DB::transaction(function () use ($request) {
$validator = Validator::make($request->all(), [
'refferal' => 'unique:refferal_code,code',
'cluster' => 'required|string',
'address' => 'required|string',
'email' => 'required|unique:' . ($request->type_account === 'Company' ? 'm_company' : 'm_users') . ',email',
'type_paket' => 'required|in:Basic, Free',
'phone_no' => 'required',
'type_account' => 'string|in:Personal, Company',
'company_name' => 'required|string',
'username' => 'string|required|unique:' . ($request->type_account === 'Personal' ? 'm_users' : '') . ',username'
]);
if ($validator->fails()) {
return response()->json(['status' => 'failed', 'message' => $validator->errors()->first(), 'code' => 400], 400);
}
$data = $validator->validated();
$company_name = $data['company_name'];
$type_account = $data['type_account'];
$refferal = $data['refferal'];
$cluster = $data['cluster'];
$address = $data['address'];
$phone_no = $data['phone_no'];
$email = $data['email'];
$username = $data['username'];
$type_paket = $data['type_paket'];
// Get last registration number
$getCompany = $this->getCompany();
$formDataCompany = array(
'company_name' => $company_name,
'newRegistrationNumber' => $getCompany['newRegistrationNumber'],
'cluster' => $cluster,
'phone_no' => $phone_no,
'email' => $email,
'address' => $address,
'type_account' => $type_account,
'username' => $username,
'type_paket' => $type_paket
);
if(empty($refferal)) {
$addCompany = $this->addCompany($formDataCompany, null);
} else {
$getRefferal = $this->getRefferalCode($refferal);
if(empty($getRefferal)) {
return response()->json(['status' => 'failed', 'message' => 'Refferal Code Not Found!', 'code' => 404], 404);
}
$addCompany = $this->addCompany($formDataCompany, $getRefferal['id']);
}
if(empty($addCompany)) {
return;
die();
}
$addRole = $this->addRole((int)$addCompany['id']);
if(empty($addRole)) {
return;
die();
}
$addHR = $this->addHR((int)$addCompany['id'], (int)$addRole['id'], $formDataCompany);
if(empty($addHR)) {
return;
die();
}
$getMenu = $this->getMenu();
if(empty($getMenu)) {
return;
die();
}
$this->addTransaction((int)$addCompany['id'], $formDataCompany);
$addMenuCompany = $this->addMenuCompany($getMenu, (int)$addCompany['id']);
if(empty($addMenuCompany)) {
return;
die();
}
$addMenuRole = $this->addMenuRole($getMenu, (int)$addRole['id']);
if($addMenuRole) {
return response()->json(['status' => 'success', 'message' => 'Register is successfully!', 'code' => 200], 200);
}
},3);
}
protected function getRefferalCode($refferal)
{
$result = RefferalCode::query()
->select('id','code','amount','exp','type','allocation','description')
->where('code', $refferal)
->first();
return $result;
}
protected function addCompany($formData, $id_refferal)
{
$formData = array(
'company_name' => $formData['company_name'],
'registration_no' => $formData['newRegistrationNumber'],
'cluster' => $formData['cluster'],
'date_register' => Carbon::now(),
'template_id' => 1,
'email' => $formData['email'],
'address' => $formData['address'],
'phone_no' => $formData['phone_no'],
'type_account' => $formData['type_account'],
'is_active' => true,
'discount_id' => $id_refferal === null ? null : (int)$id_refferal
);
$result = Company::create($formData);
return $result;
}
protected function addRole($id_company)
{
$formData = [
'name' => 'Admin',
'company_id' => $id_company,
'description' => '-'
];
$result = Role::create($formData);
return $result;
}
protected function addHR($id_company, $id_role, $data)
{
$generatePassword = Str::password(10);
$formData = array(
'name'=> $data['company_name'],
'phone_number'=> $data['phone_no'],
'email'=> $data['email'],
'username' => $data['username'],
'password'=> $generatePassword,
'role_id'=> (int)$id_role,
'ktp_number'=> $data['type_account'] === 'Company' ? 'CP-' : 'PR-' . Str::random(5),
'employee_type'=>'employee',
'address' => $data['address'],
'status_resource'=> 'active',
'company_id'=> (int)$id_company
);
$result = HumanResource::create($formData);
return [
'result' => $result,
'generatePassword' => $generatePassword
];
}
protected function addMenuCompany($baseDataMenu, $id_company)
{
$data = MenuCompany::where('company_id', $id_company);
if($data->exists()){
$data->delete();
}
if (is_array($baseDataMenu) && count($baseDataMenu) > 0 && isset($baseDataMenu)) {
$countRes = 0;
foreach ($baseDataMenu as $menu) {
$dataInsert = array(
"menu_id" => $menu['id'],
"parent_menu_id" => $menu['parent_id'],
"company_id" => $id_company,
"icon" => $menu['icon'],
"alias_name" => $menu['alias_name'],
"url" => $menu['url'],
"sequence" => $menu['sequence'],
"created_by" => $this->currentName
);
$result = MenuCompany::create($dataInsert);
if ($result) {
$countRes++;
} else {
$countRes--;
}
}
if ($countRes > 0) {
return $result;
} else {
return $result;
}
} else {
return false;
}
}
protected function addMenuRole($baseDataMenu, $id_role)
{
if (is_array($baseDataMenu) && count($baseDataMenu) > 0 && isset($baseDataMenu)) {
$countRes = 0;
foreach ($baseDataMenu as $menu) {
$dataInsert = array(
"menu_id" => $menu['id'],
"role_id" => $id_role,
);
$result = RoleMenu::create($dataInsert);
if ($result) {
$countRes++;
} else {
$countRes--;
}
}
if ($countRes > 0) {
return $result;
} else {
return $result;
}
} else {
return false;
}
}
protected function addTransaction($id_company, $data)
{
$currentDate = Carbon::now();
$finalDate = $currentDate->copy()->addDays(30);
if(is_array($data)) {
$formData = array(
'company_id' => $id_company,
'type_paket' => $data['type_paket'],
'exp_ospro' => $finalDate,
'amount' => $data['type_paket'] === 'Free' ? 0 : 250000
);
$result = ProductTransaction::create($formData);
return $result;
}
}
protected function getMenu()
{
$result = Menu::query()
->select("id", "name", "parent_id", "alias_name", "icon", "url", "sequence")
->whereNotIn('alias_name', ['Dashboard Customer', 'Registration Management', 'Demo Management'])
->get();
return $result;
}
protected function getCompany()
{
$newRegistrationNumber = '';
$company = Company::query()
->select('id','type_account','registration_no','discount_id')
->orderByDesc('id')
->first();
if(!empty($company)) {
$lastRegistrationNumber = $company['registration_no'];
$lastNumber = (int)preg_replace('/\D/', '', $lastRegistrationNumber);
$newNumber = $lastNumber + 1;
// $newRegistrationNumber = Str::beforeLast($lastRegistrationNumber, $lastNumber) . $newNumber;
$newRegistrationNumber = 'RG-'. $newNumber;
}
return [
'newRegistrationNumber' => $newRegistrationNumber
];
}
}

2733
composer.lock generated

File diff suppressed because it is too large Load Diff

3
routes/web.php

@ -8,6 +8,7 @@ $router->group(['prefix' => 'api', 'middleware' => 'cors'], function () use ($ro
$router->post('/login', 'AuthController@login');
// Landing Route
$router->post('/send-email', 'AuthController@sendEmail');
$router->post('/user-register','UserRegisterController@add');
$router->post('/company-management-user/add', 'CompanyController@add');
$router->post('/product-transaction-user/add', 'ProductTransactionController@add');
$router->post('/company-management-user/search', 'CompanyController@search');
@ -591,8 +592,6 @@ $router->group(['prefix' => 'api', 'middleware' => 'cors'], function () use ($ro
$router->get('/information-storage/{company_name}', 'Controller@storage');
$router->post('/register-user','UserRegisterController@add');
$router->post('/refferal-code/search', 'RefferalCodeController@search');
$router->post('/refferal-code/add', 'RefferalCodeController@add');
$router->get('/refferal-code/edit/{id}', 'RefferalCodeController@edit');

Loading…
Cancel
Save