<reponame>coreyjwhite/phetch
"""REST endpoint for local database info."""
# TODO add last formulary update

from sqlalchemy import desc, text

from ..base import BaseResource
from ... import db
from ...models.meditech import MeditechAdministration, MeditechTransaction


class LocalDatabaseResource(BaseResource):

    path = "/database/local"

    summary = "Local database information and operations"

    responses = {
        "get": {
            "tags": ["transactions"],
            "summary": "Local database information",
            "description": "",
            "operationId": "getLocalDb",
            "produces": ["application/json"],
            "responses": {
                200: {
                    "description": "OK",
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "administrations": {
                                        "type": "object",
                                        "properties": {
                                            "start": {
                                                "type": "string",
                                                "format": "date-time",
                                            },
                                            "end": {
                                                "type": "string",
                                                "format": "date-time",
                                            },
                                        },
                                    },
                                    "transactions": {
                                        "type": "object",
                                        "properties": {
                                            "start": {
                                                "type": "string",
                                                "format": "date-time",
                                            },
                                            "end": {
                                                "type": "string",
                                                "format": "date-time",
                                            },
                                        },
                                    },
                                },
                            },
                            "example": {
                                "administrations": {
                                    "start": "2019-07-01T00:02:00",
                                    "end": "2021-11-20T23:59:00",
                                },
                                "transactions": {
                                    "start": "2019-07-01T00:00:00",
                                    "end": "2021-11-20T23:56:00",
                                },
                            },
                        }
                    },
                }
            },
        }
    }

    def get(self):

        # Info queries
        earliest_admin = MeditechAdministration.query.order_by("date").first()
        earliest_tx = MeditechTransaction.query.order_by("date").first()
        latest_admin = MeditechAdministration.query.order_by(desc("date")).first()
        latest_tx = MeditechTransaction.query.order_by(desc("date")).first()

        # Response object
        return {
            "administrations": {
                "start": earliest_admin.date.isoformat(),
                "end": latest_admin.date.isoformat(),
            },
            "transactions": {
                "start": earliest_tx.date.isoformat(),
                "end": latest_tx.date.isoformat(),
            },
        }

"""REST endpoint for local database info."""
# TODO add last formulary update

from sqlalchemy import desc, text

from ..base import BaseResource
from ... import db
from ...models.meditech import MeditechAdministration, MeditechTransaction


class LocalDatabaseResource(BaseResource):

    path = "/database/local"

    summary = "Local database information and operations"

    responses = {
        "get": {
            "tags": ["transactions"],
            "summary": "Local database information",
            "description": "",
            "operationId": "getLocalDb",
            "produces": ["application/json"],
            "responses": {
                200: {
                    "description": "OK",
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "administrations": {
                                        "type": "object",
                                        "properties": {
                                            "start": {
                                                "type": "string",
                                                "format": "date-time",
                                            },
                                            "end": {
                                                "type": "string",
                                                "format": "date-time",
                                            },
                                        },
                                    },
                                    "transactions": {
                                        "type": "object",
                                        "properties": {
                                            "start": {
                                                "type": "string",
                                                "format": "date-time",
                                            },
                                            "end": {
                                                "type": "string",
                                                "format": "date-time",
                                            },
                                        },
                                    },
                                },
                            },
                            "example": {
                                "administrations": {
                                    "start": "2019-07-01T00:02:00",
                                    "end": "2021-11-20T23:59:00",
                                },
                                "transactions": {
                                    "start": "2019-07-01T00:00:00",
                                    "end": "2021-11-20T23:56:00",
                                },
                            },
                        }
                    },
                }
            },
        }
    }

    def get(self):

        # Info queries
        earliest_admin = MeditechAdministration.query.order_by("date").first()
        earliest_tx = MeditechTransaction.query.order_by("date").first()
        latest_admin = MeditechAdministration.query.order_by(desc("date")).first()
        latest_tx = MeditechTransaction.query.order_by(desc("date")).first()

        # Response object
        return {
            "administrations": {
                "start": earliest_admin.date.isoformat(),
                "end": latest_admin.date.isoformat(),
            },
            "transactions": {
                "start": earliest_tx.date.isoformat(),
                "end": latest_tx.date.isoformat(),
            },
        }
REST endpoint for local database info. # TODO add last formulary update # Info queries # Response object