Account Settings V2 (AccountSettingsV2Service) Class
Namespace: CXT.IO.Dashboard.Api.ServicesImplements: IAccountSettingsV2Service
This class is the concrete implementation of IAccountSettingsV2Service. It acts as a BFF orchestrator, handling the business logic for V2 account visibility by fetching from and saving to one or more downstream V1 Account Settings APIs.
Purpose
- Aggregates account settings from multiple underlying API instances, each representing a different business unit.
- Routes "save" requests to the correct downstream API based on the business unit of the accounts being modified.
- Adapts V2 BFF models to the V1 models used by the downstream APIs.
- Handles authentication (via JWT) when communicating with downstream services.
Dependencies
ILogger<AccountSettingsV2Service>: For logging.IConfiguration: To access application settings.IJwtAccessTokenService: A service to obtain JWT access tokens required for authenticating requests to downstream APIs.IHttpClientFactory: Used to createHttpClientinstances.ASEndpointConfiguration: A runtime configuration (asendpoints.json) defining the list of downstream Account Settings API endpoints and their associated business units.
Method Implementations
Get account settings
csharp
public async Task<List<AccountSettingsV2ResponseModel>> GetAccountSettings(string gcn, CancellationToken cancellationToken)- Description: Retrieves and aggregates all V2 account settings for a client from all configured downstream API endpoints.
- Logic:
- Iterates through all
ASEndpointentries defined in the injectedASEndpointConfiguration. - For each endpoint, it asynchronously calls a private
GetAccountSettingsFromEndpointhelper method. - The helper method obtains a JWT, creates an authenticated
HttpClient, and makes aGETrequest to theapi/v1/account-settings/{gcn}endpoint of the downstream API. Note: The V2 BFF service calls V1 of the core API. - It deserializes the JSON response into an
AccountSettingsResponseobject (from the NuGet package). - The results from all endpoints are aggregated.
- The aggregated data is transformed into a
List<AccountSettingsV2ResponseModel>(the V2 BFF response model) and returned. During this mapping, theApiVersionproperty is set to"v2".
- Iterates through all
Save account settings
csharp
public async Task<bool> SaveAccountSettings(string gcn, AccountSettingsV2SaveRequestModel accounts, CancellationToken cancellationToken)- Description: Saves or updates V2 account settings, routing them to the correct downstream APIs based on business unit.
- Logic:
- Groups the incoming
accounts.RecordsbyBusinessUnit. - For each
BusinessUnitgroup:- It finds the corresponding downstream
ASEndpointfrom the configuration. - It transforms the list of V2 records (
AccountSettingsV2RecordsModel) into a V1AccountSettingsAddRequestobject, which is the model expected by the downstream API. - It calls a private
SaveAccountSettingsToEndpointhelper method.
- It finds the corresponding downstream
- The helper method obtains a JWT, creates an authenticated
HttpClient, and makes aPOSTrequest with theAccountSettingsAddRequestpayload to theapi/v1/account-settingsendpoint of the downstream API. Note: The V2 BFF service calls V1 of the core API. - The method tracks the success of each individual save operation. It returns
trueonly if all operations, across all targeted business units, were successful.
- Groups the incoming
Key Private Helpers
AccessToken(): A private async method that uses the injected_jwtTokenServiceto fetch a valid JWT access token.CreateAuthenticatedHttpClient(...): A private method that usesIHttpClientFactoryto create anHttpClient, sets itsBaseAddress, and adds theAuthorization: Bearer <token>header.