table:
  schema: public
  name: patients
object_relationships:
  - name: food_plan
    using:
      foreign_key_constraint_on: published_food_plan_id
  - name: gender
    using:
      foreign_key_constraint_on: gender_id
  - name: goal
    using:
      foreign_key_constraint_on: goal_id
  - name: interest
    using:
      manual_configuration:
        remote_table:
          schema: public
          name: patient_interest
        insertion_order: null
        column_mapping:
          patient_interest_id: id
  - name: macros_reference_value
    using:
      foreign_key_constraint_on: macros_reference_value_id
  - name: menu
    using:
      foreign_key_constraint_on: menu_id
  - name: personal_trainer
    using:
      foreign_key_constraint_on: personal_trainer_id
  - name: reach_goal
    using:
      foreign_key_constraint_on: reach_goal_id
  - name: smartfit_unit
    using:
      foreign_key_constraint_on: smartfit_unit_acronym
  - name: subscription
    using:
      foreign_key_constraint_on: subscription_id
  - name: user
    using:
      foreign_key_constraint_on: user_id
array_relationships:
  - name: anamneses
    using:
      foreign_key_constraint_on:
        column: patient_id
        table:
          schema: public
          name: anamnesis
  - name: anthropometries
    using:
      foreign_key_constraint_on:
        column: patient_id
        table:
          schema: public
          name: anthropometries
  - name: appointments
    using:
      foreign_key_constraint_on:
        column: patient_id
        table:
          schema: public
          name: appointment
  - name: food_plans
    using:
      foreign_key_constraint_on:
        column: patient_id
        table:
          schema: public
          name: food_plan
  - name: food_records
    using:
      foreign_key_constraint_on:
        column: patient_id
        table:
          schema: public
          name: food_record
  - name: patient_meals
    using:
      foreign_key_constraint_on:
        column: patient_id
        table:
          schema: public
          name: patient_meal
  - name: patient_restrictions
    using:
      foreign_key_constraint_on:
        column: patient_id
        table:
          schema: public
          name: patient_restrictions
  - name: photos
    using:
      foreign_key_constraint_on:
        column: patient_id
        table:
          schema: public
          name: patient_photo
  - name: restrictions
    using:
      manual_configuration:
        remote_table:
          schema: public
          name: patient_restrictions_view
        insertion_order: null
        column_mapping:
          id: patient_id
  - name: subscriptions
    using:
      foreign_key_constraint_on:
        column: patient_id
        table:
          schema: public
          name: subscription
computed_fields:
  - name: bioimpedance_credit
    definition:
      function:
        schema: public
        name: patients_bioimpedance_credit_sum
  - name: can_upgrade_subscription
    definition:
      function:
        schema: public
        name: patients_can_upgrade_subscription
  - name: finished_appointments_count
    definition:
      function:
        schema: public
        name: patients_finished_appointments_count
  - name: is_centauro
    definition:
      function:
        schema: public
        name: patients_is_centauro
  - name: is_subscriber
    definition:
      function:
        schema: public
        name: patients_is_subscriber
  - name: meeting_count
    definition:
      function:
        schema: public
        name: patients_meeting_count
  - name: teleconsultations_credit
    definition:
      function:
        schema: public
        name: patients_teleconsultations_credit_sum
    comment: ""
insert_permissions:
  - role: marketplace
    permission:
      check:
        user_id:
          _eq: X-Hasura-User-Id
      set:
        created_by_user_id: x-hasura-User-Id
      columns:
        - birth_date
        - cell_phone
        - country_code
        - document_number
        - email
        - gender_id
        - height
        - name
        - notes
        - photo_url
        - published_food_plan_id
        - tenant
        - user_id
        - weight
        - weight_goal
  - role: nutritionist_manager
    permission:
      check: {}
      set:
        created_by_user_id: x-hasura-User-Id
      columns:
        - birth_date
        - cell_phone
        - country_code
        - document_number
        - email
        - gender_id
        - height
        - name
        - notes
        - photo_url
        - published_food_plan_id
        - tenant
        - user_id
        - weight
        - weight_goal
  - role: user
    permission:
      check: {}
      set:
        created_by_user_id: x-hasura-User-Id
      columns:
        - birth_date
        - cell_phone
        - country_code
        - document_number
        - email
        - gender_id
        - height
        - name
        - notes
        - photo_url
        - published_food_plan_id
        - tenant
        - user_id
        - weight
        - weight_goal
