Skip to content

Commit 6cd1392

Browse files
authored
Merge pull request #122 from Southern-Exposure-Seed-Exchange/develop
Update master with #121
2 parents 84f1fe7 + e50114c commit 6cd1392

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

client/src/Pages/Checkout.elm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ update msg postgridApiKey model authStatus maybeSessionToken checkoutDetails =
641641
, getHelcimCheckoutToken
642642
authStatus
643643
maybeSessionToken
644+
model.email
644645
(checkoutDetailsToCartInventoryNotifications details)
645646
model.shippingAddress
646647
model.skipAddressVerification
@@ -1112,8 +1113,8 @@ encodeStringAsMaybe str =
11121113
Encode.string str
11131114

11141115

1115-
getHelcimCheckoutToken : AuthStatus -> Maybe String -> CartInventoryNotifications -> CheckoutAddress -> Bool -> Cmd Msg
1116-
getHelcimCheckoutToken authStatus maybeSessionToken cartInventoryNotifications shippingAddress skipAddressVerification =
1116+
getHelcimCheckoutToken : AuthStatus -> Maybe String -> String -> CartInventoryNotifications -> CheckoutAddress -> Bool -> Cmd Msg
1117+
getHelcimCheckoutToken authStatus maybeSessionToken email cartInventoryNotifications shippingAddress skipAddressVerification =
11171118
case authStatus of
11181119
User.Authorized _ ->
11191120
let
@@ -1137,6 +1138,7 @@ getHelcimCheckoutToken authStatus maybeSessionToken cartInventoryNotifications s
11371138
, ( "sessionToken", encodeMaybe Encode.string maybeSessionToken )
11381139
, ( "shippingAddress", encodeAddress shippingAddress False )
11391140
, ( "skipAddressVerification", Encode.bool skipAddressVerification )
1141+
, ( "email", Encode.string email )
11401142
]
11411143
in
11421144
Api.post Api.CheckoutHelcimTokenAnonymous

server/src/Routes/Checkout.hs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import Routes.AvalaraUtils (createAvalaraTransaction, createAvalaraCustomer)
6262
import Routes.Utils (getDisabledCheckoutDetails, getClientIP, generateUniqueToken)
6363
import Routes.Carts (ValidateCartParameters(..))
6464
import Routes.Customers (RegistrationParametersWith(..), registerNewCustomer)
65-
import Validation (Validation(..))
65+
import Validation (Validation(..), singleFieldError)
6666
import Workers (Task(..), AvalaraTask(..), enqueueTask)
6767

6868
import qualified Avalara
@@ -1508,10 +1508,12 @@ getCheckoutTokenRoute token CheckoutTokenParameters {..} = withValidatedCookie t
15081508
(Entity cartId _) <- getBy (UniqueCustomerCart $ Just customerId)
15091509
>>= maybe (throwIO CartNotFound) return
15101510
checkCartAndShippingAddress ctpCartInventoryNotifications cartId ctpShippingAddress ctpSkipShippingAddressVerification $
1511-
lift $ getCheckoutToken (customerHelcimCustomerId customer)
1511+
lift $ getCheckoutToken $
1512+
maybe (Left $ customerEmail customer) Right (customerHelcimCustomerId customer)
15121513

15131514
data AnonymousCheckoutTokenParameters = AnonymousCheckoutTokenParameters
15141515
{ actpCartToken :: T.Text
1516+
, actpEmail :: T.Text
15151517
, actpCartInventoryNotifications :: CartInventoryNotifications
15161518
, actpShippingAddress :: AddressData
15171519
, actpSkipShippingAddressVerification :: Bool
@@ -1521,6 +1523,7 @@ instance FromJSON AnonymousCheckoutTokenParameters where
15211523
parseJSON = withObject "AnonymousCheckoutTokenParameters" $ \v ->
15221524
AnonymousCheckoutTokenParameters
15231525
<$> v .: "sessionToken"
1526+
<*> v .: "email"
15241527
<*> v .: "cartInventoryNotifications"
15251528
<*> v .: "shippingAddress"
15261529
<*> v .: "skipAddressVerification"
@@ -1531,17 +1534,27 @@ type AnonymousCheckoutTokenRoute =
15311534

15321535
anonymousGetCheckoutTokenRoute :: AnonymousCheckoutTokenParameters -> App (CheckoutResult CheckoutTokenData)
15331536
anonymousGetCheckoutTokenRoute AnonymousCheckoutTokenParameters {..} = runDB $ do
1537+
customerExists <- lift $ V.uniqueCustomer actpEmail
1538+
when customerExists $
1539+
lift $ singleFieldError "email" "Email is already associated with an existing account. Log in or use a different email."
15341540
(Entity cartId _) <- getBy (UniqueAnonymousCart $ Just actpCartToken)
15351541
>>= maybe (throwIO CartNotFound) return
15361542
checkCartAndShippingAddress actpCartInventoryNotifications cartId (NewAddress actpShippingAddress) actpSkipShippingAddressVerification $
1537-
lift $ getCheckoutToken Nothing
1543+
lift $ getCheckoutToken (Left actpEmail)
15381544

1539-
getCheckoutToken :: Maybe Helcim.CustomerId -> App CheckoutTokenData
1540-
getCheckoutToken mbHelcimCustomerId = do
1541-
mbCustomerCode <- case mbHelcimCustomerId of
1542-
Just helcimCustomerId -> do
1545+
getCheckoutToken :: Either T.Text Helcim.CustomerId -> App CheckoutTokenData
1546+
getCheckoutToken emailOrHelcimCustomerId = do
1547+
mbCustomerCode <- case emailOrHelcimCustomerId of
1548+
Right helcimCustomerId -> do
15431549
getCustomer helcimCustomerId >>= either (throwIO . HelcimError) (return . Just . creCustomerCode)
1544-
Nothing -> return Nothing
1550+
Left email -> do
1551+
getCustomers GetCustomersRequest
1552+
{ gcrSearch = Just email
1553+
, gcrCustomerCode = Nothing
1554+
, gcrLimit = Just 1
1555+
, gcrPage = Just 1
1556+
, gcrIncludeCards = Nothing
1557+
} >>= either (throwIO . HelcimError) (return . fmap creCustomerCode . listToMaybe)
15451558
createVerifyCheckout mbCustomerCode >>= \case
15461559
Left e -> throwIO $ HelcimError e
15471560
Right CheckoutCreateResponse {..} ->

0 commit comments

Comments
 (0)