88from bcsd_api .graphql .convert import to_filter
99
1010from . import service
11- from .schema import AnswerRequest , ApplicationResponse
11+ from .schema import AnswerRequest
1212from .types import (
13- AnswerType ,
1413 ApplicationFilterInput ,
15- ApplicationType ,
14+ ApplicationListItem ,
15+ BatchResult ,
16+ MyApplication ,
17+ ApplicationAnswer ,
1618 PagedApplications ,
19+ PaymentInfo ,
1720 SubmitInput ,
1821)
1922
2023
21- def _to_app_type (app : ApplicationResponse ) -> ApplicationType :
22- data = app .model_dump ()
23- data ["answers" ] = [AnswerType (** a ) for a in data ["answers" ]]
24- return ApplicationType (** data )
24+ def _to_my_app (app , payment_info = None ) -> MyApplication :
25+ answers = [ApplicationAnswer (question_id = a .question_id , value = a .value ) for a in app .answers ]
26+ pi = None
27+ if payment_info :
28+ pi = PaymentInfo (
29+ bank = payment_info .bank , account = payment_info .account ,
30+ amount = payment_info .amount , holder = payment_info .holder ,
31+ )
32+ return MyApplication (
33+ id = app .id , status = app .status ,
34+ form_template_id = app .form_template_id ,
35+ track = app .track , submitted_at = app .submitted_at ,
36+ answers = answers , payment_info = pi ,
37+ )
2538
2639
27- def resolve_submit (info : Info [GqlContext , None ], input : SubmitInput ) -> ApplicationType :
40+ def resolve_my_application (info : Info [GqlContext , None ]) -> MyApplication | None :
41+ user = require_user (info .context )
42+ ctx = info .context
43+ app = service .my_application (ctx .app_repo , ctx .ans_repo , user ["sub" ])
44+ if not app :
45+ return None
46+ pi = service .get_payment_info (ctx .setting_repo )
47+ return _to_my_app (app , pi )
48+
49+
50+ def resolve_submit (info : Info [GqlContext , None ], input : SubmitInput ) -> MyApplication :
2851 user = require_user (info .context )
2952 ctx = info .context
3053 answers = [AnswerRequest (question_id = a .question_id , value = a .value ) for a in input .answers ]
3154 app = service .submit (
3255 ctx .app_repo , ctx .ans_repo ,
3356 ctx .form_repo , ctx .question_repo ,
34- input .form_id , answers , user ["sub" ],
57+ input .form_template_id , answers , input . track , user ["sub" ],
3558 )
36- return _to_app_type (app )
59+ pi = service .get_payment_info (ctx .setting_repo )
60+ return _to_my_app (app , pi )
61+
62+
63+ def resolve_cancel (info : Info [GqlContext , None ], id : strawberry .ID ) -> MyApplication :
64+ user = require_user (info .context )
65+ ctx = info .context
66+ app = service .cancel (ctx .app_repo , ctx .ans_repo , id , user ["sub" ])
67+ return _to_my_app (app )
3768
3869
3970def resolve_applications (
@@ -44,56 +75,38 @@ def resolve_applications(
4475 require_fee_edit (info .context .authz , user ["sub" ])
4576 ctx = info .context
4677 filt = to_filter (filter , ApplicationFilter ) if filter else ApplicationFilter .model_validate ({})
47- rows = ctx .app_repo . find_all ( )
48- apps = [service . _with_answers ( ctx . ans_repo , r ) for r in rows ]
49- paged = apply_filter ([ a . model_dump () for a in apps ] , filt )
50- items = [_to_app_type ( ApplicationResponse ( ** r ) ) for r in paged .items ]
78+ items = service . list_applications ( ctx .app_repo , ctx . member_repo )
79+ rows = [i . model_dump ( ) for i in items ]
80+ paged = apply_filter (rows , filt )
81+ list_items = [ApplicationListItem ( ** r ) for r in paged .items ]
5182 return PagedApplications (
52- items = items , total = paged .total ,
83+ items = list_items , total = paged .total ,
5384 page = paged .page , size = paged .size ,
5485 )
5586
5687
57- def resolve_application (
88+ def resolve_approve (
5889 info : Info [GqlContext , None ], id : strawberry .ID ,
59- ) -> ApplicationType :
90+ ) -> MyApplication :
6091 user = require_user (info .context )
6192 require_fee_edit (info .context .authz , user ["sub" ])
6293 ctx = info .context
94+ service .approve (
95+ ctx .app_repo , ctx .member_repo , ctx .form_repo ,
96+ [id ], user ["sub" ], ctx .authz ,
97+ )
6398 app = service .get_application (ctx .app_repo , ctx .ans_repo , id )
64- return _to_app_type (app )
99+ return _to_my_app (app )
65100
66101
67- def resolve_my_applications (info : Info [GqlContext , None ]) -> list [ApplicationType ]:
68- user = require_user (info .context )
69- ctx = info .context
70- apps = service .my_applications (ctx .app_repo , ctx .ans_repo , user ["sub" ])
71- return [_to_app_type (a ) for a in apps ]
72-
73-
74- def resolve_confirm_payment (
75- info : Info [GqlContext , None ], id : strawberry .ID ,
76- ) -> ApplicationType :
77- user = require_user (info .context )
78- require_fee_edit (info .context .authz , user ["sub" ])
79- app = service .confirm_payment (info .context .app_repo , id , user ["sub" ])
80- return _to_app_type (app )
81-
82-
83- def resolve_approve (
102+ def resolve_batch_approve (
84103 info : Info [GqlContext , None ], ids : list [strawberry .ID ],
85- ) -> list [ ApplicationType ] :
104+ ) -> BatchResult :
86105 user = require_user (info .context )
87106 require_fee_edit (info .context .authz , user ["sub" ])
88107 ctx = info .context
89- apps = service .approve (
108+ approved = service .approve (
90109 ctx .app_repo , ctx .member_repo , ctx .form_repo ,
91110 ids , user ["sub" ], ctx .authz ,
92111 )
93- return [_to_app_type (a ) for a in apps ]
94-
95-
96- def resolve_cancel (info : Info [GqlContext , None ], id : strawberry .ID ) -> bool :
97- user = require_user (info .context )
98- service .cancel (info .context .app_repo , id , user ["sub" ])
99- return True
112+ return BatchResult (count = len (approved ), ids = approved )
0 commit comments