add transaction

main
nocode 1 year ago
parent 687ef5b28e
commit 91dd62561e
  1. 19
      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
{
@ -113,13 +114,14 @@ 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;
}
try {
$userId = \App\Models\User::insertGetId($dataInsert);
if ($userId) {
@ -127,14 +129,14 @@ class AuthApiController extends BaseAuthApiController
'name' => $dataInsert['name'] ?? '',
'type' => self::AGENTS,
// 'grant' => $this->_default_grant_two,
'grant' => !empty($grantParent)?$grantParent+1:1,
'grant' => !empty($grantParent) ? $grantParent + 1 : 1,
'phone' => $dataInsert['phone'] ?? '',
'address' => $dataInsert['address'] ?? '',
];
$agentInsertId = Agent::insertGetId($dataAgentInsert);
if ($agentInsertId) {
if(!empty($parentPath)){
if (!empty($parentPath)) {
$myParentPath = $this->addParentPath($parentPath, $agentInsertId);
Agent::where('id', $agentInsertId)->update(['parent_path' => $myParentPath]);
}
@ -148,12 +150,19 @@ class AuthApiController extends BaseAuthApiController
}
;
}
DB::commit();
return response()->json(['status' => true, 'msg' => $this->getMessageReponse(1)]);
} catch (\Exception $e) {
DB::rollBack();
}
}
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