Last active
November 20, 2019 09:36
-
-
Save frikky/ed56fc195f65a4bd567c48d305ac0206 to your computer and use it in GitHub Desktop.
To roll back a user (simple structure, could be way more complex)
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
// Handles the rollback to a previous document | |
func handleRollbackUser(ctx context.Context, e FirestoreEvent) error { | |
// This can be grabbed from split of e.OldValue.Name | |
clientDoc := client.Collection("users").Doc(strings.Split(e.OldValue.Name, "/")[6]) | |
// Again, you have to | |
type NewUser struct { | |
Username string `json:"userId"` | |
Email string `json:"email"` | |
DateEdited int64 `json:"date_edited"` | |
} | |
// Might crash on this | |
newDate, err := strconv.Atoi(e.OldValue.Fields.DateEdited.IntegerValue) | |
if err != nil { | |
return err | |
} | |
translatedUser := NewUser{ | |
Username: e.OldValue.Fields.Username.StringValue, | |
Email: e.OldValue.Fields.Email.StringValue, | |
DateEdited: int64(newDate), | |
} | |
setter, err := clientDoc.Set(ctx, translatedUser) | |
if err != nil { | |
return err | |
} | |
log.Printf("Successfully updated document. Data: %#v", setter) | |
return errors.New("Should have rolled back to a previous version") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment