Customers

Address Book

Overview by Shivang


Get Customer Address Book

  • Route: GET ../customers/address-book/{customerId}
  • Controller: CustomerAddressBookController
  • Method: get
  • Request: Request
  • Description: Endpoint to retrieve all addresses associated with a specific customer.

The get method in the CustomerAddressBookController retrieves the address book for a specified customer. It first retrieves the customer using the customerService, ensuring that a 404 error is thrown if the customer is not found. The method checks if the user is authorized to view the customer's information. Upon successful authorization, it calls the private method allCustomerAddresses, which returns a collection of the customer's addresses using the CustomerAddressResource. This data is formatted appropriately for the response.


Create Customer Address

  • Route: POST ../customers/address-book/{customerId}
  • Controller: CustomerAddressBookController
  • Method: store
  • Request: CreateCustomerAddressRequest
  • Description: Endpoint to add a new address for a specific customer.

The store method in the CustomerAddressBookController allows for the creation of a new address for a specified customer. It first retrieves the customer using the customerService, ensuring that a 404 error is thrown if the customer is not found. The method checks if the user is authorized to update the customer's information. Upon successful authorization, it calls the createCustomerAddress method from the customerAddressService to create the new address using the data obtained from the request. A log entry is created to indicate the successful address creation. Finally, the method returns a collection of all addresses associated with the customer using the allCustomerAddresses method.


Set Primary Address

  • Route: PUT ../customers/address-book/{customerId}/primary-address
  • Controller: CustomerAddressBookController
  • Method: setPrimary
  • Request: Request
  • Description: Endpoint to set a specified address as the primary address for a customer.

The setPrimary method in the CustomerAddressBookController allows a customer to designate a specific address as their primary address. The method first validates the request to ensure that an address_id is provided. It logs the request for debugging purposes and attempts to retrieve the specified address using the customerAddressService.If the address belongs to the specified customer, it checks for authorization to update the customer's information. Upon successful authorization, the address is updated to set it as the primary address. A log entry is created to indicate the successful update, and the method returns a collection of all customer addresses. If an exception occurs during the address retrieval or update process, an error message is logged, and a 400 Bad Request response is returned with the exception message.


#Update Customer Address

  • Route: PUT ../customers/address-book/{addressId}
  • Controller: CustomerAddressBookController
  • Method: update
  • Request: UpdateCustomerAddressRequest
    • Class: UpdateCustomerAddressRequest
    • Extends: FormRequest
  • Description: Endpoint to update an existing address associated with a specific customer.

The update method in the CustomerAddressBookController updates a customer's address using the provided address ID and new address data from the request. It retrieves the customer ID from the authenticated user and fetches the current address via the customerAddressService, ensuring the user is authorized to make changes. Upon successful update, it logs the action and returns all addresses for the customer. If an error occurs, it catches the CustomerModuleException, logs the error message, and responds with a 400 Bad Request.


Delete Customer Address

  • Route: DELETE /{addressId}
  • Controller: CustomerAddressBookController
  • Method: delete
  • Request: DeleteCustomerAddressRequest
    • Class: DeleteCustomerAddressRequest
    • Extends: FormRequest
  • Description: Endpoint to delete an existing address associated with a specific customer.

The delete method in the CustomerAddressBookController deletes a customer's address using the provided address ID. It retrieves the customer ID from the authenticated user and fetches the current address via the customerAddressService, ensuring the user is authorized to perform the deletion. Upon successful deletion, it logs the action and returns all remaining addresses for the customer. If an error occurs, it catches the CustomerModuleException, logs the error message, and responds with a 400 Bad Request.


title: Customer Address Route by Atul

Introduction

This Page provides an overview of the routes defined for the Customer Address Book set of operations in the Toolstation E-Com Api.

List of Routes for Customer Address Book

  1. customers/address-book/_ping
  2. customers/address-book/{customerId}
  3. customers/address-book/{customerId}/primary-address
  4. customers/address-book/{addressId}

GET: customers/address-book/_ping

  • Method: GET
  • Controller: PingController
  • Description: Endpoint to check if the Customer Address Book is valid and running successfully. This is a common endpoint for health checks or status pings.

GET: customers/address-book/{customerId}

  • Controller: CustomerAddressBookController
  • Method: Get
  • Request: Request
  • Parameters:
    • customerId - Identifies the customer.
  • Description: Endpoint for retrieving a customer's address information.

The get function retrieves a customer's address information by first fetching the customer object, authorizing the user to view the information, and then retrieving all the addresses associated with the customer.

