title: Customers
domain: store
routes:
  - method: POST
    path: /customers
  - method: GET
    path: /customers/:id
  - method: POST
    path: /customers/:id
  - method: POST
    path: /customers/:id/addresses
  - method: POST
    path: /customers/:id/:address_id
  - method: POST
    path: /customers/:id/payment-methods
  - method: POST
    path: /customers/:id/reset-password
  - method: POST
    path: /customers/:id/reset-password-token
route: /customers
description: >
  Customers can create a login to view order history and manage details. 
  Customers must have unique emails.
endpoints:
  - path: /
    method: POST
    title: Create customer
    body:
      - name: email
        type: String
        required: true
        description: The Customer's email; must be unique.
      - name: first_name
        type: String
        required: true
        description: The Customer's first name.
      - name: last_name
        type: String
        required: true
        description: The Customer's last name.
      - name: password
        type: String
        required: true
        description: The Customer's desired password.
      - name: phone
        type: String
        description: The customer's phone number.
    description: >
      Creates a customer profile with the given details. If the email has been
      used on previous orders the newly created Customer profile will be able to
      view the order history associated with this email.
  - path: /:id
    method: GET
    title: Retrieve customer
    params:
      - name: id
        type: String
        description: The id of the Customer to retrieve.
    description: >
      Retrieves the customer's details. The Customer identified by `id` must be
      authenticated.

  - path: /:id
    method: POST
    title: Update customer
    params:
      - name: id
        type: String
        description: The id of the customer.
    body:
      - name: first_name
        type: String
        description: The Customer's first name.
      - name: last_name
        type: String
        description: The Customer's last name.
      - name: password
        type: String
        description: >
          The Customer's password. Will create a hashed value for the database.
      - name: phone
        type: String
        description: The Customer's phone number.
    description: >
      Updates the customer's details. The customer identified by `id` must be
      authenticated in order to call this endpoint.
  - path: /:id/addresses
    method: POST
    title: Create customer shipping address
    params:
      - name: id
        type: String
        description: The id of the Customer.
    body:
      - name: address
        type: Address
        description: The address to add.
    description: >
      Adds an address to the customer's saved addresses. The customer must be 
      authenticated prior to making this call.

  - path: /:id/addresses/:address_id
    method: POST
    title: Update customer shipping address
    params:
      - name: id
        type: String
        description: The id of the Customer.
      - name: address_id
        type: String
        description: The id of the saved address to update.
    body:
      - name: address
        type: Address
        description: The address to update.
    description: >
      Updates a saved customer address. The customer must be authenticated prior
      to making this call.

  - path: /:id/payment-methods
    method: GET
    title: Retrieve saved payment methods
    params:
      - name: id
        type: String
        description: The id of the customer.
    description: >
      Asks each payment provider for saved payment methods if any exist. This 
      will only return in methods in the case where the payment provider allows
      payment methods to be saved. The customer must be authenticated in order 
      to call this endpoint.
  - path: /password-reset
    method: POST
    title: Reset password
    body:
      - name: email
        type: String
        required: true
        description: The email of the Customer.
      - name: token
        type: String
        required: true
        description: >
          The token that will be used to reset the password. Should be generated
          with a prior call to `/password-token`.
      - name: password
        type: String
        required: true
        description: >
          The new password to authenticate the user with.
    description: >
      Resets a customer's password. The reset call requires a valid token

  - path: /password-token
    method: POST
    title: Generate password reset token
    body:
      - name: email
        type: String
        required: true
        description: The email of the Customer profile to reset password for.
    description: >
      Generates a reset password token. The token should be sent to the customer
      via an email provider. Note that this doesn't send any emails it only 
      generates the token and informs listeners to
      `customer.password_token_generated` that the token was created.

response: |
  {
    "customer": {
      "id": "cus_4eThzYSuGv",
      "email": "iron@man.com",
      "first_name": "Tony",
      "last_name": "Stark",
      "billing_address": {
        "id": "addr_WgNn0BSfIu",
        "customer_id": "cus_4eThzYSuGv",
        "company": "Stark Industries",
        "first_name": "Tony",
        "last_name": "Stark",
        "address_1": "Hollywood Boulevard 1",
        "address_2": null,
        "city": "Los Angeles",
        "country_code": "US",
        "province": "CA",
        "postal_code": "90046",
        "phone": null,
        "created_at": "2020-12-11T17:03:54.458Z",
        "updated_at": "2020-12-11T17:03:54.458Z",
        "deleted_at": null,
        "metadata": null
      },
      "shipping_addresses": [{
        "id": "addr_WgNn0BSfIu",
        "customer_id": "cus_4eThzYSuGv",
        "company": "Stark Industries",
        "first_name": "Tony",
        "last_name": "Stark",
        "address_1": "Hollywood Boulevard 1",
        "address_2": null,
        "city": "Los Angeles",
        "country_code": "US",
        "province": "CA",
        "postal_code": "90046",
        "phone": null,
        "created_at": "2020-12-11T17:03:54.458Z",
        "updated_at": "2020-12-11T17:03:54.458Z",
        "deleted_at": null,
        "metadata": null
      }],
      "phone": "4242424242",
      "has_account": false,
      "orders": [{
        "id": "order_fi13oadpo2r3vc2g4592",
        "status": "pending",
        "fulfillment_status": "shipped",
        "payment_status": "captured",
        "display_id": 50433,
        "total": 4004000,
        "metadata": null
      }],
      "created_at": "2020-12-11T17:03:54.458Z",
      "updated_at": "2020-12-11T17:03:54.458Z",
      "metadata": {}
    }
  }
