Skip to content

Commit

Permalink
[support] #55 #70 (通知機能の大幅改修), [fix] #34 #37 #69
Browse files Browse the repository at this point in the history
  • Loading branch information
yoneyan committed Mar 16, 2021
1 parent 490e808 commit a2d218b
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 64 deletions.
22 changes: 12 additions & 10 deletions pkg/api/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ type BGPRouter struct {
gorm.Model
NOCID uint `json:"noc_id"`
NOC NOC `json:"noc"`
Connection []Connection `json:"connection"`
HostName string `json:"hostname"`
Address string `json:"address"`
TunnelRouter []*TunnelEndPointRouter `json:"tunnel_endpoint_router"`
Expand Down Expand Up @@ -275,14 +276,15 @@ type Token struct {

type Notice struct {
gorm.Model
UserID uint `json:"user_id"`
GroupID uint `json:"group_id"`
Everyone *bool `json:"everyone"`
StartTime time.Time `json:"start_time"`
EndingTime time.Time `json:"ending_time"`
Important *bool `json:"important"`
Fault *bool `json:"fault"`
Info *bool `json:"info"`
Title string `json:"title"`
Data string `json:"data"`
UserID uint `json:"user_id"`
GroupID uint `json:"group_id"`
NOCID uint `json:"noc_id"`
Everyone *bool `json:"everyone"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
Important *bool `json:"important"`
Fault *bool `json:"fault"`
Info *bool `json:"info"`
Title string `json:"title"`
Data string `json:"data" gorm:"size:65535"`
}
54 changes: 43 additions & 11 deletions pkg/api/core/notice/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,55 @@ package notice

import (
"github.com/homenoc/dsbd-backend/pkg/api/core"
"time"
)

const (
ID = 0
UserID = 1
GroupID = 2
UserIDAndGroupID = 3
Everyone = 4
GroupData = 5
UserData = 6
Important = 10
Fault = 11
Info = 12
UpdateAll = 150
ID = 0
UIDOrAll = 1
UIDOrGIDOrAll = 2
UIDOrGIDOrNOCAllOrAll = 3
NOCAll = 4
Important = 10
Fault = 11
Info = 12
UpdateAll = 150
)

type Input struct {
UserID uint `json:"user_id"`
GroupID uint `json:"group_id"`
NOCID uint `json:"noc_id"`
Everyone *bool `json:"everyone"`
StartTime string `json:"start_time"`
EndTime *string `json:"end_time"`
Important *bool `json:"important"`
Fault *bool `json:"fault"`
Info *bool `json:"info"`
Title string `json:"title"`
Data string `json:"data"`
}

type Notice struct {
ID uint `json:"ID"`
UserID uint `json:"user_id"`
GroupID uint `json:"group_id"`
NOCID uint `json:"noc_id"`
Everyone bool `json:"everyone"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
Important bool `json:"important"`
Fault bool `json:"fault"`
Info bool `json:"info"`
Title string `json:"title"`
Data string `json:"data" gorm:"size:65535"`
}

type Result struct {
Notice []Notice `json:"notice"`
}

type ResultAdmin struct {
Notice []core.Notice `json:"notice"`
}

Expand Down
36 changes: 31 additions & 5 deletions pkg/api/core/notice/v0/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
"log"
"net/http"
"strconv"
"time"
)

func AddAdmin(c *gin.Context) {
var input core.Notice
var input notice.Input

resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN"))
if resultAdmin.Err != nil {
Expand All @@ -33,7 +34,32 @@ func AddAdmin(c *gin.Context) {
return
}

if _, err = dbNotice.Create(&input); err != nil {
// 時間はJST基準
jst, _ := time.LoadLocation("Asia/Tokyo")

// 2999年12月31日 00:00:00
var endTime = time.Date(2999, time.December, 31, 0, 0, 0, 0, jst)

startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", input.StartTime, jst)
if input.EndTime != nil {
endTime, _ = time.ParseInLocation("2006-01-02 15:04:05", *input.EndTime, jst)
}

noticeSlackAddAdmin(input)

if _, err = dbNotice.Create(&core.Notice{
UserID: input.UserID,
GroupID: input.GroupID,
NOCID: input.NOCID,
Everyone: input.Everyone,
StartTime: startTime,
EndTime: endTime,
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
}
Expand Down Expand Up @@ -81,7 +107,7 @@ func UpdateAdmin(c *gin.Context) {
c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()})
return
}
c.JSON(http.StatusOK, notice.Result{})
c.JSON(http.StatusOK, notice.ResultAdmin{})
}

func GetAdmin(c *gin.Context) {
Expand All @@ -101,7 +127,7 @@ func GetAdmin(c *gin.Context) {
c.JSON(http.StatusInternalServerError, common.Error{Error: result.Err.Error()})
return
}
c.JSON(http.StatusOK, notice.Result{Notice: result.Notice})
c.JSON(http.StatusOK, notice.ResultAdmin{Notice: result.Notice})
}

func GetAllAdmin(c *gin.Context) {
Expand All @@ -114,6 +140,6 @@ func GetAllAdmin(c *gin.Context) {
if result := dbNotice.GetAll(); result.Err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: result.Err.Error()})
} else {
c.JSON(http.StatusOK, notice.Result{Notice: result.Notice})
c.JSON(http.StatusOK, notice.ResultAdmin{Notice: result.Notice})
}
}
4 changes: 2 additions & 2 deletions pkg/api/core/notice/v0/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package v0

