Skip to content

Commit 5f5abf0

Browse files
small refactoring of decision workflow
1 parent 0f2ff67 commit 5f5abf0

File tree

2 files changed

+34
-51
lines changed

2 files changed

+34
-51
lines changed

repositories/case_and_decisions.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ func (repo *MarbleDbRepository) SelectCasesWithPivot(
1616
return nil, err
1717
}
1818

19-
// both sides of the query should be ordered by case_id so that a merge join is possible
19+
// Both sides of the query should be ordered by case_id so that a merge join is possible
20+
// NB: may select a snoozed case, in which case adding the decision will automatically unsnooze it.
2021
query := `SELECT c.id, c.status, c.created_at, c.org_id
2122
FROM cases AS c
2223
INNER JOIN (

usecases/decision_workflows/decision_workflows.go

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (d DecisionsWorkflows) AutomaticDecisionToCase(
9090
return false, nil
9191
}
9292

93-
if scenario.DecisionToCaseWorkflowType == models.WorkflowCreateCase {
93+
createNewCaseForDecision := func(ctx context.Context) (bool, error) {
9494
caseName, err := d.caseNameEvaluator.EvalCaseName(ctx, params, scenario)
9595
if err != nil {
9696
return false, errors.Wrap(err, "error creating case for decision")
@@ -101,46 +101,25 @@ func (d DecisionsWorkflows) AutomaticDecisionToCase(
101101
if err != nil {
102102
return false, errors.Wrap(err, "error creating case for decision")
103103
}
104+
104105
err = d.webhookEventCreator.CreateWebhookEvent(ctx, tx, models.WebhookEventCreate{
105106
Id: webhookEventId,
106107
OrganizationId: newCase.OrganizationId,
107108
EventContent: models.NewWebhookEventCaseCreatedWorkflow(newCase.GetMetadata()),
108109
})
109-
if err != nil {
110-
return false, err
111-
}
112-
113-
return true, nil
110+
return true, err
114111
}
115112

116-
if scenario.DecisionToCaseWorkflowType == models.WorkflowAddToCaseIfPossible {
113+
switch scenario.DecisionToCaseWorkflowType {
114+
case models.WorkflowCreateCase:
115+
return createNewCaseForDecision(ctx)
116+
case models.WorkflowAddToCaseIfPossible:
117117
matchedCase, added, err := d.addToOpenCase(ctx, tx, scenario, decision)
118118
if err != nil {
119119
return false, errors.Wrap(err, "error adding decision to open case")
120120
}
121-
122121
if !added {
123-
caseName, err := d.caseNameEvaluator.EvalCaseName(ctx, params, scenario)
124-
if err != nil {
125-
return false, errors.Wrap(err, "error creating case for decision")
126-
}
127-
128-
input := automaticCreateCaseAttributes(scenario, decision, caseName)
129-
newCase, err := d.caseEditor.CreateCase(ctx, tx, "", input, false)
130-
if err != nil {
131-
return false, errors.Wrap(err, "error creating case for decision")
132-
}
133-
134-
err = d.webhookEventCreator.CreateWebhookEvent(ctx, tx, models.WebhookEventCreate{
135-
Id: webhookEventId,
136-
OrganizationId: newCase.OrganizationId,
137-
EventContent: models.NewWebhookEventCaseCreatedWorkflow(newCase.GetMetadata()),
138-
})
139-
if err != nil {
140-
return false, err
141-
}
142-
143-
return true, nil
122+
return createNewCaseForDecision(ctx)
144123
}
145124

146125
err = d.webhookEventCreator.CreateWebhookEvent(ctx, tx, models.WebhookEventCreate{
@@ -153,9 +132,9 @@ func (d DecisionsWorkflows) AutomaticDecisionToCase(
153132
}
154133

155134
return true, nil
135+
default:
136+
return false, errors.New(fmt.Sprintf("unknown workflow type: %s", scenario.DecisionToCaseWorkflowType))
156137
}
157-
158-
return false, errors.New(fmt.Sprintf("unknown workflow type: %s", scenario.DecisionToCaseWorkflowType))
159138
}
160139

161140
func automaticCreateCaseAttributes(
@@ -190,29 +169,32 @@ func (d DecisionsWorkflows) addToOpenCase(
190169
return models.CaseMetadata{}, false, errors.Wrap(err, "error selecting cases with pivot")
191170
}
192171

193-
if len(eligibleCases) == 0 {
172+
var bestMatchCase models.CaseMetadata
173+
switch len(eligibleCases) {
174+
case 0:
194175
return models.CaseMetadata{}, false, nil
195-
}
196-
197-
caseIds := make([]string, 0, len(eligibleCases))
198-
for _, c := range eligibleCases {
199-
caseIds = append(caseIds, c.Id)
200-
}
176+
case 1:
177+
bestMatchCase = eligibleCases[0]
178+
default:
179+
caseIds := make([]string, len(eligibleCases))
180+
for i, c := range eligibleCases {
181+
caseIds[i] = c.Id
182+
}
201183

202-
decisionCounts, err := d.repository.CountDecisionsByCaseIds(ctx, tx, scenario.OrganizationId, caseIds)
203-
if err != nil {
204-
return models.CaseMetadata{}, false, errors.Wrap(err, "error counting decisions by case ids")
205-
}
184+
decisionCounts, err := d.repository.CountDecisionsByCaseIds(ctx, tx, scenario.OrganizationId, caseIds)
185+
if err != nil {
186+
return models.CaseMetadata{}, false, errors.Wrap(err, "error counting decisions by case ids")
187+
}
206188

207-
cases := make([]caseMetadataWithDecisionCount, 0, len(eligibleCases))
208-
for _, c := range eligibleCases {
209-
cases = append(cases, caseMetadataWithDecisionCount{
210-
CaseMetadata: c,
211-
DecisionCount: decisionCounts[c.Id],
212-
})
189+
cases := make([]caseMetadataWithDecisionCount, len(eligibleCases))
190+
for i, c := range eligibleCases {
191+
cases[i] = caseMetadataWithDecisionCount{
192+
CaseMetadata: c,
193+
DecisionCount: decisionCounts[c.Id],
194+
}
195+
}
196+
bestMatchCase = findBestMatchCase(cases)
213197
}
214-
215-
bestMatchCase := findBestMatchCase(cases)
216198
err = d.caseEditor.UpdateDecisionsWithEvents(ctx, tx, bestMatchCase.Id, "", []string{decision.DecisionId})
217199
if err != nil {
218200
return models.CaseMetadata{}, false, errors.Wrap(err, "error updating case")

0 commit comments

Comments
 (0)