diff --git a/app/Factory/CompanyGatewayFactory.php b/app/Factory/CompanyGatewayFactory.php new file mode 100644 index 000000000000..9559101091f7 --- /dev/null +++ b/app/Factory/CompanyGatewayFactory.php @@ -0,0 +1,30 @@ +company_id = $company_id; + $company_gateway->user_id = $user_id; + + return $company_gateway; + + } +} \ No newline at end of file diff --git a/app/Http/CompanyGatewayController.php b/app/Http/CompanyGatewayController.php deleted file mode 100644 index 9acd5bf9d9d8..000000000000 --- a/app/Http/CompanyGatewayController.php +++ /dev/null @@ -1,195 +0,0 @@ -company_repo = $company_repo; - - } - - /** - * Display a listing of the resource. - * - * @return \Illuminate\Http\Response - */ - public function index() - { - $company_gateways = CompanyGateway::whereCompanyId(auth()->user()->company()->id); - - return $this->listResponse($company_gateways); - - } - - /** - * Show the form for creating a new resource. - * - * @return \Illuminate\Http\Response - */ - public function create(CreateCompanyRequest $request) - { - - $company = CompanyFactory::create(auth()->user()->company()->account->id); - - return $this->itemResponse($company); - } - - /** - * Store a newly created resource in storage. - * - * @param \App\Http\Requests\SignupRequest $request - * @return \Illuminate\Http\Response - */ - public function store(StoreCompanyRequest $request) - { - $this->forced_includes = ['company_user']; - - $company = CreateCompany::dispatchNow($request->all(), auth()->user()->company()->account); - - if($request->file('logo')) - { - \Log::error('logo exists'); - $path = UploadAvatar::dispatchNow($request->file('logo'), $company->company_key); - - if($path){ - $company->logo = $path; - $company->save(); - } - - } - - auth()->user()->companies()->attach($company->id, [ - 'account_id' => $company->account->id, - 'is_owner' => 1, - 'is_admin' => 1, - 'is_locked' => 0, - 'permissions' => json_encode([]), - 'settings' => json_encode(DefaultSettings::userSettings()), - ]); - - /* - * Required dependencies - */ - auth()->user()->setCompany($company); - - /* - * Create token - */ - $company_token = CreateCompanyToken::dispatchNow($company, auth()->user()); - - //todo Need to discuss this with Hillel which is the best representation to return - //when a company is created. Do we send the entire account? Do we only send back the created CompanyUser? - $this->entity_transformer = CompanyUserTransformer::class; - $this->entity_type = CompanyUser::class; - - //return $this->itemResponse($company); - $ct = CompanyUser::whereUserId(auth()->user()->id); - - return $this->listResponse($ct); - - } - - /** - * Display the specified resource. - * - * @param int $id - * @return \Illuminate\Http\Response - */ - public function show(ShowCompanyRequest $request, Company $company) - { - - return $this->itemResponse($company); - - } - - /** - * Show the form for editing the specified resource. - * - * @param int $id - * @return \Illuminate\Http\Response - */ - public function edit(EditCompanyRequest $request, Company $company) - { - - return $this->itemResponse($company); - - } - - /** - * Update the specified resource in storage. - * - * @param \Illuminate\Http\Request $request - * @param int $id - * @return \Illuminate\Http\Response - */ - public function update(UpdateCompanyRequest $request, Company $company) - { - $company = $this->company_repo->save($request->all(), $company); - - if($request->file('logo')) - { - \Log::error('logo exists'); - $path = UploadAvatar::dispatchNow($request->file('logo'), $company->company_key); - - if($path){ - $company->logo = $path; - $company->save(); - } - - } - - return $this->itemResponse($company); - } - - /** - * Remove the specified resource from storage. - * - * @param int $id - * @return \Illuminate\Http\Response - */ - public function destroy(DestroyCompanyRequest $request, Company $company) - { - - $company->delete(); - - return response()->json([], 200); - } -} diff --git a/app/Http/Requests/CompanyGateway/CreateCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/CreateCompanyGatewayRequest.php new file mode 100644 index 000000000000..24bf160e57bc --- /dev/null +++ b/app/Http/Requests/CompanyGateway/CreateCompanyGatewayRequest.php @@ -0,0 +1,32 @@ +user()->isAdmin(); + + } + +} \ No newline at end of file diff --git a/app/Http/Requests/CompanyGateway/DestroyCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/DestroyCompanyGatewayRequest.php new file mode 100644 index 000000000000..5bdc4771ce5e --- /dev/null +++ b/app/Http/Requests/CompanyGateway/DestroyCompanyGatewayRequest.php @@ -0,0 +1,49 @@ +user()->can('edit', $this->company_gateway); + } + + public function rules() + { + $rules = []; + + return $rules; + } + + + public function sanitize() + { + $input = $this->all(); + + //$input['id'] = $this->encodePrimaryKey($input['id']); + + //$this->replace($input); + + return $this->all(); + } + +} \ No newline at end of file diff --git a/app/Http/Requests/CompanyGateway/EditCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/EditCompanyGatewayRequest.php new file mode 100644 index 000000000000..0cc9211ff3c0 --- /dev/null +++ b/app/Http/Requests/CompanyGateway/EditCompanyGatewayRequest.php @@ -0,0 +1,49 @@ +user()->can('edit', $this->company_gateway); + } + + public function rules() + { + $rules = []; + + return $rules; + } + + + public function sanitize() + { + $input = $this->all(); + + //$input['id'] = $this->encodePrimaryKey($input['id']); + + //$this->replace($input); + + return $this->all(); + } + +} \ No newline at end of file diff --git a/app/Http/Requests/CompanyGateway/ShowCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/ShowCompanyGatewayRequest.php new file mode 100644 index 000000000000..0b53c63228e4 --- /dev/null +++ b/app/Http/Requests/CompanyGateway/ShowCompanyGatewayRequest.php @@ -0,0 +1,49 @@ +user()->can('view', $this->company_gateway); + } + + public function rules() + { + $rules = []; + + return $rules; + } + + + public function sanitize() + { + $input = $this->all(); + + //$input['id'] = $this->encodePrimaryKey($input['id']); + + //$this->replace($input); + + return $this->all(); + } + +} \ No newline at end of file diff --git a/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php new file mode 100644 index 000000000000..e440f2cb894b --- /dev/null +++ b/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php @@ -0,0 +1,50 @@ +user()->isAdmin(); + + } + + public function rules() + { + $this->sanitize(); + + $rules = []; + + return $rules; + } + + public function sanitize() + { + $input = $this->all(); + + $input['config'] = encrypt($input['config']); + + $this->replace($input); + + return $this->all(); + } +} \ No newline at end of file diff --git a/app/Http/Requests/CompanyGateway/UpdateCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/UpdateCompanyGatewayRequest.php new file mode 100644 index 000000000000..064d5f3e1b1d --- /dev/null +++ b/app/Http/Requests/CompanyGateway/UpdateCompanyGatewayRequest.php @@ -0,0 +1,51 @@ +user()->can('edit', $this->company_gateway); + } + + public function rules() + { + $this->sanitize(); + + $rules = []; + + return $rules; + } + + + public function sanitize() + { + $input = $this->all(); + + $input['config'] = encrypt($input['config']); + + $this->replace($input); + + return $this->all(); + } + +} \ No newline at end of file diff --git a/app/Transformers/CompanyGatewayTransformer.php b/app/Transformers/CompanyGatewayTransformer.php index 20a04b0d86b7..b25ab8d3109f 100644 --- a/app/Transformers/CompanyGatewayTransformer.php +++ b/app/Transformers/CompanyGatewayTransformer.php @@ -49,7 +49,7 @@ class CompanyGatewayTransformer extends EntityTransformer 'show_address' => (bool)$company_gateway->show_address, 'show_shipping_address' => (bool)$company_gateway->show_shipping_address, 'update_details' => (bool)$company_gateway->update_details, - 'config' => (string)$company_gateway->config ?: '', + 'config' => (string) decrypt($company_gateway->config) ?: '', 'priority_id' => (int)$company_gateway->priority_id, 'min_limit' => (float)$company_gateway->min_limit, 'max_limit' => (float)$company_gateway->max_limit, diff --git a/routes/api.php b/routes/api.php index f1f95af809fa..80213c8e8041 100644 --- a/routes/api.php +++ b/routes/api.php @@ -74,6 +74,8 @@ Route::group(['middleware' => ['api_db','api_secret_check','token_auth'], 'prefi Route::resource('companies', 'CompanyController'); // name = (companies. index / create / show / update / destroy / edit + Route::resource('company_gateways', 'CompanyGatewayController'); + Route::post('refresh', 'Auth\LoginController@refresh'); /* Route::resource('tasks', 'TaskController'); // name = (tasks. index / create / show / update / destroy / edit diff --git a/tests/Feature/CompanyGatewayApiTest.php b/tests/Feature/CompanyGatewayApiTest.php new file mode 100644 index 000000000000..2b9eab80b008 --- /dev/null +++ b/tests/Feature/CompanyGatewayApiTest.php @@ -0,0 +1,92 @@ +makeTestData(); + + Session::start(); + + $this->faker = \Faker\Factory::create(); + + Model::reguard(); + } + + + public function testCompanyGatewayPost() + { + $data = [ + 'config' => 'random config', + ]; + + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token + ])->post('/api/v1/company_gateways', $data); + + + $response->assertStatus(200); + } + + public function testCompanyGatewayPut() + { + $data = [ + 'config' => 'changed', + ]; + + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token + ])->put('/api/v1/company_gateways/'.$this->encodePrimaryKey($this->client->id), $data); + + + $response->assertStatus(200); + } + + public function testCompanyGatewayGet() + { + + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token + ])->get('/api/v1/company_gateways/'.$this->encodePrimaryKey($this->client->id)); + + + $response->assertStatus(200); + + } + +} \ No newline at end of file