diff --git a/controllers/authController.go b/controllers/authController.go index 76bba1c..dd329cb 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -69,7 +69,7 @@ func CreateDefaultAdmin() models.Admin { var aid string for { aid = GenerateID(6) - if ValidateID(aid) == true { + if ValidateID(aid, 3) == true { break } } @@ -237,7 +237,7 @@ func Enroll(c *fiber.Ctx) error { var sid string for { sid = GenerateID(6) - if ValidateID(sid) == true { + if ValidateID(sid, 1) == true { break } } @@ -372,7 +372,7 @@ func RegisterTeacher(c *fiber.Ctx) error { // For the unlikely event that an ID is already in use this will simply try again till it gets a id not in use for { tid = GenerateID(6) - isValid := ValidateID(tid) + isValid := ValidateID(tid, 2) if isValid == true { break } @@ -458,7 +458,7 @@ func CreateAdmin(c *fiber.Ctx) error { var aid string for { aid = GenerateID(6) - if ValidateID(aid) == true { + if ValidateID(aid, 3) == true { break } } diff --git a/controllers/idController.go b/controllers/idController.go index 64b24ae..9b2f740 100644 --- a/controllers/idController.go +++ b/controllers/idController.go @@ -18,7 +18,7 @@ var idCollection *mongo.Collection = database.OpenCollection(database.Client, "c var table = [...]byte{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'} -func ValidateID(id string) bool { // true: valid id, false: id already in use +func ValidateID(id string, userType int) bool { // true: valid id, false: id already in use ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) var foundID models.Id err := idCollection.FindOne(ctx, bson.M{"cid": id}).Decode(&foundID) @@ -26,6 +26,7 @@ func ValidateID(id string) bool { // true: valid id, false: id already in use if err != nil { // If there is no id found create new ID object to be stored and return true (unless insert error then try again) var newID models.Id newID.CID = id + newID.ParentType = userType newID.ID = primitive.NewObjectID() _, insertErr := idCollection.InsertOne(context.Background(), newID) if insertErr != nil {