This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
userUpdate.sml
86 lines (73 loc) · 1.98 KB
/
userUpdate.sml
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
fragment realworld {
"std" 1.0
"url/url" 1.0
}
NoChange = struct{}
# userUpdate is analogous to "PUT /api/user" resolving a mutation
# causing a permanent mutation of the t:User entity identified by
# p:username in case of success
userUpdate = (
# username identifies the user to be updated
username Username,
# newUsername doesn't change User.email when not given
newUsername ?Username,
# newEmail doesn't change User.email when not given
newEmail ?EmailAddress,
# newBio resets User.bio to unset when not given,
# use NoChange to leave User.bio unchanged
newBio ?(String or NoChange),
# newImage resets User.image to unset when not given,
# use NoChange to leave User.image unchanged
newImage ?(url::Url or NoChange),
) -> (
std::Mutation<UserResolver> or
ErrUnauth or
ErrUserNotFound or
ErrUsernameReserved or
ErrEmailReserved
) => {
user = entity<User>(predicate: (u) => u.username == username)
userByNewUsername = entity<User>(predicate: (u) => u.username == newUsername)
userByNewEmail = entity<User>(predicate: (u) => u.email == newEmail)
& = match {
// Ensure the profile exists
user == Nil then ErrUserNotFound
// Ensure only the owner is allowed to update a profile
!isOwner(owner: User from user) then ErrUnauth
// Ensure username uniqueness
userByNewUsername != Nil then ErrUsernameReserved
// Ensure email uniqueness
userByNewEmail != Nil then ErrEmailReserved
else {
user = User from user
updatedProfile = User{
username: newUsername as v {
Username then v
else user.username
},
email: newEmail as v {
EmailAddress then v
else user.email
},
bio: newBio as v {
NoChange then user.bio
String then v
else Nil
},
image: newImage as v {
NoChange then user.image
url::Url then v
else Nil
},
..user,
}
& = std::Mutation{
effects: {
// Update the profile
std::mutated(user, updatedProfile),
},
data: UserResolver{user: updatedProfile},
}
}
}
}