diff --git a/pkg/api/core/notice/v0/admin.go b/pkg/api/core/notice/v0/admin.go index f6854860..b32a0ba9 100644 --- a/pkg/api/core/notice/v0/admin.go +++ b/pkg/api/core/notice/v0/admin.go @@ -87,26 +87,61 @@ func DeleteAdmin(c *gin.Context) { } func UpdateAdmin(c *gin.Context) { - var input core.Notice + var input notice.Input resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN")) if resultAdmin.Err != nil { c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()}) return } - err := c.BindJSON(&input) - log.Println(err) - tmp := dbNotice.Get(notice.ID, &core.Notice{Model: gorm.Model{ID: input.ID}}) + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + if err = c.BindJSON(&input); err != nil { + log.Println(err) + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + // 時間はJST基準 + jst, _ := time.LoadLocation("Asia/Tokyo") + + startTime, _ := time.ParseInLocation(layoutInput, input.StartTime, jst) + endTime, _ := time.ParseInLocation(layoutInput, *input.EndTime, jst) + + tmp := dbNotice.Get(notice.ID, &core.Notice{Model: gorm.Model{ID: uint(id)}}) if tmp.Err != nil { c.JSON(http.StatusInternalServerError, common.Error{Error: tmp.Err.Error()}) return } - if err = dbNotice.Update(notice.UpdateAll, updateAdminUser(input, tmp.Notice[0])); err != nil { + log.Println(startTime) + log.Println(endTime) + + noticeSlackReplaceAdmin(tmp.Notice[0], input) + + if err = dbNotice.Update(notice.UpdateAll, core.Notice{ + Model: gorm.Model{ID: uint(id)}, + UserID: input.UserID, + GroupID: input.GroupID, + NOCID: input.NOCID, + StartTime: startTime, + EndTime: endTime, + Everyone: input.Everyone, + Important: input.Important, + Fault: input.Fault, + Info: input.Info, + Title: input.Title, + Data: input.Data, + }); err != nil { c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) return } + c.JSON(http.StatusOK, notice.ResultAdmin{}) } diff --git a/pkg/api/core/notice/v0/slack.go b/pkg/api/core/notice/v0/slack.go index 1111acf2..e48f30f8 100644 --- a/pkg/api/core/notice/v0/slack.go +++ b/pkg/api/core/notice/v0/slack.go @@ -2,10 +2,15 @@ package v0 import ( "github.com/ashwanthkumar/slack-go-webhook" + "github.com/homenoc/dsbd-backend/pkg/api/core" "github.com/homenoc/dsbd-backend/pkg/api/core/notice" "github.com/homenoc/dsbd-backend/pkg/api/core/tool/notification" + "strconv" + "time" ) +const layoutInput = "2006-01-02 15:04:05" + func noticeSlackAddAdmin(input notice.Input) { // 審査ステータスのSlack通知 attachment := slack.Attachment{} @@ -22,3 +27,64 @@ func noticeSlackAddAdmin(input notice.Input) { AddField(slack.Field{Title: "data", Value: input.Data}) notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true}) } + +func noticeSlackReplaceAdmin(before core.Notice, after notice.Input) { + // 審査ステータスのSlack通知 + attachment := slack.Attachment{} + + attachment.AddField(slack.Field{Title: "Title", Value: "通知の変更"}). + AddField(slack.Field{Title: "申請者", Value: "管理者"}). + AddField(slack.Field{Title: "更新状況", Value: changeText(before, after)}) + notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true}) +} + +func changeText(before core.Notice, after notice.Input) string { + data := "" + //Title + if after.Title != "" && after.Title != before.Title { + data += "Title: " + before.Title + " => " + after.Title + "\n" + } + + //Data + if after.Data != "" && after.Data != before.Data { + data += "Contents: " + before.Data + " => " + after.Data + "\n" + } + + if after.UserID != before.UserID { + data += "UserID: " + strconv.Itoa(int(before.UserID)) + " => " + strconv.Itoa(int(after.UserID)) + "\n" + } + + if after.GroupID != before.GroupID { + data += "GroupID: " + strconv.Itoa(int(before.UserID)) + " => " + strconv.Itoa(int(after.UserID)) + "\n" + } + + if after.NOCID != before.NOCID { + data += "NOCID: " + strconv.Itoa(int(before.NOCID)) + " => " + strconv.Itoa(int(after.NOCID)) + "\n" + } + + if after.StartTime != before.StartTime.Add(9*time.Hour).Format(layoutInput) { + data += "Start Time: " + before.StartTime.Add(9*time.Hour).Format(layoutInput) + " => " + after.StartTime + "\n" + } + + if *after.EndTime != before.EndTime.Add(9*time.Hour).Format(layoutInput) { + data += "End Time: " + before.EndTime.Add(9*time.Hour).Format(layoutInput) + " => " + *after.EndTime + "\n" + } + + if *after.Everyone != *before.Everyone { + data += "EveryOne: " + strconv.FormatBool(*before.Everyone) + " => " + strconv.FormatBool(*after.Everyone) + "\n" + } + + if *after.Info != *before.Info { + data += "Info: " + strconv.FormatBool(*before.Info) + " => " + strconv.FormatBool(*after.Info) + "\n" + } + + if *after.Fault != *before.Fault { + data += "Fault: " + strconv.FormatBool(*before.Fault) + " => " + strconv.FormatBool(*after.Fault) + "\n" + } + + if *after.Important != *before.Important { + data += "Important: " + strconv.FormatBool(*before.Important) + " => " + strconv.FormatBool(*after.Important) + "\n" + } + + return data +} diff --git a/pkg/api/core/notice/v0/update.go b/pkg/api/core/notice/v0/update.go deleted file mode 100644 index 531a4d3d..00000000 --- a/pkg/api/core/notice/v0/update.go +++ /dev/null @@ -1,51 +0,0 @@ -package v0 - -import "github.com/homenoc/dsbd-backend/pkg/api/core" - -func updateAdminUser(input, replace core.Notice) core.Notice { - - //Title - if input.Title != "" { - replace.Title = input.Title - } - //Data - if input.Data != "" { - replace.Data = input.Data - } - - // uint boolean - //UserID - if input.UserID != replace.UserID { - replace.UserID = input.UserID - } - //GroupID - if input.GroupID != replace.GroupID { - replace.GroupID = input.GroupID - } - //StartTime - if input.StartTime != replace.StartTime { - replace.StartTime = input.StartTime - } - //EndTime - if input.EndTime != replace.EndTime { - replace.EndTime = input.EndTime - } - //Everyone - if input.Everyone != replace.Everyone { - replace.Everyone = input.Everyone - } - //Important - if input.Important != replace.Important { - replace.Important = input.Important - } - //Fault - if input.Fault != replace.Fault { - replace.Fault = input.Fault - } - //Info - if input.Info != replace.Info { - replace.Info = input.Info - } - - return replace -} diff --git a/pkg/api/store/notice/v0/notice.go b/pkg/api/store/notice/v0/notice.go index 56349a5e..0197dba3 100644 --- a/pkg/api/store/notice/v0/notice.go +++ b/pkg/api/store/notice/v0/notice.go @@ -51,9 +51,11 @@ func Update(base int, data core.Notice) error { result = db.Model(&core.Notice{Model: gorm.Model{ID: data.ID}}).Update(core.Notice{ UserID: data.UserID, GroupID: data.GroupID, + NOCID: data.NOCID, StartTime: data.StartTime, EndTime: data.EndTime, Important: data.Important, + Everyone: data.Everyone, Fault: data.Fault, Info: data.Info, Title: data.Title,