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\Http\Requests\Api\RegisterApiRequest;
use App\Http\Requests\Api\LoginApiRequest;
use Illuminate\Support\Facades\DB;
class AuthApiController extends BaseAuthApiController
{
@ -112,48 +113,56 @@ class AuthApiController extends BaseAuthApiController
$dataInsert = $this->transformRegisterData($request->all());
DB::beginTransaction();
if (!empty($dataInsert)) {
if (!empty($checkUserAgent->agent_id)) {
$agentParent = Agent::find($checkUserAgent->agent_id);
$grantParent = $agentParent->grant;
$parentPath = $agentParent->parent_path??null;
$parentPath = $agentParent->parent_path ?? null;
}
$userId = \App\Models\User::insertGetId($dataInsert);
if ($userId) {
$dataAgentInsert = [
'name' => $dataInsert['name'] ?? '',
'type' => self::AGENTS,
// 'grant' => $this->_default_grant_two,
'grant' => !empty($grantParent)?$grantParent+1:1,
'phone' => $dataInsert['phone'] ?? '',
'address' => $dataInsert['address'] ?? '',
];
$agentInsertId = Agent::insertGetId($dataAgentInsert);
if ($agentInsertId) {
if(!empty($parentPath)){
$myParentPath = $this->addParentPath($parentPath, $agentInsertId);
Agent::where('id', $agentInsertId)->update(['parent_path' => $myParentPath]);
try {
$userId = \App\Models\User::insertGetId($dataInsert);
if ($userId) {
$dataAgentInsert = [
'name' => $dataInsert['name'] ?? '',
'type' => self::AGENTS,
// 'grant' => $this->_default_grant_two,
'grant' => !empty($grantParent) ? $grantParent + 1 : 1,
'phone' => $dataInsert['phone'] ?? '',
'address' => $dataInsert['address'] ?? '',
];
$agentInsertId = Agent::insertGetId($dataAgentInsert);
if ($agentInsertId) {
if (!empty($parentPath)) {
$myParentPath = $this->addParentPath($parentPath, $agentInsertId);
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)]);
}
public function addParentPath($parentPath, $userIdAdd){
public function addParentPath($parentPath, $userIdAdd)
{
return rtrim($parentPath, '-') . '-' . $userIdAdd . '-';
}
public function generateNewCode($code)

Loading…
Cancel
Save