11package org .inhahackers .optmo_user_be .function ;
22
33import com .microsoft .azure .functions .*;
4- import com .microsoft .azure .functions .annotation .AuthorizationLevel ;
5- import com .microsoft .azure .functions .annotation .FunctionName ;
6- import com .microsoft .azure .functions .annotation .HttpTrigger ;
7- import lombok .RequiredArgsConstructor ;
8- import org .inhahackers .optmo_user_be .dto .EmailRequest ;
94import org .inhahackers .optmo_user_be .dto .UserResponse ;
105import org .inhahackers .optmo_user_be .exception .JwtAuthenticationException ;
116import org .inhahackers .optmo_user_be .service .JwtTokenService ;
127import org .inhahackers .optmo_user_be .service .UserService ;
13- import org .springframework .context .ApplicationContext ;
14- import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
158import org .springframework .stereotype .Component ;
169
1710import java .util .Optional ;
11+ import java .util .function .Function ;
1812
19- @ Component
20- @ RequiredArgsConstructor
21- public class UserFunction {
13+ @ Component ("userFunction" )
14+ public class UserFunction implements Function <HttpRequestMessage <Optional <String >>, HttpResponseMessage > {
2215
2316 private final JwtTokenService jwtTokenService ;
2417 private final UserService userService ;
2518
26- @ FunctionName ("userFunction" )
27- public HttpResponseMessage run (
28- @ HttpTrigger (
29- name = "user" ,
30- methods = {HttpMethod .POST },
31- authLevel = AuthorizationLevel .ANONYMOUS )
32- HttpRequestMessage <Void > request ,
33- final ExecutionContext executionContext ) {
34-
35- executionContext .getLogger ().info ("Processing userFunction request" );
19+ public UserFunction (JwtTokenService jwtTokenService , UserService userService ) {
20+ this .jwtTokenService = jwtTokenService ;
21+ this .userService = userService ;
22+ }
3623
24+ @ Override
25+ public HttpResponseMessage apply (HttpRequestMessage <Optional <String >> request ) {
3726 try {
38- // 요청 바디 파싱
27+ // 쿼리 파라미터에서 email 추출
3928 String email = request .getQueryParameters ().get ("email" );
40- if (email == null || email .isEmpty ()) {
29+
30+ if (email == null || email .isBlank ()) {
4131 return request .createResponseBuilder (HttpStatus .BAD_REQUEST )
4232 .body ("Email parameter is required" )
4333 .build ();
4434 }
4535
46- // 유저 정보 조회 및 생성
36+ // 유저 조회 또는 생성
4737 var user = userService .findOrCreateUserByEmail (email );
4838
49- // JWT 토큰 생성 (필요하다면 갱신용)
39+ // JWT 발급
5040 String newToken = jwtTokenService .generateToken (
51- user .getId (),
52- user .getEmail (),
53- user .getRole ().name ()
41+ user .getId (), user .getEmail (), user .getRole ().name ()
5442 );
5543
5644 // 응답 DTO 생성
@@ -64,7 +52,7 @@ public HttpResponseMessage run(
6452 .totalLlmElecEstimate (user .getTotalLlmElecEstimate ())
6553 .build ();
6654
67- // 응답 헤더에 Authorization 토큰 포함
55+ // 응답 반환
6856 return request .createResponseBuilder (HttpStatus .OK )
6957 .header ("Content-Type" , "application/json" )
7058 .header ("Authorization" , "Bearer " + newToken )
@@ -76,13 +64,7 @@ public HttpResponseMessage run(
7664 .body ("Invalid JWT: " + e .getMessage ())
7765 .build ();
7866
79- } catch (IllegalArgumentException e ) {
80- return request .createResponseBuilder (HttpStatus .BAD_REQUEST )
81- .body (e .getMessage ())
82- .build ();
83-
8467 } catch (Exception e ) {
85- executionContext .getLogger ().severe ("ERROR: " + e .getMessage ());
8668 return request .createResponseBuilder (HttpStatus .INTERNAL_SERVER_ERROR )
8769 .body ("Internal error: " + e .getMessage ())
8870 .build ();
0 commit comments