Account Settings Service (AccountSettingsService) Class
Namespace: Cxt.Account.Settings.API.Services
Implements: IAccountSettingsService
The AccountSettingsService class is the concrete implementation of the IAccountSettingsService interface. It handles the core business logic for managing client account visibility settings within the Account Settings API.
Purpose
This service orchestrates the operations related to adding, updating, deleting, and retrieving account preferences. It acts as an intermediary between the API controllers and the data access layer (represented by IAccountSettingsClient).
Dependencies
The AccountSettingsService has the following key dependencies, typically injected via constructor dependency injection:
IAccountSettingsClient: An abstraction for the client or service responsible for the actual CUD (Create, Update, Delete) operations and retrieval of account settings data from the underlying data store or downstream system. This is a critical dependency for data persistence. (Details inIAccountSettingsClient Interface)IAccountSettingsMapper: An abstraction for a mapper component responsible for transforming data between different model types (e.g., from data store models or DTOs to API response models likeAccountSettingsResponse). (Details inIAccountSettingsMapper Interface)ILogger<AccountSettingsService>: The standard ASP.NET Core logging interface used for logging informational messages and errors within the service.
Method Implementations
Below is a summary of how AccountSettingsService implements the methods defined in IAccountSettingsService:
Retrieve Client Account Settings Preferences Service
public async Task<AccountSettingsResponse> RetrieveClientAccountSettingsPreferencesService(string gcn, CancellationToken cancellationToken)- Description: Retrieves all stored account setting preferences for a client based on their GCN.
- Parameters:
Parameter Type Description gcnstringThe Global Client Number. cancellationTokenCancellationTokenToken for observing cancellation requests. - Returns:
Task<AccountSettingsResponse>- AnAccountSettingsResponseobject containing the client's preferences. - Logic:
- Calls
_accountSettingsClient.RetrieveClientAccountSettingsPreferencesClient(gcn, cancellationToken)to fetch the raw preference data for the given GCN. - If no client preferences are found (result is null or empty), it returns an empty
AccountSettingsResponse. - Otherwise, it uses
_accountSettingsMapper.MapToAccountSettingsResponse(clientPreferences)to map the retrieved data to anAccountSettingsResponseobject, which is then returned.
- Calls
Add Client Account Settings Preferences Service
public async Task<bool> AddClientAccountSettingsPreferencesService(AccountSettingsAddRequest request, CancellationToken cancellationToken)- Description: Adds or updates account settings preferences for a client based on the provided request.
- Parameters:
Parameter Type Description requestAccountSettingsAddRequestAn AccountSettingsAddRequestobject containing the client's GCN, business unit, and the list of accounts with their visibility settings.cancellationTokenCancellationTokenA CancellationTokento observe while waiting for the task to complete. - Returns:
Task<bool>-trueif all operations were successful,falseotherwise. - Logic:
- Iterates through each
AccountSettingsProfileinrequest.ClientAccountProfilesand then through eachAccountSettingsProfileAccountwithin those profiles. - For every account, it constructs an
AddAccountSettingDtoobject. During this mapping:- The boolean
AccountHiddenfrom the request is converted to an integer (1fortrue,0forfalse) for the DTO.
- The boolean
- Calls
_accountSettingsClient.AddClientAccountSettingsPreferencesClient(accountSetting)for each constructed DTO to persist the setting. - Tracks an
isAllAddedflag. If any individual add operation via the client fails, this flag is set tofalse. - Logs an error if an add operation fails, including the GCN and the last 4 digits of the account number.
- Returns
isAllAdded. - Contains a
//TODO: Add audit logcomment, indicating a planned enhancement for audit logging.
- Iterates through each
Update Client Account Settings Preferences Service
public async Task<bool> UpdateClientAccountSettingsPreferencesService(AccountSettingsUpdateRequest request, CancellationToken cancellationToken)- Description: Updates existing account settings preferences for a client.
- Parameters:
Parameter Type Description requestAccountSettingsUpdateRequestAn AccountSettingsUpdateRequestobject containing the details of the preferences to be updated.cancellationTokenCancellationTokenA CancellationToken. - Returns:
Task<bool>-trueif all updates were successful,falseotherwise. - Logic:
- Similar to the add operation, it iterates through profiles and accounts in the
request. - Maps each account to an
AddAccountSettingDto(note: it reusesAddAccountSettingDtofor updates, likely because the client interfaceUpdateClientAccountSettingsPreferencesClientexpects this DTO).- Converts boolean
AccountHiddento an integer (1fortrue,0forfalse).
- Converts boolean
- Calls
_accountSettingsClient.UpdateClientAccountSettingsPreferencesClient(accountSetting)for each account. - Tracks an
isAllUpdatedflag, setting it tofalseif any update fails. - Logs an error on failure, including GCN and the last 4 digits of the account number.
- Returns
isAllUpdated. - Contains a
//TODO: Add audit logcomment.
- Similar to the add operation, it iterates through profiles and accounts in the
Delete Client Account Settings Preferences Service
public async Task<bool> DeleteClientAccountSettingsPreferencesService(AccountSettingsDeleteRequest request, CancellationToken cancellationToken)- Description: Deletes a specific account setting preference for a client.
- Parameters:
Parameter Type Description requestAccountSettingsDeleteRequestAn AccountSettingsDeleteRequestobject specifying the GCN, account number, and account type of the preference to delete.cancellationTokenCancellationTokenA CancellationToken. - Returns:
Task<bool>-trueif the deletion was successful,falseotherwise. - Logic:
- Directly calls
_accountSettingsClient.DeleteClientAccountSettingsPreferencesClient(request)with the providedAccountSettingsDeleteRequest. - If the client operation returns
false(indicating failure), it sets anisAllDeletedflag tofalse. - Logs an error on failure, including GCN and the last 4 digits of the account number.
- Returns
isAllDeleted. - Contains a
//TODO: Add audit logcomment.
- Directly calls
Key Behaviors and Patterns
- Orchestration: The service primarily orchestrates calls to other services/clients (
IAccountSettingsClient,IAccountSettingsMapper). - Data Transformation: It performs necessary transformations between the API request/response models (defined in the
Investec.AccountSettings.ModelsNuGet package) and DTOs (likeAddAccountSettingDto) that are likely expected by the data access layer. - Error Handling: Basic error handling involves logging failures and returning a boolean status indicating the overall success of bulk operations (add/update).
- Partial Success/Failure: For add and update operations that involve multiple accounts, the service attempts to process all of them and returns
trueonly if all individual operations succeed. It does not appear to implement transactional rollback for partial failures within a single request.
This service is central to the API's functionality, acting as the bridge between the external contract (controller) and the internal data handling mechanisms.