---
swagger: "2.0"

info:
  version: 1.0.0
  title: API Specifications

schemes:
  - https

host: qa-ows-sound-recordings.theorchard.io
paths:
  /hello/:
    get:
      summary: Check the health of the application.
      responses:
        200:
          description: 200 OK
          examples:
            application/json: {"status": "ok"}
  /acrids/:
    post:
      summary: Register an audio fingerprint for an asset by ACRID
      parameters:
        - in: body
          name: body
          schema:
            type: object
            required:
              - acr_id
              - asset_id
            properties:
              acr_id:
                type: string
              asset_id:
                type: string
      responses:
        200:
          description: ACRID already linked to OrchardAsset
          examples:
            application/json: {
              "track_id": 123
            }
        201:
          description: ACRID linked to OrchardAsset
          examples:
            application/json: {
              "track_id": 123
            }
        404:
          description: Asset not found
          examples:
            application/json: {
              "error": "AssetNotFound"
            }
        409:
          description: Conflict
          examples:
            application/json: {
              "error": "AlreadyFingerprinted"
            }
        422:
          description: Malformed input data
          examples:
            application/json: {
              "code": "bad_request",
              "message": {
                "json": {
                  "asset": {
                    "filename": [
                      "Not a valid UUID."
                    ]
                  }
                }
              }
            }
  /sound_recordings/:
    post:
      summary: Upsert a sound recording by fingerprinted asset
      parameters:
        - in: body
          name: body
          schema:
            type: object
            required:
              - track_id
            properties:
              track_id:
                type: integer
      responses:
        200:
          description: Created OrchardSoundRecording
          examples:
            application/json: {
              "orchard_sound_recording_id": "732a67e7-51b4-4191-9eac-502203643872"  # yamllint disable-line rule:line-length
            }
        201:
          description: Updated OrchardSoundRecording
          examples:
            application/json: {
              "orchard_sound_recording_id": "732a67e7-51b4-4191-9eac-502203643872"  # yamllint disable-line rule:line-length
            }
        404:
          description: Asset not found
          examples:
            application/json: {
              "code": "not_found_error",
              "message": "Asset not found, no OrchardSoundRecording result"
            }
        422:
          description: Malformed input data
          examples:
            application/json: {
              "code": "bad_request",
              "message": {
                "json": {
                  "asset": {
                    "filename": [
                      "Not a valid UUID."
                    ]
                  }
                }
              }
            }
    get:
      summary: Fetch sound recordings containing metadata and asset list
      parameters:
        - in: query
          name: ids
          description: Inclusive comma separated list OrchardSoundRecording.id (uuid)  # yamllint disable-line rule:line-length
          type: string
          required: false
        - in: query
          name: tuids
          description: Inclusive comma separated list of Track.id (int)
          type: string
          required: false
        - in: query
          name: product_ids
          description: Inclusive comma separated list of Product.id (int)
          type: string
          required: false
        - in: query
          name: project_ids
          description: Inclusive comma separated list of Project.id (int)
          type: string
          required: false
        - in: query
          name: track_isrcs
          description: Inclusive comma separated list of Track.isrc (str)
          type: string
          required: false
        - in: query
          name: upcs
          description: Inclusive comma separated list of Product.upc (str)
          type: string
          required: false
        - in: query
          name: asset_ids
          description: Inclusive comma separated list of OrchardAsset.id (str)
          type: string
          required: false
        - in: query
          name: include_deleted
          description: Filter regarding soft deleted Track assets
          type: boolean
          required: false
        - in: query
          name: include_transfer_to_content
          description: Include products in status "transfer_to_content"
          type: boolean
          required: false
        - in: query
          name: term
          description: Search osr by ISRC based on term
          type: boolean
          required: false
      responses:
        200:
          description: List of OrchardSoundRecording(s) with assets and ACRID
          examples:
            application/json: [
              {
                "id": "07eb4939-ac2c-409e-add5-f43957aef5b3",
                "isrc": "GBUM71105426",
                "assets": [
                  {
                    "asset_id": "0872c4f4-9837-42ba-bf7c-192aa3564f5a",
                    "acr_id": "acr666",
                    "filename": "057bf980-751d-43d3-9522-daaab47fa916",
                    "extension": "wav",
                    "tuid": 4928559,
                    "source": "ows-assets-db"
                  },
                  {
                    "asset_id": "98aca11d-0aa7-4999-849e-396c1f6abd25",
                    "acr_id": "acr666",
                    "filename": "ffdbacf3-ade6-44d2-8482-e22812fa4d69",
                    "extension": "flac",
                    "tuid": 4928559,
                    "source": "ows-assets-db"
                  }
                ]
              }
            ]
        422:
          description: Malformed input data
          examples:
            application/json: {
              "code": "bad_request",
              "message": {
                "query": [
                  "At least one field required from ids,tuids"
                ]
              }
            }
  /sound_recordings/{osr_id}/versions/{version_id}:
    get:
      summary: Fetch sound recording data based on version
      parameters:
        - in: path
          name: osr_id
          description: OrchardSoundRecording.id (uuid)
          type: string
          required: true
        - in: path
          name: version_id
          description: OrchardSoundRecording version ID (str) or 'latest' (str)
          type: string
          required: true
          enum: ['VERSION ID', 'latest']
      responses:
        200:
          description: OrchardSoundRecording data
          examples:
            application/json:
              {
                "version_id": "VERSION ID",
                "creation_date": "2022-01-01",
                "data": {
                  "track_connection": {
                    "tracks":[
                      {
                        "product":{
                          "id": "123",
                          "display_upc": "123",
                          "status": "in_content",
                          "release_date": "2022-01-01",
                          "label": {
                            "name": "LABEL NAME",
                            "type": "Vendor",
                            "uuid": "UUID",
                            "id": {
                              "vendor": 123,
                              "subaccount": 0
                            }
                          },
                          "subgenre": {
                            "id": 123,
                            "name": "Sound Effects",
                            "genre": {
                              "name": "Soundtracks",
                              "id": 123
                            }
                          }
                        },
                        "tuid": "123",
                        "isrc": "ISRC",
                        "name": "NAME",
                        "explicit": "N",
                        "version": "",
                        "duration_minutes": 0,
                        "duration_seconds": 8,
                        "meta_language_code": "ENG",
                        "language": {
                          "iso_639_1_code": "EN"
                        },
                        "p_info": "2022 Inc",
                        "participations": [
                          {
                            "participant": {
                              "uuid": "UUID",
                              "name": "PARTICIPANT NAME"
                            },
                            "participated_as": "performer"
                          }
                        ],
                        "territories": [],
                        "primary": true
                      }
                    ]
                  },
                  "assets": [],
                  "isrc": "ISRC"
                }
              }
        404:
          description: Not found
          examples:
            application/json: {
              "code": "not_found_error",
              "message": "Orchard Sound Recording not found"
            }
        422:
          description: Malformed input data
          examples:
            application/json: {
              "code": "bad_request",
              "message": {
                "view_args": {
                  "osr_id": [
                    "Not a valid UUID."
                  ]
                }
              }
            }
  /sound_recordings/{osr_id}:
    patch:
      summary: Patch sound recording data based on ID
      parameters:
        - in: path
          name: osr_id
          description: OrchardSoundRecording.id (uuid)
          type: string
          required: true
        - in: body
          name: body
          schema:
            type: object
            required:
              - primary_track_id
            properties:
              primary_track_id:
                type: integer
      responses:
        200:
          description: OrchardSoundRecording data
          examples:
            application/json:
              {
                "created_at": "2021-09-23T21:05:14.316000Z",
                "last_modified_at": "2021-10-14T20:03:46.871000Z",
                "created_by": "something",
                "primary_track_id": 123,
                "last_modified_by": "something",
                "isrc": "ISRC",
                "id": "732a67e7-51b4-4191-9eac-502203643872",
                "touched_by_backfill": "2022-03-01T16:31:04.796000Z"
              }
        404:
          description: Not found
          examples:
            application/json: {
              "error": "OrchardSoundRecordingNotFound/OrchardSoundRecordingWithoutAssets"  # yamllint disable-line rule:line-length
            }
        409:
          description: Not found
          examples:
            application/json: {
              "error": "PrimaryTrackNotRelated"
            }
        422:
          description: Malformed input data
          examples:
            application/json: {
              "code": "bad_request",
              "message": {
                "view_args": {
                  "osr_id": [
                    "Not a valid UUID."
                  ]
                }
              }
            }
  /sound_recordings/touch:
    patch:
      summary: Patch sound recording data based on vendor ID and datetime
      parameters:
        - in: body
          name: body
          schema:
            type: object
            required:
              - vendor_id
              - modified_before
            properties:
              vendor_id:
                type: integer
              modified_before:
                type: string
      responses:
        200:
          description: Number of updated OSR
          examples:
            application/json:
              {
                "nodes_updated": 10000
              }
        422:
          description: Malformed input data
          examples:
            application/json: {
              "code": "bad_request",
              "message": {
                "view_args": {
                  "vendor_id": [
                    "Not a valid integer."
                  ]
                }
              }
            }
  /{obj_type}/{obj_id}/rules:
    get:
      summary: Fetch fingerprint rules
      parameters:
        - in: path
          name: obj_type
          description: entity type for attached rules
          type: string
          required: true
          enum: [vendors, subaccounts, tracks]
        - in: path
          name: obj_id
          description: entity id for attached rules
          type: string
          required: true
      responses:
        200:
          description: List of Fingerprint Rule
          examples:
            application/json: [
              {
                "start": null,
                "end": null,
                "territory": "US",
                "policy": "carveout",
                "service": "meta",
                "created_at": "2023-05-05T09:30:32.031000+00:00",
                "created_by_profile_id": "123",
                "created_by_profile_type": "ContentProfile",
                "last_modified_at": "2023-05-05T09:30:32.031000+00:00",
                "last_modified_by_profile_id": "123",
                "last_modified_by_profile_type": "ContentProfile",
                "active": true
              }
            ]
        403:
          description: Unauthorized Request
          examples:
            application/json: {
              "message": {
                "error": "NotAuthorizedOperation"
              }
            }
    post:
      summary: Create fingerprint rules
      parameters:
        - in: path
          name: obj_type
          description: entity type for attached rules
          type: string
          required: true
          enum: [vendors, subaccounts, tracks]
        - in: path
          name: obj_id
          description: entity id for attached rules
          type: string
          required: true
        - in: body
          name: body
          schema:
            type: object
            required:
              - start
              - end
              - service
              - territory
              - policy
            properties:
              start:
                type: string
              end:
                type: string
              service:
                type: string
              territory:
                type: string
              policy:
                type: string
      responses:
        200:
          description: Object with number of create/update operation
          examples:
            application/json: {
              "num_created": 1,
              "num_deleted": 0
            }
        403:
          description: Unauthorized Request
          examples:
            application/json: {
              "message": {
                "error": "NotAuthorizedOperation"
              }
            }
  /{obj_type}/bulk/rules:
    post:
      summary: Create fingerprint rules in a bulk way, for several tracks at a time
      parameters:
        - in: path
          name: obj_type
          description: entity type for attached rules
          type: string
          required: true
          enum: [vendors, subaccounts, tracks]
        - in: body
          name: body
          schema:
            type: object
            required: true
            additionalProperties:
              type: array
              items:
                type: object
                required:
                  - start
                  - end
                  - service
                  - territory
                  - policy
                properties:
                  start:
                    type: string
                  end:
                    type: string
                  service:
                    type: string
                  territory:
                    type: string
                  policy:
                    type: string
      responses:
        200:
          description: Object with number of create/update operation
          examples:
            application/json: {
              "num_created": 1,
              "num_deleted": 0
            }
        403:
          description: Unauthorized Request
          examples:
            application/json: {
              "message": {
                "error": "NotAuthorizedOperation"
              }
            }
  /{obj_type}/{obj_id}/rules/history:
    get:
      summary: Fetch fingerprint rules history
      parameters:
        - in: path
          name: obj_type
          description: entity type for attached rules
          type: string
          required: true
          enum: [vendors, subaccounts, tracks]
        - in: path
          name: obj_id
          description: entity id for attached rules
          type: string
          required: true
        - in: query
          name: service
          description: service to filter on
          type: string
          required: false
        - in: query
          name: policy
          description: policy to filter on
          type: string
          required: false
      responses:
        200:
          description: List of Fingerprint Rule
          examples:
            application/json: {
              "history": [
                {
                  'territory': 'US',
                  'service': 'meta',
                  'policy': 'monetize',
                  'start': "2023-05-05T09:30:32.031000+00:00",
                  'end': "2024-05-05T09:30:32.031000+00:00",
                  'timestamp': "2019-05-05T09:30:32.031000+00:00",
                  'profile_type': 'ContentProfile',
                  'profile_id': '123',
                  'event_type': 'created'
                },
                {
                  'territory': 'US',
                  'service': 'meta',
                  'policy': 'monetize',
                  'start': "2023-05-05T09:30:32.031000+00:00",
                  'end': "2024-05-05T09:30:32.031000+00:00",
                  'timestamp': "2020-05-05T09:30:32.031000+00:00",
                  'profile_type': 'ContentProfile',
                  'profile_id': '123',
                  'event_type': 'deleted'
                }
              ]
            }
        403:
          description: Unauthorized Request
          examples:
            application/json: {
              "message": {
                "error": "NotAuthorizedOperation"
              }
            }
