Skip to content

Commit

Permalink
made some code quality changes
Browse files Browse the repository at this point in the history
  • Loading branch information
shalearkane committed Apr 12, 2024
1 parent 6c9df53 commit 03ad139
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 67 deletions.
87 changes: 22 additions & 65 deletions controller/student.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ import (
db "github.com/FrosTiK-SD/mongik/db"
models "github.com/FrosTiK-SD/mongik/models"
"github.com/google/go-cmp/cmp"
jsoniter "github.com/json-iterator/go"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

func getAliasEmailList(email string) []string {
var aliasEmailList []string
aliasEmailList = append(aliasEmailList, email)
Expand Down Expand Up @@ -80,6 +77,19 @@ func SetVerificationToNotVerified(verification *misc.Verification) {
verification.VerifiedAt = 0
}

func CheckSocialProfile(updatedSocialProfile *studentModel.SocialProfile, currentSocialProfile *studentModel.SocialProfile) {
if updatedSocialProfile == nil {
currentSocialProfile = nil
return
}

if updatedSocialProfile.URL != currentSocialProfile.URL || updatedSocialProfile.Username != currentSocialProfile.Username {
currentSocialProfile.URL = updatedSocialProfile.URL
currentSocialProfile.Username = updatedSocialProfile.Username
SetVerificationToNotVerified(&currentSocialProfile.Verification)
}
}

func InvalidateVerifiedFieldsOnChange(updated *studentModel.Student, current *studentModel.Student) {
// invalidate academic details
if !cmp.Equal(updated.Academics, current.Academics) {
Expand All @@ -88,68 +98,15 @@ func InvalidateVerifiedFieldsOnChange(updated *studentModel.Student, current *st
}

// invalidate social profiles
if !cmp.Equal(updated.SocialProfiles.LinkedIn, current.SocialProfiles.LinkedIn) {
current.SocialProfiles.LinkedIn = updated.SocialProfiles.LinkedIn
if current.SocialProfiles.LinkedIn != nil {
SetVerificationToNotVerified(&current.SocialProfiles.LinkedIn.Verification)
}
}

if !cmp.Equal(updated.SocialProfiles.Github, current.SocialProfiles.Github) {
current.SocialProfiles.Github = updated.SocialProfiles.Github
if current.SocialProfiles.Github != nil {
SetVerificationToNotVerified(&current.SocialProfiles.Github.Verification)
}
}

if !cmp.Equal(updated.SocialProfiles.MicrosoftTeams, current.SocialProfiles.MicrosoftTeams) {
current.SocialProfiles.MicrosoftTeams = updated.SocialProfiles.MicrosoftTeams
if current.SocialProfiles.MicrosoftTeams != nil {
SetVerificationToNotVerified(&current.SocialProfiles.MicrosoftTeams.Verification)
}
}

if !cmp.Equal(updated.SocialProfiles.Skype, current.SocialProfiles.Skype) {
current.SocialProfiles.Skype = updated.SocialProfiles.Skype
if current.SocialProfiles.LinkedIn != nil {
SetVerificationToNotVerified(&current.SocialProfiles.Skype.Verification)
}
}

if !cmp.Equal(updated.SocialProfiles.GoogleScholar, current.SocialProfiles.GoogleScholar) {
current.SocialProfiles.GoogleScholar = updated.SocialProfiles.GoogleScholar
if current.SocialProfiles.GoogleScholar != nil {
SetVerificationToNotVerified(&current.SocialProfiles.GoogleScholar.Verification)
}
}

if !cmp.Equal(updated.SocialProfiles.Codeforces, current.SocialProfiles.Codeforces) {
current.SocialProfiles.Codeforces = updated.SocialProfiles.Codeforces
if current.SocialProfiles.Codeforces != nil {
SetVerificationToNotVerified(&current.SocialProfiles.Codeforces.Verification)
}
}

if !cmp.Equal(updated.SocialProfiles.CodeChef, current.SocialProfiles.CodeChef) {
current.SocialProfiles.CodeChef = updated.SocialProfiles.CodeChef
if current.SocialProfiles.CodeChef != nil {
SetVerificationToNotVerified(&current.SocialProfiles.CodeChef.Verification)
}
}

if !cmp.Equal(updated.SocialProfiles.LeetCode, current.SocialProfiles.LeetCode) {
current.SocialProfiles.LeetCode = updated.SocialProfiles.LeetCode
if current.SocialProfiles.LeetCode != nil {
SetVerificationToNotVerified(&current.SocialProfiles.LeetCode.Verification)
}
}

if !cmp.Equal(updated.SocialProfiles.Kaggle, current.SocialProfiles.Kaggle) {
current.SocialProfiles.Kaggle = updated.SocialProfiles.Kaggle
if current.SocialProfiles.Kaggle != nil {
SetVerificationToNotVerified(&current.SocialProfiles.Kaggle.Verification)
}
}
CheckSocialProfile(updated.SocialProfiles.LinkedIn, current.SocialProfiles.LinkedIn)
CheckSocialProfile(updated.SocialProfiles.Github, current.SocialProfiles.Github)
CheckSocialProfile(updated.SocialProfiles.MicrosoftTeams, current.SocialProfiles.MicrosoftTeams)
CheckSocialProfile(updated.SocialProfiles.Skype, current.SocialProfiles.Skype)
CheckSocialProfile(updated.SocialProfiles.GoogleScholar, current.SocialProfiles.GoogleScholar)
CheckSocialProfile(updated.SocialProfiles.Codeforces, current.SocialProfiles.Codeforces)
CheckSocialProfile(updated.SocialProfiles.CodeChef, current.SocialProfiles.CodeChef)
CheckSocialProfile(updated.SocialProfiles.LeetCode, current.SocialProfiles.LeetCode)
CheckSocialProfile(updated.SocialProfiles.Kaggle, current.SocialProfiles.Kaggle)

var newWorkExperienceArray []studentModel.WorkExperience

Expand Down
4 changes: 2 additions & 2 deletions controller/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ func VerifyToken(cacheClient *bigcache.BigCache, idToken string, defaultJwkSet *
exp := rawJWT.Expiration()

// Validations
if time.Now().Sub(rawJWT.IssuedAt()) < 0 || time.Now().Sub(exp) > 0 || rawJWT.Subject() == "" || rawJWT.Issuer() != fmt.Sprintf("https://securetoken.google.com/%s", os.Getenv(constants.FIREBASE_PROJECT_ID)) || !util.ArrayContains(rawJWT.Audience(), os.Getenv(constants.FIREBASE_PROJECT_ID)) {
if time.Since(rawJWT.IssuedAt()) < 0 || time.Since(exp) > 0 || rawJWT.Subject() == "" || rawJWT.Issuer() != fmt.Sprintf("https://securetoken.google.com/%s", os.Getenv(constants.FIREBASE_PROJECT_ID)) || !util.ArrayContains(rawJWT.Audience(), os.Getenv(constants.FIREBASE_PROJECT_ID)) {
return nil, &exp, &constants.ERROR_INVALID_TOKEN
}

// Get the email
email, found := rawJWT.Get("email")
if found == false {
if !found {
return nil, &exp, &constants.ERROR_GETTING_EMAIL
}

Expand Down

0 comments on commit 03ad139

Please sign in to comment.