Source code for luna_backport3.app.handlers.persons_handler

import asyncio

from sanic.response import HTTPResponse

from app.handlers.base_handler import BaseHandler
from app.handlers.schemas import CREATE_PERSON_SCHEMA
from db.db_context import PersonFilters


[docs]class PersonsHandler(BaseHandler): """ Persons handler """
[docs] async def post(self) -> HTTPResponse: """ Person creation. See `spec_create_person`_. .. _spec_create_person: _static/api.html#operation/createPerson Returns: json with person id """ reqJson = self.request.json self.validateJson(reqJson, CREATE_PERSON_SCHEMA, useJsonSchema=False) personId = await self.dbContext.createPerson( accountId=self.accountId, userData=reqJson.get("user_data"), externalId=reqJson.get("external_id") ) return self.success(201, outputJson={"person_id": personId})
[docs] async def get(self) -> HTTPResponse: """ Get persons. See `spec_get_persons`_. .. _spec_get_persons: _static/api.html#operation/getPersons Returns: dict with person count and list with persons """ userData = self.getQueryParam("user_data", require=False) externalId = self.getQueryParam("external_id", require=False) page, pageSize = self.getPagination(defaultPage=1, defaultPageSize=10, maxPageSize=100) # noinspection PyArgumentList personFilters = PersonFilters(accountId=self.accountId, userData=userData, externalId=externalId) persons, count = await asyncio.gather( self.dbContext.getPersons(personFilters=personFilters, page=page, pageSize=pageSize), self.dbContext.getPersonsCount(personFilters=personFilters), ) return self.success(200, outputJson={"count": count, "persons": persons})