From 0a641c3d8b7a392c217281b9d3c87f7fe23c9533 Mon Sep 17 00:00:00 2001 From: AkshatIITk Date: Wed, 28 Aug 2024 21:18:48 +0530 Subject: [PATCH] Intialised mailing system for every action on pvfs --- application/admin.pvf.go | 73 ++++++++++++++++++++-------------- application/router.go | 6 +-- application/verify.pvf.go | 83 +++++++++++++++++++++++++++------------ cmd/main.go | 2 +- cmd/verification.go | 5 ++- 5 files changed, 108 insertions(+), 61 deletions(-) diff --git a/application/admin.pvf.go b/application/admin.pvf.go index 677dcc3..185fe7f 100644 --- a/application/admin.pvf.go +++ b/application/admin.pvf.go @@ -88,8 +88,8 @@ func sendVerificationLinkForPvfHandler(mail_channel chan mail.Mail) gin.HandlerF "https://placement.iitk.ac.in/verify?token=" + token + "&rcid=" + util.ParseString(rid) + "\n\n" + "Your prompt response is appreciated to ensure timely processing of " + pvf.Name + "'s placement applications.\n\n" + "Please note:\n" + - "The PVF verifies the student's involvement and contributions to the project/internship. " + - "Only projects/internships conducted with IIT Kanpur faculty or external organizations require verification. " + + "The PVF verifies the student's involvement and contributions to the project/internship. \n" + + "Only projects/internships conducted with IIT Kanpur faculty or external organizations require verification. \n" + "If you have any questions regarding the PVF process, please don't hesitate to contact the Students' Placement Office at spo@iitk.ac.in.\n\n" + "Thank you for your time and support." @@ -183,39 +183,54 @@ func deletePVFHandler(ctx *gin.Context) { ctx.JSON(http.StatusOK, gin.H{"status": "deleted PVF"}) } -func putPVFHandlerForAdmin(ctx *gin.Context) { - var jp PVF +func putPVFHandlerForAdmin(mail_channel chan mail.Mail) gin.HandlerFunc { + return func(ctx *gin.Context) { + var pvf PVF - rid, err := util.ParseUint(ctx.Param("rid")) - if err != nil { - ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } + // rid, err := util.ParseUint(ctx.Param("rid")) + // if err != nil { + // ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + // return + // } - err = ctx.ShouldBindJSON(&jp) - if err != nil { - ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } + err := ctx.ShouldBindJSON(&pvf) + if err != nil { + ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } - if jp.ID == 0 { - ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "id is required"}) - return - } + if pvf.ID == 0 { + ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "id is required"}) + return + } - var oldJp PVF - err = fetchPvfForAdmin(ctx, rid, jp.ID, &oldJp) - if err != nil { - ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } + var oldPvf PVF - err = updatePVF(ctx, &jp) - if err != nil { - ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return + // err = fetchPvfForAdmin(ctx, rid, oldPvf.ID, &oldPvf) + err = fetchPVF(ctx, pvf.ID, &oldPvf) + if err != nil { + ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + err = updatePVF(ctx, &pvf) + if err != nil { + ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + message := "Dear " + oldPvf.Name + ",\n\n" + + + "Action has been taken on your Project Verification Form. Kindly check the status on the RAS Portal." + + "\n\n" + + "Regards,\n" + + "Students' Placement Team, IIT kanpur" + // logrus.Infof("EmaIL : %s OR %s", pvf.IITKEmail, oldPvf.IITKEmail) + mail_channel <- mail.GenerateMail(oldPvf.IITKEmail, + "Project Verification Update for "+oldPvf.Name+"'s Internship/Project", + message, + ) + ctx.JSON(http.StatusOK, gin.H{"status": "Updated PVF with id " + util.ParseString(pvf.ID)}) } - ctx.JSON(http.StatusOK, gin.H{"status": "Updated PVF with id " + util.ParseString(jp.ID)}) } func getAllStudentPvfHandler(ctx *gin.Context) { diff --git a/application/router.go b/application/router.go index 5d818c1..337b808 100644 --- a/application/router.go +++ b/application/router.go @@ -19,7 +19,7 @@ func AdminRouter(mail_channel chan mail.Mail, r *gin.Engine) { admin.DELETE("event/:eid/student/:sid", deleteStudentFromEventHandler) admin.GET("/pvf", getAllPvfForAdminHandler) - admin.PUT("/pvf", putPVFHandlerForAdmin) + admin.PUT("/pvf", putPVFHandlerForAdmin(mail_channel)) admin.GET("/pvf/:pid", getPvfForAdminHandler) admin.GET("pvf/:pid/verification/send", sendVerificationLinkForPvfHandler(mail_channel)) admin.GET("pvf/student/:sid/verification/send", sendVerificationLinkForStudentAllPvfHandler(mail_channel)) @@ -113,12 +113,12 @@ func CompanyRouter(r *gin.Engine) { } } -func PvfVerificationRouter(r *gin.Engine) { +func PvfVerificationRouter(mail_channel chan mail.Mail, r *gin.Engine) { pvf := r.Group("/api/verification") pvf.Use() { pvf.GET("/pvf", getPvfForVerificationHandler) // pvf.PUT("pvf/:pid/verify", verifyPvfHandler) - pvf.PUT("/pvf", putPVFHandler) + pvf.PUT("/pvf", putPVFHandler(mail_channel)) } } diff --git a/application/verify.pvf.go b/application/verify.pvf.go index f5e4795..c5adf04 100644 --- a/application/verify.pvf.go +++ b/application/verify.pvf.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/spo-iitk/ras-backend/mail" "github.com/spo-iitk/ras-backend/middleware" "github.com/spo-iitk/ras-backend/util" ) @@ -33,38 +34,68 @@ func getPvfForVerificationHandler(ctx *gin.Context) { ctx.JSON(http.StatusOK, jps) } -func putPVFHandler(ctx *gin.Context) { - var jp PVF +func putPVFHandler(mail_channel chan mail.Mail) gin.HandlerFunc { + return func(ctx *gin.Context) { + var pvf PVF - err := ctx.ShouldBindJSON(&jp) - if err != nil { - ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } - pid := middleware.GetPVFID(ctx) + err := ctx.ShouldBindJSON(&pvf) + if err != nil { + ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + pid := middleware.GetPVFID(ctx) - jp.ID = pid + pvf.ID = pid - if jp.ID == 0 { - ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "id is required"}) - return - } + if pvf.ID == 0 { + ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "id is required"}) + return + } - var oldJp PVF - err = fetchPVF(ctx, jp.ID, &oldJp) - if err != nil { - ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return - } + var oldJp PVF + err = fetchPVF(ctx, pvf.ID, &oldJp) + if err != nil { + ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } - // jp.ActionTakenBy = middleware.GetUserID(ctx) + // jp.ActionTakenBy = middleware.GetUserID(ctx) - // publishNotice := oldJp.Deadline == 0 && jp.Deadline != 0 + // publishNotice := oldJp.Deadline == 0 && jp.Deadline != 0 + + err = updatePVF(ctx, &pvf) + if err != nil { + ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + var action string + if pvf.IsVerified.Bool { + action = "APPROVED" + } else { + action = "DENIED" + } + messageMentor := "Dear " + oldJp.MentorName + ",\n\n" + + "The action " + action + " on the Project Verification Form of the " + oldJp.Name + " has been taken.\n\n" + + "If you have not done this action please reach out to spo@iitk.ac.in\n\n" + + "Regards,\n" + + "Students' Placement Team, IIT kanpur" + + mail_channel <- mail.GenerateMail(oldJp.MentorEmail, + "Project Verification Update for "+oldJp.Name+"'s Internship/Project", + messageMentor, + ) + messageStudent := "Dear " + oldJp.Name + ",\n\n" + + + "Action has been taken on your Project Verification Form. Kindly check the status on the RAS Portal." + + "\n\n" + + "Regards,\n" + + "Students' Placement Team, IIT kanpur" + + mail_channel <- mail.GenerateMail(oldJp.IITKEmail, + "Project Verification Update for "+oldJp.Name+"'s Internship/Project", + messageStudent, + ) + ctx.JSON(http.StatusOK, gin.H{"status": "Updated PVF with id " + util.ParseString(pvf.ID)}) - err = updatePVF(ctx, &jp) - if err != nil { - ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) - return } - ctx.JSON(http.StatusOK, gin.H{"status": "Updated PVF with id " + util.ParseString(jp.ID)}) } diff --git a/cmd/main.go b/cmd/main.go index 40d77cc..95ae3e3 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -55,7 +55,7 @@ func main() { return adminCompanyServer().ListenAndServe() }) g.Go(func() error { - return verificationServer().ListenAndServe() + return verificationServer(mail_channel).ListenAndServe() }) log.Println("Starting Server...") diff --git a/cmd/verification.go b/cmd/verification.go index 58c6f5c..1cca3a1 100644 --- a/cmd/verification.go +++ b/cmd/verification.go @@ -7,10 +7,11 @@ import ( "github.com/gin-gonic/gin" "github.com/spf13/viper" "github.com/spo-iitk/ras-backend/application" + "github.com/spo-iitk/ras-backend/mail" "github.com/spo-iitk/ras-backend/middleware" ) -func verificationServer() *http.Server { +func verificationServer(mail_channel chan mail.Mail) *http.Server { PORT := viper.GetString("PORT.VERIFICATION") fmt.Print(PORT) engine := gin.New() @@ -19,7 +20,7 @@ func verificationServer() *http.Server { engine.Use(gin.CustomRecovery(recoveryHandler)) engine.Use(gin.Logger()) - application.PvfVerificationRouter(engine) + application.PvfVerificationRouter(mail_channel, engine) server := &http.Server{ Addr: ":" + PORT,