New API add Customer

main
nocode 1 year ago
parent e14410f9a5
commit 0f0ae367e3
  1. 18
      Modules/Agents/Entities/Customer.php
  2. 16
      Modules/Agents/Http/Controllers/AgentActionApi.php
  3. 62
      Modules/Agents/Http/Requests/CreateCustomerRequest.php
  4. 9
      Modules/Agents/Routes/api.php
  5. 111
      Modules/Agents/Services/AgentService.php

@ -0,0 +1,18 @@
<?php
namespace Modules\Agents\Entities;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Customer extends Model
{
use HasFactory;
protected $table = 'se_customers';
protected $fillable = ['id', 'name', 'phone', 'email', 'grade', 'msg', 'agent_id', 'created_at', 'created_by', 'updated_at', 'updated_by', 'deleted_at'];
// protected static function newFactory()
// {
// return \Modules\Agents\Database\factories\CustomerFactory::new();
// }
}

@ -7,10 +7,22 @@ use Illuminate\Http\Request;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Modules\Agents\Http\Requests\CreateCustomerRequest;
use Modules\Agents\Services\AgentService;
class AgentActionApi extends BaseController
{
public function addCustomer(Request $request){
dd("Running................");
protected $agentService;
public function __construct(AgentService $agentService)
{
$this->agentService = $agentService;
}
public function createCustomer(CreateCustomerRequest $request)
{
$result = $this->agentService->createCustomer($request->all());
if (!$result['status']) {
return response()->json(['status' => $result['status'], 'msg' => $result['msg']]);
}
return response()->json(['status' => $result['status'], 'msg' => $result['msg']]);
}
}

@ -0,0 +1,62 @@
<?php
namespace Modules\Agents\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
class CreateCustomerRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'agents_code' => 'required',
'guest_name' => 'required',
'phone' => 'required',
'email' => 'required',
];
}
/**
* Get the error messages for the defined validation rules.
*
* @return array
*/
public function messages()
{
return [
'agents_code.required' => 'Mã đại lý không được để trống.',
'guest_name.required' => 'Tên khách hàng không được để trống.',
'phone.required' => 'SĐT không được để trống.',
'email.required' => 'Email không được để trống.'
];
}
/**
* Handle a failed validation attempt.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return void
*
* @throws \Illuminate\Http\Exceptions\HttpResponseException
*/
protected function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json([
'status' => false,
'msg' => $validator->errors()
], 200));
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
}

@ -13,10 +13,7 @@ use Modules\Agents\Http\Controllers\AgentActionApi;
| is assigned the "api" middleware group. Enjoy building your API!
*/
Route::group( ['middleware' => ['auth:api']], function() {
Route::post('/add_customer', [AgentActionApi::class, 'addCustomer'])->name('agent_add_customer');
Route::group(['prefix' => 'agent','middleware' => ['auth:api']], function() {
// Route::post('/add_customer', [AgentActionApi::class, 'addCustomer'])->name('agent_add_customer');
});
// Route::get('/RP006', function(Request $request){
// var_dump( $request->bearerToken());
// })/*->middleware('auth:api')*/; //disable auth middleware to inspect header
Route::post('/agent/add_customer', [AgentActionApi::class, 'createCustomer'])->name('agent_add_customer');

@ -0,0 +1,111 @@
<?php
namespace Modules\Agents\Services;
use Illuminate\Support\Arr;
use Modules\Agents\Entities\Customer;
use App\Models\Agent;
use App\Models\AgentUser;
class AgentService
{
protected $modelCustomer;
protected $modelAgent;
protected $modelAgentUser;
/**
* Summary of __construct
* @param \Modules\Agents\Entities\Customer $modelCustomer
* @param \App\Models\Agent $modelAgent
* @param \App\Models\AgentUser $modelAgentUser
*/
public function __construct(
Customer $modelCustomer,
Agent $modelAgent,
AgentUser $modelAgentUser
) {
$this->modelCustomer = $modelCustomer;
$this->modelAgent = $modelAgent;
$this->modelAgentUser = $modelAgentUser;
}
/**
* Summary of createCustomer
* @param array $input
* @return array
*/
public function createCustomer($input)
{
// format data input
$dataInput = $this->formatCreateCustomer($input);
// check agent user with code input
$checkAgentUser = $this->modelAgentUser->where('code', $dataInput['agents_code'])->first();
if (empty($checkAgentUser)) {
return array('status' => false, 'msg' => $this->getMessageReturn('no_data_agent'));
}
$agentId = $checkAgentUser->agent_id;
\DB::beginTransaction();
try {
$checkHasAgent = $this->modelCustomer->where('agent_id', $agentId)->latest()->first();
if (empty($checkHasAgent)) {
$dataInsert = Arr::except($dataInput, ['agents_code']);
$dataInsert['agent_id'] = $agentId;
$result = $this->modelCustomer->insert($dataInsert);
\DB::commit();
return ['status' => true, 'msg' => $this->getMessageReturn('success_insert_customer')];
}
} catch (\Exception $e) {
\DB::rollBack();
}
return ['status' => false, 'msg' => $this->getMessageReturn('failed_insert')];
}
/**
* Summary of formatCreateCustomer
* @param array $data
* @return array
*/
public function formatCreateCustomer($data)
{
$format = [];
if (!empty($data['agents_code'])) {
$format['agents_code'] = $data['agents_code'];
}
if (!empty($data['guest_name'])) {
$format['name'] = $data['guest_name'];
}
if (!empty($data['phone'])) {
$format['phone'] = $data['phone'];
}
if (!empty($data['email'])) {
$format['email'] = $data['email'];
}
if (!empty($data['grade'])) {
$format['grade'] = $data['grade'];
}
if (!empty($data['grade'])) {
$format['grade'] = $data['grade'];
}
if (!empty($data['message'])) {
$format['msg'] = $data['message'];
}
return $format;
}
/**
* Summary of getMessageReturn
* @param mixed $errorKey
* @return array
*/
public function getMessageReturn($errorKey)
{
$msgArr = [
'no_data_agent' => 'Đại lý không tồn tại.',
'success_insert_customer' => 'Thêm khách hàng thành công.',
'failed_insert' => 'Thêm khách hàng thành công.',
];
return $msgArr[$errorKey] ?? "Không có dữ liệu.";
}
}
Loading…
Cancel
Save