userActions.ts module provides utility functions for managing Cryptlex users from payment platform webhook events.
Functions
checkUserExists
Checks if a user exists by email address.The email address to search for
The Cryptlex API client instance
Returns the user ID if found, or
null if no user exists with that emailcreateUser
Creates a new user in Cryptlex with the provided details.User’s email address
User’s first name (from payment platform)
The Cryptlex API client instance
User’s last name
User’s company name
Additional metadata to attach to the user
The ID of the newly created user
- Generates a random 10-character password using
nanoid(10) - Sets user role to
'user' - Only includes optional fields if they are defined and not null
updateUser
Updates an existing user’s details.The ID of the user to update
New first name (only updated if not empty)
The Cryptlex API client instance
New last name
New company name
New email address (only updated if not empty or null)
The ID of the updated user
- Only includes fields in the update request if they are defined and not null/empty
- Uses PATCH request to
/v3/users/{id}
insertUser
Checks for an existing user with the same email or creates a new one if it doesn’t exist. Returns the user ID.User’s email address
Name to be set for the created user
The Cryptlex API client instance
User’s last name
User’s company name
The user ID (existing or newly created)
- Handles race conditions: if two webhook events try to create the same user simultaneously, it checks again after creation failure
- Does NOT update existing users (use
upsertUserfor that)
upsertUser
Updates an existing user with the same email or creates a new one if it doesn’t exist. Returns the user ID.User’s email address
Name to be set for the user
The Cryptlex API client instance
User’s last name
User’s company name
The user ID (existing or newly created)
- Unlike
insertUser, this function WILL update existing users - Handles race conditions similar to
insertUser - Preferred for customer creation events where you want to ensure the latest data
When to Use Each Function
| Function | Use Case |
|---|---|
checkUserExists | Internal helper - not typically called directly |
createUser | When you’re certain a new user needs to be created |
updateUser | When you have a user ID and need to update details |
insertUser | For license creation events where you need a user ID but shouldn’t modify existing users |
upsertUser | For customer creation/update events where you want to keep user data in sync |