update/contact-priority-and-fix-db-search

This commit is contained in:
SowinskiBraeden committed 2022-01-27 15:04:13 -08:00
1 parent e19e270e8d
commit e47ece5bb0
2 files changed
+92 -38

No files matched your search

+89 -35
View File
@@ -51,16 +51,16 @@ func UpdateStudentName(c *fiber.Ctx) error {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{ update := bson.M{
"$set": bson.M{ "$set": bson.M{
"PersonalData.FirstName": data["firstname"], "personaldata.firstname": data["firstname"],
"PersonalData.middlename": data["middlename"], "personaldata.middlename": data["middlename"],
"PersonalData.lastname": data["lastname"], "personaldata.lastname": data["lastname"],
"updated_at": update_time, "updated_at": update_time,
}, },
} }
result, updateErr := studentCollection.UpdateOne( result, updateErr := studentCollection.UpdateOne(
ctx, ctx,
bson.M{"sid": data["sid"]}, bson.M{"schooldata.sid": data["sid"]},
update, update,
) )
if updateErr != nil { if updateErr != nil {
@@ -114,14 +114,14 @@ func UpdateStudentGradeLevel(c *fiber.Ctx) error {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{ update := bson.M{
"$set": bson.M{ "$set": bson.M{
"SchoolData.gradelevel": data["gradelevel"], "schooldata.gradelevel": data["gradelevel"],
"updated_at": update_time, "updated_at": update_time,
}, },
} }
result, updateErr := studentCollection.UpdateOne( result, updateErr := studentCollection.UpdateOne(
ctx, ctx,
bson.M{"sid": data["sid"]}, bson.M{"schooldata.sid": data["sid"]},
update, update,
) )
if updateErr != nil { if updateErr != nil {
@@ -175,14 +175,14 @@ func UpdateStudentHomeroom(c *fiber.Ctx) error {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{ update := bson.M{
"$set": bson.M{ "$set": bson.M{
"SchoolData.homeroom": data["homeroom"], "schooldata.homeroom": data["homeroom"],
"updated_at": update_time, "updated_at": update_time,
}, },
} }
result, updateErr := studentCollection.UpdateOne( result, updateErr := studentCollection.UpdateOne(
ctx, ctx,
bson.M{"sid": data["sid"]}, bson.M{"schooldata.sid": data["sid"]},
update, update,
) )
if updateErr != nil { if updateErr != nil {
@@ -231,7 +231,7 @@ func UpdateStudentPassword(c *fiber.Ctx) error {
claims := token.Claims.(*jwt.StandardClaims) claims := token.Claims.(*jwt.StandardClaims)
var student models.Student var student models.Student
findErr := studentCollection.FindOne(context.TODO(), bson.M{"sid": claims.Issuer}).Decode(&student) findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": claims.Issuer}).Decode(&student)
if findErr != nil { if findErr != nil {
cancel() cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
@@ -268,15 +268,15 @@ func UpdateStudentPassword(c *fiber.Ctx) error {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{ update := bson.M{
"$set": bson.M{ "$set": bson.M{
"AccountData.password": student.HashPassword(data["newPassword1"]), "accountdata.password": student.HashPassword(data["newPassword1"]),
"AccountData.temppassword": false, // If it were a temp password, its not now "accountdata.temppassword": false, // If it were a temp password, its not now
"updated_at": update_time, "updated_at": update_time,
}, },
} }
result, updateErr := studentCollection.UpdateOne( result, updateErr := studentCollection.UpdateOne(
ctx, ctx,
bson.M{"sid": data["sid"]}, bson.M{"schooldata.sid": data["sid"]},
update, update,
) )
if updateErr != nil { if updateErr != nil {
@@ -319,7 +319,7 @@ func ResetStudentPassword(c *fiber.Ctx) error {
} }
var student models.Student var student models.Student
findErr := studentCollection.FindOne(context.TODO(), bson.M{"sid": data["sid"]}).Decode(&student) findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": data["sid"]}).Decode(&student)
if findErr != nil { if findErr != nil {
cancel() cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
@@ -340,15 +340,15 @@ func ResetStudentPassword(c *fiber.Ctx) error {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{ update := bson.M{
"$set": bson.M{ "$set": bson.M{
"AccountData.password": student.HashPassword(tempPass), "accountdata.password": student.HashPassword(tempPass),
"AccountData.temppassword": true, "accountdata.temppassword": true,
"updated_at": update_time, "updated_at": update_time,
}, },
} }
result, updateErr := studentCollection.UpdateOne( result, updateErr := studentCollection.UpdateOne(
ctx, ctx,
bson.M{"sid": data["sid"]}, bson.M{"schooldata.sid": data["sid"]},
update, update,
) )
if updateErr != nil { if updateErr != nil {
@@ -418,7 +418,7 @@ func UpdateStudentLocker(c *fiber.Ctx) error {
} }
var locker models.Locker var locker models.Locker
err := lockerCollection.FindOne(ctx, bson.M{"sid": data["sid"]}).Decode(&locker) err := lockerCollection.FindOne(ctx, bson.M{"schooldata.sid": data["sid"]}).Decode(&locker)
if err != nil { if err != nil {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
@@ -431,14 +431,14 @@ func UpdateStudentLocker(c *fiber.Ctx) error {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{ update := bson.M{
"$set": bson.M{ "$set": bson.M{
"School.Data.FirstName": locker.ID, "schooldata.locker": locker.ID,
"updated_at": update_time, "updated_at": update_time,
}, },
} }
result, updateErr := studentCollection.UpdateOne( result, updateErr := studentCollection.UpdateOne(
ctx, ctx,
bson.M{"sid": data["sid"]}, bson.M{"schooldata.sid": data["sid"]},
update, update,
) )
if updateErr != nil { if updateErr != nil {
@@ -492,10 +492,10 @@ func UpdateStudentAddress(c *fiber.Ctx) error {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{ update := bson.M{
"$set": bson.M{ "$set": bson.M{
"PersonalData.Address": data["address"], "personaldata.address": data["address"],
"PersonalData.City": data["city"], "personaldata.city": data["city"],
"PersonalData.Province": data["province"], "personaldata.province": data["province"],
"PersonalData.Postal": data["postal"], "personaldata.postal": data["postal"],
"updated_at": update_time, "updated_at": update_time,
}, },
} }
@@ -555,7 +555,7 @@ func UpdateStudentYOG(c *fiber.Ctx) error {
} }
var student models.Student var student models.Student
findErr := studentCollection.FindOne(context.TODO(), bson.M{"sid": data["sid"]}).Decode(&student) findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": data["sid"]}).Decode(&student)
if findErr != nil { if findErr != nil {
cancel() cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
@@ -567,14 +567,14 @@ func UpdateStudentYOG(c *fiber.Ctx) error {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{ update := bson.M{
"$set": bson.M{ "$set": bson.M{
"SchoolData.YOG": student.SchoolData.YOG + 1, "schooldata.yog": student.SchoolData.YOG + 1,
"updated_at": update_time, "updated_at": update_time,
}, },
} }
result, updateErr := studentCollection.UpdateOne( result, updateErr := studentCollection.UpdateOne(
ctx, ctx,
bson.M{"sid": data["sid"]}, bson.M{"schooldata.sid": data["sid"]},
update, update,
) )
if updateErr != nil { if updateErr != nil {
@@ -642,14 +642,14 @@ func UpdateStudentEmail(c *fiber.Ctx) error {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{ update := bson.M{
"$set": bson.M{ "$set": bson.M{
"PersonalData.Email": data["email"], "personaldata.email": data["email"],
"updated_at": update_time, "updated_at": update_time,
}, },
} }
result, updateErr := studentCollection.UpdateOne( result, updateErr := studentCollection.UpdateOne(
ctx, ctx,
bson.M{"sid": data["sid"]}, bson.M{"schooldata.sid": data["sid"]},
update, update,
) )
if updateErr != nil { if updateErr != nil {
@@ -703,15 +703,15 @@ func RemoveStudentsDisabled(c *fiber.Ctx) error {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{ update := bson.M{
"$set": bson.M{ "$set": bson.M{
"AccountData.accountdisabled": false, "accountdata.accountdisabled": false,
"AccountData.attempts": 0, "accountdata.attempts": 0,
"updated_at": update_time, "updated_at": update_time,
}, },
} }
result, updateErr := studentCollection.UpdateOne( result, updateErr := studentCollection.UpdateOne(
ctx, ctx,
bson.M{"sid": data["sid"]}, bson.M{"schooldata.sid": data["sid"]},
update, update,
) )
if updateErr != nil { if updateErr != nil {
@@ -874,9 +874,63 @@ func UpdateContactEmail(c *fiber.Ctx) error {
} }
func UpdateContactPriority(c *fiber.Ctx) error { func UpdateContactPriority(c *fiber.Ctx) error {
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ var data map[string]string
"success": nil, ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
"message": "not implimented",
if err := c.BodyParser(&data); err != nil {
cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Failed to parse body",
"error": err,
})
}
// Ensure Authenticated admin sent request
if !AuthAdmin(c) {
cancel()
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"success": false,
"message": "Unauthorized: only an admin can perform this action",
})
}
// Check id of contact and new priority number is included
if data["_id"] == "" || data["priority"] == "" {
cancel()
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false,
"message": "missing required fields",
})
}
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{
"$set": bson.M{
"priority": data["priority"],
"updated_at": update_time,
},
}
result, updateErr := contactCollection.UpdateOne(
ctx,
bson.M{"_id": data["_id"]},
update,
)
if updateErr != nil {
cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "the locker could not be updated",
"error": updateErr,
})
}
defer cancel()
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": true,
"message": "successfully updated locker",
"result": result,
}) })
} }
+3 -3
View File
@@ -30,7 +30,7 @@ type Student struct {
DOB string `json:"dob" validate:"required"` DOB string `json:"dob" validate:"required"`
Photo string `json:"photo"` Photo string `json:"photo"`
Contacts []string `json:"contacts"` // List of contact ID's rather than contact object Contacts []string `json:"contacts"` // List of contact ID's rather than contact object
} } `json:"personaldata"`
SchoolData struct { SchoolData struct {
GradeLevel int `json:"gradelevel" validate:"required"` GradeLevel int `json:"gradelevel" validate:"required"`
SID string `json:"sid"` // Student ID SID string `json:"sid"` // Student ID
@@ -39,14 +39,14 @@ type Student struct {
Locker string `json:"locker"` // Locker ID Locker string `json:"locker"` // Locker ID
YOG int `json:"yog"` // Year of Graduation YOG int `json:"yog"` // Year of Graduation
Photo string `json:"photo"` Photo string `json:"photo"`
} } `json:"schooldata"`
AccountData struct { AccountData struct {
SchoolEmail string `json:"schoolemail"` SchoolEmail string `json:"schoolemail"`
Password string `json:"-" validate:"min=10,max=32"` Password string `json:"-" validate:"min=10,max=32"`
AccountDisabled bool `bson:"accountdisabled"` AccountDisabled bool `bson:"accountdisabled"`
TempPassword bool `json:"temppassword"` TempPassword bool `json:"temppassword"`
Attempts int `json:"attempts"` // login attempts max 5 Attempts int `json:"attempts"` // login attempts max 5
} } `json:"accountdata"`
Created_at time.Time `json:"created_at"` Created_at time.Time `json:"created_at"`
Updated_at time.Time `json:"updated_at"` Updated_at time.Time `json:"updated_at"`
} }