|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +from bcsd_api.exception import NotFound |
| 4 | +from bcsd_api.id_gen import generate_id |
| 5 | +from bcsd_api.timezone import KST |
| 6 | + |
| 7 | +from .pg_repository import PgFormRepository, PgQuestionRepository |
| 8 | +from .schema import ( |
| 9 | + CreateFormRequest, |
| 10 | + FormResponse, |
| 11 | + QuestionRequest, |
| 12 | + QuestionResponse, |
| 13 | + UpdateFormRequest, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +def _now() -> str: |
| 18 | + return datetime.now(KST).isoformat() |
| 19 | + |
| 20 | + |
| 21 | +def create_form( |
| 22 | + form_repo: PgFormRepository, |
| 23 | + q_repo: PgQuestionRepository, |
| 24 | + req: CreateFormRequest, |
| 25 | + creator_id: str, |
| 26 | +) -> FormResponse: |
| 27 | + now = _now() |
| 28 | + form_id = generate_id("F") |
| 29 | + row = { |
| 30 | + "id": form_id, "title": req.title, |
| 31 | + "description": req.description or "", |
| 32 | + "recruitment_id": req.recruitment_id, |
| 33 | + "type": req.type, "is_active": "true", |
| 34 | + "created_by": creator_id, |
| 35 | + "created_at": now, "updated_at": now, |
| 36 | + } |
| 37 | + form_repo.create(row) |
| 38 | + questions = _save_questions(q_repo, form_id, req.questions) |
| 39 | + return FormResponse(**row, questions=questions) |
| 40 | + |
| 41 | + |
| 42 | +def update_form( |
| 43 | + form_repo: PgFormRepository, |
| 44 | + q_repo: PgQuestionRepository, |
| 45 | + form_id: str, |
| 46 | + req: UpdateFormRequest, |
| 47 | +) -> FormResponse: |
| 48 | + _get_or_raise(form_repo, form_id) |
| 49 | + updates = {} |
| 50 | + if req.title is not None: |
| 51 | + updates["title"] = req.title |
| 52 | + if req.description is not None: |
| 53 | + updates["description"] = req.description |
| 54 | + if req.is_active is not None: |
| 55 | + updates["is_active"] = req.is_active |
| 56 | + updates["updated_at"] = _now() |
| 57 | + form_repo.update_fields(form_id, updates) |
| 58 | + if req.questions is not None: |
| 59 | + q_repo.delete_by_form(form_id) |
| 60 | + _save_questions(q_repo, form_id, req.questions) |
| 61 | + return get_form(form_repo, q_repo, form_id) |
| 62 | + |
| 63 | + |
| 64 | +def get_form( |
| 65 | + form_repo: PgFormRepository, q_repo: PgQuestionRepository, form_id: str, |
| 66 | +) -> FormResponse: |
| 67 | + row = _get_or_raise(form_repo, form_id) |
| 68 | + questions = [QuestionResponse(**q) for q in q_repo.find_by_form(form_id)] |
| 69 | + return FormResponse(**row, questions=questions) |
| 70 | + |
| 71 | + |
| 72 | +def list_forms( |
| 73 | + form_repo: PgFormRepository, |
| 74 | + q_repo: PgQuestionRepository, |
| 75 | + recruitment_id: str | None = None, |
| 76 | +) -> list[FormResponse]: |
| 77 | + if recruitment_id: |
| 78 | + rows = form_repo.find_by_recruitment(recruitment_id) |
| 79 | + else: |
| 80 | + rows = form_repo.find_all() |
| 81 | + result = [] |
| 82 | + for row in rows: |
| 83 | + qs = [QuestionResponse(**q) for q in q_repo.find_by_form(row["id"])] |
| 84 | + result.append(FormResponse(**row, questions=qs)) |
| 85 | + return result |
| 86 | + |
| 87 | + |
| 88 | +def _get_or_raise(form_repo: PgFormRepository, form_id: str) -> dict: |
| 89 | + row = form_repo.find_by_id(form_id) |
| 90 | + if not row: |
| 91 | + raise NotFound(f"form {form_id} not found") |
| 92 | + return row |
| 93 | + |
| 94 | + |
| 95 | +def _save_questions( |
| 96 | + q_repo: PgQuestionRepository, form_id: str, questions: list[QuestionRequest], |
| 97 | +) -> list[QuestionResponse]: |
| 98 | + result = [] |
| 99 | + for i, q in enumerate(questions): |
| 100 | + row = { |
| 101 | + "id": generate_id("FQ"), "form_id": form_id, |
| 102 | + "label": q.label, "type": q.type, |
| 103 | + "options": q.options or "", |
| 104 | + "required": q.required, |
| 105 | + "sort_order": q.sort_order or i, |
| 106 | + "created_at": _now(), |
| 107 | + } |
| 108 | + q_repo.create(row) |
| 109 | + result.append(QuestionResponse(**row)) |
| 110 | + return result |
0 commit comments