openapi: "3.0.0"
info:
  version: v0.1.0
  title: MasDevices API
  description: This API features a set of MasStack endpoints related to MasDevices domain.
  contact:
    name: MasDevices

servers:
  - url: https://devices.masstack.com/v1/
    description: production
  - url: https://devices.sta.masstack.com/v1/
    description: staging

security:
  - bearerAuth: [ ]

tags:
  - name: DeviceHistory
    description: API for retrieving device history information
  - name: DeviceSimHistory
    description: API for retrieving device SIM history information

paths:
  "/orgs/{orgId}/deviceHistory/{msisdn}":
    parameters:
      - $ref: '#/components/parameters/OrgIdParam'
      - $ref: '#/components/parameters/MsisdnParam'
      - $ref: '#/components/parameters/DatetimeFromParam'
      - $ref: '#/components/parameters/DatetimeToParam'
      - $ref: '#/components/parameters/DateReferenceParam'
      - $ref: '#/components/parameters/SortParam'
      - $ref: '#/components/parameters/OnlyFirstDeviceChangeParam'
    get:
      tags:
        - DeviceHistory
      operationId: retrieveDeviceHistory
      description: >
        Show device history. If a period is not specified, this service will offer device history details for the last 90 days.
      responses:
        '200':
          description: Device history retrieved successfully. If the device history is empty for the specified/default period, an empty list will be returned.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/DeviceHistoryResponseDTO"
        '500':
          description: The request failed to be processed by the server
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GenericResponseDTO'

  "/orgs/{orgId}/deviceSimHistory/{msisdn}":
    parameters:
      - $ref: '#/components/parameters/OrgIdParam'
      - $ref: '#/components/parameters/MsisdnParam'
      - $ref: '#/components/parameters/DatetimeFromParam'
      - $ref: '#/components/parameters/DatetimeToParam'
    get:
      tags:
        - DeviceSimHistory
      operationId: retrieveDeviceSimHistory
      description: >
        Show device SIM history. If a period is not specified, this service will offer device sim history details for the last 90 days.
      responses:
        '200':
          description: Device sim history retrieved successfully. If the device SIM history is empty for the specified/default period, an empty list will be returned.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/DeviceSimHistoryResponseDTO"
        '500':
          description: The request failed to be processed by the server
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GenericResponseDTO'

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      
  parameters:
    MsisdnParam:
      name: msisdn
      in: path
      required: true
      schema:
        type: string
      description: The MSISDN of the device history we want to retrieve.
    OrgIdParam:
      in: path
      name: orgId
      description: Executes the request in the context of the given organization
      required: true
      example: "masmovil"
      schema:
        type: string
    DatetimeFromParam:
      in: query
      name: fromDate
      description: From datetime used as filter
      required: false
      example: "2021-10-10T22:00:00Z"
      schema:
        type: string
        format: date-time
    DatetimeToParam:
      in: query
      name: toDate
      description: To datetime used as filter
      required: false
      example: "2021-10-20T22:00:00Z"
      schema:
        type: string
        format: date-time
    DateReferenceParam:
      in: query
      name: dateReference
      description: The date reference field to take into account when looking for the defined (or default) period.
      required: false
      schema:
        type: string
        enum: [ first_seen, last_seen ]
        default: last_seen
    SortParam:
      in: query
      name: sort
      description: The order in which to return the results, based on the specified time reference.
      required: false
      schema:
        type: string
        enum: [ ASC, DESC ]
        default: DESC
    OnlyFirstDeviceChangeParam:
      in: query
      name: onlyFirstDeviceChange
      description: If true, only the first device change in the specified (or default) period will be returned.
      required: false
      schema:
        type: boolean
        default: false

  schemas:
    GenericResponseDTO:
      type: object
      properties:
        error:
          type: string
        message:
          type: string
        trace_id:
          type: string

    DeviceHistoryResponseDTO:
      type: object
      required:
        - msisdn
        - deviceHistory
      properties:
        msisdn:
          type: string
          description: The MSISDN of the device.
          example: "612345678"
        deviceHistory:
          type: array
          items:
            $ref: "#/components/schemas/DeviceHistoryDTO"
          description: List of device history entries.

    DeviceSimHistoryResponseDTO:
      type: object
      required:
        - msisdn
        - deviceSimHistory
      properties:
        msisdn:
          type: string
          description: The MSISDN of the device.
          example: "612345678"
        deviceSimHistory:
          type: array
          items:
            $ref: "#/components/schemas/DeviceSimHistoryDTO"
          description: List of device SIM history entries.

    DeviceHistoryDTO:
      type: object
      required:
        - imei
        - tenant
        - first_seen
        - last_seen
        - is_device_change
      properties:
        imei:
          type: string
          description: The IMEI of the device.
          example: "123456789012345"
        tenant:
          type: string
          description: The tenant of the device.
          example: "test-org"
        is_device_change:
          type: boolean
          description: Indicates that this device is a change from the previous one.
          example: true
        first_seen:
          type: string
          format: date-time
          description: First datetime when the device was detected in network events for this particular period.
          example: "2024-09-09T00:00:00Z"
        last_seen:
          type: string
          format: date-time
          description: Last datetime when the device was detected in network events for this particular period.
          example: "2024-09-09T00:00:00Z"
    
    DeviceSimHistoryDTO:
      type: object
      required:
        - imsi
        - tenant
        - first_seen
        - last_seen
      properties:
        imsi:
          type: string
          description: The IMSI of the device.
          example: "123456789012345"
        tenant:
          type: string
          description: The tenant of the device.
          example: "test-org"
        first_seen:
          type: string
          format: date-time
          description: First datetime when the sim was detected in network events for this particular period.
          example: "2024-09-09T00:00:00Z"
        last_seen:
          type: string
          format: date-time
          description: Last datetime when the sim was detected in network events for this particular period.
          example: "2024-09-09T00:00:00Z"