select_permissions:
  - role: marketplace
    permission:
      columns:
        - authorization_responsible_url
        - birth_date
        - cell_phone
        - collaborator
        - country_code
        - created_at
        - document_number
        - email
        - gender_id
        - height
        - id
        - internal_collaborator
        - level
        - macros_reference_value_id
        - menu_id
        - name
        - notes
        - patient_interest_id
        - photo_url
        - published_food_plan_id
        - reach_goal_id
        - sign_up_at
        - tenant
        - time_exercise
        - time_sleep
        - time_wakeup
        - timezone
        - user_id
        - weight
        - weight_goal
      computed_fields:
        - finished_appointments_count
        - is_subscriber
        - meeting_count
        - teleconsultations_credit
      filter: {}
      allow_aggregations: true
  - role: nutritionist_manager
    permission:
      columns:
        - authorization_responsible_url
        - birth_date
        - cell_phone
        - collaborator
        - country_code
        - created_at
        - document_number
        - email
        - gender_id
        - goal_id
        - height
        - id
        - internal_collaborator
        - level
        - macros_reference_value_id
        - menu_id
        - name
        - notes
        - other_restriction
        - patient_interest_id
        - photo_url
        - published_food_plan_id
        - reach_goal_id
        - sign_up_at
        - subscription_id
        - telegram_id
        - tenant
        - time_exercise
        - time_sleep
        - time_wakeup
        - timezone
        - updated_at
        - user_id
        - weight
        - weight_goal
      computed_fields:
        - finished_appointments_count
        - is_subscriber
        - meeting_count
        - teleconsultations_credit
      filter: {}
      allow_aggregations: true
  - role: patient
    permission:
      columns:
        - authorization_responsible_url
        - birth_date
        - cell_phone
        - collaborator
        - country_code
        - created_at
        - document_number
        - email
        - gender_id
        - goal_id
        - height
        - id
        - internal_collaborator
        - is_smartfit
        - level
        - macros_reference_value_id
        - menu_id
        - name
        - notes
        - other_restriction
        - patient_interest_id
        - photo_url
        - published_food_plan_id
        - reach_goal_id
        - sign_up_at
        - smartfit_token
        - smartfit_unit_acronym
        - subscription_id
        - telegram_id
        - tenant
        - time_exercise
        - time_sleep
        - time_wakeup
        - timezone
        - updated_at
        - user_id
        - weight
        - weight_goal
      computed_fields:
        - bioimpedance_credit
        - can_upgrade_subscription
        - finished_appointments_count
        - is_centauro
        - is_subscriber
        - meeting_count
        - teleconsultations_credit
      filter:
        id:
          _eq: X-Hasura-User-Id
      allow_aggregations: true
  - role: personal_trainer
    permission:
      columns:
        - birth_date
        - cell_phone
        - collaborator
        - country_code
        - created_at
        - document_number
        - email
        - gender_id
        - height
        - id
        - internal_collaborator
        - level
        - name
        - notes
        - photo_url
        - published_food_plan_id
        - reach_goal_id
        - sign_up_at
        - tenant
        - time_exercise
        - time_sleep
        - time_wakeup
        - timezone
        - user_id
        - weight
        - weight_goal
      computed_fields:
        - finished_appointments_count
        - is_subscriber
      filter:
        personal_trainer_id:
          _eq: X-Hasura-User-Id
      allow_aggregations: true
  - role: user
    permission:
      columns:
        - authorization_responsible_url
        - birth_date
        - cell_phone
        - collaborator
        - country_code
        - created_at
        - document_number
        - email
        - gender_id
        - goal_id
        - height
        - id
        - internal_collaborator
        - level
        - macros_reference_value_id
        - menu_id
        - name
        - notes
        - other_restriction
        - patient_interest_id
        - photo_url
        - published_food_plan_id
        - reach_goal_id
        - sign_up_at
        - subscription_id
        - telegram_id
        - tenant
        - time_exercise
        - time_sleep
        - time_wakeup
        - timezone
        - updated_at
        - user_id
        - weight
        - weight_goal
      computed_fields:
        - finished_appointments_count
        - is_subscriber
        - meeting_count
        - teleconsultations_credit
      filter: {}
      allow_aggregations: true
update_permissions:
  - role: marketplace
    permission:
      columns:
        - birth_date
        - cell_phone
        - country_code
        - document_number
        - email
        - gender_id
        - height
        - internal_collaborator
        - macros_reference_value_id
        - name
        - notes
        - photo_url
        - published_food_plan_id
        - tenant
        - user_id
        - weight
        - weight_goal
      filter:
        user_id:
          _eq: X-Hasura-User-Id
      check: {}
  - role: nutritionist_manager
    permission:
      columns:
        - birth_date
        - cell_phone
        - country_code
        - document_number
        - email
        - gender_id
        - height
        - internal_collaborator
        - macros_reference_value_id
        - name
        - notes
        - photo_url
        - published_food_plan_id
        - tenant
        - user_id
        - weight
        - weight_goal
      filter: {}
      check: null
      set:
        updated_at: now()
  - role: patient
    permission:
      columns:
        - auth_token
        - auth_token_created_at
        - authorization_responsible_url
        - birth_date
        - cell_phone
        - country_code
        - device_id
        - document_number
        - email
        - gender_id
        - goal_id
        - height
        - level
        - name
        - notes
        - other_restriction
        - patient_interest_id
        - photo_url
        - reach_goal_id
        - time_exercise
        - time_sleep
        - time_wakeup
        - timezone
        - user_id
        - weight
        - weight_goal
      filter:
        id:
          _eq: X-Hasura-User-Id
      check: null
      set:
        updated_at: now()
  - role: user
    permission:
      columns:
        - birth_date
        - cell_phone
        - country_code
        - document_number
        - email
        - gender_id
        - height
        - internal_collaborator
        - macros_reference_value_id
        - name
        - notes
        - photo_url
        - published_food_plan_id
        - tenant
        - user_id
        - weight
        - weight_goal
      filter: {}
      check: null
      set:
        updated_at: now()
