|
1 | 1 | package events |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "time" |
6 | 7 |
|
7 | 8 | "github.com/rs/xid" |
8 | 9 | ) |
9 | 10 |
|
10 | | -//#region EventDataType |
11 | | - |
12 | | -// EventObjectType holds the event type we are working. |
13 | | -type EventObjectType string |
14 | | - |
15 | | -const ( |
16 | | - // SubscriptionDataType describes the type of the data we are holding. |
17 | | - // It allow us easily map data from the database to a struct |
18 | | - SubscriptionDataType EventObjectType = "subscription" |
19 | | - |
20 | | - // PurchaseDataType describes the type of the data we are holding. |
21 | | - // It allow us easily map data from the database to a struct |
22 | | - PurchaseDataType EventObjectType = "purchase" |
23 | | -) |
24 | | - |
25 | | -//#endregion EventDataType |
26 | | - |
27 | | -//#region event types |
28 | | - |
29 | | -// EventType is a unique key defined by event |
30 | | -type EventType string |
31 | | - |
32 | | -const ( |
33 | | - // AppSubscribedEventType defines the string constant name for the event that happens when |
34 | | - // a user subscribes to a plan |
35 | | - AppSubscribedEventType EventType = "app.subscription.created" |
36 | | - |
37 | | - // AppSubscriptionCancelledEventType defines the string constant name for the event that happens when |
38 | | - // a user cancels a subscription |
39 | | - AppSubscriptionCancelledEventType EventType = "app.subscription.cancelled" |
40 | | - |
41 | | - // AppSubscriptionModifiedEventType defines the string constant name for the event that happens when |
42 | | - // a subscription is modified |
43 | | - AppSubscriptionModifiedEventType EventType = "app.subscription.modified" |
44 | | - |
45 | | - // AppPurchasedEventType indicates which type the struct is. Could make it easier for us data map this event from the database |
46 | | - AppPurchasedEventType EventType = "app.purchase.created" |
47 | | -) |
48 | | - |
49 | | -func (et EventType) Valid() bool { |
50 | | - return et == AppSubscribedEventType || et == AppPurchasedEventType || |
51 | | - et == AppSubscriptionCancelledEventType || et == AppSubscriptionModifiedEventType |
52 | | -} |
53 | | - |
54 | | -//#endregion event types |
55 | | - |
56 | 11 | // App holds information about the application which the event was triggered |
57 | 12 | type App struct { |
58 | 13 | // ID defines the application id that is provided by the publisher |
@@ -116,17 +71,22 @@ type Event struct { |
116 | 71 | When time.Time `bson:"when" json:"when"` |
117 | 72 |
|
118 | 73 | // Data has the event specific payload |
119 | | - Data interface{} `bson:"data" json:"data"` |
| 74 | + Data json.RawMessage `bson:"data" json:"data"` |
120 | 75 | } |
121 | 76 |
|
122 | 77 | // New creates a new event with the status of created |
123 | 78 | func New(name EventType, data interface{}) Event { |
| 79 | + marshalledJson, err := json.Marshal(data) |
| 80 | + if err != nil { |
| 81 | + panic(fmt.Sprintf("error creating a new event: %s", err.Error())) |
| 82 | + } |
| 83 | + |
124 | 84 | return Event{ |
125 | 85 | ID: fmt.Sprintf("evt_%s", xid.New().String()), |
126 | 86 | Type: name, |
127 | 87 | When: time.Now(), |
128 | 88 | APIVersion: CurrentAPIVersion, |
129 | | - Data: data, |
| 89 | + Data: marshalledJson, |
130 | 90 | } |
131 | 91 | } |
132 | 92 |
|
|
0 commit comments