openapi: 3.1.0
info:
  title: iClinic external integration spec
  description: |-
    A public API for customer server notifications
  contact:
    email: info@iclinic.ua
  version: 0.0.6

components:
  schemas:
    ApiEvent:
      type: object
      properties:
        name:
          type: string
          description: event name
          enum: [PatientSaved, PatientDeleted, ReceptionSaved, ReceptionDeleted, PaymentOrderSaved, PaymentOrderDeleted, PaymentItemSaved, PaymentItemDeleted, FinanceItemSaved, FinanceItemDeleted, PriceListItemSaved, PriceListItemDeleted, LeadSaved, LeadDeleted]
        initiator:
          type: string
          description: email of user, who has triggered this event
        resourceId:
          type: string
          description: an identifier of saved or removed resource
          maxLength: 64
        resource:
          oneOf:
            - $ref: '#/components/schemas/PatientSaved'
            - $ref: '#/components/schemas/PatientDeleted'
            - $ref: '#/components/schemas/ReceptionSaved'
            - $ref: '#/components/schemas/ReceptionDeleted'
            - $ref: '#/components/schemas/PaymentOrderSaved'
            - $ref: '#/components/schemas/PaymentOrderDeleted'
            - $ref: '#/components/schemas/PaymentItemSaved'
            - $ref: '#/components/schemas/PaymentItemDeleted'
            - $ref: '#/components/schemas/FinanceItemSaved'
            - $ref: '#/components/schemas/FinanceItemDeleted'
            - $ref: '#/components/schemas/PriceListItemSaved'
            - $ref: '#/components/schemas/PriceListItemDeleted'
            - $ref: '#/components/schemas/LeadSaved'
            - $ref: '#/components/schemas/LeadDeleted'

    Patient:
      type: object
      properties:
        id:
          type: string
          description: patient identifier
          maxLength: 64
        firstName:
          type: string
          maxLength: 50
        patronymicName:
          type: string
          maxLength: 50
        gender:
          type: string
          enum:
            - M
            - F
        phoneHash:
          type: string
          description: sha256(firstName + phone)
          maxLength: 64

    PatientSaved:
      type: object
      properties:
        patient:
          $ref: '#/components/schemas/Patient'
        cardNumber:
          type: string
        source:
          type: string
        categories:
          type: array
          items:
            type: string
        curator:
          type: string
          description: doctor's email, can be null
    PatientDeleted:
      type: object
      properties:
        patient:
          $ref: '#/components/schemas/Patient'

    ReceptionSaved:
      type: object
      properties:
        patient:
          $ref: '#/components/schemas/Patient'
        clinicId:
          type: string
          description: clinic identifier
          maxLength: 64
        doctor:
          type: string
          description: doctor's email
        date:
          type: string
          format: date
          description: format yyyy-MM-dd
        timeFrom:
          type: integer
          minimum: 0
          maximum: 1440
          description: start time in minutes
        timeTo:
          type: integer
          minimum: 0
          maximum: 1440
          description: end time in minutes
        comments:
          type: string
          maxLength: 500
        cancelled:
          type: boolean
    ReceptionDeleted:
      type: object
      properties:
        patient:
          $ref: '#/components/schemas/Patient'

    PaymentOrderSaved:
      type: object
      properties:
        patient:
          $ref: '#/components/schemas/Patient'
        date:
          type: string
          format: date
          description: format yyyyMMdd
        amount:
          type: number
          description: includes discount
        paidAmount:
          type: number
        state:
          type: string
          enum:
            - CREATED
            - PAID
            - PENDING
        discount:
          type: number
          description: in absolute numbers
        notes:
          type: string
          description: payment order diagnosis or description, which is visible to patient
        internalNotes:
          type: string
          description: internal notes, which are not visible to patient
        doctor:
          type: string
          description: doctor's email, can be null
        clinicId:
          type: string
          description: clinic identifier
          maxLength: 64
        receptionId:
          type: string
          description: reception identifier. Can be null if payment order not linked with any reception.
          maxLength: 64
    PaymentOrderDeleted:
      type: object
      properties:
        patient:
          $ref: '#/components/schemas/Patient'

    PaymentItemSaved:
      type: object
      properties:
        patient:
          $ref: '#/components/schemas/Patient'
        date:
          type: string
          format: date
          description: format yyyyMMdd
        amount:
          type: number
        cashless:
          type: boolean
        upfront:
          type: boolean
        bonus:
          type: boolean
        insurance:
          type: boolean
        internalNotes:
          type: string
          description: internal notes, which are not visible to patient
        method:
          type: string
          description: finance account name
        clinicId:
          type: string
          description: clinic identifier
          maxLength: 64
        paymentOrderId:
          type: string
          description: payment order identifier. Can be null for upfronts.
          maxLength: 64
    PaymentItemDeleted:
      type: object
      properties:
        patient:
          $ref: '#/components/schemas/Patient'

    FinanceItemSaved:
      type: object
      properties:
        patient:
          $ref: '#/components/schemas/Patient'
        date:
          type: string
          format: date
          description: format yyyyMMdd
        amount:
          type: number
        cashless:
          type: boolean
        insurance:
          type: boolean
          description: true for an insurance company payment
        internalNotes:
          type: string
        method:
          type: string
          description: finance account name
        clinicId:
          type: string
          description: clinic identifier
          maxLength: 64
        paymentOrderId:
          type: string
          description: payment order identifier. Can be null for manually entered financial transactions.
          maxLength: 64
    FinanceItemDeleted:
      type: object
      properties:
        patient:
          $ref: '#/components/schemas/Patient'

    PriceListItemSaved:
      type: object
      properties:
        code:
          type: string
        description:
          type: string
        price:
          type: number
        currency:
          type: string
        categoryName:
          type: string
    PriceListItemDeleted:
      type: object
      properties:
        code:
          type: string

    LeadSaved:
      type: object
      properties:
        patient:
          $ref: '#/components/schemas/Patient'
          description: can be null if lead is cancelled.
        utm:
          type: string
        state:
          type: string
          enum:
            - CREATED
            - COMPLETED
            - CANCELLED
        receptionId:
          type: string
          description: reception identifier. Can be null if lead not linked with any reception.
          maxLength: 64
    LeadDeleted:
      type: object
      properties:
        patient:
          $ref: '#/components/schemas/Patient'
          description: can be null

#    PaymentOrderFiscalized:
#      type: object
#      properties:
#        patient:
#          $ref: '#/components/schemas/Patient'
#        date:
#          type: string
#          format: date
#          description: format yyyyMMdd
#        amount:
#          type: number
#        discount:
#          type: number
#    PaymentOrderUnfiscalized:
#      type: object
#      properties:
#        patient:
#          $ref: '#/components/schemas/Patient'

#    todo payment item fiscalized
