Skip to content

Add Add-on purchase API services

Corinna Gogolok requested to merge 408494_add_add_on_purchase_api_services into master

What does this MR do and why?

Part of https://gitlab.com/gitlab-org/gitlab/-/issues/414605+

New API endpoints to sync Add-on purchases are going to be added in another iteration. These endpoints allow to create, get and update an Add-on purchase. In this change, two new service to create and update an Add-on purchase are added that will later be used by the endpoints.

The endpoints will be added in Add API endpoints to sync add-on purchases (!123382 - merged).

How to set up and validate locally

  1. Create the code suggestions add-on in the rails console (note: This record will be added as a post migration or lazy creation in another MR):

    add_on = GitlabSubscriptions::AddOn.create(name: :code_suggestions, description: 'Add-on for code suggestions')
  2. Load a namespace and user to use:

    namespace = Namespace.find(<NAMESPACE_ID>)
    user = User.find(<USER_ID>)
  3. Test the service for creation.

    Test scenarios
    1. With a validation error:
      params = {
        quantity: 0,
        expires_on: Date.current + 1.month,
        purchase_xid: '1a2b3c'
      }
      
      GitlabSubscriptions::AddOnPurchases::CreateService.new(
        user,
        namespace,
        add_on,
        params
      ).execute
      Expected result: Error with the message that the Add-on purchase couldn't be saved.
    2. With a success:
      params = {
        quantity: 1,
        expires_on: Date.current + 1.month,
        purchase_xid: '1a2b3c'
      }
      
      GitlabSubscriptions::AddOnPurchases::CreateService.new(
        user,
        namespace,
        add_on,
        params
      ).execute
      Expected result: Success.
    3. With an error when the purchase already exists:
      params = {
        quantity: 1,
        expires_on: Date.current + 1.month,
        purchase_xid: '1a2b3c'
      }
      
      GitlabSubscriptions::AddOnPurchases::CreateService.new(
        user,
        namespace,
        add_on,
        params
      ).execute
      Expected result: Error with the message that the Add-on purchase already exists and the update endpoint should be used instead.
  4. Test the service to update an add-on purchase record.

    Test scenarios
    1. With a validation error:
      params = {
        quantity: 0,
        expires_on: Date.current + 2.months,
        purchase_xid: '1a2b3c'
      }
      
      GitlabSubscriptions::AddOnPurchases::UpdateService.new(
        user,
        namespace,
        add_on,
        params
      ).execute
      Expected result: Error with the message that the Add-on purchase couldn't be saved.
    2. With a success:
      params = {
        quantity: 2,
        expires_on: Date.current + 2.months,
        purchase_xid: '1a2b3c'
      }
      
      GitlabSubscriptions::AddOnPurchases::UpdateService.new(
        user,
        namespace,
        add_on,
        params
      ).execute
      Expected result: Success.
    3. With an error when the purchase does not exist:
      namespace = Namespace.find(<ANOTHER_NAMESPACE_ID>)
      params = {
        quantity: 2,
        expires_on: Date.current + 2.months,
        purchase_xid: '1a2b3c'
      }
      
      GitlabSubscriptions::AddOnPurchases::UpdateService.new(
        user,
        namespace,
        add_on,
        params
      ).execute
      Expected result: Error with the message that the Add-on purchase does not exist and the create endpoint should be used instead.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Corinna Gogolok

Merge request reports

Loading