From 409dc1ce7282430aae485fd1b37d72cde66f1357 Mon Sep 17 00:00:00 2001 From: yoneyan Date: Fri, 23 Jul 2021 04:25:36 +0900 Subject: [PATCH 1/2] [fix] #103 --- pkg/api/core/group/info/v0/info.go | 59 +++++++++++++++--------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/pkg/api/core/group/info/v0/info.go b/pkg/api/core/group/info/v0/info.go index 60bc75ff..3d4af57c 100644 --- a/pkg/api/core/group/info/v0/info.go +++ b/pkg/api/core/group/info/v0/info.go @@ -192,43 +192,44 @@ func Get(c *gin.Context) { }) } } - for _, tmpTicket := range dbUserResult.User[0].Ticket { - var resultChat []info.Chat - if tmpTicket.GroupID == nil { - for _, tmpChat := range tmpTicket.Chat { + } - var userID uint = 0 - if tmpChat.UserID != nil { - userID = *tmpChat.UserID - } + for _, tmpTicket := range dbUserResult.User[0].Ticket { + var resultChat []info.Chat + if tmpTicket.GroupID == nil { + for _, tmpChat := range tmpTicket.Chat { - resultChat = append(resultChat, info.Chat{ - CreatedAt: tmpChat.CreatedAt, - TicketID: tmpChat.TicketID, - UserID: userID, - Admin: tmpChat.Admin, - Data: tmpChat.Data, - }) + var userID uint = 0 + if tmpChat.UserID != nil { + userID = *tmpChat.UserID } - resultTicket = append(resultTicket, info.Ticket{ - ID: tmpTicket.ID, - CreatedAt: tmpTicket.CreatedAt, - GroupID: 0, - UserID: authResult.User.ID, - Chat: resultChat, - Solved: tmpTicket.Solved, - Title: tmpTicket.Title, + resultChat = append(resultChat, info.Chat{ + CreatedAt: tmpChat.CreatedAt, + TicketID: tmpChat.TicketID, + UserID: userID, + Admin: tmpChat.Admin, + Data: tmpChat.Data, }) } + + resultTicket = append(resultTicket, info.Ticket{ + ID: tmpTicket.ID, + CreatedAt: tmpTicket.CreatedAt, + GroupID: 0, + UserID: authResult.User.ID, + Chat: resultChat, + Solved: tmpTicket.Solved, + Title: tmpTicket.Title, + }) } - sort.Slice(resultTicket, func(i, j int) bool { - if resultTicket[i].ID < resultTicket[j].ID { - return true - } - return false - }) } + sort.Slice(resultTicket, func(i, j int) bool { + if resultTicket[i].ID < resultTicket[j].ID { + return true + } + return false + }) // Info var resultInfo []info.Info From aacb5e18a60f4c5dc958b655be7ccb0700ef4112 Mon Sep 17 00:00:00 2001 From: yoneyan Date: Fri, 23 Jul 2021 07:44:34 +0900 Subject: [PATCH 2/2] [support] #104 --- pkg/api/core/interface.go | 6 ++ pkg/api/core/mail/v0/mail.go | 2 +- pkg/api/core/support/ticket/v0/admin.go | 9 ++- pkg/api/core/template/interface.go | 1 + pkg/api/core/template/v0/admin.go | 8 ++ pkg/api/core/user/v0/update.go | 11 ++- pkg/api/core/user/v0/user.go | 17 +++- pkg/api/store/store.go | 1 + pkg/api/store/template/mail/v0/mail.go | 100 ++++++++++++++++++++++++ 9 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 pkg/api/store/template/mail/v0/mail.go diff --git a/pkg/api/core/interface.go b/pkg/api/core/interface.go index 18924840..810ac8f7 100644 --- a/pkg/api/core/interface.go +++ b/pkg/api/core/interface.go @@ -401,3 +401,9 @@ type RequestTemplate struct { InfoType uint `json:"info_type"` Comment string `json:"comment"` } + +type MailTemplate struct { + ProcessID string `json:"process_id" gorm:"primaryKey" gorm:"size:100"` + Subject string `json:"subject" gorm:"size:100"` + Message string `json:"message" gorm:"size:10000"` +} diff --git a/pkg/api/core/mail/v0/mail.go b/pkg/api/core/mail/v0/mail.go index 871cebb7..64df950d 100644 --- a/pkg/api/core/mail/v0/mail.go +++ b/pkg/api/core/mail/v0/mail.go @@ -52,7 +52,7 @@ func SendMail(d mailStruct.Mail) error { "From:" + from.String() + "\r\n" + "To:" + to.String() + "\r\n" + encodeSubject(d.Subject) + "\r\n" + - "\r\n" + d.Content + config.Conf.Mail.Contract + "\r\n" + "\r\n" + d.Content + "\r\n" auth := smtp.PlainAuth("", config.Conf.Mail.User, config.Conf.Mail.Pass, config.Conf.Mail.Host) err := smtp.SendMail(config.Conf.Mail.Host+":"+strconv.Itoa(config.Conf.Mail.Port), auth, diff --git a/pkg/api/core/support/ticket/v0/admin.go b/pkg/api/core/support/ticket/v0/admin.go index bf0f8471..7be2aa3e 100644 --- a/pkg/api/core/support/ticket/v0/admin.go +++ b/pkg/api/core/support/ticket/v0/admin.go @@ -16,6 +16,7 @@ import ( "github.com/homenoc/dsbd-backend/pkg/api/core/user" dbChat "github.com/homenoc/dsbd-backend/pkg/api/store/support/chat/v0" dbTicket "github.com/homenoc/dsbd-backend/pkg/api/store/support/ticket/v0" + dbMailTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/mail/v0" dbUser "github.com/homenoc/dsbd-backend/pkg/api/store/user/v0" "gorm.io/gorm" "log" @@ -291,6 +292,12 @@ func GetAdminWebSocket(c *gin.Context) { if resultUser.Err != nil { log.Println(resultUser.Err) } + + mailTemplate := core.MailTemplate{ProcessID: "signature"} + err = dbMailTemplate.Get(&mailTemplate) + if err != nil { + log.Println(err) + } if len(resultUser.User) != 0 { for _, userTmp := range resultUser.User { //グループ側にメール送信 @@ -298,7 +305,7 @@ func GetAdminWebSocket(c *gin.Context) { ToMail: userTmp.Email, Subject: "Supportより新着メッセージ", Content: " " + userTmp.Name + "様\n\n" + "チャットより新着メッセージがあります\n" + - "Webシステムよりご覧いただけます。\n", + "Webシステムよりご覧いただけます。" + mailTemplate.Message, }) } } diff --git a/pkg/api/core/template/interface.go b/pkg/api/core/template/interface.go index 3c59ae28..a53c77f2 100644 --- a/pkg/api/core/template/interface.go +++ b/pkg/api/core/template/interface.go @@ -47,6 +47,7 @@ type ResultAdmin struct { PaymentMembershipTemplate []core.PaymentMembershipTemplate `json:"payment_membership_template"` PaymentDonateTemplate []core.PaymentDonateTemplate `json:"payment_donate_template"` PaymentCouponTemplate []core.PaymentCouponTemplate `json:"payment_coupon_template"` + MailTemplate []core.MailTemplate `json:"mail_template"` } type ResultDatabase struct { diff --git a/pkg/api/core/template/v0/admin.go b/pkg/api/core/template/v0/admin.go index bb57fd3a..80a132fc 100644 --- a/pkg/api/core/template/v0/admin.go +++ b/pkg/api/core/template/v0/admin.go @@ -14,6 +14,7 @@ import ( dbIPv4RouteTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/ipv4_route/v0" dbIPv6Template "github.com/homenoc/dsbd-backend/pkg/api/store/template/ipv6/v0" dbIPv6RouteTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/ipv6_route/v0" + dbMailTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/mail/v0" dbNTTTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/ntt/v0" dbPaymentCouponTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/payment_coupon/v0" dbPaymentDonateTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/payment_donate/v0" @@ -114,6 +115,12 @@ func GetByAdmin(c *gin.Context) { return } + resultMailTemplate, err := dbMailTemplate.GetAll() + if err != nil { + c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) + return + } + c.JSON(http.StatusOK, template.ResultAdmin{ Services: resultService.Services, Connections: resultConnection.Connections, @@ -130,5 +137,6 @@ func GetByAdmin(c *gin.Context) { PaymentMembershipTemplate: resultPaymentMembership, PaymentDonateTemplate: resultDonateMembership, PaymentCouponTemplate: resultPaymentCoupon, + MailTemplate: resultMailTemplate, }) } diff --git a/pkg/api/core/user/v0/update.go b/pkg/api/core/user/v0/update.go index a8a12f44..060f54c7 100644 --- a/pkg/api/core/user/v0/update.go +++ b/pkg/api/core/user/v0/update.go @@ -8,6 +8,7 @@ import ( "github.com/homenoc/dsbd-backend/pkg/api/core/tool/config" toolToken "github.com/homenoc/dsbd-backend/pkg/api/core/tool/token" "github.com/homenoc/dsbd-backend/pkg/api/core/user" + dbMailTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/mail/v0" dbUser "github.com/homenoc/dsbd-backend/pkg/api/store/user/v0" "log" "strings" @@ -24,6 +25,12 @@ func replaceUser(serverData core.User, input user.Input) (core.User, error) { serverData.NameEn = input.NameEn } + mailTemplate := core.MailTemplate{ProcessID: "signature"} + err := dbMailTemplate.Get(&mailTemplate) + if err != nil { + log.Println(err) + } + //E-Mail if input.Email != "" { if !strings.Contains(input.Email, "@") { @@ -48,7 +55,7 @@ func replaceUser(serverData core.User, input user.Input) (core.User, error) { Subject: "本人確認のメールにつきまして", Content: " " + serverData.Name + "様\n\n" + "以下のリンクから本人確認を完了してください。\n" + config.Conf.Controller.User.Url + "/api/v1/verify/" + mailToken + "\n" + - "本人確認が完了次第、ログイン可能になります。\n", + "本人確認が完了次第、ログイン可能になります。" + mailTemplate.Message, }) } @@ -59,7 +66,7 @@ func replaceUser(serverData core.User, input user.Input) (core.User, error) { v0.SendMail(mail.Mail{ ToMail: serverData.Email, Subject: "[通知] パスワード変更", - Content: " " + serverData.Name + "様\n\n" + "パスワードが変更されました。\n", + Content: " " + serverData.Name + "様\n\n" + "パスワードが変更されました。" + mailTemplate.Message, }) } diff --git a/pkg/api/core/user/v0/user.go b/pkg/api/core/user/v0/user.go index dfa2e7bc..7eadfebd 100644 --- a/pkg/api/core/user/v0/user.go +++ b/pkg/api/core/user/v0/user.go @@ -17,6 +17,7 @@ import ( toolToken "github.com/homenoc/dsbd-backend/pkg/api/core/tool/token" "github.com/homenoc/dsbd-backend/pkg/api/core/user" dbGroup "github.com/homenoc/dsbd-backend/pkg/api/store/group/v0" + dbMailTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/mail/v0" dbUser "github.com/homenoc/dsbd-backend/pkg/api/store/user/v0" "gorm.io/gorm" "log" @@ -85,12 +86,18 @@ func Add(c *gin.Context) { AddField(slack.Field{Title: "Name(English)", Value: input.NameEn}) notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true}) + mailTemplate := core.MailTemplate{ProcessID: "signature"} + err = dbMailTemplate.Get(&mailTemplate) + if err != nil { + log.Println(err) + } + v0.SendMail(mail.Mail{ ToMail: data.Email, Subject: "本人確認のメールにつきまして", Content: " " + input.Name + "様\n\n" + "以下のリンクから本人確認を完了してください。\n" + config.Conf.Controller.User.Url + "/api/v1/verify/" + mailToken + "\n" + - "本人確認が完了次第、ログイン可能になります。\n", + "本人確認が完了次第、ログイン可能になります。" + mailTemplate.Message, }) c.JSON(http.StatusOK, user.Result{}) @@ -202,12 +209,18 @@ func AddGroup(c *gin.Context) { AddField(slack.Field{Title: "Name(English)", Value: input.NameEn}) notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true}) + mailTemplate := core.MailTemplate{ProcessID: "signature"} + err = dbMailTemplate.Get(&mailTemplate) + if err != nil { + log.Println(err) + } + v0.SendMail(mail.Mail{ ToMail: data.Email, Subject: "本人確認メールにつきまして", Content: " " + input.Name + "様\n\n" + "以下のリンクから本人確認を完了してください。\n" + config.Conf.Controller.User.Url + "/api/v1/verify/" + mailToken + "\n" + - "本人確認が完了次第、ログイン可能になります。\n" + "仮パスワード: " + pass, + "本人確認が完了次第、ログイン可能になります。\n" + "仮パスワード: " + pass + mailTemplate.Message, }) c.JSON(http.StatusOK, user.Result{}) diff --git a/pkg/api/store/store.go b/pkg/api/store/store.go index d0baf15f..873205e5 100644 --- a/pkg/api/store/store.go +++ b/pkg/api/store/store.go @@ -56,6 +56,7 @@ func InitDB() { &core.PaymentCouponTemplate{}, &core.PaymentMembershipTemplate{}, &core.PaymentDonateTemplate{}, + &core.MailTemplate{}, ) log.Println(result) } diff --git a/pkg/api/store/template/mail/v0/mail.go b/pkg/api/store/template/mail/v0/mail.go new file mode 100644 index 00000000..f656f24b --- /dev/null +++ b/pkg/api/store/template/mail/v0/mail.go @@ -0,0 +1,100 @@ +package v0 + +import ( + "fmt" + "github.com/homenoc/dsbd-backend/pkg/api/core" + "github.com/homenoc/dsbd-backend/pkg/api/store" + "log" + "time" +) + +func Create(mail *core.MailTemplate) error { + db, err := store.ConnectDB() + if err != nil { + log.Println("database ntt error") + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + dbSQL, err := db.DB() + if err != nil { + log.Printf("database error: %v", err) + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer dbSQL.Close() + + err = db.Create(&mail).Error + return err +} + +func Delete(mail *core.MailTemplate) error { + db, err := store.ConnectDB() + if err != nil { + log.Println("database mail error") + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + dbSQL, err := db.DB() + if err != nil { + log.Printf("database error: %v", err) + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer dbSQL.Close() + + return db.Delete(mail).Error +} + +func Update(base int, c core.MailTemplate) error { + db, err := store.ConnectDB() + if err != nil { + log.Println("database mail error") + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + dbSQL, err := db.DB() + if err != nil { + log.Printf("database error: %v", err) + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer dbSQL.Close() + + err = nil + + return err +} + +func Get(data *core.MailTemplate) error { + //var mailStruct []core.MailTemplate + + db, err := store.ConnectDB() + if err != nil { + log.Println("database ntt error") + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + dbSQL, err := db.DB() + if err != nil { + log.Printf("database error: %v", err) + return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer dbSQL.Close() + + err = db.Find(&data).Error + + return nil +} + +func GetAll() ([]core.MailTemplate, error) { + var mailStruct []core.MailTemplate + + db, err := store.ConnectDB() + if err != nil { + log.Println("database ntt error") + return mailStruct, fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + dbSQL, err := db.DB() + if err != nil { + log.Printf("database error: %v", err) + return mailStruct, fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) + } + defer dbSQL.Close() + + err = db.Find(&mailStruct).Error + return mailStruct, nil + +}