11package main
22
33import (
4- "context"
54 "fmt"
65 "identity-service/layer/utils"
7- "log"
8- "net/http"
96 "time"
107
11- "cloud.google.com/go/firestore"
12-
138 "github.com/aws/aws-lambda-go/events"
14- "github.com/aws/aws-lambda-go/lambda"
159)
1610
17- type deps struct {
18- client * firestore.Client
19- ctx context.Context
20- }
21-
22- func (d * deps ) handler (request events.APIGatewayProxyRequest ) (events.APIGatewayProxyResponse , error ) {
11+ func handler (d * utils.Deps , request events.APIGatewayProxyRequest ) (events.APIGatewayProxyResponse , error ) {
2312 var userId , sessionId string = utils .GetDataFromBody ([]byte (request .Body ))
2413 if userId == "" {
2514 return events.APIGatewayProxyResponse {
@@ -28,52 +17,63 @@ func (d *deps) handler(request events.APIGatewayProxyRequest) (events.APIGateway
2817 }, nil
2918 }
3019
31- dsnap , err := d .client .Collection ("users" ).Doc (userId ).Get (d .ctx )
32-
33- var userUrl string
34- var chaincode string
35- var discordId string
20+ dsnap , err := d .Client .Collection ("users" ).Doc (userId ).Get (d .Ctx )
21+ if err != nil {
22+ return events.APIGatewayProxyResponse {
23+ Body : fmt .Sprintf ("Error retrieving user: %v" , err ),
24+ StatusCode : 500 ,
25+ }, nil
26+ }
3627
37- if str , ok := dsnap .Data ()["discordId" ].(string ); ok {
38- discordId = str
39- } else {
40- discordId = ""
28+ data := dsnap .Data ()
29+
30+ var user utils.User
31+ err = dsnap .DataTo (& user )
32+ if err != nil {
33+ utils .LogProfileSkipped (d .Client , d .Ctx , "UserData Type Error: " + fmt .Sprintln (err ), userId , sessionId )
34+ return events.APIGatewayProxyResponse {
35+ Body : "Profile Skipped No User Data" ,
36+ StatusCode : 200 ,
37+ }, nil
4138 }
4239
43- if str , ok := dsnap . Data ()[ "profileURL" ].( string ); ok {
44- userUrl = str
45- } else {
46- utils .LogProfileSkipped (d .client , d .ctx , userId , "Profile URL not available" , sessionId )
47- utils .SetProfileStatusBlocked (d .client , d .ctx , userId , "Profile URL not available" , sessionId , discordId )
40+ discordId := user . DiscordID
41+
42+ if user . ProfileURL == "" {
43+ utils .LogProfileSkipped (d .Client , d .Ctx , "Profile URL not available" , userId , sessionId )
44+ utils .SetProfileStatusBlocked (d .Client , d .Ctx , userId , "Profile URL not available" , sessionId , discordId )
4845 return events.APIGatewayProxyResponse {
4946 Body : "Profile Skipped No Profile URL" ,
5047 StatusCode : 200 ,
5148 }, nil
5249 }
5350
54- if str , ok := dsnap .Data ()["chaincode" ].(string ); ok {
55- if str == "" {
56- utils .LogProfileSkipped (d .client , d .ctx , userId , "Profile Service Blocked or Chaincode is empty" , sessionId )
57- utils .SetProfileStatusBlocked (d .client , d .ctx , userId , "Profile Service Blocked or Chaincode is empty" , sessionId , discordId )
58- return events.APIGatewayProxyResponse {
59- Body : "Profile Skipped Profile Service Blocked" ,
60- StatusCode : 200 ,
61- }, nil
62- }
63- chaincode = str
64- } else {
65- utils .LogProfileSkipped (d .client , d .ctx , userId , "Chaincode Not Found" , sessionId )
66- utils .SetProfileStatusBlocked (d .client , d .ctx , userId , "Chaincode Not Found" , sessionId , discordId )
51+ _ , chaincodeExists := data ["chaincode" ]
52+ if ! chaincodeExists {
53+ utils .LogProfileSkipped (d .Client , d .Ctx , "Chaincode Not Found" , userId , sessionId )
54+ utils .SetProfileStatusBlocked (d .Client , d .Ctx , userId , "Chaincode Not Found" , sessionId , discordId )
6755 return events.APIGatewayProxyResponse {
6856 Body : "Profile Skipped Chaincode Not Found" ,
6957 StatusCode : 200 ,
7058 }, nil
7159 }
7260
61+ if user .Chaincode == "" {
62+ utils .LogProfileSkipped (d .Client , d .Ctx , "Profile Service Blocked or Chaincode is empty" , userId , sessionId )
63+ utils .SetProfileStatusBlocked (d .Client , d .Ctx , userId , "Profile Service Blocked or Chaincode is empty" , sessionId , discordId )
64+ return events.APIGatewayProxyResponse {
65+ Body : "Profile Skipped Profile Service Blocked" ,
66+ StatusCode : 200 ,
67+ }, nil
68+ }
69+
70+ userUrl := user .ProfileURL
71+ chaincode := user .Chaincode
72+
7373 var userData utils.Diff
7474 err = dsnap .DataTo (& userData )
7575 if err != nil {
76- utils .LogProfileSkipped (d .client , d .ctx , userId , "UserData Type Error: " + fmt .Sprintln (err ), sessionId )
76+ utils .LogProfileSkipped (d .Client , d .Ctx , "UserData Type Error: " + fmt .Sprintln (err ), userId , sessionId )
7777 return events.APIGatewayProxyResponse {
7878 Body : "Profile Skipped No User Data" ,
7979 StatusCode : 200 ,
@@ -83,33 +83,31 @@ func (d *deps) handler(request events.APIGatewayProxyRequest) (events.APIGateway
8383 if userUrl [len (userUrl )- 1 ] != '/' {
8484 userUrl = userUrl + "/"
8585 }
86+
87+ _ , serviceErr := utils .GetWithContext (d .Ctx , userUrl + "health" , 5 * time .Second )
8688 var isServiceRunning bool
87- c := & http.Client {
88- Timeout : 5 * time .Second ,
89- }
90- _ , serviceErr := c .Get (userUrl + "health" )
9189 if serviceErr != nil {
9290 isServiceRunning = false
9391 } else {
9492 isServiceRunning = true
9593 }
9694
97- utils .LogHealth (d .client , d .ctx , userId , isServiceRunning , sessionId )
95+ utils .LogHealth (d .Client , d .Ctx , userId , isServiceRunning , sessionId )
9896 if ! isServiceRunning {
99- utils .LogProfileSkipped (d .client , d .ctx , userId , "Profile Service Down" , sessionId )
100- utils .SetProfileStatusBlocked (d .client , d .ctx , userId , "Profile Service Down" , sessionId , discordId )
97+ utils .LogProfileSkipped (d .Client , d .Ctx , "Profile Service Down" , userId , sessionId )
98+ utils .SetProfileStatusBlocked (d .Client , d .Ctx , userId , "Profile Service Down" , sessionId , discordId )
10199 return events.APIGatewayProxyResponse {
102100 Body : "Profile Skipped Service Down" ,
103101 StatusCode : 200 ,
104102 }, nil
105103 }
106104
107- dataErr : = utils .Getdata (d .client , d .ctx , userId , userUrl , chaincode , utils .DiffToRes (userData ), sessionId , discordId )
108- if dataErr != "" {
109- return events. APIGatewayProxyResponse {
110- Body : "Profile Skipped " + dataErr ,
111- StatusCode : 200 ,
112- } , nil
105+ err = utils .Getdata (d .Client , d .Ctx , userId , userUrl , chaincode , utils .DiffToRes (userData ), sessionId , discordId )
106+ if err != nil {
107+ if profileErr , ok := err .( * utils. ProfileError ); ok {
108+ return utils . HandleProfileSkippedError ( profileErr . Message ), nil
109+ }
110+ return utils . HandleProfileSkippedError ( err . Error ()) , nil
113111 }
114112
115113 return events.APIGatewayProxyResponse {
@@ -119,16 +117,5 @@ func (d *deps) handler(request events.APIGatewayProxyRequest) (events.APIGateway
119117}
120118
121119func main () {
122- ctx := context .Background ()
123- client , err := utils .InitializeFirestoreClient (ctx )
124- if err != nil {
125- log .Fatalf ("Failed to initialize Firestore client: %v" , err )
126- }
127-
128- d := deps {
129- client : client ,
130- ctx : ctx ,
131- }
132-
133- lambda .Start (d .handler )
120+ utils .InitializeLambdaWithFirestore ("call-profile" , handler )
134121}
0 commit comments