This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
action_paymails.go
222 lines (177 loc) · 6.33 KB
/
action_paymails.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package bux
import (
"context"
"errors"
"time"
"github.com/BuxOrg/bux/utils"
"github.com/mrz1836/go-datastore"
)
// GetPaymailAddress will get a paymail address model
func (c *Client) GetPaymailAddress(ctx context.Context, address string, opts ...ModelOps) (*PaymailAddress, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_paymail_address")
// Get the paymail address
paymailAddress, err := getPaymailAddress(ctx, address, append(opts, c.DefaultModelOptions()...)...)
if err != nil {
return nil, err
} else if paymailAddress == nil {
return nil, ErrMissingPaymail
}
return paymailAddress, nil
}
// GetPaymailAddresses will get all the paymail addresses from the Datastore
func (c *Client) GetPaymailAddresses(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, queryParams *datastore.QueryParams, opts ...ModelOps) ([]*PaymailAddress, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_paymail_addresses")
// Get the paymail address
paymailAddresses, err := getPaymailAddresses(
ctx, metadataConditions, conditions, queryParams,
c.DefaultModelOptions(opts...)...,
)
if err != nil {
return nil, err
}
return paymailAddresses, nil
}
// GetPaymailAddressesCount will get a count of all the paymail addresses from the Datastore
func (c *Client) GetPaymailAddressesCount(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, opts ...ModelOps) (int64, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_paymail_addresses_count")
// Get the paymail address
count, err := getPaymailAddressesCount(
ctx, metadataConditions, conditions,
c.DefaultModelOptions(opts...)...,
)
if err != nil {
return 0, err
}
return count, nil
}
// GetPaymailAddressesByXPubID will get all the paymail addresses for an xPubID from the Datastore
func (c *Client) GetPaymailAddressesByXPubID(ctx context.Context, xPubID string, metadataConditions *Metadata,
conditions *map[string]interface{}, queryParams *datastore.QueryParams) ([]*PaymailAddress, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_paymail_by_xpub")
if conditions == nil {
*conditions = make(map[string]interface{})
}
// add the xpub_id to the conditions
(*conditions)["xpub_id"] = xPubID
// Get the paymail address
paymailAddresses, err := getPaymailAddresses(
ctx, metadataConditions, conditions, queryParams,
c.DefaultModelOptions()...,
)
if err != nil {
return nil, err
}
return paymailAddresses, nil
}
// NewPaymailAddress will create a new paymail address
func (c *Client) NewPaymailAddress(ctx context.Context, xPubKey, address, publicName, avatar string,
opts ...ModelOps) (*PaymailAddress, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "new_paymail_address")
// Get the xPub (make sure it exists)
_, err := getXpubWithCache(ctx, c, xPubKey, "", c.DefaultModelOptions()...)
if err != nil {
return nil, err
}
// Check if the paymail address already exists
paymail, err := getPaymailAddress(ctx, address, opts...)
if paymail != nil {
return nil, errors.New("paymail address already exists")
}
if err != nil {
return nil, err
}
// Start the new paymail address model
paymailAddress := newPaymail(
address,
append(opts, c.DefaultModelOptions(
New(),
WithXPub(xPubKey),
)...)...,
)
// Set the optional fields
paymailAddress.Avatar = avatar
paymailAddress.PublicName = publicName
// Save the model
if err = paymailAddress.Save(ctx); err != nil {
return nil, err
}
return paymailAddress, nil
}
// DeletePaymailAddress will delete a paymail address
func (c *Client) DeletePaymailAddress(ctx context.Context, address string, opts ...ModelOps) error {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "delete_paymail_address")
// Get the paymail address
paymailAddress, err := getPaymailAddress(ctx, address, append(opts, c.DefaultModelOptions()...)...)
if err != nil {
return err
} else if paymailAddress == nil {
return ErrMissingPaymail
}
// todo: make a better approach for deleting paymail addresses?
var randomString string
if randomString, err = utils.RandomHex(16); err != nil {
return err
}
// We will do a soft delete to make sure we still have the history for this address
// setting the Domain to a random string solved the problem of the unique index on Alias/Domain
// todo: figure out a different approach - history table?
paymailAddress.Alias = paymailAddress.Alias + "@" + paymailAddress.Domain
paymailAddress.Domain = randomString
paymailAddress.DeletedAt.Valid = true
paymailAddress.DeletedAt.Time = time.Now()
return paymailAddress.Save(ctx)
}
// UpdatePaymailAddressMetadata will update the metadata in an existing paymail address
func (c *Client) UpdatePaymailAddressMetadata(ctx context.Context, address string,
metadata Metadata, opts ...ModelOps) (*PaymailAddress, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "update_paymail_address_metadata")
// Get the paymail address
paymailAddress, err := getPaymailAddress(ctx, address, append(opts, c.DefaultModelOptions()...)...)
if err != nil {
return nil, err
} else if paymailAddress == nil {
return nil, ErrMissingPaymail
}
// Update the metadata
paymailAddress.UpdateMetadata(metadata)
// Save the model
if err = paymailAddress.Save(ctx); err != nil {
return nil, err
}
return paymailAddress, nil
}
// UpdatePaymailAddress will update optional fields of the paymail address
func (c *Client) UpdatePaymailAddress(ctx context.Context, address, publicName, avatar string,
opts ...ModelOps) (*PaymailAddress, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "update_paymail_address")
// Get the paymail address
paymailAddress, err := getPaymailAddress(ctx, address, append(opts, c.DefaultModelOptions()...)...)
if err != nil {
return nil, err
} else if paymailAddress == nil {
return nil, ErrMissingPaymail
}
// Update the public name
if paymailAddress.PublicName != publicName {
paymailAddress.PublicName = publicName
}
// Update the avatar
if paymailAddress.Avatar != avatar {
paymailAddress.Avatar = avatar
}
// Save the model
if err = paymailAddress.Save(ctx); err != nil {
return nil, err
}
return paymailAddress, nil
}