Skip to content

Commit

Permalink
have separate email field as well
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-fi committed Mar 20, 2020
1 parent 766add9 commit 2182421
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 2 deletions.
17 changes: 17 additions & 0 deletions frontend/pages/AccountPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
AccountChangeDescription,
AccountChangeUrl,
AccountChangeUsername,
AccountChangeEmail,
AccountDelete,
AccountDeleteSecret,
AccountRename,
Expand Down Expand Up @@ -306,6 +307,22 @@ export default class AccountPage extends React.Component<AccountPageProps, Accou
<ClipboardButton text={account.Username} />
</td>
</tr>
<tr>
<th>
Email
<span className="margin-left">
<CommandIcon
command={AccountChangeEmail(account.Id, account.Email)}
/>
</span>
</th>
<td>
<OptionalContent>{account.Email}</OptionalContent>
</td>
<td>
<ClipboardButton text={account.Email} />
</td>
</tr>
{secretRows}
<tr>
<th>
Expand Down
12 changes: 12 additions & 0 deletions pkg/apitypes/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@
{ "key": "Username", "optional": true }
]
},
{
"command": "account.ChangeEmail",
"chain": "authenticated",
"ctor": ["Account","Email"],
"crudNature": "update",
"title": "Change email",
"fields": [
{ "key": "Account", "hideIfDefaultValue": true },
{ "key": "Email", "placeholder": "[email protected]", "optional": true }
]
},
{
"command": "account.ChangeDescription",
"chain": "authenticated",
Expand Down Expand Up @@ -119,6 +130,7 @@
{ "key": "Title", "placeholder": "Reddit", "optional": true, "help": "If you don`t specify this, then URL is required" },
{ "key": "Url", "placeholder": "https://www.reddit.com/", "title": "URL", "optional": true, "help": "If you don`t specify this, then Title is required" },
{ "key": "Username", "placeholder": "[email protected]", "optional": true },
{ "key": "Email", "optional": true, "help": "If your account has username AND email, specify it here. If the account uses email as username, then write email in username." },
{ "key": "Password", "type": "password", "optional": true },
{ "key": "PasswordRepeat", "placeholder": "(optional, to prevent typos)", "title": "Repeat password", "type": "password", "optional": true }
]
Expand Down
1 change: 1 addition & 0 deletions pkg/apitypes/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"Created": {"_": "datetime"},
"FolderId": {"_": "string"},
"Title": {"_": "string"},
"Email": {"_": "string"},
"Url": {"_": "string"},
"Username": {"_": "string"},
"Description": {"_": "string"}
Expand Down
20 changes: 20 additions & 0 deletions pkg/commands/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ func (h *Handlers) AccountChangeUsername(a *apitypes.AccountChangeUsername, ctx
return nil
}

func (h *Handlers) AccountChangeEmail(a *apitypes.AccountChangeEmail, ctx *command.Ctx) error {
if h.userData(ctx).WrappedAccountById(a.Account) == nil {
return errAccountNotFound
}

ctx.RaisesEvent(domain.NewAccountEmailChanged(
a.Account,
a.Email,
ctx.Meta))

return nil
}

func (h *Handlers) AccountChangeUrl(a *apitypes.AccountChangeUrl, ctx *command.Ctx) error {
if h.userData(ctx).WrappedAccountById(a.Account) == nil {
return errAccountNotFound
Expand Down Expand Up @@ -227,6 +240,13 @@ func (h *Handlers) AccountCreate(a *apitypes.AccountCreate, ctx *command.Ctx) er
ctx.Meta))
}

if a.Email != "" {
ctx.RaisesEvent(domain.NewAccountEmailChanged(
accountId,
a.Email,
ctx.Meta))
}

if a.Password != "" {
// if PasswordRepeat given, verify it
if err := verifyRepeatPassword(a.Password, a.PasswordRepeat); a.PasswordRepeat != "" && err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/domain/events.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@
}
]
},
{
"event": "account.EmailChanged",
"ctor": ["Id", "Email"],
"fields": [
{
"key": "Id", "type": {"_": "string"}
},
{
"key": "Email", "type": {"_": "string"}
}
]
},
{
"event": "account.FolderCreated",
"ctor": ["Id", "ParentId", "Name"],
Expand Down
2 changes: 2 additions & 0 deletions pkg/state/userstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ func (l *UserStorage) processEvent(ev ehevent.Event) error {
}
case *domain.AccountUsernameChanged:
l.accounts[e.Id].Account.Username = e.Username
case *domain.AccountEmailChanged:
l.accounts[e.Id].Account.Email = e.Email
case *domain.AccountUrlChanged:
l.accounts[e.Id].Account.Url = e.Url
case *domain.AccountRenamed:
Expand Down
8 changes: 6 additions & 2 deletions pkg/state/userstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,23 @@ func addAccount(t *testing.T, tc *testContext) {
domain.NewAccountUrlChanged(testAccId, "https://google.com/", ehevent.Meta(t0, joonasUid)))

tc.appendAndLoad(
domain.NewAccountUsernameChanged(testAccId, "[email protected]", ehevent.Meta(t0, joonasUid)))
domain.NewAccountUsernameChanged(testAccId, "joonas.fi", ehevent.Meta(t0, joonasUid)))

tc.appendAndLoad(
domain.NewAccountEmailChanged(testAccId, "[email protected]", ehevent.Meta(t0, joonasUid)))

tc.appendAndLoad(
domain.NewAccountDescriptionChanged(testAccId, "Notes for account\nLine 2", ehevent.Meta(t0, joonasUid)))

assert.EqualJson(t, tc.user.accounts[testAccId].Account, `{
"Created": "2020-02-20T14:02:00Z",
"Description": "Notes for account\nLine 2",
"Email": "[email protected]",
"FolderId": "root",
"Id": "accId1",
"Title": "google.com",
"Url": "https://google.com/",
"Username": "joonas@example.com"
"Username": "joonas.fi"
}`)
}

Expand Down

1 comment on commit 2182421

@joonas-fi
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implements #107

Please sign in to comment.