diff --git a/app/Http/Controllers/Api/AuthApiController.php b/app/Http/Controllers/Api/AuthApiController.php index 0d2e8dd..5f5ced2 100755 --- a/app/Http/Controllers/Api/AuthApiController.php +++ b/app/Http/Controllers/Api/AuthApiController.php @@ -14,6 +14,10 @@ use App\Models\AgentUser; class AuthApiController extends BaseAuthApiController { public $_default_usertype = 'agents'; + public $_default_grant_two = 'agents'; + + const AGENTS = 'agents'; + const GENERAL = 'general'; // public function login(Request $request) @@ -25,12 +29,12 @@ class AuthApiController extends BaseAuthApiController if ($validator->fails()) { return $this->sendError('Validation Error.', $validator->errors()); } - + if (!Auth::attempt(['email' => $request->username, 'password' => $request->password])) { return $this->sendError('Unauthorised.', ['error' => 'Unauthorised']); } $user = User::where('email', $request->username)->first(); - + if (empty($user)) { $this->sendError('Error: ', ['error' => 'Email invalid']); } @@ -45,21 +49,24 @@ class AuthApiController extends BaseAuthApiController 'phone' => $user->phone, 'last_login' => $user->last_login, ]; - + //Find Agent USer $agentUser = AgentUser::where('user_id', $user->id)->latest()->first(); - if(!empty($agentUser->agent_root_id)){ + if (!empty($agentUser->agent_root_id)) { + // Agent User Root $agentRootInfo = Agent::find($agentUser->agent_root_id); } - if(!empty($agentUser->agent_id)){ + if (!empty($agentUser->agent_id)) { + // Agent User Current $myAgent = Agent::find($agentUser->agent_id); } - $phone = $myAgent->phone??null; - $data['general_agents_code'] = $agentRootInfo->code??''; - $data['general_agents_name'] = $agentRootInfo->name??''; + $phone = $myAgent->phone ?? null; + $data['general_agents_code'] = $agentRootInfo->code ?? ''; + $data['general_agents_name'] = $agentRootInfo->name ?? ''; $data['downline_register'] = $this->getLinkParam('downline_register', $phone); $data['promotional_link'] = $this->getLinkParam('promotional_link', $phone); $data['role'] = $user->user_type; + // Creating a token without scopes... $token = $user->createToken($user->id . ' token ' . time(), ['*'])->accessToken; @@ -80,19 +87,41 @@ class AuthApiController extends BaseAuthApiController if ($validator->fails()) { return $this->sendError('Validation Error.', $validator->errors()); } + if (empty($request->agents_code)) { + return response()->json(['status' => true, 'message' => 'Đăng ký thất bại, vui lòng gửi lên mã đại lý.']); + } + $checkUserAgent = AgentUser::where('code', '=', $request->agents_code)->first(); + if (empty($checkUserAgent)) { + return response()->json(['status' => true, 'message' => 'Đăng ký thất bại, Không tìm thấy đại lý.']); + } + $dataInsert = $this->transformRegisterData($request->all()); + if (!empty($dataInsert)) { - $user = \App\Models\User::insert($dataInsert); + $userId = \App\Models\User::insertGetId($dataInsert); + if ($userId) { + $dataAgentInsert = [ + 'name' => $dataInsert['name']??'', + 'type' => self::AGENTS, + 'grant' => $this->_default_grant_two + ]; + $agentInsertId = Agent::insertGetId($dataAgentInsert); + if($agentInsertId){ + AgentUser::insert(['user_id' => $userId, 'agent_id' => $agentInsertId, 'status' => 1, 'code' => $dataInsert['phone']]); + }; + } return response()->json(['status' => true, 'msg' => $this->getMessageReponse(1)]); } + return response()->json(['status' => false, 'msg' => $this->getMessageReponse(2)]); } - public function logout(Request $request){ + public function logout(Request $request) + { $token = $request->user()->token(); // expried token $token->revoke(); - // response + // Response return response()->json([ 'status' => true, 'message' => 'Đăng xuất thành công.' @@ -147,18 +176,19 @@ class AuthApiController extends BaseAuthApiController } return null; } - function getLinkParam($type, $phone){ + function getLinkParam($type, $phone) + { $arr = [ 'downline_register' => 'daisu.sundayenglish.com/dk', 'promotional_link' => 'thongtin.sundayenglish.com' ]; - $domain = $arr[$type]??''; - if(empty($phone)){ + $domain = $arr[$type] ?? ''; + if (empty($phone)) { return $domain; - }else{ + } else { $params = http_build_query(['mds' => $phone]); - return $domain.'?'.$params; + return $domain . '?' . $params; } - + } } diff --git a/app/Models/Agent.php b/app/Models/Agent.php index 078ef78..af0b8cd 100755 --- a/app/Models/Agent.php +++ b/app/Models/Agent.php @@ -14,7 +14,6 @@ class Agent extends Model 'id', 'name', 'root_path', - 'code', 'grant', 'type', 'phone', diff --git a/app/Models/AgentUser.php b/app/Models/AgentUser.php index 4ee4286..4332f7a 100755 --- a/app/Models/AgentUser.php +++ b/app/Models/AgentUser.php @@ -15,6 +15,7 @@ class AgentUser extends Model 'user_id', 'agent_root_id', 'root_id', + 'code', 'status', 'created_by', 'updated_by',