add transaction

main
nocode 1 year ago
parent 687ef5b28e
commit 91dd62561e
  1. 69
      app/Http/Controllers/Api/AuthApiController.php

@ -12,6 +12,7 @@ use App\Models\Agent;
use App\Models\AgentUser; use App\Models\AgentUser;
use App\Http\Requests\Api\RegisterApiRequest; use App\Http\Requests\Api\RegisterApiRequest;
use App\Http\Requests\Api\LoginApiRequest; use App\Http\Requests\Api\LoginApiRequest;
use Illuminate\Support\Facades\DB;
class AuthApiController extends BaseAuthApiController class AuthApiController extends BaseAuthApiController
{ {
@ -112,48 +113,56 @@ class AuthApiController extends BaseAuthApiController
$dataInsert = $this->transformRegisterData($request->all()); $dataInsert = $this->transformRegisterData($request->all());
DB::beginTransaction();
if (!empty($dataInsert)) { if (!empty($dataInsert)) {
if (!empty($checkUserAgent->agent_id)) { if (!empty($checkUserAgent->agent_id)) {
$agentParent = Agent::find($checkUserAgent->agent_id); $agentParent = Agent::find($checkUserAgent->agent_id);
$grantParent = $agentParent->grant; $grantParent = $agentParent->grant;
$parentPath = $agentParent->parent_path??null; $parentPath = $agentParent->parent_path ?? null;
} }
try {
$userId = \App\Models\User::insertGetId($dataInsert); $userId = \App\Models\User::insertGetId($dataInsert);
if ($userId) { if ($userId) {
$dataAgentInsert = [ $dataAgentInsert = [
'name' => $dataInsert['name'] ?? '', 'name' => $dataInsert['name'] ?? '',
'type' => self::AGENTS, 'type' => self::AGENTS,
// 'grant' => $this->_default_grant_two, // 'grant' => $this->_default_grant_two,
'grant' => !empty($grantParent)?$grantParent+1:1, 'grant' => !empty($grantParent) ? $grantParent + 1 : 1,
'phone' => $dataInsert['phone'] ?? '', 'phone' => $dataInsert['phone'] ?? '',
'address' => $dataInsert['address'] ?? '', 'address' => $dataInsert['address'] ?? '',
]; ];
$agentInsertId = Agent::insertGetId($dataAgentInsert); $agentInsertId = Agent::insertGetId($dataAgentInsert);
if ($agentInsertId) { if ($agentInsertId) {
if(!empty($parentPath)){ if (!empty($parentPath)) {
$myParentPath = $this->addParentPath($parentPath, $agentInsertId); $myParentPath = $this->addParentPath($parentPath, $agentInsertId);
Agent::where('id', $agentInsertId)->update(['parent_path' => $myParentPath]); Agent::where('id', $agentInsertId)->update(['parent_path' => $myParentPath]);
}
AgentUser::insert([
'user_id' => $userId,
'agent_id' => $agentInsertId,
'status' => 1,
'code' => $agentCodeNew ?? '',
'agent_root_id' => $checkUserAgent->agent_root_id
]);
} }
AgentUser::insert([ ;
'user_id' => $userId,
'agent_id' => $agentInsertId,
'status' => 1,
'code' => $agentCodeNew ?? '',
'agent_root_id' => $checkUserAgent->agent_root_id
]);
} }
; DB::commit();
return response()->json(['status' => true, 'msg' => $this->getMessageReponse(1)]);
} catch (\Exception $e) {
DB::rollBack();
} }
return response()->json(['status' => true, 'msg' => $this->getMessageReponse(1)]);
} }
return response()->json(['status' => false, 'msg' => $this->getMessageReponse(2)]); return response()->json(['status' => false, 'msg' => $this->getMessageReponse(2)]);
} }
public function addParentPath($parentPath, $userIdAdd){ public function addParentPath($parentPath, $userIdAdd)
{
return rtrim($parentPath, '-') . '-' . $userIdAdd . '-'; return rtrim($parentPath, '-') . '-' . $userIdAdd . '-';
} }
public function generateNewCode($code) public function generateNewCode($code)

Loading…
Cancel
Save