func UpdateWarningSent(entity *models.Warning, db gorp.SqlExecutor) bool { entity.Sent = true entity.Last_modified_date = time.Now().String() _, err := db.Update(entity) checkErr(err, "ERROR UpdateWarningSent ERROR") return err == nil }
func GetReplyByHash(enc Encoder, db gorp.SqlExecutor, parms martini.Params) (int, string) { var warning *models.Warning hash := parms["hash"] respReply := getReplyRespHash(hash, db) readReply := getReplyReadHash(hash, db) if respReply == nil && readReply == nil { fmt.Println("FAILED: neither resp or read hash matches") return http.StatusBadRequest, "" } else if respReply != nil { respReply.Read_hash = "" warning = GetWarning(respReply.Id_warning, db) warning.WarnResp = respReply } else if readReply != nil { readReply.Resp_hash = "" warning = GetWarning(readReply.Id_warning, db) warning.WarnResp = readReply } clearReturn(warning) return http.StatusOK, Must(enc.EncodeOne(warning)) }
func clearReturn(entity *models.Warning) { entity.Message = "" entity.Ip = "" entity.Browser = "" entity.Operating_system = "" entity.Device = "" entity.Raw = "" entity.Created_by = "" entity.Last_modified_by = "" entity.Last_modified_date = "" entity.WarnResp.Reply_to = "" entity.WarnResp.Ip = "" entity.WarnResp.Browser = "" entity.WarnResp.Operating_system = "" entity.WarnResp.Device = "" entity.WarnResp.Raw = "" if entity.WarnResp.Reply_date == "0000-00-00 00:00:00" { entity.WarnResp.Reply_date = "" } if entity.WarnResp.Response_read == "0000-00-00 00:00:00" { entity.WarnResp.Response_read = "" } }
// After registered in the Database, the warn is processed in order to verify: // - @isSameWarnSentByIp // - @isSameWarnSentTwiceOrMoreDifferentIp // - if none of above occurs the warn is processed by its type(Email, SMS, Whatsapp, etc...) // - @routers.email.ProcessEmail // - @routers.sms.ProcessSMS func processWarn(warning *models.Warning, db gorp.SqlExecutor, status *models.DefaultStruct) { fmt.Println("processWarn") status.Lang_key = warning.Lang_key if isSameWarnSentByIp(warning, db) { status.Id = http.StatusBadRequest status.Name = strings.Replace(messages.GetLocaleMessage(warning.Lang_key, "MSG_SMS_SAME_WARN_BY_IP"), "{{ip}}", warning.Ip, 1) status.Name = strings.Replace(status.Name, "{{time}}", "2", 1) } else if isSameWarnSentTwiceOrMoreDifferentIp(warning, db) { status.Id = http.StatusBadRequest status.Name = strings.Replace(messages.GetLocaleMessage(warning.Lang_key, "MSG_SMS_SAME_WARN_DIFF_IP"), "{{time}}", "2", 1) } else { if warning.WarnResp != nil && warning.WarnResp.Reply_to != "" { ProcessWarnReply(warning, db) } else { warning.WarnResp = nil } switch warning.Id_contact_type { case 1: go ProcessEmail(warning, db) case 2: ProcessSMS(warning, db, status) case 3: go ProcessWhatsapp(warning, db) default: return } } }
// Receives a warning tru, inserts the request and process the warning and then respond to the interface //TODO: use (session sessions.Session, r *http.Request) to prevent flood func AddWarning(entity models.Warning, enc Encoder, db gorp.SqlExecutor, r *http.Request) (int, string) { fmt.Println("AddWarning") if isInvalidWarning(&entity) { return http.StatusBadRequest, Must(enc.EncodeOne(entity)) } status := &models.DefaultStruct{ Id: http.StatusOK, Name: messages.GetLocaleMessage(entity.Lang_key, "MSG_WARNING_SENT_SUCCESS"), Lang_key: entity.Lang_key, Type: models.MSG_TYPE_WARNING, } entity.Sent = false entity.Created_by = "system" //entity.Created_date = time.Now().String() err := db.Insert(&entity) checkErr(err, "INSERT WARNING ERROR") if err != nil { return http.StatusBadRequest, Must(enc.EncodeOne(entity)) } ingnored := InIgnoreList(db, entity.Contact) if ingnored != nil && ingnored.Confirmed { status = &models.DefaultStruct{ Id: http.StatusBadRequest, Name: messages.GetLocaleMessage(entity.Lang_key, "MSG_IGNORED_USER"), Lang_key: entity.Lang_key, Type: models.MSG_TYPE_WARNING, } } else { processWarn(&entity, db, status) } return http.StatusCreated, Must(enc.EncodeOne(status)) }
func sendSMSWarn(entity *models.Warning, db gorp.SqlExecutor) { message := SelectMessage(db, entity.Id_message) sms_message := messages.GetLocaleMessage(entity.Lang_key, "MSG_SMS_GREET") + "\r\n" + "'" + message.Name + "'" if entity.WarnResp == nil { sms_message += "\r\n" + messages.GetLocaleMessage(entity.Lang_key, "MSG_FOOTER") } else { if entity.WarnResp.Id_contact_type == 1 { go SendEmailReplyRequestAcknowledge(entity.WarnResp, db) } else { go SendWhatsappReplyRequestAcknowledge(entity, db) } sms_message += "\r\n" + messages.GetLocaleMessage(entity.Lang_key, "MSG_REPLY_FOOTER") + " " + ShortUrl(models.URL_REPLY+"/"+entity.WarnResp.Resp_hash) } sms := &models.SMS{ CredencialKey: os.Getenv("WARNACREDENCIAL"), Content: sms_message, URLPath: models.URL_MAIN_MOBILE_PRONTO, Scheme: "http", Host: models.URL_DOMAIN_MOBILE_PRONTO, Project: os.Getenv("WARNAPROJECT"), AuxUser: "******", MobileNumber: strings.Replace(entity.Contact, "+", "", 1), SendProject: "N", } sent, response := SendSMS(sms) if sent { entity.Message = response UpdateWarningSent(entity, db) } }
//Deploys the message to be sent into an email struct, call the service and in case of successful send, update the warn as sent. func sendEmailWarn(entity *models.Warning, db gorp.SqlExecutor) { fmt.Println("sendEmailWarn") //reads the e-mail template from a local file wab_email_template := wab_root + "/resource/warning.html" if entity.WarnResp != nil { wab_email_template = wab_root + "/resource/warning-reply.html" } template_byte, err := ioutil.ReadFile(wab_email_template) checkErr(err, "Email File Opening ERROR") template_email_string := string(template_byte[:]) subject := GetRandomSubject(entity.Lang_key) email_content := sendWarningSetup(subject.Name, template_email_string, entity, db) email := &models.Email{ TemplatePath: wab_email_template, Content: email_content, Subject: subject.Name, ToAddress: entity.Contact, FromName: models.WARN_A_BRODA, LangKey: entity.Lang_key, Async: false, UseContent: true, HTMLContent: true, } sent, response := SendMail(email) if sent { entity.Message = response UpdateWarningSent(entity, db) } }