diff --git a/pkg/api/api.go b/pkg/api/api.go index 87ce2b26..ac0d1cf7 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -151,30 +151,40 @@ func AdminRestAPI() { v1.PUT("/service/:id", service.UpdateAdmin) v1.GET("/service", service.GetAllAdmin) v1.GET("/service/:id", service.GetAdmin) - v1.PUT("/service/:id/ip/:ip_id", service.UpdateIP) + + // + // JPNIC Admin + // + v1.POST("/service/:id/jpnic_admin", service.AddJPNICAdminAdmin) + v1.DELETE("/service/:id/jpnic_admin/:jpnic_id", service.DeleteJPNICAdminAdmin) + v1.PUT("/service/:id/jpnic_admin", service.UpdateJPNICAdminAdmin) + + // + // JPNIC Admin + // + v1.POST("/service/:id/jpnic_tech", service.AddJPNICTechAdmin) + v1.DELETE("/service/:id/jpnic_tech/:jpnic_id", service.DeleteJPNICTechAdmin) + v1.PUT("/service/:id/jpnic_tech/:jpnic_id", service.UpdateJPNICTechAdmin) // // IP // + v1.PUT("/service/:id/ip/:ip_id", service.UpdateIPAdmin) + v1.POST("/service/:id/ip", service.AddIPAdmin) + v1.DELETE("/service/:id/ip/:ip_id", service.DeleteIPAdmin) v1.PUT("/ip/:id", ip.UpdateAdmin) // - // IP + // Plan // - v1.POST("/mail", mail.SendAdmin) + v1.POST("/plan", service.AddPlanAdmin) + v1.DELETE("/plan/:id", service.DeletePlanAdmin) + v1.PUT("/plan/:id", service.UpdatePlanAdmin) - //// - //// JPNIC Admin - //// - //v1.POST("/group/network/jpnic", jpnicAdmin.AddAdmin) - //v1.DELETE("/group/network/jpnic", jpnicAdmin.DeleteAdmin) - //v1.GET("/group/network/jpnic", jpnicAdmin.GetAdmin) - //// - //// JPNIC Admin - //// - //v1.POST("/group/network/jpnic", jpnicTech.AddAdmin) - //v1.DELETE("/group/network/jpnic", jpnicTech.DeleteAdmin) - //v1.GET("/group/network/jpnic", jpnicTech.GetAdmin) + // + // Mail + // + v1.POST("/mail", mail.SendAdmin) } } ws := router.Group("/ws") diff --git a/pkg/api/core/group/service/jpnicAdmin/v0/slack.go b/pkg/api/core/group/service/jpnicAdmin/v0/slack.go new file mode 100644 index 00000000..99dd5c34 --- /dev/null +++ b/pkg/api/core/group/service/jpnicAdmin/v0/slack.go @@ -0,0 +1,129 @@ +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/group" + "github.com/homenoc/dsbd-backend/pkg/api/core/tool/notification" + "strconv" +) + +func noticeSlackAddAdmin(before, after core.Group) { + // 審査ステータスのSlack通知 + attachment := slack.Attachment{} + + attachment.AddField(slack.Field{Title: "Title", Value: "Group情報の更新"}). + AddField(slack.Field{Title: "申請者", Value: "管理者"}). + AddField(slack.Field{Title: "Group", Value: strconv.Itoa(int(before.ID)) + ":" + before.Org}). + AddField(slack.Field{Title: "更新状況", Value: changeTextAdmin(before, after)}) + notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true}) +} + +func noticeSlackUpdateAdmin(before, after core.Group) { + // 審査ステータスのSlack通知 + attachment := slack.Attachment{} + + attachment.AddField(slack.Field{Title: "Title", Value: "Group情報の更新"}). + AddField(slack.Field{Title: "申請者", Value: "管理者"}). + AddField(slack.Field{Title: "Group", Value: strconv.Itoa(int(before.ID)) + ":" + before.Org}). + AddField(slack.Field{Title: "更新状況", Value: changeTextAdmin(before, after)}) + notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true}) +} + +func addText(before core.Group, after group.Input) string { + data := "" + + if after.Org != "" && after.Org != before.Org { + data += "Org: " + before.Org + "=>" + after.Org + "\n" + } + + if after.OrgEn != "" && after.OrgEn != before.OrgEn { + data += "Org(En): " + before.OrgEn + "=>" + after.OrgEn + "\n" + } + + if after.PostCode != "" && after.PostCode != before.PostCode { + data += "PostCode: " + before.PostCode + "=>" + after.PostCode + "\n" + } + + if after.Address != "" && after.Address != before.Address { + data += "Address: " + before.Address + "=>" + after.Address + "\n" + } + + if after.AddressEn != "" && after.AddressEn != before.AddressEn { + data += "Address(En): " + before.AddressEn + "=>" + after.AddressEn + "\n" + } + + if after.Tel != "" && after.Tel != before.Tel { + data += "Tel: " + before.Tel + "=>" + after.Tel + "\n" + } + + if after.Country != "" && after.Country != before.Country { + data += "Country: " + before.Country + "=>" + after.Country + "\n" + } + + return data +} + +func changeTextAdmin(before, after core.Group) string { + data := "" + + if after.Org != "" && after.Org != before.Org { + data += "Org: " + before.Org + "=>" + after.Org + "\n" + } + + if after.OrgEn != "" && after.OrgEn != before.OrgEn { + data += "Org(En): " + before.OrgEn + "=>" + after.OrgEn + "\n" + } + + if after.PostCode != "" && after.PostCode != before.PostCode { + data += "PostCode: " + before.PostCode + "=>" + after.PostCode + "\n" + } + + if after.Address != "" && after.Address != before.Address { + data += "Address: " + before.Address + "=>" + after.Address + "\n" + } + + if after.AddressEn != "" && after.AddressEn != before.AddressEn { + data += "Address(En): " + before.AddressEn + "=>" + after.AddressEn + "\n" + } + + if after.Tel != "" && after.Tel != before.Tel { + data += "Tel: " + before.Tel + "=>" + after.Tel + "\n" + } + + if after.Country != "" && after.Country != before.Country { + data += "Country: " + before.Country + "=>" + after.Country + "\n" + } + + return data +} + +func expiredStatusText(status uint) string { + if status == 0 { + return "0" + } else if status == 1 { + return "ユーザより廃止" + } else if status == 2 { + return "運営委員より廃止" + } else if status == 3 { + return "審査落ち" + } else { + return "status不明" + } +} + +func statusText(status uint) string { + if status == 0 { + return "0" + } else if status == 1 { + return "ネットワーク情報 記入段階" + } else if status == 2 { + return "審査中" + } else if status == 3 { + return "接続情報 記入段階" + } else if status == 4 { + return "開通作業中" + } else { + return "status不明" + } +} diff --git a/pkg/api/core/group/service/jpnicTech/v0/slack.go b/pkg/api/core/group/service/jpnicTech/v0/slack.go new file mode 100644 index 00000000..bb9301ac --- /dev/null +++ b/pkg/api/core/group/service/jpnicTech/v0/slack.go @@ -0,0 +1,171 @@ +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/group" + "github.com/homenoc/dsbd-backend/pkg/api/core/tool/notification" + "strconv" +) + +func noticeSlack(loginUser core.User, before core.Group, after group.Input) { + // 審査ステータスのSlack通知 + attachment := slack.Attachment{} + + attachment.AddField(slack.Field{Title: "Title", Value: "Group情報の更新"}). + AddField(slack.Field{Title: "申請者", Value: strconv.Itoa(int(loginUser.ID)) + "-" + loginUser.Name}). + AddField(slack.Field{Title: "Group", Value: strconv.Itoa(int(before.ID)) + ":" + before.Org}). + AddField(slack.Field{Title: "更新状況", Value: changeText(before, after)}) + notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true}) +} + +func noticeSlackAdmin(before, after core.Group) { + // 審査ステータスのSlack通知 + attachment := slack.Attachment{} + + attachment.AddField(slack.Field{Title: "Title", Value: "Group情報の更新"}). + AddField(slack.Field{Title: "申請者", Value: "管理者"}). + AddField(slack.Field{Title: "Group", Value: strconv.Itoa(int(before.ID)) + ":" + before.Org}). + AddField(slack.Field{Title: "更新状況", Value: changeTextAdmin(before, after)}) + notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true}) +} + +func changeText(before core.Group, after group.Input) string { + data := "" + + if after.Org != "" && after.Org != before.Org { + data += "Org: " + before.Org + "=>" + after.Org + "\n" + } + + if after.OrgEn != "" && after.OrgEn != before.OrgEn { + data += "Org(En): " + before.OrgEn + "=>" + after.OrgEn + "\n" + } + + if after.PostCode != "" && after.PostCode != before.PostCode { + data += "PostCode: " + before.PostCode + "=>" + after.PostCode + "\n" + } + + if after.Address != "" && after.Address != before.Address { + data += "Address: " + before.Address + "=>" + after.Address + "\n" + } + + if after.AddressEn != "" && after.AddressEn != before.AddressEn { + data += "Address(En): " + before.AddressEn + "=>" + after.AddressEn + "\n" + } + + if after.Tel != "" && after.Tel != before.Tel { + data += "Tel: " + before.Tel + "=>" + after.Tel + "\n" + } + + if after.Country != "" && after.Country != before.Country { + data += "Country: " + before.Country + "=>" + after.Country + "\n" + } + + return data +} + +func changeTextAdmin(before, after core.Group) string { + data := "" + if after.Open != nil { + if *before.Open != *after.Open { + if *after.Open { + data += "開通: 未開通 => 開通済み\n" + } else { + data += "開通: 開通 => 未開通\n" + } + } + } + + if after.Pass != nil { + if *before.Pass != *after.Pass { + if *after.Pass { + data += "審査: 未審査 => 審査合格済み\n" + } else { + data += "審査: 審査合格 => 未審査状態\n" + } + } + } + + if after.Lock != nil { + if !*before.Lock != *after.Lock { + if *after.Lock { + data += "ユーザ変更: 禁止 => 許可\n" + } else { + data += "ユーザ変更: 許可 => 禁止\n" + } + } + } + + if after.ExpiredStatus != nil { + if *before.ExpiredStatus != *after.ExpiredStatus { + data += "ExpiredStatus: " + expiredStatusText(*before.ExpiredStatus) + " => " + + expiredStatusText(*after.ExpiredStatus) + "\n" + } + } + + if after.Status != nil { + if *before.Status != *after.Status { + data += "ExpiredStatus: " + statusText(*before.Status) + " => " + statusText(*after.Status) + "\n" + } + } + + if after.Org != "" && after.Org != before.Org { + data += "Org: " + before.Org + "=>" + after.Org + "\n" + } + + if after.OrgEn != "" && after.OrgEn != before.OrgEn { + data += "Org(En): " + before.OrgEn + "=>" + after.OrgEn + "\n" + } + + if after.PostCode != "" && after.PostCode != before.PostCode { + data += "PostCode: " + before.PostCode + "=>" + after.PostCode + "\n" + } + + if after.Address != "" && after.Address != before.Address { + data += "Address: " + before.Address + "=>" + after.Address + "\n" + } + + if after.AddressEn != "" && after.AddressEn != before.AddressEn { + data += "Address(En): " + before.AddressEn + "=>" + after.AddressEn + "\n" + } + + if after.Tel != "" && after.Tel != before.Tel { + data += "Tel: " + before.Tel + "=>" + after.Tel + "\n" + } + + if after.Country != "" && after.Country != before.Country { + data += "Country: " + before.Country + "=>" + after.Country + "\n" + } + + return data +} + +func expiredStatusText(status uint) string { + if status == 0 { + return "0" + } else if status == 1 { + return "ユーザより廃止" + } else if status == 2 { + return "運営委員より廃止" + } else if status == 3 { + return "審査落ち" + } else { + return "status不明" + } +} + +func statusText(status uint) string { + if status == 0 { + return "0" + } else if status == 1 { + return "ネットワーク情報 記入段階" + } else if status == 2 { + return "審査中" + } else if status == 3 { + return "接続情報 記入段階" + } else if status == 4 { + return "開通作業中" + } else { + return "status不明" + } +} diff --git a/pkg/api/core/group/service/v0/admin.go b/pkg/api/core/group/service/v0/admin.go index 60e7e8d6..565cacba 100644 --- a/pkg/api/core/group/service/v0/admin.go +++ b/pkg/api/core/group/service/v0/admin.go @@ -8,8 +8,13 @@ import ( 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/group/service/ip" + "github.com/homenoc/dsbd-backend/pkg/api/core/group/service/jpnicTech" serviceTemplate "github.com/homenoc/dsbd-backend/pkg/api/core/template/service" "github.com/homenoc/dsbd-backend/pkg/api/core/tool/notification" + dbPlan "github.com/homenoc/dsbd-backend/pkg/api/store/group/service/ip/plan/v0" + dbIP "github.com/homenoc/dsbd-backend/pkg/api/store/group/service/ip/v0" + dbJPNICTech "github.com/homenoc/dsbd-backend/pkg/api/store/group/service/jpnicTech/v0" dbService "github.com/homenoc/dsbd-backend/pkg/api/store/group/service/v0" dbServiceTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/service/v0" "github.com/jinzhu/gorm" @@ -85,7 +90,7 @@ func AddAdmin(c *gin.Context) { } } - grpIP, err = ipProcess(true, input.IP) + grpIP, err = ipProcess(true, true, input.IP) if err != nil { c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) return @@ -103,7 +108,7 @@ func AddAdmin(c *gin.Context) { return } - grpIP, err = ipProcess(false, input.IP) + grpIP, err = ipProcess(true, false, input.IP) if err != nil { c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) return @@ -174,6 +179,123 @@ func AddAdmin(c *gin.Context) { c.JSON(http.StatusOK, service.Result{}) } +func AddIPAdmin(c *gin.Context) { + var input service.IPInput + + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + err = c.BindJSON(&input) + if err != nil { + log.Println(err) + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN")) + if resultAdmin.Err != nil { + c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()}) + return + } + + resultIP, err := ipProcess(true, false, []service.IPInput{input}) + if err != nil { + log.Println(err) + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + if err = dbService.JoinIP(uint(id), resultIP[0]); err != nil { + c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) + return + } + c.JSON(http.StatusOK, service.Result{}) +} + +func AddPlanAdmin(c *gin.Context) { + var input core.Plan + + err := c.BindJSON(&input) + if err != nil { + log.Println(err) + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN")) + if resultAdmin.Err != nil { + c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()}) + return + } + + if _, err = dbPlan.Create(&input); err != nil { + c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) + return + } + c.JSON(http.StatusOK, service.Result{}) +} + +func AddJPNICAdminAdmin(c *gin.Context) { + var input core.JPNICAdmin + + resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN")) + if resultAdmin.Err != nil { + c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()}) + return + } + + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + err = c.BindJSON(&input) + if err != nil { + log.Println(err) + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + if err = dbService.JoinJPNICAdmin(uint(id), input); err != nil { + c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) + return + } + c.JSON(http.StatusOK, common.Result{}) +} + +func AddJPNICTechAdmin(c *gin.Context) { + var input core.JPNICTech + + resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN")) + if resultAdmin.Err != nil { + c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()}) + return + } + + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + err = c.BindJSON(&input) + if err != nil { + log.Println(err) + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + if err = dbService.JoinJPNICTech(uint(id), input); err != nil { + c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) + return + } + c.JSON(http.StatusOK, common.Result{}) +} + func DeleteAdmin(c *gin.Context) { resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN")) if resultAdmin.Err != nil { @@ -194,6 +316,104 @@ func DeleteAdmin(c *gin.Context) { c.JSON(http.StatusOK, service.Result{}) } +func DeleteIPAdmin(c *gin.Context) { + resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN")) + if resultAdmin.Err != nil { + c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()}) + return + } + + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + ipID, err := strconv.Atoi(c.Param("ip_id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + if err = dbService.DeleteIP(uint(id), uint(ipID)); err != nil { + c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) + return + } + c.JSON(http.StatusOK, common.Result{}) +} + +func DeletePlanAdmin(c *gin.Context) { + resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN")) + if resultAdmin.Err != nil { + c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()}) + return + } + + planID, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + if err = dbPlan.Delete(&core.Plan{Model: gorm.Model{ID: uint(planID)}}); err != nil { + c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) + return + } + c.JSON(http.StatusOK, common.Result{}) +} + +func DeleteJPNICAdminAdmin(c *gin.Context) { + resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN")) + if resultAdmin.Err != nil { + c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()}) + return + } + + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + jpnicID, err := strconv.Atoi(c.Param("jpnic_id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + if err = dbService.DeleteJPNICAdmin(uint(id), uint(jpnicID)); err != nil { + c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) + return + } + c.JSON(http.StatusOK, common.Result{}) +} + +func DeleteJPNICTechAdmin(c *gin.Context) { + resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN")) + if resultAdmin.Err != nil { + c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()}) + return + } + + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + jpnicID, err := strconv.Atoi(c.Param("jpnic_id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + if err = dbService.DeleteJPNICTech(uint(id), uint(jpnicID)); err != nil { + c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) + return + } + c.JSON(http.StatusOK, common.Result{}) +} + func UpdateAdmin(c *gin.Context) { var input core.Service @@ -233,10 +453,10 @@ func UpdateAdmin(c *gin.Context) { c.JSON(http.StatusOK, service.Result{}) } -func UpdateIP(c *gin.Context) { - var input core.Service +func UpdateIPAdmin(c *gin.Context) { + var input service.IPInput - id, err := strconv.Atoi(c.Param("id")) + _, err := strconv.Atoi(c.Param("id")) if err != nil { c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) return @@ -261,24 +481,135 @@ func UpdateIP(c *gin.Context) { return } - input.ID = uint(id) - input.IP[0].ID = uint(ipID) - + resultIP, err := ipProcess(true, false, []service.IPInput{input}) + if err != nil { + log.Println(err) + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + resultIP[0].ID = uint(ipID) log.Println(input) - if err = dbService.Update(service.ReplaceIP, input); err != nil { + if err = dbIP.Update(ip.UpdateAll, resultIP[0]); err != nil { c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) return } c.JSON(http.StatusOK, service.Result{}) } -func AppendJPNICTech(c *gin.Context) { +func UpdatePlanAdmin(c *gin.Context) { + var input core.Plan + + resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN")) + if resultAdmin.Err != nil { + c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()}) + return + } + + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + err = c.BindJSON(&input) + if err != nil { + log.Println(err) + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + input.ID = uint(id) + + if err = dbPlan.Update(input); err != nil { + c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) + return + } + c.JSON(http.StatusOK, common.Result{}) +} + +func UpdateJPNICAdminAdmin(c *gin.Context) { + var input core.JPNICAdmin + + resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN")) + if resultAdmin.Err != nil { + c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()}) + return + } + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + err = c.BindJSON(&input) + if err != nil { + log.Println(err) + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + if err = dbService.UpdateJPNICAdmin(uint(id), input); err != nil { + c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) + return + } + c.JSON(http.StatusOK, common.Result{}) } -func DeleteJPNICTech(c *gin.Context) { +func UpdateJPNICTechAdmin(c *gin.Context) { + var input core.JPNICTech + resultAdmin := auth.AdminAuthentication(c.Request.Header.Get("ACCESS_TOKEN")) + if resultAdmin.Err != nil { + c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()}) + return + } + + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + jpnicID, err := strconv.Atoi(c.Param("jpnic_id")) + if err != nil { + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + err = c.BindJSON(&input) + if err != nil { + log.Println(err) + c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) + return + } + + ok := false + + resultJPNICTech := dbService.Get(service.ID, &core.Service{Model: gorm.Model{ID: uint(id)}}) + if resultJPNICTech.Err != nil { + c.JSON(http.StatusInternalServerError, common.Error{Error: resultJPNICTech.Err.Error()}) + return + } + + for _, tmp := range resultJPNICTech.Service[0].JPNICTech { + if tmp.ID == uint(jpnicID) { + ok = true + input.Model = tmp.Model + break + } + } + + if ok { + if err = dbJPNICTech.Update(jpnicTech.UpdateAll, input); err != nil { + c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) + return + } + c.JSON(http.StatusOK, common.Result{}) + } else { + c.JSON(http.StatusBadRequest, common.Error{Error: "Not Found GroupID"}) + } } func GetAdmin(c *gin.Context) { diff --git a/pkg/api/core/group/service/v0/check.go b/pkg/api/core/group/service/v0/check.go index ab580cb7..1308a497 100644 --- a/pkg/api/core/group/service/v0/check.go +++ b/pkg/api/core/group/service/v0/check.go @@ -90,7 +90,7 @@ func checkJPNICTechUser(input core.JPNICTech) error { return nil } -func ipCheck(restrict bool, ip service.IPInput) error { +func ipCheck(admin, restrict bool, ip service.IPInput) error { nowTime := time.Now() @@ -103,15 +103,17 @@ func ipCheck(restrict bool, ip service.IPInput) error { return fmt.Errorf("no network name") } } - startDate, _ := time.Parse("2006-01-02", ip.StartDate) - if startDate.UTC().Unix() < nowTime.UTC().Unix() { - return fmt.Errorf("invalid start Date") - } + if !admin { + startDate, _ := time.Parse("2006-01-02", ip.StartDate) + if startDate.UTC().Unix() < nowTime.UTC().Unix() { + return fmt.Errorf("invalid start Date") + } - if ip.EndDate != nil { - endDate, _ := time.Parse("2006-01-02", *ip.EndDate) - if endDate.UTC().Unix() < nowTime.UTC().Unix() && startDate.UTC().Unix() >= endDate.UTC().Unix() { - return fmt.Errorf("invalid end Date") + if ip.EndDate != nil { + endDate, _ := time.Parse("2006-01-02", *ip.EndDate) + if endDate.UTC().Unix() < nowTime.UTC().Unix() && startDate.UTC().Unix() >= endDate.UTC().Unix() { + return fmt.Errorf("invalid end Date") + } } } diff --git a/pkg/api/core/group/service/v0/ip.go b/pkg/api/core/group/service/v0/ip.go index f8020081..b83d5dc1 100644 --- a/pkg/api/core/group/service/v0/ip.go +++ b/pkg/api/core/group/service/v0/ip.go @@ -6,11 +6,11 @@ import ( "time" ) -func ipProcess(restrict bool, input []service.IPInput) ([]core.IP, error) { +func ipProcess(admin, restrict bool, input []service.IPInput) ([]core.IP, error) { var net []core.IP for _, tmpIP := range input { - if err := ipCheck(restrict, tmpIP); err != nil { + if err := ipCheck(admin, restrict, tmpIP); err != nil { return nil, err } diff --git a/pkg/api/core/group/service/v0/service.go b/pkg/api/core/group/service/v0/service.go index a6f4829a..2bef7ef1 100644 --- a/pkg/api/core/group/service/v0/service.go +++ b/pkg/api/core/group/service/v0/service.go @@ -88,7 +88,7 @@ func Add(c *gin.Context) { } } - grpIP, err = ipProcess(true, input.IP) + grpIP, err = ipProcess(false, true, input.IP) if err != nil { c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) return @@ -106,7 +106,7 @@ func Add(c *gin.Context) { return } - grpIP, err = ipProcess(false, input.IP) + grpIP, err = ipProcess(false, false, input.IP) if err != nil { c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) return diff --git a/pkg/api/core/group/service/v0/slack.go b/pkg/api/core/group/service/v0/slack.go index 5ee5ef47..d24df806 100644 --- a/pkg/api/core/group/service/v0/slack.go +++ b/pkg/api/core/group/service/v0/slack.go @@ -21,6 +21,28 @@ func noticeSlackAdmin(before, after core.Service) { notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true}) } +func noticeSlackJPNICAdminAdmin(before, after core.Service) { + // 審査ステータスのSlack通知 + attachment := slack.Attachment{} + + attachment.AddField(slack.Field{Title: "Title", Value: "Service情報の更新"}). + AddField(slack.Field{Title: "申請者", Value: "管理者"}). + AddField(slack.Field{Title: "Group", Value: strconv.Itoa(int(before.ID)) + "-" + before.Group.Org}). + AddField(slack.Field{Title: "更新状況", Value: changeText(before, after)}) + notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true}) +} + +func noticeSlackJPNICTechAdmin(before, after core.Service) { + // 審査ステータスのSlack通知 + attachment := slack.Attachment{} + + attachment.AddField(slack.Field{Title: "Title", Value: "Service情報の更新"}). + AddField(slack.Field{Title: "申請者", Value: "管理者"}). + AddField(slack.Field{Title: "Group", Value: strconv.Itoa(int(before.ID)) + "-" + before.Group.Org}). + AddField(slack.Field{Title: "更新状況", Value: changeText(before, after)}) + notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true}) +} + func changeText(before, after core.Service) string { data := "" if after.Open != nil { diff --git a/pkg/api/core/interface.go b/pkg/api/core/interface.go index c44608cc..1a528986 100644 --- a/pkg/api/core/interface.go +++ b/pkg/api/core/interface.go @@ -293,3 +293,26 @@ type Notice struct { Title string `json:"title"` Data string `json:"data" gorm:"size:65535"` } + +type Request struct { + gorm.Model + RequestTemplateID uint `json:"request_template_id"` + RequestTemplate RequestTemplate `json:"request_template"` + TargetID *uint `json:"target_id"` + Reason1 string `json:"reason_1"` + Reason2 string `json:"reason_2"` + Accept *bool `json:"accept"` + User User `json:"user"` + Group Group `json:"group"` +} + +// Type 1:追加 2:修正 3:削除 +// InfoType 1:グループ情報 2:サービス情報 3:IP 4:JPNICAdmin 5:JPNICTech 6:接続情報 +type RequestTemplate struct { + gorm.Model + Title string `json:"title"` + Data string `json:"data"` + RequestType uint `json:"request_type"` + InfoType uint `json:"info_type"` + Comment string `json:"comment"` +} 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/group/service/ip/plan/v0/plan.go b/pkg/api/store/group/service/ip/plan/v0/plan.go new file mode 100644 index 00000000..1d1c2cf5 --- /dev/null +++ b/pkg/api/store/group/service/ip/plan/v0/plan.go @@ -0,0 +1,46 @@ +package v0 + +import ( + "fmt" + "github.com/homenoc/dsbd-backend/pkg/api/core" + "github.com/homenoc/dsbd-backend/pkg/api/store" + "github.com/jinzhu/gorm" + "log" + "time" +) + +func Create(plan *core.Plan) (*core.Plan, error) { + db, err := store.ConnectDB() + if err != nil { + log.Println("database connection error") + return plan, fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer db.Close() + + err = db.Create(&plan).Error + return plan, err +} + +func Delete(plan *core.Plan) error { + db, err := store.ConnectDB() + if err != nil { + log.Println("database connection error") + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer db.Close() + + return db.Delete(plan).Error +} + +func Update(u core.Plan) error { + db, err := store.ConnectDB() + if err != nil { + log.Println("database connection error") + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer db.Close() + + err = db.Model(&core.Plan{Model: gorm.Model{ID: u.ID}}).Update(u).Error + + return err +} diff --git a/pkg/api/store/group/service/jpnicTech/v0/jpnicTech.go b/pkg/api/store/group/service/jpnicTech/v0/jpnicTech.go index 8e7f651e..f3780199 100644 --- a/pkg/api/store/group/service/jpnicTech/v0/jpnicTech.go +++ b/pkg/api/store/group/service/jpnicTech/v0/jpnicTech.go @@ -41,26 +41,14 @@ func Update(base int, u core.JPNICTech) error { } defer db.Close() - var result *gorm.DB - if base == jpnicTech.UpdateAll { - err = db.Model(&core.JPNICTech{Model: gorm.Model{ID: u.ID}}).Update(core.JPNICTech{ - Org: u.Org, - OrgEn: u.OrgEn, - PostCode: u.PostCode, - Address: u.Address, - AddressEn: u.AddressEn, - Dept: u.Dept, - DeptEn: u.DeptEn, - Tel: u.Tel, - Fax: u.Fax, - Country: u.Country, - }).Error + err = db.Model(&core.JPNICTech{Model: gorm.Model{ID: u.ID}}).Update(u).Error } else { log.Println("base select error") return fmt.Errorf("(%s)error: base select\n", time.Now()) } - return result.Error + + return err } func Get(base int, data *core.JPNICTech) jpnicTech.ResultDatabase { diff --git a/pkg/api/store/group/service/v0/service.go b/pkg/api/store/group/service/v0/service.go index 37258b14..19fa47b4 100644 --- a/pkg/api/store/group/service/v0/service.go +++ b/pkg/api/store/group/service/v0/service.go @@ -88,7 +88,7 @@ func Update(base int, c core.Service) error { return result } -func JoinJPNICTech(serviceID, jpnicTechID uint) error { +func JoinJPNICAdmin(serviceID uint, input core.JPNICAdmin) error { db, err := store.ConnectDB() if err != nil { log.Println("database connection error") @@ -96,8 +96,70 @@ func JoinJPNICTech(serviceID, jpnicTechID uint) error { } defer db.Close() - db.Model(&core.Service{Model: gorm.Model{ID: serviceID}}).Association("JPNICTech"). - Append(&core.JPNICTech{Model: gorm.Model{ID: jpnicTechID}}) + db.Model(&core.Service{Model: gorm.Model{ID: serviceID}}). + Association("JPNICAdmin"). + Append(input) + + return nil +} + +func UpdateJPNICAdmin(serviceID uint, input core.JPNICAdmin) error { + db, err := store.ConnectDB() + if err != nil { + log.Println("database connection error") + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer db.Close() + + db.Model(&core.Service{Model: gorm.Model{ID: serviceID}}). + Association("JPNICAdmin"). + Replace(&core.JPNICAdmin{Model: gorm.Model{ID: input.ID}}, &input) + + return nil +} + +func DeleteJPNICAdmin(serviceID, jpnicAdminID uint) error { + db, err := store.ConnectDB() + if err != nil { + log.Println("database connection error") + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer db.Close() + + db.Model(&core.Service{Model: gorm.Model{ID: serviceID}}). + Association("JPNICAdmin"). + Delete(&core.JPNICAdmin{Model: gorm.Model{ID: jpnicAdminID}}) + + return nil +} + +func JoinJPNICTech(serviceID uint, input core.JPNICTech) error { + db, err := store.ConnectDB() + if err != nil { + log.Println("database connection error") + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer db.Close() + + db.Model(&core.Service{Model: gorm.Model{ID: serviceID}}). + Association("JPNICTech"). + Append(&input) + + return nil +} + +func UpdateJPNICTech(serviceID uint, before, after core.JPNICTech) error { + db, err := store.ConnectDB() + if err != nil { + log.Println("database connection error") + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer db.Close() + + after.Model = before.Model + db.Model(&core.Service{Model: gorm.Model{ID: serviceID}}). + Association("JPNICTech"). + Replace(&before, &after) return nil } @@ -110,12 +172,43 @@ func DeleteJPNICTech(serviceID, jpnicTechID uint) error { } defer db.Close() - db.Model(&core.Service{Model: gorm.Model{ID: serviceID}}).Association("JPNICTech"). + db.Model(&core.Service{Model: gorm.Model{ID: serviceID}}). + Association("JPNICTech"). Delete(&core.JPNICTech{Model: gorm.Model{ID: jpnicTechID}}) return nil } +func JoinIP(serviceID uint, input core.IP) error { + db, err := store.ConnectDB() + if err != nil { + log.Println("database connection error") + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer db.Close() + + db.Model(&core.Service{Model: gorm.Model{ID: serviceID}}). + Association("IP"). + Append(&input) + + return nil +} + +func DeleteIP(serviceID, jpnicTechID uint) error { + db, err := store.ConnectDB() + if err != nil { + log.Println("database connection error") + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer db.Close() + + db.Model(&core.Service{Model: gorm.Model{ID: serviceID}}). + Association("IP"). + Delete(&core.IP{Model: gorm.Model{ID: jpnicTechID}}) + + return nil +} + func Get(base int, data *core.Service) service.ResultDatabase { db, err := store.ConnectDB() if err != nil { 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,