import (
"fmt"
"github.com/homenoc/dsbd-backend/pkg/api/core"
"github.com/homenoc/dsbd-backend/pkg/api/core/notice"
)

func check(input core.Notice) error {
func check(input notice.Input) error {
// check
if input.Title == "" {
return fmt.Errorf("no data: title")
Expand Down
82 changes: 73 additions & 9 deletions pkg/api/core/notice/v0/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ import (
"github.com/homenoc/dsbd-backend/pkg/api/core"
auth "github.com/homenoc/dsbd-backend/pkg/api/core/auth/v0"
"github.com/homenoc/dsbd-backend/pkg/api/core/common"
"github.com/homenoc/dsbd-backend/pkg/api/core/group/service"
"github.com/homenoc/dsbd-backend/pkg/api/core/notice"
dbService "github.com/homenoc/dsbd-backend/pkg/api/store/group/service/v0"
dbNotice "github.com/homenoc/dsbd-backend/pkg/api/store/notice/v0"
"log"
"net/http"
"strconv"
)

type noticeHandler struct {
notice []notice.Notice
}

func Get(c *gin.Context) {
userToken := c.Request.Header.Get("USER_TOKEN")
accessToken := c.Request.Header.Get("ACCESS_TOKEN")
Expand All @@ -22,28 +30,84 @@ func Get(c *gin.Context) {
}

var noticeResult notice.ResultDatabase
var responseNotice []notice.Notice

h := noticeHandler{notice: responseNotice}

// Todo #issue #37 Critical
if result.User.GroupID != 0 {
noticeResult = dbNotice.Get(notice.GroupID, &core.Notice{
UserID: result.User.ID,
GroupID: result.User.GroupID,
Everyone: &[]bool{true}[0],
})

serviceResult := dbService.Get(service.Open, &core.Service{GroupID: result.User.GroupID})
if serviceResult.Err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: result.Err.Error()})
return
}

var nocIDs []string

for _, tmpService := range serviceResult.Service {
for _, tmpConnection := range tmpService.Connection {
if tmpConnection.BGPRouter.NOCID != 0 {
if !arrayContains(nocIDs, strconv.Itoa(int(tmpConnection.BGPRouter.NOCID))) {
nocIDs = append(nocIDs, strconv.Itoa(int(tmpConnection.BGPRouter.NOCID)))
}
}
}
}

noticeResult = dbNotice.GetArray(notice.UIDOrGIDOrNOCAllOrAll, &core.Notice{
UserID: result.User.ID,
GroupID: result.User.GroupID,
}, nocIDs)
if noticeResult.Err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: result.Err.Error()})
return
}

log.Println(noticeResult.Notice)

for _, tmpNotice := range noticeResult.Notice {
h.appendNotice(tmpNotice)
}

} else {
noticeResult = dbNotice.Get(notice.UserID, &core.Notice{
UserID: result.User.ID,
Everyone: &[]bool{true}[0],
noticeResult = dbNotice.Get(notice.UIDOrAll, &core.Notice{
UserID: result.User.ID,
})
if noticeResult.Err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: result.Err.Error()})
return
}
for _, tmpNotice := range noticeResult.Notice {
h.appendNotice(tmpNotice)
}
}

c.JSON(http.StatusOK, notice.Result{Notice: noticeResult.Notice})
c.JSON(http.StatusOK, notice.Result{Notice: h.notice})
}

func arrayContains(arr []string, str string) bool {
for _, v := range arr {
if v == str {
return true
}
}
return false
}

func (h *noticeHandler) appendNotice(data core.Notice) {
h.notice = append(h.notice, notice.Notice{
ID: data.ID,
UserID: data.UserID,
GroupID: data.GroupID,
NOCID: data.NOCID,
Everyone: *data.Everyone,
StartTime: data.StartTime,
EndTime: data.EndTime,
Important: *data.Important,
Fault: *data.Fault,
Info: *data.Info,
Title: data.Title,
Data: data.Data,
})
}
24 changes: 24 additions & 0 deletions pkg/api/core/notice/v0/slack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package v0

import (
"github.com/ashwanthkumar/slack-go-webhook"
"github.com/homenoc/dsbd-backend/pkg/api/core/notice"
"github.com/homenoc/dsbd-backend/pkg/api/core/tool/notification"
)

func noticeSlackAddAdmin(input notice.Input) {
// 審査ステータスのSlack通知
attachment := slack.Attachment{}

endTime := "無期限"
if input.EndTime != nil {
endTime = *input.EndTime
}

attachment.AddField(slack.Field{Title: "Title", Value: "通知の追加"}).
AddField(slack.Field{Title: "申請者", Value: "管理者"}).
AddField(slack.Field{Title: "通知時期", Value: input.StartTime + " - " + endTime}).
AddField(slack.Field{Title: "title", Value: input.Title}).
AddField(slack.Field{Title: "data", Value: input.Data})
notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true})
}
4 changes: 2 additions & 2 deletions pkg/api/core/notice/v0/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func updateAdminUser(input, replace core.Notice) core.Notice {
replace.StartTime = input.StartTime
}
//EndTime
if input.EndingTime != replace.EndingTime {
replace.EndingTime = input.EndingTime
if input.EndTime != replace.EndTime {
replace.EndTime = input.EndTime
}
//Everyone
if input.Everyone != replace.Everyone {
Expand Down
Loading

0 comments on commit a2d218b

Please sign in to comment.