Repositories
Overview
The repository classes, including AccountRepository, ApplicationRepository, CustomerRepository, and SavedAccountApplicationRepository, are each responsible for managing data operations related to a specific model within the system. Each repository specifies the class name of the model it manages, handling tasks such as retrieving, saving, updating, and deleting records associated with that model. These repositories act as the primary interface between the application and the database, ensuring consistent and reliable data management for their respective models.
Repositories Structure
- Namespace: Toolstation\Customers\Repositories
- Extends:
BaseRepository
Customer Module Repositories
The customer module havings following repositories.
| Repository Name | Description |
|---|---|
Toolstation\Customers\Repositories\AccountRepository | Handles account-related data operations. |
Toolstation\Customers\Repositories\ApplicationRepository | Handles application-related data operations. |
Toolstation\Customers\Repositories\CustomerRepository | Handles customer-related data operations. |
Toolstation\Customers\Repositories/SavedAccountApplicationRespository | Handles saved account application-related data operations. |
AccountRepository Overview
The AccountRepository class provides a set of methods for managing accounts and account attributes. It extends the BaseRepository class and uses Eloquent models for accounts and account attributes. The class provides methods for creating accounts, updating or creating account attributes, and deleting account attributes.
- Namespace: Toolstation\Customers\Repositories
- Implements: Toolstation\Core\Repositories\BaseRepository
Key Components
- Model Specification:
This method specifies the class name of the model that the repository is responsible for, in this case, the Account model. It ensures that the repository operations are performed on the correct model class.
public function model()
{
return Account::class;
}
- Account Creation:
This method creates a new account record using the provided data. It accepts an array of account data, sets default values if certain fields are missing, and timestamps the creation and update times to ensure data consistency.
public function createAccount(array $accountData)
{
return Account::create([
'name' => $accountData['name'] ?? '',
'number' => $accountData['number'] ?? '',
'who' => $accountData['who'] ?? '',
'created' => now(),
'updated' => now(),
]);
}
- Update or Create Account Attribute:
This method updates an existing account attribute or creates a new one if it does not exist. It matches attributes based on the account ID and attribute key, then sets the attribute’s value and creation timestamp, ensuring that the attribute data is up-to-date.
public function updateOrCreateAttribute(Account $account, array $attributeData)
{
return AccountAttribute::updateOrCreate(
['uan_uac_id' => $account->id, 'uan_sst_uid' => $attributeData['key']],
[
'accountId' => $account->id,
'attributeId' => $attributeData['key'],
'value' => $attributeData['value'],
'created' => now(),
]
);
}
- Delete Account Attribute:
This method deletes a specified attribute from an account. It logs the deletion process, retrieves the attribute based on the account ID and attribute key, and if the attribute exists, it deletes it from the database, ensuring that obsolete or incorrect attributes are removed.
public function deleteAttribute(Account $account, string $attributeKey)
{
Log::info("Deleting account attribute {$attributeKey}", [
'attribute_key' => $attributeKey,
'account_id' => $account->id,
]);
$attribute = $account->attributes()->where('uan_sst_uid', $attributeKey)->first();
if ($attribute) {
$attribute->delete();
}
}
ApplicationRepository Overview
The ApplicationRepository class is tailored to handle operations related to application data within the system. It functions as the primary repository for managing the lifecycle of application records, including tasks such as fetching, updating, and deleting applications. This class plays a crucial role in maintaining the integrity and consistency of application data, acting as the main interface between the application logic and the underlying data storage. By centralizing these operations, the ApplicationRepository ensures that application data is managed effectively and adheres to the system’s business rules.
- Namespace: Toolstation\Customers\Repositories
- Implements: Toolstation\Core\Repositories\BaseRepository
Key Components
- Model Specification:
This method specifies the class name of the model that the repository is responsible for, in this case, the Application model. It ensures that the repository operations are performed on the correct model class.
public function model()
{
return Application::class;
}
CustomerRepository Overview
The CustomerRepository class is focused on managing customer-related data within the system. It serves as the main repository for operations such as retrieving, updating, and deleting customer records, providing a consistent interface for interacting with customer data. This class plays a key role in ensuring that customer information is handled in a reliable and organized manner, facilitating seamless access and manipulation of customer data throughout the application. By encapsulating these operations, the CustomerRepository supports the efficient management of customer-related tasks, ensuring data consistency and integrity.
- Namespace: Toolstation\Customers\Repositories
- Implements: Toolstation\Core\Repositories\BaseRepository
Key Components
- Model Specification:
This method specifies that the Customer model is the primary model this repository is responsible for, ensuring all operations are performed on the correct data entity.
public function model()
{
return Customer::class;
}
- Finding Customer Address:
This method retrieves a customer's address by its ID, with an optional customer ID for more specific queries.
public function findCustomerAddress(string $addressId, ?string $customerId = null)
- Creating a Customer:
This method creates a new customer record in the database using the provided customer data.
public function createCustomer($customerData)
- Updating a Customer:
This method updates an existing customer record with new data provided in the $customerData array.
public function updateCustomer($customerId, array $customerData)
- Managing Customer Addresses:
This method creates a new address associated with a customer, ensuring that key address details are correctly stored.
public function createAddressForCustomer($customer, $addressData)
- Managing Customer Issues:
This method creates an issue related to a customer's account, storing relevant details such as issue type, order ID, and comments.
public function createIssueForCustomer(Customer $customer, array $issue)
- Managing Customer Notes:
This method adds a note to a customer's account, capturing important information and flags for future reference.
public function createNoteForCustomer(Customer $customer, array $note)
- Customer Attribute Management:
This method creates or updates a customer attribute, ensuring that key-value pairs related to customer properties are accurately stored.
public function createAttributeForCustomer($customer, $attributeData)
- Deleting Customer Attributes:
This method deletes a specific customer attribute, allowing for the removal of obsolete or incorrect data.
public function deleteAttributeForCustomer($customer, $attributeId)
SavedAccountApplicationRepository Overview
The SavedAccountApplicationRepository class extends the BaseRepository and is designed to handle data operations related to SavedAccountApplication models. This repository class provides a centralized approach for managing operations such as creating or updating saved account applications. By encapsulating these operations, the repository ensures a consistent and organized method for handling SavedAccountApplication data, which aids in maintainability and testing.
- Namespace: Toolstation\Customers\Repositories
- Implements: Toolstation\Core\Repositories\BaseRepository
Key Components
- Model Specification:
This method specifies that the SavedAccountApplication model is the primary model this repository is responsible for. It ensures that all operations are performed on the correct data entity.
public function model()
{
return SavedAccountApplication::class;
}
- Creating or Updating an Application:
This method creates a new SavedAccountApplication record or updates an existing one based on the uaa_token value. If an application with the given token already exists, it updates the record with the new data provided in $applicationData. If no such application exists, it creates a new record. The status field defaults to SAVED if not provided in the input data.
public function createApplication(array $applicationData)
{
return SavedAccountApplication::updateOrCreate([
'uaa_token' => $applicationData['token'],
], [
'state' => $applicationData['state'],
'email' => $applicationData['email'],
'pin' => $applicationData['pin'],
'token' => $applicationData['token'],
'status' => $applicationData['status'] ?? SavedAccountApplication::STATUS['SAVED'],
]);
}