2022-12-09 12:09:22 +03:00
|
|
|
from typing import List
|
|
|
|
|
2022-12-09 02:33:22 +03:00
|
|
|
from fastapi import HTTPException
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
from med_backend.forms.crud import (
|
|
|
|
create_form_assigment,
|
2022-12-09 12:09:22 +03:00
|
|
|
create_submission,
|
|
|
|
create_submission_answer,
|
2022-12-09 02:33:22 +03:00
|
|
|
create_user_form_rev_question,
|
|
|
|
get_form,
|
|
|
|
get_questions,
|
2022-12-09 12:09:22 +03:00
|
|
|
get_submissions,
|
2022-12-09 02:33:22 +03:00
|
|
|
)
|
2022-12-09 12:09:22 +03:00
|
|
|
from med_backend.forms.schemas import Form, FormAnswer, FormAssigment, Question
|
2022-12-09 02:33:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
async def get_full_form(session: AsyncSession, form_id: int) -> Form:
|
|
|
|
form = await get_form(session, form_id)
|
|
|
|
if not form:
|
|
|
|
raise HTTPException(status_code=404, detail="Form doesn't exist")
|
|
|
|
questions = await get_questions(session, form_id)
|
|
|
|
|
|
|
|
return Form(
|
|
|
|
id=form_id,
|
|
|
|
name=form.name,
|
|
|
|
questions=[
|
|
|
|
Question(id=q.id, type=q.type, question=q.question) for q in questions
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def assign_form(session: AsyncSession, data: FormAssigment, form_id: int):
|
|
|
|
form = await get_form(session, form_id)
|
|
|
|
if not form:
|
|
|
|
raise HTTPException(status_code=404, detail="Form doesn't exist")
|
|
|
|
await create_form_assigment(session, form_id, data.user_id)
|
|
|
|
for field in data.question_refs:
|
|
|
|
await create_user_form_rev_question(
|
|
|
|
session,
|
|
|
|
field.id,
|
|
|
|
data.user_id,
|
|
|
|
field.ref_min,
|
|
|
|
field.ref_max,
|
|
|
|
)
|
2022-12-09 12:09:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
async def submit_form(
|
|
|
|
session: AsyncSession,
|
|
|
|
data: List[FormAnswer],
|
|
|
|
form_id: int,
|
|
|
|
user_id: int,
|
|
|
|
):
|
|
|
|
subm = await create_submission(session, form_id, user_id)
|
|
|
|
for answer in data:
|
|
|
|
await create_submission_answer(session, subm.id, answer.field_id, answer.answer)
|
|
|
|
|
|
|
|
|
|
|
|
async def get_form_submissions(session: AsyncSession, form_id: int):
|
|
|
|
form = await get_form(session, form_id)
|
|
|
|
if not form:
|
|
|
|
raise HTTPException(status_code=404, detail="Form doesn't exist")
|
|
|
|
submissions = await get_submissions(session, form_id)
|
|
|
|
return submissions
|