Skip to content

Commit

Permalink
Merge pull request #137 from nicolasbock/set-627
Browse files Browse the repository at this point in the history
[SET-627] Enable chatter
  • Loading branch information
nicolasbock committed May 8, 2024
2 parents b840ddc + e184a2a commit bb26ce5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
1 change: 0 additions & 1 deletion cmd/monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func init() {
}

func main() {

cfg, err := config.NewConfigFromFile(*configs)
if err != nil {
panic(err)
Expand Down
29 changes: 21 additions & 8 deletions pkg/common/salesforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var ErrAuthentication = simpleforce.ErrAuthentication
type SalesforceClient interface {
GetCaseByNumber(number string) (*Case, error)
PostComment(caseId, body string, isPublic bool) *simpleforce.SObject
PostChatter(caseId, body string, isPublic bool) *simpleforce.SObject
Query(query string) (*simpleforce.QueryResult, error)
SObject(objectName ...string) *simpleforce.SObject
}
Expand All @@ -42,14 +43,6 @@ type Case struct {
Id, CaseNumber, AccountId, Customer string
}

func (sf *BaseSalesforceClient) PostComment(caseId, body string, isPublic bool) *simpleforce.SObject {
return sf.SObject("CaseComment").
Set("ParentId", caseId).
Set("CommentBody", html.UnescapeString(body)).
Set("IsPublished", isPublic).
Create()
}

func (sf *BaseSalesforceClient) GetCaseByNumber(number string) (*Case, error) {
q := "SELECT Id,CaseNumber,AccountId FROM Case WHERE CaseNumber LIKE '%" + number + "%'"
result, err := sf.Query(q)
Expand All @@ -74,6 +67,26 @@ func (sf *BaseSalesforceClient) GetCaseByNumber(number string) (*Case, error) {
return nil, ErrNoCaseFound{number}
}

func (sf *BaseSalesforceClient) PostComment(caseId, body string, isPublic bool) *simpleforce.SObject {
return sf.SObject("CaseComment").
Set("ParentId", caseId).
Set("CommentBody", html.UnescapeString(body)).
Set("IsPublished", isPublic).
Create()
}

func (sf *BaseSalesforceClient) PostChatter(caseId, body string, isPublic bool) *simpleforce.SObject {
visibility := "InternalUsers"
if isPublic {
visibility = "AllUsers"
}
return sf.SObject("FeedItem").
Set("ParentId", caseId).
Set("Body", body).
Set("Visibility", visibility).
Create()
}

func GetCaseNumberFromFilename(filename string) (string, error) {
regex, err := regexp.Compile(`(\d{6,})`)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ type SalesForce struct {
Password string `yaml:"password"`
SecurityToken string `yaml:"security-token"`
MaxCommentLength int `yaml:"max-comment-length"`
EnableChatter bool `yaml:"enable-chatter"`
}

func NewSalesForce() SalesForce {
return SalesForce{
MaxCommentLength: 4000 - 1000, // A very conservative buffer of max length per Salesforce comment (4000) without header text for comments
EnableChatter: false,
}
}

Expand Down Expand Up @@ -129,8 +131,8 @@ func NewConfigFromFile(filePaths []string) (*Config, error) {

if err := s.Snuffle(); err != nil {
return nil, err

}

return &config, nil
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func TestNewSalesforce(t *testing.T) {
if salesforce.MaxCommentLength != 3000 {
t.Errorf("Expected MaxCommentLength to be 3000, got '%d'", salesforce.MaxCommentLength)
}

if salesforce.EnableChatter {
t.Errorf("Expected EnableChatter to be false, got true")
}
}

func TestNewConfigFromFile(t *testing.T) {
Expand Down
13 changes: 10 additions & 3 deletions pkg/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/flosch/pongo2/v4"
"github.com/lileio/pubsub/v2"
"github.com/lileio/pubsub/v2/middleware/defaults"
"github.com/simpleforce/simpleforce"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -455,7 +456,7 @@ func (p *Processor) BatchSalesforceComments(ctx *context.Context, interval time.
return
}

log.Infof("Found %d reports to be sent to salesforce", len(reports))
log.Infof("Found %d reports to be sent to Salesforce", len(reports))
for _, report := range reports {
if reportMap[report.Subscriber] == nil {
reportMap[report.Subscriber] = make(map[string]map[string][]db.Report)
Expand Down Expand Up @@ -507,8 +508,14 @@ func (p *Processor) BatchSalesforceComments(ctx *context.Context, interval time.
if len(commentChunks) > 1 {
chunkHeader = fmt.Sprintf("Split comment %d of %d\n\n", i+1, len(commentChunks))
}
comment := p.SalesforceClient.PostComment(caseId,
chunkHeader+chunk, subscriber.SFCommentIsPublic)
var comment *simpleforce.SObject
if p.Config.Salesforce.EnableChatter {
comment = p.SalesforceClient.PostChatter(caseId,
chunkHeader+chunk, subscriber.SFCommentIsPublic)
} else {
comment = p.SalesforceClient.PostComment(caseId,
chunkHeader+chunk, subscriber.SFCommentIsPublic)
}
if comment == nil {
log.Errorf("Failed to post comment to case id: %s", caseId)
continue
Expand Down

0 comments on commit bb26ce5

Please sign in to comment.