1465 lines
37 KiB
Go
1465 lines
37 KiB
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"net/smtp"
|
|
"school-management/database"
|
|
"school-management/models"
|
|
"time"
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
"github.com/gofiber/fiber/v2"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
var lockerCollection *mongo.Collection = database.OpenCollection(database.Client, "lockers")
|
|
|
|
func UpdateStudentName(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 and names are included
|
|
// Middle name is optional
|
|
if data["sid"] == "" || data["firstname"] == "" || data["lastname"] == "" {
|
|
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{
|
|
"personaldata.firstname": data["firstname"],
|
|
"personaldata.middlename": data["middlename"],
|
|
"personaldata.lastname": data["lastname"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := studentCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"schooldata.sid": data["sid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the student could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated student",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateStudentGradeLevel(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 Authorized 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 required fields are included
|
|
if data["sid"] == "" || data["gradelevel"] == "" {
|
|
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{
|
|
"schooldata.gradelevel": data["gradelevel"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := studentCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"schooldata.sid": data["sid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the student could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated student",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
/*
|
|
Far later on this function is going to be completely automated.
|
|
Instead of an admin sending a request to update the homeroom of
|
|
a student or teacher, the system will take the room number of
|
|
the teacher's or student's Block 2 class from their schedule.
|
|
|
|
Though this function would remain for students only, for example
|
|
a student requests a course change, if its their block 2 the
|
|
admin would have to alter their homeroom to be the new class
|
|
number.
|
|
*/
|
|
func UpdateStudentHomeroom(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 required fields are included
|
|
if data["sid"] == "" || data["homeroom"] == "" {
|
|
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{
|
|
"schooldata.homeroom": data["homeroom"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := studentCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"schooldata.sid": data["sid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the student could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated student",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateStudentPassword(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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,
|
|
})
|
|
}
|
|
|
|
cookie := c.Cookies("jwt")
|
|
|
|
token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(SecretKey), nil
|
|
})
|
|
if err != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "not authorized",
|
|
})
|
|
}
|
|
|
|
claims := token.Claims.(*jwt.StandardClaims)
|
|
|
|
var student models.Student
|
|
findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": claims.Issuer}).Decode(&student)
|
|
if findErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "student not found",
|
|
})
|
|
}
|
|
|
|
// Check required fields are included
|
|
if data["currentpassword"] == "" || data["newpassword1"] == "" || data["newpassword2"] == "" {
|
|
cancel()
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "missing required fields",
|
|
})
|
|
}
|
|
|
|
if !student.ComparePasswords(data["currentpassword"]) {
|
|
cancel()
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "Your current password is incorrect",
|
|
})
|
|
}
|
|
|
|
if data["newpassword1"] != data["newpassword2"] {
|
|
cancel()
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "Your new password must match",
|
|
})
|
|
}
|
|
|
|
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
"accountdata.password": student.HashPassword(data["newpassword1"]),
|
|
"accountdata.temppassword": false, // If it were a temp password, its not now
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := studentCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"schooldata.sid": claims.Issuer},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the student password could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated student password",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
// This is for students to reset their password if they are unable to login
|
|
func ResetStudentPassword(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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,
|
|
})
|
|
}
|
|
|
|
// Check required fields are included (email must be personal email)
|
|
if data["sid"] == "" || data["email"] == "" {
|
|
cancel()
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "missing required fields",
|
|
})
|
|
}
|
|
|
|
var student models.Student
|
|
findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": data["sid"]}).Decode(&student)
|
|
if findErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "student not found",
|
|
})
|
|
}
|
|
|
|
if student.PersonalData.Email == data["email"] {
|
|
cancel()
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "Your personal email is incorrect",
|
|
})
|
|
}
|
|
|
|
tempPass := student.GeneratePassword(12, 1, 1, 1)
|
|
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
"accountdata.password": student.HashPassword(tempPass),
|
|
"accountdata.temppassword": true,
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := studentCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"schooldata.sid": data["sid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the student password could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
// Send student personal email temp password
|
|
message := []byte("Your temporary password is " + tempPass)
|
|
auth := smtp.PlainAuth("", systemEmail, systemPassword, "smtp.gmail.com")
|
|
|
|
err := smtp.SendMail("smtp.gmail.com:587", auth, systemEmail, []string{student.PersonalData.Email}, message)
|
|
if err != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "Could not send password to students email",
|
|
"error": err,
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated student password",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateStudentLocker(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 and locker are included
|
|
if data["sid"] == "" || data["lockerNumber"] == "" {
|
|
cancel()
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "missing required fields",
|
|
})
|
|
}
|
|
|
|
var locker models.Locker
|
|
err := lockerCollection.FindOne(ctx, bson.M{"schooldata.sid": data["sid"]}).Decode(&locker)
|
|
if err != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "locker not found",
|
|
"error": err,
|
|
})
|
|
}
|
|
|
|
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
"schooldata.studentlocker": locker.ID,
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := studentCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"schooldata.sid": data["sid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the student could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated student",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateStudentAddress(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 required fields are included
|
|
if data["sid"] == "" || data["address"] == "" || data["city"] == "" || data["province"] == "" || data["postal"] == "" {
|
|
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{
|
|
"personaldata.address": data["address"],
|
|
"personaldata.city": data["city"],
|
|
"personaldata.province": data["province"],
|
|
"personaldata.postal": data["postal"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := studentCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"sid": data["sid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the student could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated student",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
// In the case a student gets help back a grade, we need to update their YOG (Year of Graduation)
|
|
func UpdateStudentYOG(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 required fields are included
|
|
if data["sid"] == "" {
|
|
cancel()
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "missing required fields",
|
|
})
|
|
}
|
|
|
|
var student models.Student
|
|
findErr := studentCollection.FindOne(context.TODO(), bson.M{"schooldata.sid": data["sid"]}).Decode(&student)
|
|
if findErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "student not found",
|
|
})
|
|
}
|
|
|
|
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
"schooldata.yog": student.SchoolData.YOG + 1,
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := studentCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"schooldata.sid": data["sid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the student could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated student",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateStudentContacts(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateStudentPhoto(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateStudentEmail(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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,
|
|
})
|
|
}
|
|
|
|
verifiedAdmin := AuthAdmin(c)
|
|
verifiedStudent, sid := AuthStudent(c)
|
|
// Ensure Authenticated admin sent request
|
|
if !verifiedAdmin && !verifiedStudent {
|
|
cancel()
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "Unauthorized: only an admin or teacher can perform this action",
|
|
})
|
|
}
|
|
|
|
// Check required fields are included
|
|
if data["email"] == "" {
|
|
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{
|
|
"personaldata.email": data["email"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := studentCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"schooldata.sid": sid},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the student could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated student",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func RemoveStudentsDisabled(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 Authorized 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 required fields are included
|
|
if data["sid"] == "" {
|
|
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{
|
|
"accountdata.accountdisabled": false,
|
|
"accountdata.attempts": 0,
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := studentCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"schooldata.sid": data["sid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the student account could not be re-enabled",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully re-enabled student account",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
/*
|
|
Far later on this function is going to be completely automated.
|
|
Instead of an admin sending a request to update the homeroom of
|
|
a student or teacher, the system will take the room number of
|
|
the teacher's or student's Block 2 class from their schedule.
|
|
|
|
Though this function would remain for students only, for example
|
|
a student requests a course change, if its their block 2 the
|
|
admin would have to alter their homeroom to be the new class
|
|
number.
|
|
*/
|
|
func UpdateTeacherHomeroom(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 required fields are included
|
|
if data["tid"] == "" || data["homeroom"] == "" {
|
|
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{
|
|
"schooldata.homeroom": data["homeroom"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := teacherCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"tid": data["tid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the teacher could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated teacher",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateTeacherPassword(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func ResetTeacherPassword(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateTeacherAddress(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 required fields are included
|
|
if data["tid"] == "" || data["address"] == "" || data["city"] == "" || data["province"] == "" || data["postal"] == "" {
|
|
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{
|
|
"address": data["address"],
|
|
"city": data["city"],
|
|
"province": data["province"],
|
|
"postal": data["postal"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := teacherCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"tid": data["tid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the teacher could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated teacher",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateTeacherPhoto(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateTeacherEmail(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 or teacher can perform this action",
|
|
})
|
|
}
|
|
|
|
// Check required fields are included
|
|
if data["tid"] == "" || data["email"] == "" {
|
|
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{
|
|
"email": data["email"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := teacherCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"tid": data["tid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the teacher could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated teacher",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateTeacherName(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 and names are included
|
|
if data["_id"] == "" || data["firstname"] == "" || data["middlename"] == "" || data["lastname"] == "" {
|
|
cancel()
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "missing required fields",
|
|
})
|
|
}
|
|
|
|
teacherObjectId, idErr := primitive.ObjectIDFromHex(data["_id"])
|
|
if idErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "teacher not found",
|
|
"error": idErr,
|
|
})
|
|
}
|
|
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
"firstname": data["firstname"],
|
|
"middlename": data["middlename"],
|
|
"lastname": data["lastname"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := teacherCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"_id": teacherObjectId},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the teacher could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated teacher",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateContactName(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 required fields are included
|
|
if data["_id"] == "" || data["firstname"] == "" || data["middlename"] == "" || data["lastname"] == "" {
|
|
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{
|
|
"firstname": data["firstname"],
|
|
"middlename": data["middlename"],
|
|
"lastname": data["lastname"],
|
|
"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 contact could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated contact",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateContactAddress(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 required fields are included
|
|
if data["_id"] == "" || data["address"] == "" || data["city"] == "" || data["province"] == "" || data["postal"] == "" {
|
|
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{
|
|
"address": data["address"],
|
|
"city": data["city"],
|
|
"province": data["province"],
|
|
"postal": data["postal"],
|
|
"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 contact could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated contact",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateContactHomePhone(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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["newnumber"] == "" {
|
|
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{
|
|
"homephone": data["newnumber"],
|
|
"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": "contact could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated contact",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateContactWorkPhone(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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["newnumber"] == "" {
|
|
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{
|
|
"workphone": data["newnumber"],
|
|
"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": "contact could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated contact",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateContactEmail(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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["email"] == "" {
|
|
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{
|
|
"email": data["email"],
|
|
"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": "contact could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated contact",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateContactPriority(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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": "contact could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated contact",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateLockerCombo(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
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 locker number is included
|
|
if data["lockernumber"] == "" || data["newlockercombo"] == "" {
|
|
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{
|
|
"lockercombo": data["newlockercombo"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := lockerCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"lockernumber": data["lockernumber"]},
|
|
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,
|
|
})
|
|
}
|