@@ -4,20 +4,22 @@ import (
44 "context"
55 "fmt"
66 "identity-service/layer/utils"
7+ "log"
78 "net/http"
89 "time"
910
11+ "cloud.google.com/go/firestore"
12+
1013 "github.com/aws/aws-lambda-go/events"
1114 "github.com/aws/aws-lambda-go/lambda"
1215)
1316
14- func handler (request events.APIGatewayProxyRequest ) (events.APIGatewayProxyResponse , error ) {
15- ctx := context .Background ()
16- client , err := utils .InitializeFirestoreClient (ctx )
17- if err != nil {
18- return events.APIGatewayProxyResponse {}, err
19- }
17+ type deps struct {
18+ client * firestore.Client
19+ ctx context.Context
20+ }
2021
22+ func (d * deps ) handler (request events.APIGatewayProxyRequest ) (events.APIGatewayProxyResponse , error ) {
2123 var userId , sessionId string = utils .GetDataFromBody ([]byte (request .Body ))
2224 if userId == "" {
2325 return events.APIGatewayProxyResponse {
@@ -26,7 +28,7 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
2628 }, nil
2729 }
2830
29- dsnap , err := client .Collection ("users" ).Doc (userId ).Get (ctx )
31+ dsnap , err := d . client .Collection ("users" ).Doc (userId ).Get (d . ctx )
3032
3133 var userUrl string
3234 var chaincode string
@@ -41,8 +43,8 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
4143 if str , ok := dsnap .Data ()["profileURL" ].(string ); ok {
4244 userUrl = str
4345 } else {
44- utils .LogProfileSkipped (client , ctx , userId , "Profile URL not available" , sessionId )
45- utils .SetProfileStatusBlocked (client , ctx , userId , "Profile URL not available" , sessionId , discordId )
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 )
4648 return events.APIGatewayProxyResponse {
4749 Body : "Profile Skipped No Profile URL" ,
4850 StatusCode : 200 ,
@@ -51,17 +53,17 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
5153
5254 if str , ok := dsnap .Data ()["chaincode" ].(string ); ok {
5355 if str == "" {
54- utils .LogProfileSkipped (client , ctx , userId , "Profile Service Blocked or Chaincode is empty" , sessionId )
55- utils .SetProfileStatusBlocked (client , ctx , userId , "Profile Service Blocked or Chaincode is empty" , sessionId , discordId )
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 )
5658 return events.APIGatewayProxyResponse {
5759 Body : "Profile Skipped Profile Service Blocked" ,
5860 StatusCode : 200 ,
5961 }, nil
6062 }
6163 chaincode = str
6264 } else {
63- utils .LogProfileSkipped (client , ctx , userId , "Chaincode Not Found" , sessionId )
64- utils .SetProfileStatusBlocked (client , ctx , userId , "Chaincode Not Found" , sessionId , discordId )
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 )
6567 return events.APIGatewayProxyResponse {
6668 Body : "Profile Skipped Chaincode Not Found" ,
6769 StatusCode : 200 ,
@@ -71,7 +73,7 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
7173 var userData utils.Diff
7274 err = dsnap .DataTo (& userData )
7375 if err != nil {
74- utils .LogProfileSkipped (client , ctx , userId , "UserData Type Error: " + fmt .Sprintln (err ), sessionId )
76+ utils .LogProfileSkipped (d . client , d . ctx , userId , "UserData Type Error: " + fmt .Sprintln (err ), sessionId )
7577 return events.APIGatewayProxyResponse {
7678 Body : "Profile Skipped No User Data" ,
7779 StatusCode : 200 ,
@@ -92,31 +94,41 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
9294 isServiceRunning = true
9395 }
9496
95- utils .LogHealth (client , ctx , userId , isServiceRunning , sessionId )
97+ utils .LogHealth (d . client , d . ctx , userId , isServiceRunning , sessionId )
9698 if ! isServiceRunning {
97- utils .LogProfileSkipped (client , ctx , userId , "Profile Service Down" , sessionId )
98- utils .SetProfileStatusBlocked (client , ctx , userId , "Profile Service Down" , sessionId , discordId )
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 )
99101 return events.APIGatewayProxyResponse {
100102 Body : "Profile Skipped Service Down" ,
101103 StatusCode : 200 ,
102104 }, nil
103105 }
104106
105- dataErr := utils .Getdata (client , ctx , userId , userUrl , chaincode , utils .DiffToRes (userData ), sessionId , discordId )
107+ dataErr := utils .Getdata (d . client , d . ctx , userId , userUrl , chaincode , utils .DiffToRes (userData ), sessionId , discordId )
106108 if dataErr != "" {
107109 return events.APIGatewayProxyResponse {
108110 Body : "Profile Skipped " + dataErr ,
109111 StatusCode : 200 ,
110112 }, nil
111113 }
112114
113- defer client .Close ()
114115 return events.APIGatewayProxyResponse {
115116 Body : "Profile Saved" ,
116117 StatusCode : 200 ,
117118 }, nil
118119}
119120
120121func main () {
121- lambda .Start (handler )
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 )
122134}
0 commit comments