|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +from bcsd_api.exception import BadRequest, Conflict, Forbidden, NotFound |
| 4 | +from bcsd_api.form.pg_repository import PgFormRepository, PgQuestionRepository |
| 5 | +from bcsd_api.id_gen import generate_id |
| 6 | +from bcsd_api.member.pg_repository import PgMemberRepository |
| 7 | +from bcsd_api.timezone import KST |
| 8 | + |
| 9 | +from .pg_repository import PgAnswerRepository, PgApplicationRepository |
| 10 | +from .schema import AnswerRequest, AnswerResponse, ApplicationResponse |
| 11 | + |
| 12 | + |
| 13 | +def _now() -> str: |
| 14 | + return datetime.now(KST).isoformat() |
| 15 | + |
| 16 | + |
| 17 | +def submit( |
| 18 | + app_repo: PgApplicationRepository, |
| 19 | + ans_repo: PgAnswerRepository, |
| 20 | + form_repo: PgFormRepository, |
| 21 | + q_repo: PgQuestionRepository, |
| 22 | + req_form_id: str, |
| 23 | + answers: list[AnswerRequest], |
| 24 | + member_id: str, |
| 25 | +) -> ApplicationResponse: |
| 26 | + form = form_repo.find_by_id(req_form_id) |
| 27 | + if not form: |
| 28 | + raise NotFound(f"form {req_form_id} not found") |
| 29 | + _check_duplicate(app_repo, req_form_id, member_id) |
| 30 | + _validate_required(q_repo, req_form_id, answers) |
| 31 | + status = "심사_대기" if form["type"] == "conversion" else "납부_대기" |
| 32 | + return _create_app(app_repo, ans_repo, req_form_id, member_id, status, answers) |
| 33 | + |
| 34 | + |
| 35 | +def _check_duplicate(repo: PgApplicationRepository, form_id: str, member_id: str) -> None: |
| 36 | + if repo.find_by_form_member(form_id, member_id): |
| 37 | + raise Conflict("already applied to this form") |
| 38 | + |
| 39 | + |
| 40 | +def _validate_required( |
| 41 | + q_repo: PgQuestionRepository, form_id: str, answers: list[AnswerRequest], |
| 42 | +) -> None: |
| 43 | + questions = q_repo.find_by_form(form_id) |
| 44 | + required_ids = {q["id"] for q in questions if q["required"] == "true"} |
| 45 | + answered_ids = {a.question_id for a in answers} |
| 46 | + missing = required_ids - answered_ids |
| 47 | + if missing: |
| 48 | + raise BadRequest(f"missing required answers: {missing}") |
| 49 | + |
| 50 | + |
| 51 | +def _create_app( |
| 52 | + app_repo, ans_repo, form_id, member_id, status, answers, |
| 53 | +) -> ApplicationResponse: |
| 54 | + now = _now() |
| 55 | + app_id = generate_id("AP") |
| 56 | + row = { |
| 57 | + "id": app_id, "form_id": form_id, |
| 58 | + "member_id": member_id, "status": status, |
| 59 | + "submitted_at": now, "updated_at": now, |
| 60 | + } |
| 61 | + app_repo.create(row) |
| 62 | + saved = _save_answers(ans_repo, app_id, answers) |
| 63 | + return ApplicationResponse(**row, answers=saved) |
| 64 | + |
| 65 | + |
| 66 | +def _save_answers( |
| 67 | + ans_repo: PgAnswerRepository, app_id: str, answers: list[AnswerRequest], |
| 68 | +) -> list[AnswerResponse]: |
| 69 | + result = [] |
| 70 | + for a in answers: |
| 71 | + row = { |
| 72 | + "id": generate_id("AA"), |
| 73 | + "application_id": app_id, |
| 74 | + "question_id": a.question_id, |
| 75 | + "value": a.value, |
| 76 | + } |
| 77 | + ans_repo.create(row) |
| 78 | + result.append(AnswerResponse(**row)) |
| 79 | + return result |
| 80 | + |
| 81 | + |
| 82 | +def list_applications( |
| 83 | + app_repo: PgApplicationRepository, ans_repo: PgAnswerRepository, form_id: str, |
| 84 | +) -> list[ApplicationResponse]: |
| 85 | + rows = app_repo.find_by_form(form_id) |
| 86 | + return [_with_answers(ans_repo, r) for r in rows] |
| 87 | + |
| 88 | + |
| 89 | +def my_applications( |
| 90 | + app_repo: PgApplicationRepository, ans_repo: PgAnswerRepository, member_id: str, |
| 91 | +) -> list[ApplicationResponse]: |
| 92 | + rows = app_repo.find_by_member(member_id) |
| 93 | + return [_with_answers(ans_repo, r) for r in rows] |
| 94 | + |
| 95 | + |
| 96 | +def get_application( |
| 97 | + app_repo: PgApplicationRepository, ans_repo: PgAnswerRepository, app_id: str, |
| 98 | +) -> ApplicationResponse: |
| 99 | + row = app_repo.find_by_id(app_id) |
| 100 | + if not row: |
| 101 | + raise NotFound(f"application {app_id} not found") |
| 102 | + return _with_answers(ans_repo, row) |
| 103 | + |
| 104 | + |
| 105 | +def _with_answers(ans_repo: PgAnswerRepository, row: dict) -> ApplicationResponse: |
| 106 | + answers = [AnswerResponse(**a) for a in ans_repo.find_by_application(row["id"])] |
| 107 | + return ApplicationResponse(**row, answers=answers) |
| 108 | + |
| 109 | + |
| 110 | +def confirm_payment( |
| 111 | + app_repo: PgApplicationRepository, app_id: str, admin_id: str, |
| 112 | +) -> ApplicationResponse: |
| 113 | + row = app_repo.find_by_id(app_id) |
| 114 | + if not row: |
| 115 | + raise NotFound(f"application {app_id} not found") |
| 116 | + if row["status"] != "납부_대기": |
| 117 | + raise BadRequest("application is not in payment pending status") |
| 118 | + app_repo.update_fields(app_id, {"status": "납부_완료", "updated_at": _now()}) |
| 119 | + return ApplicationResponse(**app_repo.find_by_id(app_id)) |
| 120 | + |
| 121 | + |
| 122 | +def approve( |
| 123 | + app_repo: PgApplicationRepository, |
| 124 | + member_repo: PgMemberRepository, |
| 125 | + form_repo: PgFormRepository, |
| 126 | + app_ids: list[str], |
| 127 | + admin_id: str, |
| 128 | +) -> list[ApplicationResponse]: |
| 129 | + now = _now() |
| 130 | + result = [] |
| 131 | + for app_id in app_ids: |
| 132 | + row = app_repo.find_by_id(app_id) |
| 133 | + if not row: |
| 134 | + continue |
| 135 | + form = form_repo.find_by_id(row["form_id"]) |
| 136 | + new_status = "Regular" if form and form["type"] == "conversion" else "Beginner" |
| 137 | + app_repo.update_fields(app_id, { |
| 138 | + "status": "승인", "approved_at": now, |
| 139 | + "approved_by": admin_id, "updated_at": now, |
| 140 | + }) |
| 141 | + member_repo.update_status(row["member_id"], new_status) |
| 142 | + result.append(ApplicationResponse(**app_repo.find_by_id(app_id))) |
| 143 | + return result |
| 144 | + |
| 145 | + |
| 146 | +def cancel( |
| 147 | + app_repo: PgApplicationRepository, app_id: str, member_id: str, |
| 148 | +) -> None: |
| 149 | + row = app_repo.find_by_id(app_id) |
| 150 | + if not row: |
| 151 | + raise NotFound(f"application {app_id} not found") |
| 152 | + if row["member_id"] != member_id: |
| 153 | + raise Forbidden("cannot cancel another member's application") |
| 154 | + if row["status"] not in ("납부_대기", "심사_대기"): |
| 155 | + raise BadRequest("cannot cancel after payment or approval") |
| 156 | + app_repo.update_fields(app_id, {"status": "취소", "updated_at": _now()}) |
0 commit comments