Skip to content

Commit

Permalink
Update updated_at while updating user details in user document
Browse files Browse the repository at this point in the history
  • Loading branch information
MehulKChaudhari committed Mar 23, 2024
1 parent 2192b7c commit 10940f7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
75 changes: 38 additions & 37 deletions call-profile/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -10,7 +11,6 @@ import (
"os"
"sync"
"time"
"bytes"

"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
Expand All @@ -24,15 +24,15 @@ import (
"google.golang.org/api/option"

// validation packages
"github.com/go-ozzo/ozzo-validation/v4"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/golang-jwt/jwt/v5"
)

var wg sync.WaitGroup

/*
Structures
Structures
*/
type Log struct {
Type string `firestore:"type,omitempty"`
Expand Down Expand Up @@ -94,7 +94,7 @@ type Claims struct {
}

/*
Structures Conversions
Structures Conversions
*/
func diffToRes(diff Diff) Res {
return Res{
Expand Down Expand Up @@ -154,21 +154,21 @@ func diffToMap(diff Diff) map[string]interface{} {
}

/*
Setting Constants Map
Setting Constants Map
*/
var Constants map[string]string = map[string]string{
"ENV_DEVELOPMENT": "DEVELOPMENT",
"ENV_PRODUCTION": "PRODUCTION",
"STORED": "stored",
"FIRE_STORE_CRED": "firestoreCred",
"DISCORD_BOT_URL": "discordBotURL",
"ENV_DEVELOPMENT": "DEVELOPMENT",
"ENV_PRODUCTION": "PRODUCTION",
"STORED": "stored",
"FIRE_STORE_CRED": "firestoreCred",
"DISCORD_BOT_URL": "discordBotURL",
"IDENTITY_SERVICE_PRIVATE_KEY": "identityServicePrivateKey",
"PROFILE_SERVICE_HEALTH": "PROFILE_SERVICE_HEALTH",
"PROFILE_SKIPPED": "PROFILE_SKIPPED",
"PROFILE_DIFF_STORED": "PROFILE_DIFF_STORED",
"STATUS_BLOCKED": "BLOCKED",
"PROFILE_SERVICE_BLOCKED": "PROFILE_SERVICE_BLOCKED",
"NOT_APPROVED": "NOT APPROVED",
"PROFILE_SERVICE_HEALTH": "PROFILE_SERVICE_HEALTH",
"PROFILE_SKIPPED": "PROFILE_SKIPPED",
"PROFILE_DIFF_STORED": "PROFILE_DIFF_STORED",
"STATUS_BLOCKED": "BLOCKED",
"PROFILE_SERVICE_BLOCKED": "PROFILE_SERVICE_BLOCKED",
"NOT_APPROVED": "NOT APPROVED",
"PROFILE_SKIPPED_DUE_TO_UNAUTHENTICATED_ACCESS_TO_PROFILE_DATA": "profileSkippedDueToUnAuthenticatedAccessToProfileData",
"PROFILE_SKIPPED_DUE_TO_ERROR_IN_GETTING_PROFILE_DATA": "profileSkippedDueToErrorInGettingProfileData",
"SKIPPED_SAME_LAST_REJECTED_DIFF": "skippedSameLastRejectedDiff",
Expand All @@ -179,7 +179,7 @@ var Constants map[string]string = map[string]string{
}

/*
Setting Firestore Key for development/production
Setting Firestore Key for development/production
*/
func getParameter(parameter string) string {
if os.Getenv(("environment")) == Constants["ENV_DEVELOPMENT"] {
Expand Down Expand Up @@ -211,7 +211,7 @@ func getParameter(parameter string) string {
*/

/*
Function to initialize the firestore client
Function to initialize the firestore client
*/
func initializeFirestoreClient(ctx context.Context) (*firestore.Client, error) {
sa := option.WithCredentialsJSON([]byte(getParameter(Constants["FIRE_STORE_CRED"])))
Expand Down Expand Up @@ -248,19 +248,19 @@ Functions to generate jwt token

func generateJWTToken() string {
signKey, errGeneratingRSAKey := jwt.ParseRSAPrivateKeyFromPEM([]byte(getParameter(Constants["IDENTITY_SERVICE_PRIVATE_KEY"])))
if(errGeneratingRSAKey != nil) {
return "";
if errGeneratingRSAKey != nil {
return ""
}
expirationTime := time.Now().Add(1 * time.Minute)
t := jwt.New(jwt.GetSigningMethod("RS256"))
t.Claims = &Claims{
t.Claims = &Claims{
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expirationTime),
},
}
tokenString, err := t.SignedString(signKey)
if(err != nil) {
return "";
if err != nil {
return ""
}
return tokenString
}
Expand All @@ -270,7 +270,7 @@ func generateJWTToken() string {
*/

/*
Logs the health of the user's service
Logs the health of the user's service
*/
func logHealth(client *firestore.Client, ctx context.Context, userId string, isServiceRunning bool, sessionId string) {
newLog := Log{
Expand All @@ -289,7 +289,7 @@ func logHealth(client *firestore.Client, ctx context.Context, userId string, isS
}

/*
Logs the status of the user's profileDiff
Logs the status of the user's profileDiff
*/
func logProfileSkipped(client *firestore.Client, ctx context.Context, userId string, reason string, sessionId string) {
newLog := Log{
Expand All @@ -312,7 +312,7 @@ func logProfileStored(client *firestore.Client, ctx context.Context, userId stri
Type: Constants["PROFILE_DIFF_STORED"],
Timestamp: time.Now(),
Meta: map[string]interface{}{
"userId": userId,
"userId": userId,
"sessionId": sessionId,
},
Body: map[string]interface{}{
Expand All @@ -323,25 +323,26 @@ func logProfileStored(client *firestore.Client, ctx context.Context, userId stri
}

/*
Function for setting the profileStatus in user object in firestore
Function for setting the profileStatus in user object in firestore
*/
func setProfileStatusBlocked(client *firestore.Client, ctx context.Context, userId string, reason string, sessionId string, discordId string) {
client.Collection("users").Doc(userId).Set(ctx, map[string]interface{}{
"profileStatus": Constants["STATUS_BLOCKED"],
"chaincode": "",
"updated_at": time.Now().UnixNano() / int64(time.Millisecond),
}, firestore.MergeAll)

if discordId != "" {
tokenString := generateJWTToken();
tokenString := generateJWTToken()
postBody, _ := json.Marshal(map[string]string{
"userId": discordId,
"reason": reason,
})

responseBody := bytes.NewBuffer(postBody)

httpClient := &http.Client{}
req, _ := http.NewRequest("POST", os.Getenv(Constants["DISCORD_BOT_URL"]) + "/profile/blocked", responseBody)
req, _ := http.NewRequest("POST", os.Getenv(Constants["DISCORD_BOT_URL"])+"/profile/blocked", responseBody)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tokenString))
httpClient.Do(req)
Expand All @@ -363,7 +364,7 @@ func setProfileStatusBlocked(client *firestore.Client, ctx context.Context, user
}

/*
sets the user's profile diff to not approved
sets the user's profile diff to not approved
*/
func setNotApproved(client *firestore.Client, ctx context.Context, lastdiffId string) {
client.Collection("profileDiffs").Doc(lastdiffId).Set(ctx, map[string]interface{}{
Expand All @@ -372,7 +373,7 @@ func setNotApproved(client *firestore.Client, ctx context.Context, lastdiffId st
}

/*
Get the last profile diff of the user
Get the last profile diff of the user
*/
func getLastDiff(client *firestore.Client, ctx context.Context, userId string, approval string) (Res, string) {
query := client.Collection("profileDiffs").Where("userId", "==", userId).Where("approval", "==", approval).OrderBy("timestamp", firestore.Desc).Limit(1).Documents(ctx)
Expand All @@ -396,7 +397,7 @@ func getLastDiff(client *firestore.Client, ctx context.Context, userId string, a
}

/*
Generate and Store Profile Diff
Generate and Store Profile Diff
*/
func generateAndStoreDiff(client *firestore.Client, ctx context.Context, res Res, userId string, sessionId string) {
var diff Diff = resToDiff(res, userId)
Expand All @@ -409,7 +410,7 @@ func generateAndStoreDiff(client *firestore.Client, ctx context.Context, res Res
}

/*
Getting data from the user's service
Getting data from the user's service
*/
func getdata(client *firestore.Client, ctx context.Context, userId string, userUrl string, chaincode string, userData Res, sessionId string, discordId string) string {
var status string = ""
Expand Down Expand Up @@ -493,7 +494,7 @@ func getdata(client *firestore.Client, ctx context.Context, userId string, userU
}

/*
Function to extract userId from the request body
Function to extract userId from the request body
*/
func getDataFromBody(body []byte) (string, string) {
type extractedBody struct {
Expand All @@ -507,7 +508,7 @@ func getDataFromBody(body []byte) (string, string) {
}

/*
Main Handler Function
Main Handler Function
*/
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
ctx := context.Background()
Expand Down Expand Up @@ -616,7 +617,7 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
}

/*
Starts the lambda (Entry Point)
Starts the lambda (Entry Point)
*/
func main() {
lambda.Start(handler)
Expand Down
5 changes: 3 additions & 2 deletions verify/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import (
"fmt"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"math/rand"
Expand All @@ -17,11 +17,11 @@ import (
"google.golang.org/api/option"

"cloud.google.com/go/firestore"
"crypto/sha512"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"crypto/sha512"
)

/*
Expand Down Expand Up @@ -135,6 +135,7 @@ func setProfileStatus(client *firestore.Client, ctx context.Context, id string,
newData = map[string]interface{}{
"profileStatus": status,
"chaincode": "",
"updated_at": time.Now().UnixNano() / int64(time.Millisecond),
}
}

Expand Down

0 comments on commit 10940f7

Please sign in to comment.