System email logic
This commit is contained in:
1 parent
faf5972d92
commit
de494818e7
3 files changed
+122
-6
No files matched your search
+3
-1
@@ -1,3 +1,5 @@
|
|||||||
mongoURI="mongodb://localhost:27017"
|
mongoURI="mongodb://localhost:27017"
|
||||||
dbo="school"
|
dbo="school"
|
||||||
PORT=8080
|
PORT=8080
|
||||||
|
SYSTEM_EMAIL=
|
||||||
|
SYSTEM_PASSWORD=
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
# Environment
|
||||||
|
.env
|
||||||
@@ -2,10 +2,12 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"os"
|
||||||
"school-management/database"
|
"school-management/database"
|
||||||
"school-management/models"
|
"school-management/models"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"net/smtp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
@@ -22,6 +24,9 @@ var adminCollection *mongo.Collection = database.OpenCollection(database.Client,
|
|||||||
|
|
||||||
const SecretKey = "secret"
|
const SecretKey = "secret"
|
||||||
|
|
||||||
|
var systemEmail string = os.Getenv("SYSTEM_EMAIL")
|
||||||
|
var systemPassword string = os.Getenv("SYSTEM_PASSWORD")
|
||||||
|
|
||||||
func Enroll(c *fiber.Ctx) error {
|
func Enroll(c *fiber.Ctx) error {
|
||||||
var data map[string]string
|
var data map[string]string
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
@@ -86,6 +91,23 @@ func Enroll(c *fiber.Ctx) error {
|
|||||||
student.TempPassword = true
|
student.TempPassword = true
|
||||||
// Send student personal email temp password
|
// Send student personal email temp password
|
||||||
|
|
||||||
|
smtpHost := "smpt.gmail.com"
|
||||||
|
smtpPort := "587"
|
||||||
|
|
||||||
|
message := []byte("Your temporary password is: " + tempPass)
|
||||||
|
|
||||||
|
auth := smtp.PlainAuth("", systemEmail, systemPassword, smtpHost)
|
||||||
|
|
||||||
|
err = smtp.SendMail(smtpHost+":"+smtpPort, auth, systemEmail, []string{student.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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
var sid string
|
var sid string
|
||||||
for {
|
for {
|
||||||
sid = GenerateID()
|
sid = GenerateID()
|
||||||
@@ -169,6 +191,23 @@ func RegisterTeacher(c *fiber.Ctx) error {
|
|||||||
teacher.TempPassword = true
|
teacher.TempPassword = true
|
||||||
// Send teacher personal email temp password
|
// Send teacher personal email temp password
|
||||||
|
|
||||||
|
smtpHost := "smpt.gmail.com"
|
||||||
|
smtpPort := "587"
|
||||||
|
|
||||||
|
message := []byte("Your temporary password is: " + tempPass)
|
||||||
|
|
||||||
|
auth := smtp.PlainAuth("", systemEmail, systemPassword, smtpHost)
|
||||||
|
|
||||||
|
err = smtp.SendMail(smtpHost+":"+smtpPort, auth, systemEmail, []string{teacher.Email}, message)
|
||||||
|
if err != nil {
|
||||||
|
cancel()
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "Could not send password to teacners email",
|
||||||
|
"error": err,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
var tid string
|
var tid string
|
||||||
// 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 the unlikely event that an ID is already in use this will simply try again till it gets a id not in use
|
||||||
for {
|
for {
|
||||||
@@ -250,7 +289,24 @@ func CreateAdmin(c *fiber.Ctx) error {
|
|||||||
tempPass := admin.GeneratePassword(12, 1, 1, 1)
|
tempPass := admin.GeneratePassword(12, 1, 1, 1)
|
||||||
admin.Password = admin.HashPassword(tempPass)
|
admin.Password = admin.HashPassword(tempPass)
|
||||||
admin.TempPassword = true
|
admin.TempPassword = true
|
||||||
// Send teacher personal email temp password
|
// Send admin personal email temp password
|
||||||
|
|
||||||
|
smtpHost := "smpt.gmail.com"
|
||||||
|
smtpPort := "587"
|
||||||
|
|
||||||
|
message := []byte("Your temporary password is: " + tempPass)
|
||||||
|
|
||||||
|
auth := smtp.PlainAuth("", systemEmail, systemPassword, smtpHost)
|
||||||
|
|
||||||
|
err = smtp.SendMail(smtpHost+":"+smtpPort, auth, systemEmail, []string{admin.Email}, message)
|
||||||
|
if err != nil {
|
||||||
|
cancel()
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "Could not send password to admins email",
|
||||||
|
"error": err,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
var aid string
|
var aid string
|
||||||
for {
|
for {
|
||||||
@@ -634,7 +690,8 @@ func UpdateStudentName(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check id and names are included
|
// Check id and names are included
|
||||||
if data["sid"] == "" || data["firstname"] == "" || data["middlename"] == "" || data["lastname"] == "" {
|
// Middle name is optional
|
||||||
|
if data["sid"] == "" || data["firstname"] == "" || data["lastname"] == "" {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
@@ -746,9 +803,64 @@ func UpdateStudentGradeLevel(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UpdateStudentHomeroom(c *fiber.Ctx) error {
|
func UpdateStudentHomeroom(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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var admin models.Admin
|
||||||
|
err := adminCollection.FindOne(ctx, bson.M{"aid": data["aid"]}).Decode(&admin)
|
||||||
|
if err != nil {
|
||||||
|
cancel()
|
||||||
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
|
"success": false,
|
||||||
|
"message": "invalid admin id",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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{
|
||||||
|
"homeroom": data["homeroom"],
|
||||||
|
"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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user