This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreexisting_events.go
145 lines (125 loc) · 4.65 KB
/
preexisting_events.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package copilot
import (
"errors"
"fmt"
)
// below are a list of user events which can be helpful instead of remembering the strings
const (
EventTypePreexistingSyncStarted = "preexisting_sync_started"
EventTypePreexistingSyncCompleted = "preexisting_sync_completed"
EventTypePreexistingUserCreated = "preexisting_user_created"
EventTypePreexistingThingCreated = "preexisting_thing_created"
EventTypePreexistingUserThingAssociated = "preexisting_user_thing_associated"
)
// PreexistingUserEventPayload is a preexisting user
type PreexistingUserEventPayload struct {
UserID *string `json:"user_id,omitempty"`
FirstName *string `json:"first_name,omitempty"`
LastName *string `json:"last_name,omitempty"`
Email *string `json:"email,omitempty"`
UTCOffset *string `json:"utc_offset,omitempty"`
OriginalCreationDate *int64 `json:"original_creation_date,omitempty"`
CopilotAnalysisConsent *bool `json:"copilot_analysis_consent,omitempty"`
}
// Preexisting is a preexisting thing
type PreexistingThingCreatedPayload struct {
ThingID *string `json:"thing_id,omitempty"`
UserID *string `json:"user_id,omitempty"`
FirmwareVersion *string `json:"firmware_version,omitempty"`
Model *string `json:"model,omitempty"`
OriginalCreationDate *int64 `json:"original_creation_date,omitempty"`
}
// SyncStarted tells Copilot that a sync of preexisting data has started
func SyncStarted(timestamp int64, eventID string) error {
if eventID == "" {
eventID = fmt.Sprintf("%s-%d", EventTypePreexistingSyncStarted, timestamp)
}
event := Event{
EventID: eventID,
Type: EventTypePreexistingSyncStarted,
Timestamp: timestamp,
Payload: map[string]string{},
}
return event.sendEvent()
}
// SyncCompleted tells Copilot that a sync of preexisting data has completed
func SyncCompleted(timestamp int64, eventID string) error {
if eventID == "" {
eventID = eventIDHelper(EventTypePreexistingSyncCompleted, "", timestamp)
}
event := Event{
EventID: eventID,
Type: EventTypePreexistingSyncCompleted,
Timestamp: timestamp,
Payload: map[string]string{},
}
return event.sendEvent()
}
// PreexistingUserCreated tells Copilot that a user has previously been created. Ideally, the payload.OriginalCreationDate
// field should be set to a Unix timestamp in milliseconds of when the user first was created.
func PreexistingUserCreated(userID string, timestamp int64, eventID string, payload *PreexistingUserEventPayload) error {
// basic error checking and set some defaults
if userID == "" {
return errors.New("userID cannot be blank")
}
if payload == nil {
payload = &PreexistingUserEventPayload{}
}
payload.UserID = &userID
if eventID == "" {
eventID = eventIDHelper(EventTypePreexistingUserCreated, userID, timestamp)
}
event := Event{
EventID: eventID,
Type: EventTypePreexistingUserCreated,
Timestamp: timestamp,
Payload: payload,
}
return event.sendEvent()
}
// PreexistingThingCreated tells Copilot that a thing has previously been created. Ideally, the payload.OriginalCreationDate
// field should be set to a Unix timestamp in milliseconds of when the user first was created.
func PreexistingThingCreated(thingID string, timestamp int64, eventID string, payload *PreexistingThingCreatedPayload) error {
// basic error checking and set some defaults
if thingID == "" {
return errors.New("thingID cannot be blank")
}
if payload == nil {
payload = &PreexistingThingCreatedPayload{}
}
payload.ThingID = &thingID
if eventID == "" {
eventID = eventIDHelper(EventTypePreexistingThingCreated, thingID, timestamp)
}
event := Event{
EventID: eventID,
Type: EventTypePreexistingThingCreated,
Timestamp: timestamp,
Payload: payload,
}
return event.sendEvent()
}
// PreexistingThingUserAssociated tells Copilot about a preexisting thing/user association
func PreexistingThingUserAssociated(thingID string, userID string, timestamp int64, eventID string, originalAssociationDate int64) error {
// basic error checking and set some defaults
if thingID == "" || userID == "" {
return errors.New("thingID and userID cannot be blank")
}
payload := map[string]interface{}{
"user_id": userID,
"thing_id": thingID,
}
if originalAssociationDate != 0 {
payload["original_association_date"] = originalAssociationDate
}
if eventID == "" {
eventID = eventIDHelper(EventTypePreexistingUserThingAssociated, thingID, timestamp)
}
event := Event{
EventID: eventID,
Type: EventTypePreexistingUserThingAssociated,
Timestamp: timestamp,
Payload: payload,
}
return event.sendEvent()
}