Skip to content

Commit

Permalink
Make create-daml-app setup idempotent (digital-asset#12320)
Browse files Browse the repository at this point in the history
* Make create-daml-app setup idempotent

changelog_begin
changelog_end

* review feedback

changelog_begin
changelog_end
  • Loading branch information
cocreature authored Jan 11, 2022
1 parent 98b7ad7 commit b845587
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions templates/create-daml-app/daml/Setup.daml
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
module Setup where

import DA.Foldable
import DA.Action (unless)
import DA.Foldable (forA_)
import qualified DA.Text as T
import Daml.Script

setup : Script ()
setup = do
forA_ [("Alice", "alice"), ("Bob", "bob"), ("Charlie", "charlie")] $ \(displayName, userName) -> do
p <- allocatePartyWithHint displayName (PartyIdHint ("p" <> userName))
createUser (User userName (Some p)) [CanActAs p]
let displayNames = ["Alice", "Bob", "Charlie"]
forA_ displayNames $ \displayName -> do
let userId = toUserId displayName
u <- getUser userId
case u of
Some _ ->
-- user already exists do nothing
pure ()
None -> do
let partyIdHint = toPartyIdHint displayName
p <- allocatePartyWithHint displayName (PartyIdHint partyIdHint)
createUser (User userId (Some p)) [CanActAs p]
pure ()

toUserId : Text -> Text
toUserId = T.asciiToLower

-- TODO Drop hints once we have aliases.
toPartyIdHint : Text -> Text
toPartyIdHint p = "p" <> toUserId p

0 comments on commit b845587

Please sign in to comment.