delete_permissions:
  - role: marketplace
    permission:
      filter:
        user_id:
          _eq: X-Hasura-User-Id
  - role: nutritionist_manager
    permission:
      filter: {}
  - role: user
    permission:
      filter: {}
event_triggers:
  - name: cache_patient
    definition:
      enable_manual: false
      insert:
        columns: '*'
      update:
        columns: '*'
    retry_conf:
      num_retries: 3
      interval_sec: 60
      timeout_sec: 60
    webhook: '{{ACTIONS_HANDLER_URL}}/patients/cache'
    headers:
      - name: Authorization
        value_from_env: ACTIONS_HANDLER_ACCESS_TOKEN
  - name: check_new_subscriber_pending_bioimpedances
    definition:
      enable_manual: true
      update:
        columns:
          - subscription_id
    retry_conf:
      num_retries: 3
      interval_sec: 3600
      timeout_sec: 60
    webhook_from_env: ACTIONS_HANDLER_BIOIMPEDANCE_IMPORTATION_URL
    headers:
      - name: Authorization
        value_from_env: ACTIONS_HANDLER_ACCESS_TOKEN
  - name: check_pending_credits
    definition:
      enable_manual: true
      insert:
        columns: '*'
      update:
        columns:
          - sign_up_at
    retry_conf:
      num_retries: 5
      interval_sec: 3600
      timeout_sec: 60
    webhook: '{{ACTIONS_HANDLER_URL}}/patients/check_pending_credits'
    headers:
      - name: Authorization
        value_from_env: ACTIONS_HANDLER_ACCESS_TOKEN
  - name: check_subscription
    definition:
      enable_manual: true
      insert:
        columns: '*'
      update:
        columns:
          - sign_up_at
    retry_conf:
      num_retries: 5
      interval_sec: 3600
      timeout_sec: 60
    webhook_from_env: ACTIONS_HANDLER_PATIENT_CHECK_SUBSCRIPTION_URL
    headers:
      - name: Authorization
        value_from_env: ACTIONS_HANDLER_ACCESS_TOKEN
  - name: first_patient_signup
    definition:
      enable_manual: false
      insert:
        columns: '*'
      update:
        columns:
          - sign_up_at
    retry_conf:
      num_retries: 5
      interval_sec: 3600
      timeout_sec: 60
    webhook: '{{ACTIONS_HANDLER_URL}}/patients/check_sign_up'
    headers:
      - name: Authorization
        value_from_env: ACTIONS_HANDLER_ACCESS_TOKEN
  - name: notify_food_plan_ready
    definition:
      enable_manual: false
      update:
        columns:
          - published_food_plan_id
    retry_conf:
      num_retries: 3
      interval_sec: 60
      timeout_sec: 60
    webhook_from_env: ACTIONS_HANDLER_NOTIFY_FOOD_PLAN_READY
    headers:
      - name: Authorization
        value_from_env: ACTIONS_HANDLER_ACCESS_TOKEN
  - name: nutri_unavailable_change
    definition:
      enable_manual: false
      update:
        columns:
          - user_id
    retry_conf:
      num_retries: 3
      interval_sec: 60
      timeout_sec: 60
    webhook: '{{ACTIONS_HANDLER_URL}}/notification/patient/nutri_changed'
    headers:
      - name: Authorization
        value_from_env: ACTIONS_HANDLER_ACCESS_TOKEN
  - name: published_food_plan
    definition:
      enable_manual: false
      update:
        columns:
          - published_food_plan_id
    retry_conf:
      num_retries: 5
      interval_sec: 3600
      timeout_sec: 180
    webhook_from_env: BACKOFFICE_HANDLER_URL
    headers:
      - name: Authorization
        value_from_env: BACKOFFICE_ACCESS_TOKEN
  - name: sort_competitions_when_patient_subscribe
    definition:
      enable_manual: true
      update:
        columns:
          - subscription_id
          - internal_collaborator
          - collaborator
    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
