Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soumik/migrate/student #26

Draft
wants to merge 21 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1. Go stores empty arrays as null by default when you assign an empty array and store it in mongodb
2. Omitempty doesnot work on structs
3. Our datetimes are in Unix Time Stamp in UTC Timezone
4. Synchronise your computer clock if your iat in JWT is causing issues.
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module github.com/FrosTiK-SD/auth

go 1.21.1
go 1.21.6

require (
github.com/FrosTiK-SD/mongik v0.1.18
github.com/allegro/bigcache/v3 v3.1.0
github.com/gin-gonic/gin v1.9.0
github.com/lestrrat-go/jwx/v2 v2.0.16
go.mongodb.org/mongo-driver v1.13.0
go.mongodb.org/mongo-driver v1.13.1
)

require (
Expand All @@ -32,6 +32,7 @@ require (
)

require (
github.com/FrosTiK-SD/models v0.4.3
github.com/bytedance/sonic v1.8.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gin-contrib/cors v1.4.0
Expand All @@ -41,7 +42,7 @@ require (
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/json-iterator/go v1.1.12
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.2 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
Expand Down
68 changes: 8 additions & 60 deletions go.sum

Large diffs are not rendered by default.

355 changes: 355 additions & 0 deletions handler/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,355 @@
package handler

import (
"fmt"
"regexp"
"strconv"
"strings"
"time"

"github.com/FrosTiK-SD/auth/model"
Student "github.com/FrosTiK-SD/auth/model"
Constant "github.com/FrosTiK-SD/models/constant"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)

func GetDOBFromString(input string) (primitive.DateTime, error) {
t, err := time.Parse("02-01-2006", input)
if err != nil {
return 0, err
}
return primitive.NewDateTimeFromTime(t), nil
}

func GetCategoryFromString(input string) Student.ReservationCategory {
category := Student.ReservationCategory{
Category: "",
IsPWD: false,
IsEWS: false,
}

if input == "" {
return category
}

for _, c := range [4]string{"GEN", "SC", "ST", "OBC-NCL"} {
re := regexp.MustCompile("(?i)" + c)

if re.MatchString(input) {
category.Category = c
break
}
}

if regexp.MustCompile("(?i)pwd").MatchString(input) {
category.IsPWD = true
}

if regexp.MustCompile("(?i)ews").MatchString(input) {
category.IsEWS = true
}

return category
}

func GetRankFromString(input string, rc Student.ReservationCategory) (Student.RankDetails, error) {
srd := Student.RankDetails{
Rank: -1,
RankCategory: GetCategoryFromString(input),
}

num, err := strconv.Atoi(regexp.MustCompile(`\d+`).FindString(input))
if err != nil {
return srd, err
}
srd.Rank = num

if regexp.MustCompile("(?i)category").MatchString(input) {
srd.RankCategory = rc
}

return srd, nil

}

func GetEducationGapJeeRank(cursor *mongo.Cursor, errorArray *[]string, category model.ReservationCategory) (model.RankDetails, int) {
var type1 model.EuducationGapJeeRankStrings
var type2 model.EuducationGapJeeRankIntegers

if err1 := cursor.Decode(&type1); err1 == nil {
jeeRank, errJeeRank := GetRankFromString(type1.JeeRank, category)
if errJeeRank != nil {
fmt.Println(errJeeRank)
*errorArray = append(*errorArray, "jeeRank")
jeeRank.Rank = -1
}

educationGap, err := strconv.Atoi(regexp.MustCompile(`\d+`).FindString(type1.EducationGap))
if err != nil {
fmt.Println(educationGap)
*errorArray = append(*errorArray, "educationGap")
educationGap = -1
}

return jeeRank, educationGap
}

if err1 := cursor.Decode(&type2); err1 == nil {
return model.RankDetails{Rank: type2.JeeRank, RankCategory: model.ReservationCategory{Category: "GEN"}}, type2.EducationGap
}

return model.RankDetails{Rank: -1, RankCategory: model.ReservationCategory{Category: "GEN"}}, -1
}

func GetXXIIYear(cursor *mongo.Cursor) (int, int) {
var type1 model.XXIIYearType1
var type2 model.XXIIYearType2

if err1 := cursor.Decode(&type1); err1 == nil {
xyear, errx := strconv.Atoi(type1.XYear)
if errx != nil {
xyear = 0
}
xiiyear, errxii := strconv.Atoi(type1.XiiYear)
if errxii != nil {
xiiyear = 0
}

return xyear, xiiyear
}

if err1 := cursor.Decode(&type2); err1 == nil {
return type2.XYear, type2.XiiYear
}

return -1, -1
}

func (h *Handler) MigrateStudentDataToV2(ctx *gin.Context) {
studentCollection := h.MongikClient.MongoClient.Database(Constant.DB).Collection(Constant.StudentCollection)
cursor, errFind := studentCollection.Find(ctx, bson.D{{Key: "version", Value: bson.D{{Key: "$exists", Value: false}}}})
if errFind != nil {
ctx.AbortWithStatusJSON(400, gin.H{"count": 0, "error": errFind.Error(), "reason": "Find not successful"})
return
}

var count int64 = 0

for cursor.Next(ctx) {
var oldStudent model.OldStudent
if errDecode := cursor.Decode(&oldStudent); errDecode != nil {
fmt.Println(errDecode)
continue
}

if oldStudent.Email == "[email protected]" {
continue
}

if oldStudent.CompaniesAlloted == nil {
oldStudent.CompaniesAlloted = []string{}
}

errorArray := []string{}
var EndYearOffset int
var Course Constant.Course
var courseError bool = false

switch oldStudent.Course {
case "idd":
EndYearOffset = 5
Course = Constant.IDD
case "mtech":
EndYearOffset = 2
Course = Constant.MTECH
case "phd":
EndYearOffset = 6
Course = Constant.PHD
case "btech":
EndYearOffset = 4
Course = Constant.BTECH
default:
EndYearOffset = -1
Course = Constant.BTECH
errorArray = append(errorArray, "course")
courseError = true
}

category := GetCategoryFromString(oldStudent.Category)

kaggle := ""
if oldStudent.Kaggle != "" {
kaggle = oldStudent.Kaggle
}
if oldStudent.Kaggel != "" {
kaggle = oldStudent.Kaggel
}

jeeRank, educationGap := GetEducationGapJeeRank(cursor, &errorArray, category)

xYear, xiiYear := GetXXIIYear(cursor)
if xYear <= 0 {
errorArray = append(errorArray, "xYear")
}

if xiiYear <= 0 {
errorArray = append(errorArray, "xiiYear")
}

dob, errDOB := GetDOBFromString(oldStudent.Dob)
if errDOB != nil {
fmt.Println(errDOB)
errorArray = append(errorArray, "dob")
dob = 0
}

mobile := strconv.FormatInt(oldStudent.Mobile, 10)
mobileError := false
if mobile == "0" {
mobileError = true
errorArray = append(errorArray, "mobile")
}

gender := Constant.Gender(strings.ToLower(oldStudent.Gender))

var userError map[string]model.UserError = make(map[string]model.UserError)

for _, err := range errorArray {
userError[err] = model.MIGRATION
}

newStudent := Student.Student{
Id: oldStudent.ID,
Groups: oldStudent.Groups,
CompaniesAlloted: oldStudent.CompaniesAlloted,

Batch: &Student.Batch{
StartYear: oldStudent.Batch,
EndYear: oldStudent.Batch + EndYearOffset,
shalearkane marked this conversation as resolved.
Show resolved Hide resolved
},
RollNo: oldStudent.RollNo,
InstituteEmail: oldStudent.Email,
Department: oldStudent.Department,
Course: &Course,

FirstName: oldStudent.FirstName,
LastName: oldStudent.LastName,

Gender: &gender,
DOB: &dob,
PermanentAddress: oldStudent.PermanentAddress,
PresentAddress: oldStudent.PresentAddress,
PersonalEmail: oldStudent.PersonalEmail,
Mobile: &mobile,
Category: &category,
MotherTongue: oldStudent.MotherTongue,
ParentsDetails: &Student.ParentsDetails{
FatherName: oldStudent.FatherName,
FatherOccupation: oldStudent.FatherOccupation,
MotherName: oldStudent.MotherName,
MotherOccupation: oldStudent.MotherOccupation,
},

Academics: Student.Academics{
JEERank: &jeeRank,
XthClass: &Student.EducationDetails{
Certification: oldStudent.XBoard,
Institute: oldStudent.XInstitute,
Year: xYear,
Score: oldStudent.XPercentage,
},
XIIthClass: &Student.EducationDetails{
Certification: oldStudent.XiiBoard,
Institute: oldStudent.XiiInstitute,
Year: xiiYear,
Score: oldStudent.XiiPercentage,
},
EducationGap: &educationGap,
SemesterSPI: Student.SemesterSPI{
One: oldStudent.SemesterOne,
Two: oldStudent.SemesterTwo,
Three: oldStudent.SemesterThree,
Four: oldStudent.SemesterFour,
Five: oldStudent.SemesterFive,
Six: oldStudent.SemesterSix,
shalearkane marked this conversation as resolved.
Show resolved Hide resolved
Seven: oldStudent.SemesterSeven,
Eight: oldStudent.SemesterEight,
},
SummerTermSPI: Student.SummerTermSPI{
One: oldStudent.SummerOne,
Two: oldStudent.SummerTwo,
Three: oldStudent.SummerThree,
Four: oldStudent.SummerFour,
Five: oldStudent.SummerFive,
},
UnderGraduate: &Student.EducationDetails{
Certification: oldStudent.UgIn,
Institute: oldStudent.UgCollege,
Year: oldStudent.UgYear,
Score: oldStudent.UgScore,
},
PostGraduate: &Student.EducationDetails{
Certification: oldStudent.PgIn,
Institute: oldStudent.PgCollege,
Year: oldStudent.PgYear,
Score: oldStudent.PgScore,
},
CurrentCGPA: oldStudent.Cgpa,
ActiveBacklogs: oldStudent.ActiveBacklogs,
TotalBacklogs: oldStudent.TotalBacklogs,
},
WorkExperience: []Student.WorkExperience{},
SocialProfiles: Student.SocialProfiles{
LinkedIn: &Student.SocialProfile{
URL: oldStudent.LinkedIn,
},
MicrosoftTeams: &Student.SocialProfile{
URL: oldStudent.MicrosoftTeams,
},
Github: &Student.SocialProfile{
URL: oldStudent.Github,
},
Kaggle: &Student.SocialProfile{
URL: kaggle,
},
Skype: &Student.SocialProfile{
URL: oldStudent.Skype,
},
},
Extras: Student.Extras{
VideoResume: oldStudent.VideoResume,
},

StructVersion: 2,
CreatedAt: primitive.NewDateTimeFromTime(time.Now()),
UpdatedAt: primitive.NewDateTimeFromTime(time.Now()),
DataErrors: Student.DataErrors{
User: userError,
},
}

if courseError {
newStudent.Course = nil
}

if mobileError {
newStudent.Mobile = nil
}

ValidateData(&newStudent)

filter := bson.D{{Key: "_id", Value: oldStudent.ID}}

if _, errUpdate := studentCollection.ReplaceOne(ctx, filter, newStudent); errUpdate != nil {
ctx.AbortWithStatusJSON(400, gin.H{"count": count, "error": errUpdate.Error(), "id": oldStudent.ID})
return
}

count = count + 1
}

ctx.JSON(200, gin.H{"count": count})
}
Loading