diff --git a/app/Http/Controllers/ClientGatewayTokenController.php b/app/Http/Controllers/ClientGatewayTokenController.php index c7f270d28a..d14234223a 100644 --- a/app/Http/Controllers/ClientGatewayTokenController.php +++ b/app/Http/Controllers/ClientGatewayTokenController.php @@ -419,4 +419,11 @@ public function destroy(DestroyClientGatewayTokenRequest $request, ClientGateway return $this->itemResponse($client_gateway_token->fresh()); } + + public function setAsDefault(UpdateClientGatewayTokenRequest $request, ClientGatewayToken $client_gateway_token) + { + $client_gateway_token = $this->client_gateway_token_repo->setDefault($client_gateway_token); + + return $this->itemResponse($client_gateway_token->fresh()); + } } diff --git a/app/Repositories/ClientGatewayTokenRepository.php b/app/Repositories/ClientGatewayTokenRepository.php index 0534df8c60..0088d13f52 100644 --- a/app/Repositories/ClientGatewayTokenRepository.php +++ b/app/Repositories/ClientGatewayTokenRepository.php @@ -25,4 +25,17 @@ public function save(array $data, ClientGatewayToken $client_gateway_token): Cli return $client_gateway_token->fresh(); } + + public function setDefault(ClientGatewayToken $client_gateway_token): ClientGatewayToken + { + ClientGatewayToken::withTrashed() + ->where('company_id', $client_gateway_token->company_id) + ->where('client_id', $client_gateway_token->client_id) + ->update(['is_default' => false]); + + $client_gateway_token->is_default = true; + $client_gateway_token->save(); + + return $client_gateway_token->fresh(); + } } diff --git a/routes/api.php b/routes/api.php index 2ba4f27aa7..69b9ad7e33 100644 --- a/routes/api.php +++ b/routes/api.php @@ -184,7 +184,9 @@ Route::post('filters/{entity}', [FilterController::class, 'index'])->name('filters'); + Route::resource('client_gateway_tokens', ClientGatewayTokenController::class); + Route::post('client_gateway_tokens/{client_gateway_token}/setAsDefault', [ClientGatewayTokenController::class, 'setAsDefault'])->name('client_gateway_tokens.set_as_default'); Route::post('connected_account', [ConnectedAccountController::class, 'index']); Route::post('connected_account/gmail', [ConnectedAccountController::class, 'handleGmailOauth']); diff --git a/tests/Feature/ClientGatewayTokenApiTest.php b/tests/Feature/ClientGatewayTokenApiTest.php index 8c10ecab26..5db3ed65f9 100644 --- a/tests/Feature/ClientGatewayTokenApiTest.php +++ b/tests/Feature/ClientGatewayTokenApiTest.php @@ -97,6 +97,91 @@ protected function setUp(): void $this->cg->save(); } + public function testClientGatewaySetDefault() + { + $data = [ + 'client_id' => $this->client->hashed_id, + 'company_gateway_id' => $this->cg->hashed_id, + 'gateway_type_id' => GatewayType::CREDIT_CARD, + 'meta' => '{}', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/client_gateway_tokens', $data); + + $response->assertStatus(200); + $arr = $response->json(); + + $t1 = $arr['data']['id']; + + $this->assertNotNull($arr['data']['token']); + + + $data = [ + 'client_id' => $this->client->hashed_id, + 'company_gateway_id' => $this->cg->hashed_id, + 'gateway_type_id' => GatewayType::CREDIT_CARD, + 'is_default' => false, + 'meta' => '{}', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/client_gateway_tokens', $data); + + $response->assertStatus(200); + $arr = $response->json(); + + $t2 = $arr['data']['id']; + + $this->assertNotNull($arr['data']['token']); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson("/api/v1/client_gateway_tokens/{$t2}/setAsDefault", []); + + $response->assertStatus(200); + + $this->assertTrue($response->json()['data']['is_default']); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson("/api/v1/client_gateway_tokens/{$t1}/setAsDefault", []); + + $response->assertStatus(200); + + $this->assertTrue($response->json()['data']['is_default']); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->getJson("/api/v1/clients/{$this->client->hashed_id}", []); + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertCount(2,$arr['data']['gateway_tokens']); + + foreach($arr['data']['gateway_tokens'] as $token) + { + if($token['id'] == $t1){ + $this->assertTrue($token['is_default']); + } + else { + $this->assertFalse($token['is_default']); + } + } + + } + + + public function testClientGatewayPostPost() { $data = [