From a2d218b3b5ad616c01e04214e1eea92fe0d0128f Mon Sep 17 00:00:00 2001 From: yoneyan Date: Wed, 17 Mar 2021 05:25:40 +0900 Subject: [PATCH] =?UTF-8?q?[support]=20#55=20#70=20(=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=A9=9F=E8=83=BD=E3=81=AE=E5=A4=A7=E5=B9=85=E6=94=B9=E4=BF=AE?= =?UTF-8?q?),=20[fix]=20#34=20#37=20#69?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/api/core/interface.go | 22 +++++---- pkg/api/core/notice/interface.go | 54 +++++++++++++++----- pkg/api/core/notice/v0/admin.go | 36 ++++++++++++-- pkg/api/core/notice/v0/check.go | 4 +- pkg/api/core/notice/v0/notice.go | 82 +++++++++++++++++++++++++++---- pkg/api/core/notice/v0/slack.go | 24 +++++++++ pkg/api/core/notice/v0/update.go | 4 +- pkg/api/store/notice/v0/notice.go | 78 +++++++++++++++++++---------- 8 files changed, 240 insertions(+), 64 deletions(-) create mode 100644 pkg/api/core/notice/v0/slack.go diff --git a/pkg/api/core/interface.go b/pkg/api/core/interface.go index 3337ac8f..3cbd8ccf 100644 --- a/pkg/api/core/interface.go +++ b/pkg/api/core/interface.go @@ -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"` @@ -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"` } diff --git a/pkg/api/core/notice/interface.go b/pkg/api/core/notice/interface.go index 84f6419b..1247a9a2 100644 --- a/pkg/api/core/notice/interface.go +++ b/pkg/api/core/notice/interface.go @@ -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"` } diff --git a/pkg/api/core/notice/v0/admin.go b/pkg/api/core/notice/v0/admin.go index 2eb2b965..699b3023 100644 --- a/pkg/api/core/notice/v0/admin.go +++ b/pkg/api/core/notice/v0/admin.go @@ -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 { @@ -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 } @@ -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) { @@ -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) { @@ -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}) } } diff --git a/pkg/api/core/notice/v0/check.go b/pkg/api/core/notice/v0/check.go index 094b5162..199cdfc0 100644 --- a/pkg/api/core/notice/v0/check.go +++ b/pkg/api/core/notice/v0/check.go @@ -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") diff --git a/pkg/api/core/notice/v0/notice.go b/pkg/api/core/notice/v0/notice.go index 4f2e1280..044988c8 100644 --- a/pkg/api/core/notice/v0/notice.go +++ b/pkg/api/core/notice/v0/notice.go @@ -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") @@ -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, + }) } diff --git a/pkg/api/core/notice/v0/slack.go b/pkg/api/core/notice/v0/slack.go new file mode 100644 index 00000000..b9dfa605 --- /dev/null +++ b/pkg/api/core/notice/v0/slack.go @@ -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}) +} diff --git a/pkg/api/core/notice/v0/update.go b/pkg/api/core/notice/v0/update.go index feef7dff..531a4d3d 100644 --- a/pkg/api/core/notice/v0/update.go +++ b/pkg/api/core/notice/v0/update.go @@ -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 { diff --git a/pkg/api/store/notice/v0/notice.go b/pkg/api/store/notice/v0/notice.go index d2aafa57..46131629 100644 --- a/pkg/api/store/notice/v0/notice.go +++ b/pkg/api/store/notice/v0/notice.go @@ -45,15 +45,15 @@ func Update(base int, data core.Notice) error { if notice.UpdateAll == base { result = db.Model(&core.Notice{Model: gorm.Model{ID: data.ID}}).Update(core.Notice{ - UserID: data.UserID, - GroupID: data.GroupID, - StartTime: data.StartTime, - EndingTime: data.EndingTime, - Important: data.Important, - Fault: data.Fault, - Info: data.Info, - Title: data.Title, - Data: data.Data, + UserID: data.UserID, + GroupID: data.GroupID, + StartTime: data.StartTime, + EndTime: data.EndTime, + Important: data.Important, + Fault: data.Fault, + Info: data.Info, + Title: data.Title, + Data: data.Data, }) } else { log.Println("base select error") @@ -72,27 +72,29 @@ func Get(base int, data *core.Notice) notice.ResultDatabase { var noticeStruct []core.Notice - dateTime := time.Now().Unix() + dateTime := time.Now() if base == notice.ID { //ID err = db.First(¬iceStruct, data.ID).Error - } else if base == notice.UserID { //UserID - err = db.Where("user_id = ?", data.UserID).Find(¬iceStruct).Error - } else if base == notice.GroupID { //GroupID - err = db.Where("group_id = ?", data.GroupID).Find(¬iceStruct).Error - } else if base == notice.UserIDAndGroupID { //UserID And GroupID - err = db.Where("user_id = ? AND group_id = ?", data.UserID, data.GroupID).Find(¬iceStruct).Error - } else if base == notice.GroupData { //GroupData - err = db.Where("everyone = ? AND start_time < ? AND ? < ending_time ", data.Everyone, dateTime, dateTime). - Or("user_id = ? AND group_id = ? AND start_time < ? AND ? < ending_time", data.UserID, data.GroupID, dateTime, dateTime). - Or("group_id = ? AND start_time < ? AND ? < ending_time", data.GroupID, dateTime, dateTime). + } else if base == notice.UIDOrAll { //UserID Or All + err = db.Where("user_id = ? AND start_time < ? AND ? < end_time", data.UserID, dateTime, dateTime). + Or("everyone = ? AND start_time < ? AND ? < end_time", true, dateTime, dateTime). Order("id asc").Find(¬iceStruct).Error - } else if base == notice.UserData { //UserData - err = db.Where("everyone = ? AND start_time < ? AND ? < ending_time ", data.Everyone, dateTime, dateTime). - Or("user_id = ? AND start_time < ? AND ? < ending_time", data.UserID, dateTime, dateTime). + } else if base == notice.UIDOrGIDOrAll { //UserID Or GroupID Or All + err = db.Where("user_id = ? AND start_time < ? AND ? < end_time", data.UserID, dateTime, dateTime). + Or("group_id = ? AND start_time < ? AND ? < end_time", data.GroupID, dateTime, dateTime). + Or("everyone = ? AND start_time < ? AND ? < end_time", true, dateTime, dateTime). + Order("id asc").Find(¬iceStruct).Error + } else if base == notice.UIDOrGIDOrNOCAllOrAll { //UserID Or GroupID Or NOCAll Or All + err = db.Where("user_id = ? AND start_time < ? AND ? < end_time", data.UserID, dateTime, dateTime). + Or("group_id = ? AND start_time < ? AND ? < end_time", data.GroupID, dateTime, dateTime). + Or("everyone = ? AND start_time < ? AND ? < end_time", true, dateTime, dateTime). + Or("noc_id != ? AND start_time < ? AND ? < end_time", 0, dateTime, dateTime). + Order("id asc").Find(¬iceStruct).Error + } else if base == notice.NOCAll { //UserID Or GroupID Or NOCAll Or All + err = db.Where("user_id = ? AND user_id = ? AND noc_id != ? AND start_time < ? AND ? < end_time ", + 0, 0, 0, dateTime, dateTime). Order("id asc").Find(¬iceStruct).Error - } else if base == notice.Everyone { //Everyone - err = db.Where("everyone = ?", data.Everyone).Find(¬iceStruct).Error } else if base == notice.Important { //Important err = db.Where("important = ?", data.Important).Find(¬iceStruct).Error } else if base == notice.Fault { //Fault @@ -106,6 +108,32 @@ func Get(base int, data *core.Notice) notice.ResultDatabase { return notice.ResultDatabase{Notice: noticeStruct, Err: err} } +func GetArray(base int, data *core.Notice, array []string) notice.ResultDatabase { + db, err := store.ConnectDB() + if err != nil { + log.Println("database connection error") + return notice.ResultDatabase{Err: fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error())} + } + defer db.Close() + + var noticeStruct []core.Notice + + dateTime := time.Now() + + if base == notice.UIDOrGIDOrNOCAllOrAll { //UserID Or GroupID Or NOCAll Or All + err = db.Where("user_id = ? AND start_time < ? AND ? < end_time ", data.UserID, dateTime, dateTime). + Or("group_id = ? AND start_time < ? AND ? < end_time", data.GroupID, dateTime, dateTime). + Or("everyone = ? AND start_time < ? AND ? < end_time", true, dateTime, dateTime). + Or("noc_id IN (?) AND start_time < ? AND ? < end_time", array, dateTime, dateTime). + Order("id asc"). + Find(¬iceStruct).Error + } else { + log.Println("base select error") + return notice.ResultDatabase{Err: fmt.Errorf("(%s)error: base select\n", time.Now())} + } + return notice.ResultDatabase{Notice: noticeStruct, Err: err} +} + func GetAll() notice.ResultDatabase { db, err := store.ConnectDB() if err != nil {