Skip to content

Commit f7a21ea

Browse files
authored
Merge pull request #135 from unisoncomputing/cp/fix-bad-user-update
Fix error on setting nullable field to empty string
2 parents 139d76f + 5cbb2eb commit f7a21ea

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/Share/Postgres/Users/Queries.hs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ updateUser toUpdateUserId newName newAvatarUrl newBio newWebsite newLocation new
125125
pronouns = existingPronouns
126126
} <-
127127
MaybeT $ userProfileById toUpdateUserId
128-
let updatedName = fromNullableUpdate existingName newName
128+
let updatedName = nullifyEmpty $ fromNullableUpdate existingName newName
129129
let updatedAvatarUrl = fromNullableUpdate existingAvatarUrl newAvatarUrl
130-
let updatedBio = fromNullableUpdate existingBio newBio
131-
let updatedWebsite = fromNullableUpdate existingWebsite newWebsite
132-
let updatedLocation = fromNullableUpdate existingLocation newLocation
133-
let updatedTwitterHandle = fromNullableUpdate existingTwitterHandle newTwitterHandle
134-
let updatedPronouns = fromNullableUpdate existingPronouns newPronouns
130+
let updatedBio = nullifyEmpty $ fromNullableUpdate existingBio newBio
131+
let updatedWebsite = nullifyEmpty $ fromNullableUpdate existingWebsite newWebsite
132+
let updatedLocation = nullifyEmpty $ fromNullableUpdate existingLocation newLocation
133+
let updatedTwitterHandle = nullifyEmpty $ fromNullableUpdate existingTwitterHandle newTwitterHandle
134+
let updatedPronouns = nullifyEmpty $ fromNullableUpdate existingPronouns newPronouns
135135
PG.execute_
136136
[PG.sql|
137137
UPDATE users
@@ -145,6 +145,13 @@ updateUser toUpdateUserId newName newAvatarUrl newBio newWebsite newLocation new
145145
pronouns = #{updatedPronouns}
146146
WHERE id = #{toUpdateUserId}
147147
|]
148+
where
149+
-- Some fields must be non-empty or NULL.
150+
-- If the user sets it to non-empty, just null it out.
151+
nullifyEmpty mt = do
152+
case mt of
153+
Just t | Text.null t -> Nothing
154+
_ -> mt
148155

149156
expectUserByUserId :: (PG.QueryM m) => UserId -> m User
150157
expectUserByUserId uid = do

0 commit comments

Comments
 (0)