public function get(Request $request, $customerId)
{
    // Function implementation goes here
}       

Request Parameters for customers/address-book/{customerId}:

  • Header :- Accept-Language:- en-UK,en

Response

Response Code: 200 OK

Response:

{
    "data": [
        {
            "id": "DXX02XXXX",
            "type": 1,
            "line_1": "address1",
            "line_2": "address2",
            "line_3": "address3",
            "town": "DELHI",
            "county": "",
            "postcode": "110037",
            "country_id": 1,
            "formatted": [
                "address1",
                "address2",
                "address3",
                "DELHI",
                "110037",
                "United Kingdom"
            ]
        }
    ]
}

Response Code: 403 Forbidden

Response:

{
    "error": {
        "code": "403.99",
        "message": "Forbidden",
        "info": "https://developers.example.com",
        "data": null
    }
}

POST: customers/address-book/{customerId}

  • Controller: CustomerAddressBookController
  • Method: Post
  • Request: CreateCustomerAddressRequest
  • Parameters:
    • customerId - Identifies the customer.
  • Description: Endpoint for creating a new customer address.

The store function performs the following actions: retrieves the customer, authorizes the user, creates a new address, logs the action, and returns the updated customer addresses.

public function store(CreateCustomerAddressRequest $request, $customerId)
{
    // Function implementation goes here
}       

Request Parameters for customers/address-book/{customerId}:

  • Header :- Accept-Language:- en-UK,en

Response

Response Code: 403 Forbidden

Response:

{
    "error": {
        "code": "403.99",
        "message": "Forbidden",
        "info": "https://developers.example.com",
        "data": null
    }
}

PUT: customers/address-book/{customerId}/primary-address

  • Controller: CustomerAddressBookController
  • Method: PUT
  • Request: Request
  • Parameters:
    • customerId - Identifies the customer.
  • Description: Endpoint for setting a customer's primary address.

The setPrimary function is to allow a customer to set one of their addresses as their primary address. It performs several checks and validations to ensure that the operation is authorized and that the requested address belongs to the specified customer. If everything is successful, it updates the customer's primary address and returns the updated list of addresses. If any errors occur, it logs the details and returns an appropriate response.

public function setPrimary(Request $request, $customerId)
{
    // Function implementation goes here
}       

Request Parameters for customers/address-book/{customerId}/primary-address:

  • Header :- Accept-Language:- en-UK,en

Response

Response Code: 403 Forbidden

Response:

{
    "error": {
        "code": "403.99",
        "message": "Forbidden",
        "info": "https://developers.example.com",
        "data": null
    }
}

PUT: customers/address-book/{addressId}

  • Controller: CustomerAddressBookController
  • Method: PUT
  • Request: UpdateCustomerAddressRequest
  • Parameters:
    • customerId - Identifies the customer.
  • Description: Endpoint for updating a customer address.

The update function is responsible for updating a customer address. It retrieves the necessary data from the request, authorizes the user to update the address, updates the address, logs the result, and returns the updated customer addresses. If any exception occurs, it logs the abort and returns a JSON response with an error message.

public function update(UpdateCustomerAddressRequest $request, $addressId)
{
    // Function implementation goes here
}       

Request Parameters for customers/address-book/{addressId}:

  • Header :- Accept-Language:- en-UK,en

Response

Response Code: 403 Forbidden

Response:

{
    "error": {
        "code": "403.99",
        "message": "Forbidden",
        "info": "https://developers.example.com",
        "data": null
    }
}

DELETE: customers/address-book/{addressId}

  • Controller: CustomerAddressBookController
  • Method: Delete
  • Request: DeleteCustomerAddressRequest
  • Parameters:
    • customerId - Identifies the customer.
  • Description: Endpoint for deleting a customer address.

The delete method is responsible for deleting a customer address. It takes in a DeleteCustomerAddressRequest object and an $addressId parameter. The method retrieves the authenticated user's ID, retrieves the customer address, authorizes the user to update the customer, deletes the customer address, logs a success message, and returns the result of calling the allCustomerAddresses method. If an exception occurs, it logs an abort message and returns a JSON response with an error message.

public function delete(DeleteCustomerAddressRequest $request, $addressId)
{
    // Function implementation goes here
}       

Request Parameters for customers/address-book/{addressId}:

  • Header :- Accept-Language:- en-UK,en

Response

Response Code: 403 Forbidden

Response:

{
    "error": {
        "code": "403.99",
        "message": "Forbidden",
        "info": "https://developers.example.com",
        "data": null
    }
}

Copyright © 2026