-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: payments metadata Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> * fix: lint & tests Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> * fix: tests Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com>
- Loading branch information
1 parent
891d658
commit 4ea8b5d
Showing
14 changed files
with
183 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,6 @@ lint: | |
lint-fix: | ||
golangci-lint run --fix | ||
|
||
run-tests: | ||
go test -race -count=1 ./... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package payments | ||
|
||
const ( | ||
Collection = "Payments" | ||
ConnectorsCollection = "Connectors" | ||
TasksCollection = "Tasks" | ||
Collection = "Payments" | ||
ConnectorsCollection = "Connectors" | ||
TasksCollection = "Tasks" | ||
MetadataChangelogCollection = "MetadataChangelog" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package payments | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type Metadata map[string]any | ||
|
||
type MetadataChanges struct { | ||
PaymentID string `json:"paymentID" bson:"paymentID"` | ||
OccurredAt time.Time `json:"occurredAt" bson:"occurredAt"` | ||
Before Metadata `json:"before" bson:"before"` | ||
After Metadata `json:"after" bson:"after"` | ||
} | ||
|
||
func (mc MetadataChanges) HasChanged() bool { | ||
if mc.Before == nil { | ||
return false | ||
} | ||
|
||
return !mc.Before.Equal(mc.After) | ||
} | ||
|
||
func (p *Payment) MergeMetadata(metadata Metadata) MetadataChanges { | ||
changes := MetadataChanges{ | ||
PaymentID: p.Identifier.String(), | ||
OccurredAt: time.Now(), | ||
Before: copyMap(p.Metadata), | ||
} | ||
|
||
if p.Metadata == nil { | ||
p.Metadata = make(Metadata) | ||
} | ||
|
||
for key, value := range metadata { | ||
p.Metadata[key] = value | ||
} | ||
|
||
changes.After = p.Metadata | ||
|
||
return changes | ||
} | ||
|
||
func (m Metadata) Equal(comparableMetadata Metadata) bool { | ||
if len(m) != len(comparableMetadata) { | ||
return false | ||
} | ||
|
||
for key, value := range m { | ||
if v, ok := comparableMetadata[key]; !ok || fmt.Sprint(v) != fmt.Sprint(value) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func copyMap[K string, V any](m map[K]V) map[K]V { | ||
result := make(map[K]V) | ||
for k, v := range m { | ||
result[k] = v | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters