ReferralCodeUsage schema for tracking referral code usage in PhoenixKit.
This schema records when and by whom referral codes are used. It provides an audit trail for code usage and helps with analytics.
Fields
code_uuid: Foreign key to the referral code that was usedused_by_uuid: UUID of the user who used the codedate_used: Timestamp when the code was used
Associations
referral_code: Belongs to the referral code that was used
Usage Examples
# Record a code usage
%ReferralCodeUsage{}
|> ReferralCodeUsage.changeset(%{
code_uuid: referral_code.uuid,
used_by_uuid: user.uuid
})
|> Repo.insert()
# Get all usage records for a code
from(usage in ReferralCodeUsage, where: usage.code_uuid == ^code_uuid)
|> Repo.all()
Summary
Functions
Creates a changeset for referral code usage records.
Gets all usage records for a specific referral code.
Gets all usage records for a specific user.
Gets usage statistics for a referral code.
Checks if a user has already used a specific referral code.
Functions
Creates a changeset for referral code usage records.
Validates that all required fields are present and automatically sets date_used on new records.
Gets all usage records for a specific referral code.
Returns a query that can be executed to get usage records ordered by date_used (most recent first).
Examples
iex> ReferralCodeUsage.for_code(code_uuid) |> Repo.all()
[%ReferralCodeUsage{}, ...]
Gets all usage records for a specific user.
Returns a query that can be executed to get usage records ordered by date_used (most recent first).
Examples
iex> ReferralCodeUsage.for_user(user_uuid) |> Repo.all()
[%ReferralCodeUsage{}, ...]
Gets usage statistics for a referral code.
Returns a map with usage counts and recent activity information.
Examples
iex> ReferralCodeUsage.get_usage_stats(code_uuid)
%{
total_uses: 5,
unique_users: 3,
last_used: ~U[2024-01-15 10:30:00Z],
recent_users: [user_uuid1, user_uuid2]
}
Checks if a user has already used a specific referral code.
Returns true if the user has used the code before, false otherwise.
Examples
iex> ReferralCodeUsage.user_used_code?(user_uuid, code_uuid)
false