Skip to content

Commit

Permalink
[fix] #80
Browse files Browse the repository at this point in the history
  • Loading branch information
yoneyan committed Mar 22, 2021
1 parent 976d919 commit 0efe803
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 56 deletions.
45 changes: 40 additions & 5 deletions pkg/api/core/notice/v0/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}

Expand Down
66 changes: 66 additions & 0 deletions pkg/api/core/notice/v0/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
}
51 changes: 0 additions & 51 deletions pkg/api/core/notice/v0/update.go

This file was deleted.

2 changes: 2 additions & 0 deletions pkg/api/store/notice/v0/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0efe803

Please sign in to comment.