lib/rules/contact.go

85 lines
2.4 KiB
Go

package rules
import (
"bytes"
"context"
"html/template"
"code.tnxs.net/taxnexus/lib/api/crm/crm_client/contacts"
"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"
"code.tnxs.net/taxnexus/lib/app"
httptransport "github.com/go-openapi/runtime/client"
"go.temporal.io/sdk/workflow"
)
// StoreContactActivity posts a new contact object to datastore
func StoreContactActivity(ctx workflow.Context, w app.ContactActivityWrapper) error { //nolint:gocritic // what we want
postContactParams := contacts.NewPostContactsParamsWithTimeout(postTimeout)
if w.Obj.Name == "" {
w.Obj.Name = w.Obj.FirstName + " " + w.Obj.LastName
}
postContactParams.ContactsRequest = &crm_models.ContactRequest{
Data: []*crm_models.Contact{&w.Obj},
}
_, err := crmClient.Contacts.PostContacts(postContactParams,
httptransport.APIKeyAuth(
"X-API-Key",
"header",
w.APIKey,
))
if err != nil {
return err
}
sugar.Info("crm.storeContact: 👍 📤")
return nil
}
// NotifyContactActivity sends an email to a new lead
func NotifyContactActivity(ctx context.Context, w *app.ContactActivityWrapper) error {
sugar.Info("workflow.notifyContact: 📥")
var buf bytes.Buffer
const textBody = `
Alert! New Contact 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{
{
Subject: "New lead from " + w.Obj.Name,
ValidatedFromAddress: "support@taxnexus.net",
ToAddress: "info@taxnexus.net",
FromName: "Taxnexus Onboarding",
Text: buf.String(),
HTML: "<pre>" + buf.String() + "</pre>",
},
},
}
_, err = workflowClient.OutgoingEmailMessage.PostOutgoingEmailMessages(emailParams,
httptransport.APIKeyAuth(
"X-API-Key",
"header",
w.APIKey,
))
if err != nil {
return err
}
sugar.Info("workflow.notifyContact: 👍 📤")
return nil
}