Skip to content

Commit

Permalink
Publish application due time to calender
Browse files Browse the repository at this point in the history
  • Loading branch information
1-Harshit committed Sep 10, 2022
1 parent 34ece19 commit 0bef4f1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 23 deletions.
27 changes: 8 additions & 19 deletions application/admin.events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"github.com/spo-iitk/ras-backend/rc"
"github.com/spo-iitk/ras-backend/util"
)
Expand Down Expand Up @@ -105,7 +104,7 @@ func putEventHandler(ctx *gin.Context) {
return
}

if event.StartTime == 0 && event.EndTime == 0 {
if (event.StartTime == 0 && event.EndTime == 0) || event.Name == string(ApplicationSubmitted) || event.Name == string(Recruited) || event.Name == string(PIOPPOACCEPTED) {
ctx.JSON(http.StatusOK, event)
return
}
Expand Down Expand Up @@ -136,17 +135,10 @@ func putEventHandler(ctx *gin.Context) {
rc.CreateNotice(ctx, rid, &notice)
}

var cID string
if proforma.RecruitmentCycleID == 1 {
cID = viper.GetString("CALENDAR.CID1")
}

if proforma.RecruitmentCycleID == 2 {
cID = viper.GetString("CALENDAR.CID2")
}
cID := getCalenderID(proforma.RecruitmentCycleID)

if cID == "" {
ctx.JSON(http.StatusNotImplemented, gin.H{"error": "Please as web head to generate a new calender in admin.events:144"})
ctx.JSON(http.StatusNotImplemented, gin.H{"error": "Please as web head to generate a new calender in admin.events:218"})
return
}

Expand All @@ -169,16 +161,13 @@ func deleteEventHandler(ctx *gin.Context) {
return
}

rid := ctx.Param("rid")

var cID string
if rid == "1" {
cID = viper.GetString("CALENDAR.CID1")
rid, err := util.ParseUint(ctx.Param("rid"))
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

if rid == "2" {
cID = viper.GetString("CALENDAR.CID2")
}
cID := getCalenderID(rid)

if cID == "" {
ctx.JSON(http.StatusNotImplemented, gin.H{"error": "Please as web head to generate a new calender in admin.events:218"})
Expand Down
15 changes: 11 additions & 4 deletions application/model.hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ func (jp *Proforma) AfterUpdate(tx *gorm.DB) (err error) {
if jp.IsApproved.Valid && jp.IsApproved.Bool {
event := ProformaEvent{
ProformaID: jp.ID,
Name: string(ApplicationSubmitted),
Name: string(Recruited),
Duration: "-",
StartTime: 0,
EndTime: 0,
Sequence: 0,
Sequence: 1000,
RecordAttendance: false,
}

Expand All @@ -25,15 +25,22 @@ func (jp *Proforma) AfterUpdate(tx *gorm.DB) (err error) {

event = ProformaEvent{
ProformaID: jp.ID,
Name: string(Recruited),
Name: string(ApplicationSubmitted),
Duration: "-",
StartTime: 0,
EndTime: 0,
Sequence: 1000,
Sequence: 0,
RecordAttendance: false,
}

err = tx.Where("proforma_id = ? AND name = ?", event.ProformaID, event.Name).FirstOrCreate(&event).Error
if err != nil {
return
}

if jp.Deadline > 0 {
go insertCalenderApplicationDeadline(jp, &event)
}
}
return
}
Expand Down
54 changes: 54 additions & 0 deletions application/util.calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"google.golang.org/api/calendar/v3"
)

Expand Down Expand Up @@ -50,3 +51,56 @@ func insertCalenderEvent(event *ProformaEvent, proforma *Proforma, loc *time.Loc
logrus.Errorf("Unable to update event. %v", err)
}
}

func insertCalenderApplicationDeadline(proforma *Proforma, event *ProformaEvent) {
time_zone := "Asia/Kolkata"
loc, _ := time.LoadLocation(time_zone)

cevent := &calendar.Event{
Summary: fmt.Sprintf("Application Deadline: %s - %s", proforma.Profile, proforma.CompanyName),
Location: "Recruitment Automation System",
Description: fmt.Sprintf(
"A new opening has been created for the profile of %s in the company %s. Application is due %s\nhttps://placement.iitk.ac.in/student/rc/%d/proforma/%d",
proforma.Profile, proforma.CompanyName,
time.UnixMilli(int64(proforma.Deadline)).In(loc).Format("2006-01-02 15:04"),
proforma.RecruitmentCycleID, proforma.ID),
Start: &calendar.EventDateTime{
DateTime: time.UnixMilli(int64(proforma.Deadline)).In(loc).Format(time.RFC3339),
TimeZone: time_zone,
},
End: &calendar.EventDateTime{
DateTime: time.UnixMilli(int64(proforma.Deadline)).In(loc).Format(time.RFC3339),
TimeZone: time_zone,
},
}

cID := getCalenderID(proforma.RecruitmentCycleID)

if event.CalID == "" {
cevent, err := cal_srv.Events.Insert(cID, cevent).Do()
if err != nil {
logrus.Errorf("Unable to create event. %v", err)
}

event.CalID = cevent.Id
updateEvent(nil, event)
}

_, err := cal_srv.Events.Update(cID, event.CalID, cevent).Do()
if err != nil {
logrus.Errorf("Unable to update event. %v", err)
}
}

func getCalenderID(rid uint) (cID string) {

if rid == 1 {
cID = viper.GetString("CALENDAR.CID1")
}

if rid == 2 {
cID = viper.GetString("CALENDAR.CID2")
}

return
}

0 comments on commit 0bef4f1

Please sign in to comment.