-
Notifications
You must be signed in to change notification settings - Fork 0
/
crconverter.go
93 lines (72 loc) · 1.71 KB
/
crconverter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package pipetimer
import (
"strconv"
"sync"
"time"
"github.com/vitraum/golang-pipedrive"
)
type sourceMap map[string]string
type stageMap map[int]string
type sourceCache struct {
smap sourceMap
filled bool
sync.Mutex
}
type AgeCalculator func(a, b time.Time) time.Duration
var cache sourceCache
type ChangeResultConverter struct {
pipedrive.PipelineChangeResult
sourceMapping sourceMap
ageCalculator AgeCalculator
stages stageMap
}
func NewChangeResultConverter(cr pipedrive.PipelineChangeResult, api *pipedrive.API, age AgeCalculator, stages pipedrive.Stages) *ChangeResultConverter {
stageMapping := stageMap{}
for _, stage := range stages {
stageMapping[stage.Id] = stage.Name
}
return &ChangeResultConverter{
cr,
fetchSourceMapping(api),
age,
stageMapping,
}
}
func (cr *ChangeResultConverter) ID() string {
return strconv.Itoa(cr.Deal.Id)
}
func (cr *ChangeResultConverter) Status() string {
return cr.Deal.Status
}
func (cr *ChangeResultConverter) Added() time.Time {
return cr.Deal.Added.Time
}
func (cr *ChangeResultConverter) Value() float64 {
return cr.Deal.Value
}
func (cr *ChangeResultConverter) Source() string {
return cr.Deal.Source
}
func (cr *ChangeResultConverter) LastStage() string {
stageName, ok := cr.stages[cr.Deal.Stage]
if ok {
return stageName
} else {
return ""
}
}
func (cr *ChangeResultConverter) Age() time.Duration {
return cr.ageCalculator(cr.DecisionTime(), cr.Deal.Added.Time)
}
func (cr *ChangeResultConverter) DealUpdates() pipedrive.DealFlowUpdates {
return cr.Updates
}
func fetchSourceMapping(api *pipedrive.API) sourceMap {
cache.Lock()
defer cache.Unlock()
if cache.filled {
return cache.smap
}
result := sourceMap{}
return result
}