table:
  schema: public
  name: subscription
object_relationships:
  - name: cancellation_reason
    using:
      foreign_key_constraint_on: cancellation_reason_id
  - name: discount_coupon
    using:
      foreign_key_constraint_on: discount_coupon_id
  - name: patient
    using:
      foreign_key_constraint_on: patient_id
  - name: plan
    using:
      foreign_key_constraint_on: plan_id
  - name: plan_payment_provider
    using:
      manual_configuration:
        remote_table:
          schema: public
          name: plan_payment_provider
        insertion_order: null
        column_mapping:
          payment_provider: provider
          plan_id: plan_id
array_relationships:
  - name: installments
    using:
      foreign_key_constraint_on:
        column: subscription_id
        table:
          schema: public
          name: subscription_installment
  - name: subscription_transactions
    using:
      foreign_key_constraint_on:
        column: subscription_id
        table:
          schema: public
          name: subscription_transaction
computed_fields:
  - name: teleconsultations_credit
    definition:
      function:
        schema: public
        name: subscriptions_teleconsultations_credit_sum
    comment: ""
select_permissions:
  - role: marketplace
    permission:
      columns:
        - cancellation_at
        - cancellation_by_id
        - cancellation_by_type
        - cancellation_other_reason
        - cancellation_reason_id
        - cancellation_request_at
        - cancellation_requested_by_patient_at
        - created_at
        - discount_coupon_id
        - expires_at
        - id
        - patient_id
        - payment_provider
        - plan_id
        - subscribed_at
        - suspended_at
        - teleconsultation_renews_at
        - trial_expires_at
        - updated_at
      computed_fields:
        - teleconsultations_credit
      filter:
        patient:
          user_id:
            _eq: X-Hasura-User-Id
  - role: nutritionist_manager
    permission:
      columns:
        - cancellation_at
        - cancellation_by_id
        - cancellation_by_type
        - cancellation_other_reason
        - cancellation_reason_id
        - cancellation_request_at
        - cancellation_requested_by_patient_at
        - created_at
        - discount_coupon_id
        - expires_at
        - id
        - patient_id
        - payment_provider
        - plan_id
        - subscribed_at
        - suspended_at
        - teleconsultation_renews_at
        - trial_expires_at
        - updated_at
      computed_fields:
        - teleconsultations_credit
      filter: {}
  - role: patient
    permission:
      columns:
        - cancellation_at
        - cancellation_by_id
        - cancellation_by_type
        - cancellation_other_reason
        - cancellation_reason_id
        - cancellation_request_at
        - card_number
        - created_at
        - discount_coupon_id
        - expires_at
        - id
        - original_transaction
        - patient_id
        - payment_provider
        - plan_id
        - suspended_at
        - teleconsultation_renews_at
        - token
        - trial_expires_at
        - updated_at
      computed_fields:
        - teleconsultations_credit
      filter:
        patient_id:
          _eq: X-Hasura-User-Id
  - role: personal_trainer
    permission:
      columns:
        - cancellation_at
        - cancellation_by_id
        - cancellation_by_type
        - cancellation_other_reason
        - cancellation_reason_id
        - cancellation_request_at
        - created_at
        - expires_at
        - id
        - patient_id
        - payment_provider
        - plan_id
        - subscribed_at
        - suspended_at
        - teleconsultation_renews_at
        - trial_expires_at
        - updated_at
      computed_fields:
        - teleconsultations_credit
      filter:
        patient:
          personal_trainer_id:
            _eq: X-Hasura-User-Id
  - role: user
    permission:
      columns:
        - cancellation_at
        - cancellation_by_id
        - cancellation_by_type
        - cancellation_other_reason
        - cancellation_reason_id
        - cancellation_request_at
        - cancellation_requested_by_patient_at
        - created_at
        - discount_coupon_id
        - expires_at
        - id
        - patient_id
        - payment_provider
        - plan_id
        - subscribed_at
        - suspended_at
        - teleconsultation_renews_at
        - trial_expires_at
        - updated_at
      computed_fields:
        - teleconsultations_credit
      filter: {}
update_permissions:
  - role: marketplace
    permission:
      columns: []
      filter:
        patient:
          user_id:
            _eq: X-Hasura-User-Id
      check: null
  - role: nutritionist_manager
    permission:
      columns:
        - cancellation_at
        - cancellation_by_id
        - cancellation_by_type
        - cancellation_other_reason
        - cancellation_reason_id
        - cancellation_request_at
      filter: {}
      check: null
      set:
        updated_at: now()
  - role: user
    permission:
      columns:
        - cancellation_at
        - cancellation_by_id
        - cancellation_by_type
        - cancellation_other_reason
        - cancellation_reason_id
        - cancellation_request_at
      filter: {}
      check: null
      set:
        updated_at: now()
event_triggers:
  - name: cancel_contract_subscription
    definition:
      enable_manual: true
      update:
        columns:
          - cancellation_at
    retry_conf:
      num_retries: 3
      interval_sec: 60
      timeout_sec: 60
    webhook: '{{ACTIONS_HANDLER_URL}}/contract/cancel'
    headers:
      - name: Authorization
        value_from_env: ACTIONS_HANDLER_ACCESS_TOKEN
  - name: notify_cancelled_subscription
    definition:
      enable_manual: true
      update:
        columns:
          - cancellation_by_id
          - cancellation_other_reason
          - cancellation_by_type
          - cancellation_request_at
          - cancellation_at
    retry_conf:
      num_retries: 3
      interval_sec: 60
      timeout_sec: 60
    webhook: '{{ACTIONS_HANDLER_URL}}/notification/subscription/cancelled'
    headers:
      - name: Authorization
        value_from_env: ACTIONS_HANDLER_ACCESS_TOKEN
  - name: notify_new_subscription
    definition:
      enable_manual: false
      insert:
        columns: '*'
    retry_conf:
      num_retries: 3
      interval_sec: 60
      timeout_sec: 60
    webhook: '{{ACTIONS_HANDLER_URL}}/notification/subscription/new'
    headers:
      - name: Authorization
        value_from_env: ACTIONS_HANDLER_ACCESS_TOKEN
  - name: sort_competitions_when_cancel_subscription
    definition:
      enable_manual: false
      update:
        columns:
          - cancellation_at
    retry_conf:
      num_retries: 3
      interval_sec: 1800
      timeout_sec: 60
    webhook: '{{ACTIONS_HANDLER_URL}}/ranking/competition/sort'
    headers:
      - name: Authorization
        value_from_env: ACTIONS_HANDLER_ACCESS_TOKEN
  - name: subscription_suspended
    definition:
      enable_manual: false
      update:
        columns:
          - suspended_at
    retry_conf:
      num_retries: 3
      interval_sec: 3600
      timeout_sec: 60
    webhook: '{{ACTIONS_HANDLER_URL}}/bookings/cancelBySubscriptionSuspension'
    headers:
      - name: Authorization
        value_from_env: ACTIONS_HANDLER_ACCESS_TOKEN
