Skip to content

Commit

Permalink
Merge pull request #106 from LalitDeore/dev_lalit
Browse files Browse the repository at this point in the history
added the default alert threshold as 50% and 100% for new register user
  • Loading branch information
frikky authored Oct 1, 2024
2 parents 31e3486 + 753a799 commit a2756b7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
7 changes: 6 additions & 1 deletion db-connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,17 @@ func HandleIncrement(dataType string, orgStatistics *ExecutionInfo, increment ui
if int64(AlertThreshold.Count) < orgStatistics.MonthlyAppExecutions && AlertThreshold.Email_send == false {
for _, user := range org.Users {
if user.Role == "admin" {
var BccAddress []string
if int64(AlertThreshold.Count) >= 5000 || int64(AlertThreshold.Count) >= 10000 && AlertThreshold.Email_send == false {
BccAddress = []string{"[email protected]", "[email protected]"}
}

mailbody := Mailcheck{
Targets: []string{user.Username},
Subject: "You have reached the threshold limit of app executions",
Body: fmt.Sprintf("You have reached the threshold limit of %v percent Or %v app executions run. Please login to shuffle and check it.", AlertThreshold.Percentage, AlertThreshold.Count),
}
err = sendMailSendgrid(mailbody.Targets, mailbody.Subject, mailbody.Body, false)
err = sendMailSendgrid(mailbody.Targets, mailbody.Subject, mailbody.Body, false, BccAddress)
if err != nil {
log.Printf("[ERROR] Failed sending alert mail in increment: %s", err)
} else {
Expand Down
22 changes: 17 additions & 5 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -11826,7 +11826,7 @@ func HandleEditOrg(resp http.ResponseWriter, request *http.Request) {

}

func sendMailSendgrid(toEmail []string, subject, body string, emailApp bool) error {
func sendMailSendgrid(toEmail []string, subject, body string, emailApp bool, BccAddresses []string) error {
log.Printf("[DEBUG] In mail sending with subject %s and body length %s. TO: %s", subject, body, toEmail)
srequest := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
srequest.Method = "POST"
Expand All @@ -11842,6 +11842,7 @@ func sendMailSendgrid(toEmail []string, subject, body string, emailApp bool) err

type SendgridPersonalization struct {
To []SendgridEmail `json:"to"`
Bcc []SendgridEmail `json:"bcc"`
Subject string `json:"subject"`
}

Expand All @@ -11855,7 +11856,7 @@ func sendMailSendgrid(toEmail []string, subject, body string, emailApp bool) err

newBody := sendgridMailBody{
Personalizations: []SendgridPersonalization{
SendgridPersonalization{
{
To: []SendgridEmail{},
Subject: subject,
},
Expand All @@ -11864,7 +11865,7 @@ func sendMailSendgrid(toEmail []string, subject, body string, emailApp bool) err
Email: "Shuffle Support <[email protected]>",
},
Content: []SendgridContent{
SendgridContent{
{
Type: "text/html",
Value: body,
},
Expand All @@ -11882,6 +11883,17 @@ func sendMailSendgrid(toEmail []string, subject, body string, emailApp bool) err
})
}

// Conditionally add BCC addresses if they exist
if len(BccAddresses) > 0 {
for _, bccEmail := range BccAddresses {
newBody.Personalizations[0].Bcc = append(newBody.Personalizations[0].Bcc,
SendgridEmail{
Email: strings.TrimSpace(bccEmail),
})
}
}


parsedBody, err := json.Marshal(newBody)
if err != nil {
log.Printf("[ERROR] Failed to parse JSON in sendmail: %s", err)
Expand Down Expand Up @@ -29005,7 +29017,7 @@ func HandleUserPrivateTraining(resp http.ResponseWriter, request *http.Request)
Subject := "Thank you for your private training request"
Message := fmt.Sprintf("Hi there, Thank you for submitting request for shuffle private training. This is confirmation that we have received your private training request. You have requested a private training for %v members. We will get back to you shortly. <br> <br> Best Regards <br>Shuffle Team", tmpData.Training)

err = sendMailSendgrid(email, Subject, Message, false)
err = sendMailSendgrid(email, Subject, Message, false, []string{})
if err != nil {
log.Printf("[ERROR] Failed sending mail: %s", err)
resp.WriteHeader(http.StatusBadRequest)
Expand All @@ -29018,7 +29030,7 @@ func HandleUserPrivateTraining(resp http.ResponseWriter, request *http.Request)
Subject = fmt.Sprintf("Private training request")
Message = fmt.Sprintf("Private training request : <br>Org id: %v <br> Org Name: %v <br>Username: %v <br> Training Members: %v <br>Customer: %v <br> Message: %v", org.Id, org.Name, User.Username, tmpData.Training, org.LeadInfo.Customer, tmpData.Message)

err = sendMailSendgrid(email, Subject, Message, false)
err = sendMailSendgrid(email, Subject, Message, false, []string{})
if err != nil {
log.Printf("[ERROR] Failed sending mail: %s", err)
resp.WriteHeader(http.StatusBadRequest)
Expand Down
1 change: 1 addition & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3645,6 +3645,7 @@ type Mailcheck struct {
Authorization string `json:"authorization"`
ExecutionType string `json:"execution_type"`
Start string `json:"start"`
Bcc []string `json:"bcc"`
}

type SmsBody struct {
Expand Down

0 comments on commit a2756b7

Please sign in to comment.