lib/rules/lead.go

104 lines
2.8 KiB
Go
Raw Permalink Normal View History

2021-01-13 02:41:43 +00:00
package rules
import (
"bytes"
"context"
"html/template"
"code.tnxs.net/taxnexus/lib/api/crm/crm_client/leads"
"code.tnxs.net/taxnexus/lib/api/crm/crm_models"
"code.tnxs.net/taxnexus/lib/api/workflow/workflow_client/outgoing_email_message"
"code.tnxs.net/taxnexus/lib/api/workflow/workflow_models"
2021-01-19 01:50:45 +00:00
httptransport "github.com/go-openapi/runtime/client"
2021-01-13 02:41:43 +00:00
"go.temporal.io/sdk/workflow"
)
2021-01-21 03:04:43 +00:00
// NewLeadActivityWrapper wraps the object with workflow params
type NewLeadActivityWrapper struct {
APIKey string
SagaID string
SagaType string
//
Company string
Description string
Email string
FirstName string
LastName string
UTMSource string
}
2021-01-13 02:41:43 +00:00
// StoreLeadActivity posts a new lead object to datastore
2021-01-21 03:04:43 +00:00
func StoreLeadActivity(ctx workflow.Context, w NewLeadActivityWrapper) error { //nolint:gocritic // don't care
2021-01-19 21:18:59 +00:00
obj := &crm_models.Lead{
Company: w.Company,
Description: w.Description,
Email: w.Email,
FirstName: w.FirstName,
LastName: w.LastName,
2021-01-21 03:04:43 +00:00
Name: w.FirstName + " " + w.LastName,
2021-01-19 21:18:59 +00:00
UTMSource: w.UTMSource,
}
2021-01-13 02:41:43 +00:00
postLeadParams := leads.NewPostLeadsParamsWithTimeout(postTimeout)
postLeadParams.LeadRequest = &crm_models.LeadRequest{
2021-01-19 21:18:59 +00:00
Data: []*crm_models.Lead{obj},
2021-01-13 02:41:43 +00:00
}
2021-01-19 01:50:45 +00:00
_, err := crmClient.Leads.PostLeads(postLeadParams,
httptransport.APIKeyAuth(
"X-API-Key",
"header",
w.APIKey,
))
2021-01-13 02:41:43 +00:00
if err != nil {
return err
}
sugar.Info("crm.storeLead: 👍 📤")
return nil
}
// NotifyLeadActivity sends an email to a new lead
2021-01-21 03:04:43 +00:00
func NotifyLeadActivity(ctx context.Context, w NewLeadActivityWrapper) error { //nolint:gocritic,lll // todo #2 need email templates
2021-01-13 02:41:43 +00:00
sugar.Info("workflow.notifyLead: 📥")
var buf bytes.Buffer
const textBody = `
Alert! New Lead Inquiry from Taxnexus.io website.
Taxnexus ID: {{.ID}}
First Name: {{.FirstName}}
Last Name: {{.LastName}}
Email: {{.Email}}
Message: {{.Description}}
-- end --
`
t := template.Must(template.New("textBody").Parse(textBody))
err := t.Execute(&buf, w)
if err != nil {
return err
}
emailParams := outgoing_email_message.NewPostOutgoingEmailMessagesParamsWithTimeout(postTimeout)
emailParams.OutgoingEmailMessageRequest = &workflow_models.OutgoingEmailMessageRequest{
Data: []*workflow_models.OutgoingEmailMessage{
{
2021-01-21 03:04:43 +00:00
Subject: "New lead from " + w.FirstName + " " + w.LastName,
2021-01-13 02:41:43 +00:00
ValidatedFromAddress: "support@taxnexus.net",
ToAddress: "info@taxnexus.net",
FromName: "Taxnexus Onboarding",
Text: buf.String(),
HTML: "<pre>" + buf.String() + "</pre>",
},
},
}
2021-01-19 01:50:45 +00:00
_, err = workflowClient.OutgoingEmailMessage.PostOutgoingEmailMessages(emailParams,
httptransport.APIKeyAuth(
"X-API-Key",
"header",
w.APIKey,
))
2021-01-13 02:41:43 +00:00
if err != nil {
return err
}
sugar.Info("workflow.notifyLead: 👍 📤")
return nil
}