Skip to content

Commit 0b160bc

Browse files
authored
Merge pull request #252 from Real-Dev-Squad/develop
Dev to Main
2 parents 920b423 + 06cf450 commit 0b160bc

File tree

19 files changed

+883
-312
lines changed

19 files changed

+883
-312
lines changed

call-profile/main.go

Lines changed: 52 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
package main
22

33
import (
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

121119
func 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
}

call-profile/main_test.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ func TestHandlerIntegration(t *testing.T) {
319319
"githubId": "johndoe",
320320
"linkedin": "johndoe",
321321
"website": "https://johndoe.com",
322+
"dob": "1990-01-15",
322323
},
323324
mockServer: func() *httptest.Server {
324325
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -337,7 +338,8 @@ func TestHandlerIntegration(t *testing.T) {
337338
"designation": "Developer",
338339
"github_id": "johndoe",
339340
"linkedin_id": "johndoe",
340-
"website": "https://johndoe.com"
341+
"website": "https://johndoe.com",
342+
"dob": "1990-01-15"
341343
}`))
342344
}
343345
}))
@@ -364,8 +366,8 @@ func TestHandlerIntegration(t *testing.T) {
364366
},
365367
userData: nil,
366368
mockServer: nil,
367-
expectedBody: "Profile Skipped No Profile URL",
368-
expectedStatus: 200,
369+
expectedBody: "Error retrieving user: rpc error: code = NotFound desc = \"projects/test-project/databases/(default)/documents/users/non-existent-user\" not found",
370+
expectedStatus: 500,
369371
expectedError: false,
370372
},
371373
{
@@ -435,7 +437,7 @@ func TestHandlerIntegration(t *testing.T) {
435437
w.Write([]byte("Service Unavailable"))
436438
}))
437439
},
438-
expectedBody: "Profile Skipped error in getting profile data",
440+
expectedBody: "Profile Skipped: error in getting profile data: status code 500",
439441
expectedStatus: 200,
440442
expectedError: false,
441443
},
@@ -522,6 +524,7 @@ func TestHandlerWithRealFirestore(t *testing.T) {
522524
"githubId": "integrationtest",
523525
"linkedin": "integrationtest",
524526
"website": "https://integrationtest.com",
527+
"dob": "1992-05-20",
525528
}
526529

527530
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -540,7 +543,8 @@ func TestHandlerWithRealFirestore(t *testing.T) {
540543
"designation": "Tester",
541544
"github_id": "integrationtest",
542545
"linkedin_id": "integrationtest",
543-
"website": "https://integrationtest.com"
546+
"website": "https://integrationtest.com",
547+
"dob": "1992-05-20"
544548
}`))
545549
}
546550
}))
@@ -590,7 +594,7 @@ func TestHandlerEdgeCases(t *testing.T) {
590594
"firstName": 123, // Invalid type
591595
"lastName": "Doe",
592596
},
593-
expectedBody: "Profile Skipped error in getting profile data",
597+
expectedBody: "Profile Skipped: error in getting profile data: status code 404",
594598
expectedStatus: 200,
595599
},
596600
{
@@ -604,7 +608,7 @@ func TestHandlerEdgeCases(t *testing.T) {
604608
"chaincode": "TESTCHAIN",
605609
"profileStatus": "PENDING",
606610
},
607-
expectedBody: "Profile Skipped error in getting profile data", // Will fail health check
611+
expectedBody: "Profile Skipped: error in getting profile data: status code 404", // Will fail health check
608612
expectedStatus: 200,
609613
},
610614
}
@@ -635,9 +639,9 @@ func newFirestoreMockClient(ctx context.Context) *firestore.Client {
635639

636640
func handlerWithClient(request events.APIGatewayProxyRequest, client *firestore.Client) (events.APIGatewayProxyResponse, error) {
637641
ctx := context.Background()
638-
d := deps{
639-
client: client,
640-
ctx: ctx,
642+
d := &utils.Deps{
643+
Client: client,
644+
Ctx: ctx,
641645
}
642-
return d.handler(request)
646+
return handler(d, request)
643647
}

call-profile/testdata.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ var ResValidationTests = []struct {
163163
TwitterId: "johndoe",
164164
InstagramId: "johndoe",
165165
Website: "https://johndoe.com",
166+
DOB: "1990-01-15",
166167
},
167168
IsValid: true,
168169
Description: "Complete valid Res struct",
@@ -241,6 +242,78 @@ var ResValidationTests = []struct {
241242
IsValid: false,
242243
Description: "Invalid website URL should fail validation",
243244
},
245+
{
246+
Name: "InvalidDOBFormat_Slash",
247+
Res: utils.Res{
248+
FirstName: "John",
249+
LastName: "Doe",
250+
251+
Phone: "1234567890",
252+
YOE: 5,
253+
Company: "Tech Corp",
254+
Designation: "Developer",
255+
GithubId: "johndoe",
256+
LinkedIn: "johndoe",
257+
Website: "https://johndoe.com",
258+
DOB: "01/15/1990",
259+
},
260+
IsValid: false,
261+
Description: "DOB with slash separator should fail validation",
262+
},
263+
{
264+
Name: "InvalidDOBFormat_WrongFormat",
265+
Res: utils.Res{
266+
FirstName: "John",
267+
LastName: "Doe",
268+
269+
Phone: "1234567890",
270+
YOE: 5,
271+
Company: "Tech Corp",
272+
Designation: "Developer",
273+
GithubId: "johndoe",
274+
LinkedIn: "johndoe",
275+
Website: "https://johndoe.com",
276+
DOB: "invalid-date",
277+
},
278+
IsValid: false,
279+
Description: "Invalid date string should fail validation",
280+
},
281+
{
282+
Name: "ValidDOB_EmptyString",
283+
Res: utils.Res{
284+
FirstName: "John",
285+
LastName: "Doe",
286+
287+
Phone: "1234567890",
288+
YOE: 5,
289+
Company: "Tech Corp",
290+
Designation: "Developer",
291+
GithubId: "johndoe",
292+
LinkedIn: "johndoe",
293+
Website: "https://johndoe.com",
294+
DOB: "",
295+
},
296+
IsValid: true,
297+
Description: "Empty DOB string should be valid (optional field)",
298+
},
299+
{
300+
Name: "ValidDOB_ValidFormat",
301+
Res: utils.Res{
302+
FirstName: "John",
303+
LastName: "Doe",
304+
305+
Phone: "1234567890",
306+
YOE: 5,
307+
Company: "Tech Corp",
308+
Designation: "Developer",
309+
GithubId: "johndoe",
310+
LinkedIn: "johndoe",
311+
Website: "https://johndoe.com",
312+
DOB: "1990-01-15",
313+
},
314+
IsValid: true,
315+
Description: "Valid DOB in YYYY-MM-DD format should pass validation",
316+
},
244317
}
245318

246319
var MockResponses = struct {
@@ -293,3 +366,4 @@ var EmptyUserIdTests = []struct {
293366
Description: "Null userId should return skip message",
294367
},
295368
}
369+

0 commit comments

Comments
 (0)