Add server-side audience filters to GET /users for notification targeting.
Extend UserFilter and GetAllUsers so admins can segment platform users by profile, subscription, engagement, and device fields when building notification audiences. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
426876c02f
commit
135c2bd5ce
|
|
@ -243,6 +243,140 @@ WHERE
|
||||||
AND (sqlc.narg('created_before')::TIMESTAMPTZ IS NULL OR created_at <= sqlc.narg('created_before')::TIMESTAMPTZ)
|
AND (sqlc.narg('created_before')::TIMESTAMPTZ IS NULL OR created_at <= sqlc.narg('created_before')::TIMESTAMPTZ)
|
||||||
AND (sqlc.narg('country')::TEXT IS NULL OR LOWER(TRIM(COALESCE(country, ''))) = LOWER(TRIM(sqlc.narg('country')::TEXT)))
|
AND (sqlc.narg('country')::TEXT IS NULL OR LOWER(TRIM(COALESCE(country, ''))) = LOWER(TRIM(sqlc.narg('country')::TEXT)))
|
||||||
AND (sqlc.narg('region')::TEXT IS NULL OR LOWER(TRIM(COALESCE(region, ''))) = LOWER(TRIM(sqlc.narg('region')::TEXT)))
|
AND (sqlc.narg('region')::TEXT IS NULL OR LOWER(TRIM(COALESCE(region, ''))) = LOWER(TRIM(sqlc.narg('region')::TEXT)))
|
||||||
|
AND (sqlc.narg('education_level')::TEXT IS NULL OR LOWER(TRIM(COALESCE(education_level, ''))) = LOWER(TRIM(sqlc.narg('education_level')::TEXT)))
|
||||||
|
AND (sqlc.narg('occupation')::TEXT IS NULL OR LOWER(TRIM(COALESCE(occupation, ''))) = LOWER(TRIM(sqlc.narg('occupation')::TEXT)))
|
||||||
|
AND (sqlc.narg('age_group')::TEXT IS NULL OR LOWER(TRIM(COALESCE(age_group, ''))) = LOWER(TRIM(sqlc.narg('age_group')::TEXT)))
|
||||||
|
AND (sqlc.narg('favourite_topic')::TEXT IS NULL OR LOWER(TRIM(COALESCE(favourite_topic, ''))) = LOWER(TRIM(sqlc.narg('favourite_topic')::TEXT)))
|
||||||
|
AND (sqlc.narg('language_goal')::TEXT IS NULL OR LOWER(TRIM(COALESCE(language_goal, ''))) = LOWER(TRIM(sqlc.narg('language_goal')::TEXT)))
|
||||||
|
AND (sqlc.narg('learning_goal')::TEXT IS NULL OR LOWER(TRIM(COALESCE(learning_goal, ''))) = LOWER(TRIM(sqlc.narg('learning_goal')::TEXT)))
|
||||||
|
AND (sqlc.narg('knowledge_level')::TEXT IS NULL OR LOWER(TRIM(COALESCE(knowledge_level, ''))) = LOWER(TRIM(sqlc.narg('knowledge_level')::TEXT)))
|
||||||
|
AND (sqlc.narg('profile_completed')::BOOLEAN IS NULL OR profile_completed = sqlc.narg('profile_completed')::BOOLEAN)
|
||||||
|
AND (sqlc.narg('initial_assessment_completed')::BOOLEAN IS NULL OR initial_assessment_completed = sqlc.narg('initial_assessment_completed')::BOOLEAN)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('min_profile_completion_pct')::INT IS NULL
|
||||||
|
OR profile_completion_percentage >= sqlc.narg('min_profile_completion_pct')::INT
|
||||||
|
)
|
||||||
|
AND (sqlc.narg('last_login_after')::TIMESTAMPTZ IS NULL OR last_login >= sqlc.narg('last_login_after')::TIMESTAMPTZ)
|
||||||
|
AND (sqlc.narg('last_login_before')::TIMESTAMPTZ IS NULL OR last_login <= sqlc.narg('last_login_before')::TIMESTAMPTZ)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('days_since_last_login_min')::INT IS NULL
|
||||||
|
OR (
|
||||||
|
last_login IS NOT NULL
|
||||||
|
AND last_login <= CURRENT_TIMESTAMP - (sqlc.narg('days_since_last_login_min')::INT || ' days')::INTERVAL
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('days_since_last_login_max')::INT IS NULL
|
||||||
|
OR (
|
||||||
|
last_login IS NOT NULL
|
||||||
|
AND last_login >= CURRENT_TIMESTAMP - (sqlc.narg('days_since_last_login_max')::INT || ' days')::INTERVAL
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('min_lessons_completed')::INT IS NULL
|
||||||
|
OR (
|
||||||
|
SELECT COUNT(*)::INT
|
||||||
|
FROM lms_user_lesson_progress ulp
|
||||||
|
WHERE ulp.user_id = users.id
|
||||||
|
) >= sqlc.narg('min_lessons_completed')::INT
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('min_modules_completed')::INT IS NULL
|
||||||
|
OR (
|
||||||
|
SELECT COUNT(*)::INT
|
||||||
|
FROM lms_user_module_progress ump
|
||||||
|
WHERE ump.user_id = users.id
|
||||||
|
) >= sqlc.narg('min_modules_completed')::INT
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('has_ever_paid')::BOOLEAN IS NULL
|
||||||
|
OR (
|
||||||
|
sqlc.narg('has_ever_paid')::BOOLEAN = TRUE
|
||||||
|
AND EXISTS (
|
||||||
|
SELECT 1 FROM payments p
|
||||||
|
WHERE p.user_id = users.id AND p.status = 'SUCCESS'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
OR (
|
||||||
|
sqlc.narg('has_ever_paid')::BOOLEAN = FALSE
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM payments p
|
||||||
|
WHERE p.user_id = users.id AND p.status = 'SUCCESS'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('auto_renew')::BOOLEAN IS NULL
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1 FROM user_subscriptions us
|
||||||
|
WHERE us.user_id = users.id
|
||||||
|
AND us.status = 'ACTIVE'
|
||||||
|
AND us.expires_at > CURRENT_TIMESTAMP
|
||||||
|
AND us.auto_renew = sqlc.narg('auto_renew')::BOOLEAN
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('plan_id')::BIGINT IS NULL
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1 FROM user_subscriptions us
|
||||||
|
WHERE us.user_id = users.id
|
||||||
|
AND us.status = 'ACTIVE'
|
||||||
|
AND us.expires_at > CURRENT_TIMESTAMP
|
||||||
|
AND us.plan_id = sqlc.narg('plan_id')::BIGINT
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('plan_category')::TEXT IS NULL
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM user_subscriptions us
|
||||||
|
JOIN subscription_plans sp ON sp.id = us.plan_id
|
||||||
|
WHERE us.user_id = users.id
|
||||||
|
AND us.status = 'ACTIVE'
|
||||||
|
AND us.expires_at > CURRENT_TIMESTAMP
|
||||||
|
AND UPPER(TRIM(sp.category)) = UPPER(TRIM(sqlc.narg('plan_category')::TEXT))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('expires_within_days')::INT IS NULL
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1 FROM user_subscriptions us
|
||||||
|
WHERE us.user_id = users.id
|
||||||
|
AND us.status = 'ACTIVE'
|
||||||
|
AND us.expires_at > CURRENT_TIMESTAMP
|
||||||
|
AND us.expires_at <= CURRENT_TIMESTAMP + (sqlc.narg('expires_within_days')::INT || ' days')::INTERVAL
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('device_platform')::TEXT IS NULL
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1 FROM devices d
|
||||||
|
WHERE d.user_id = users.id
|
||||||
|
AND d.is_active = TRUE
|
||||||
|
AND LOWER(TRIM(COALESCE(d.platform, ''))) = LOWER(TRIM(sqlc.narg('device_platform')::TEXT))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
sqlc.narg('has_active_device')::BOOLEAN IS NULL
|
||||||
|
OR (
|
||||||
|
sqlc.narg('has_active_device')::BOOLEAN = TRUE
|
||||||
|
AND EXISTS (
|
||||||
|
SELECT 1 FROM devices d
|
||||||
|
WHERE d.user_id = users.id
|
||||||
|
AND d.is_active = TRUE
|
||||||
|
AND TRIM(COALESCE(d.device_token, '')) <> ''
|
||||||
|
)
|
||||||
|
)
|
||||||
|
OR (
|
||||||
|
sqlc.narg('has_active_device')::BOOLEAN = FALSE
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM devices d
|
||||||
|
WHERE d.user_id = users.id
|
||||||
|
AND d.is_active = TRUE
|
||||||
|
AND TRIM(COALESCE(d.device_token, '')) <> ''
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
AND (
|
AND (
|
||||||
sqlc.narg('subscription_status')::TEXT IS NULL
|
sqlc.narg('subscription_status')::TEXT IS NULL
|
||||||
OR (
|
OR (
|
||||||
|
|
@ -280,6 +414,28 @@ WHERE
|
||||||
WHERE us.user_id = users.id AND us.status = 'PENDING'
|
WHERE us.user_id = users.id AND us.status = 'PENDING'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
OR (
|
||||||
|
sqlc.narg('subscription_status')::TEXT = 'EXPIRED'
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM user_subscriptions us
|
||||||
|
WHERE us.user_id = users.id
|
||||||
|
AND us.status = 'ACTIVE'
|
||||||
|
AND us.expires_at > CURRENT_TIMESTAMP
|
||||||
|
)
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM user_subscriptions us
|
||||||
|
WHERE us.user_id = users.id AND us.status = 'PENDING'
|
||||||
|
)
|
||||||
|
AND EXISTS (
|
||||||
|
SELECT 1 FROM user_subscriptions us
|
||||||
|
WHERE us.user_id = users.id
|
||||||
|
AND (
|
||||||
|
us.status = 'EXPIRED'
|
||||||
|
OR us.status = 'CANCELLED'
|
||||||
|
OR (us.expires_at <= CURRENT_TIMESTAMP AND us.status <> 'PENDING')
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
LIMIT sqlc.narg('limit')::INT
|
LIMIT sqlc.narg('limit')::INT
|
||||||
|
|
|
||||||
|
|
@ -545,10 +545,144 @@ WHERE
|
||||||
AND ($5::TIMESTAMPTZ IS NULL OR created_at <= $5::TIMESTAMPTZ)
|
AND ($5::TIMESTAMPTZ IS NULL OR created_at <= $5::TIMESTAMPTZ)
|
||||||
AND ($6::TEXT IS NULL OR LOWER(TRIM(COALESCE(country, ''))) = LOWER(TRIM($6::TEXT)))
|
AND ($6::TEXT IS NULL OR LOWER(TRIM(COALESCE(country, ''))) = LOWER(TRIM($6::TEXT)))
|
||||||
AND ($7::TEXT IS NULL OR LOWER(TRIM(COALESCE(region, ''))) = LOWER(TRIM($7::TEXT)))
|
AND ($7::TEXT IS NULL OR LOWER(TRIM(COALESCE(region, ''))) = LOWER(TRIM($7::TEXT)))
|
||||||
|
AND ($8::TEXT IS NULL OR LOWER(TRIM(COALESCE(education_level, ''))) = LOWER(TRIM($8::TEXT)))
|
||||||
|
AND ($9::TEXT IS NULL OR LOWER(TRIM(COALESCE(occupation, ''))) = LOWER(TRIM($9::TEXT)))
|
||||||
|
AND ($10::TEXT IS NULL OR LOWER(TRIM(COALESCE(age_group, ''))) = LOWER(TRIM($10::TEXT)))
|
||||||
|
AND ($11::TEXT IS NULL OR LOWER(TRIM(COALESCE(favourite_topic, ''))) = LOWER(TRIM($11::TEXT)))
|
||||||
|
AND ($12::TEXT IS NULL OR LOWER(TRIM(COALESCE(language_goal, ''))) = LOWER(TRIM($12::TEXT)))
|
||||||
|
AND ($13::TEXT IS NULL OR LOWER(TRIM(COALESCE(learning_goal, ''))) = LOWER(TRIM($13::TEXT)))
|
||||||
|
AND ($14::TEXT IS NULL OR LOWER(TRIM(COALESCE(knowledge_level, ''))) = LOWER(TRIM($14::TEXT)))
|
||||||
|
AND ($15::BOOLEAN IS NULL OR profile_completed = $15::BOOLEAN)
|
||||||
|
AND ($16::BOOLEAN IS NULL OR initial_assessment_completed = $16::BOOLEAN)
|
||||||
AND (
|
AND (
|
||||||
$8::TEXT IS NULL
|
$17::INT IS NULL
|
||||||
|
OR profile_completion_percentage >= $17::INT
|
||||||
|
)
|
||||||
|
AND ($18::TIMESTAMPTZ IS NULL OR last_login >= $18::TIMESTAMPTZ)
|
||||||
|
AND ($19::TIMESTAMPTZ IS NULL OR last_login <= $19::TIMESTAMPTZ)
|
||||||
|
AND (
|
||||||
|
$20::INT IS NULL
|
||||||
OR (
|
OR (
|
||||||
$8::TEXT = 'ACTIVE'
|
last_login IS NOT NULL
|
||||||
|
AND last_login <= CURRENT_TIMESTAMP - ($20::INT || ' days')::INTERVAL
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
$21::INT IS NULL
|
||||||
|
OR (
|
||||||
|
last_login IS NOT NULL
|
||||||
|
AND last_login >= CURRENT_TIMESTAMP - ($21::INT || ' days')::INTERVAL
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
$22::INT IS NULL
|
||||||
|
OR (
|
||||||
|
SELECT COUNT(*)::INT
|
||||||
|
FROM lms_user_lesson_progress ulp
|
||||||
|
WHERE ulp.user_id = users.id
|
||||||
|
) >= $22::INT
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
$23::INT IS NULL
|
||||||
|
OR (
|
||||||
|
SELECT COUNT(*)::INT
|
||||||
|
FROM lms_user_module_progress ump
|
||||||
|
WHERE ump.user_id = users.id
|
||||||
|
) >= $23::INT
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
$24::BOOLEAN IS NULL
|
||||||
|
OR (
|
||||||
|
$24::BOOLEAN = TRUE
|
||||||
|
AND EXISTS (
|
||||||
|
SELECT 1 FROM payments p
|
||||||
|
WHERE p.user_id = users.id AND p.status = 'SUCCESS'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
OR (
|
||||||
|
$24::BOOLEAN = FALSE
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM payments p
|
||||||
|
WHERE p.user_id = users.id AND p.status = 'SUCCESS'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
$25::BOOLEAN IS NULL
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1 FROM user_subscriptions us
|
||||||
|
WHERE us.user_id = users.id
|
||||||
|
AND us.status = 'ACTIVE'
|
||||||
|
AND us.expires_at > CURRENT_TIMESTAMP
|
||||||
|
AND us.auto_renew = $25::BOOLEAN
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
$26::BIGINT IS NULL
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1 FROM user_subscriptions us
|
||||||
|
WHERE us.user_id = users.id
|
||||||
|
AND us.status = 'ACTIVE'
|
||||||
|
AND us.expires_at > CURRENT_TIMESTAMP
|
||||||
|
AND us.plan_id = $26::BIGINT
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
$27::TEXT IS NULL
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM user_subscriptions us
|
||||||
|
JOIN subscription_plans sp ON sp.id = us.plan_id
|
||||||
|
WHERE us.user_id = users.id
|
||||||
|
AND us.status = 'ACTIVE'
|
||||||
|
AND us.expires_at > CURRENT_TIMESTAMP
|
||||||
|
AND UPPER(TRIM(sp.category)) = UPPER(TRIM($27::TEXT))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
$28::INT IS NULL
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1 FROM user_subscriptions us
|
||||||
|
WHERE us.user_id = users.id
|
||||||
|
AND us.status = 'ACTIVE'
|
||||||
|
AND us.expires_at > CURRENT_TIMESTAMP
|
||||||
|
AND us.expires_at <= CURRENT_TIMESTAMP + ($28::INT || ' days')::INTERVAL
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
$29::TEXT IS NULL
|
||||||
|
OR EXISTS (
|
||||||
|
SELECT 1 FROM devices d
|
||||||
|
WHERE d.user_id = users.id
|
||||||
|
AND d.is_active = TRUE
|
||||||
|
AND LOWER(TRIM(COALESCE(d.platform, ''))) = LOWER(TRIM($29::TEXT))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
$30::BOOLEAN IS NULL
|
||||||
|
OR (
|
||||||
|
$30::BOOLEAN = TRUE
|
||||||
|
AND EXISTS (
|
||||||
|
SELECT 1 FROM devices d
|
||||||
|
WHERE d.user_id = users.id
|
||||||
|
AND d.is_active = TRUE
|
||||||
|
AND TRIM(COALESCE(d.device_token, '')) <> ''
|
||||||
|
)
|
||||||
|
)
|
||||||
|
OR (
|
||||||
|
$30::BOOLEAN = FALSE
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM devices d
|
||||||
|
WHERE d.user_id = users.id
|
||||||
|
AND d.is_active = TRUE
|
||||||
|
AND TRIM(COALESCE(d.device_token, '')) <> ''
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
AND (
|
||||||
|
$31::TEXT IS NULL
|
||||||
|
OR (
|
||||||
|
$31::TEXT = 'ACTIVE'
|
||||||
AND EXISTS (
|
AND EXISTS (
|
||||||
SELECT 1 FROM user_subscriptions us
|
SELECT 1 FROM user_subscriptions us
|
||||||
WHERE us.user_id = users.id
|
WHERE us.user_id = users.id
|
||||||
|
|
@ -557,7 +691,7 @@ WHERE
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
OR (
|
OR (
|
||||||
$8::TEXT = 'PENDING'
|
$31::TEXT = 'PENDING'
|
||||||
AND NOT EXISTS (
|
AND NOT EXISTS (
|
||||||
SELECT 1 FROM user_subscriptions us
|
SELECT 1 FROM user_subscriptions us
|
||||||
WHERE us.user_id = users.id
|
WHERE us.user_id = users.id
|
||||||
|
|
@ -570,7 +704,7 @@ WHERE
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
OR (
|
OR (
|
||||||
$8::TEXT = 'Unsubscribed'
|
$31::TEXT = 'Unsubscribed'
|
||||||
AND NOT EXISTS (
|
AND NOT EXISTS (
|
||||||
SELECT 1 FROM user_subscriptions us
|
SELECT 1 FROM user_subscriptions us
|
||||||
WHERE us.user_id = users.id
|
WHERE us.user_id = users.id
|
||||||
|
|
@ -582,23 +716,68 @@ WHERE
|
||||||
WHERE us.user_id = users.id AND us.status = 'PENDING'
|
WHERE us.user_id = users.id AND us.status = 'PENDING'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
OR (
|
||||||
|
$31::TEXT = 'EXPIRED'
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM user_subscriptions us
|
||||||
|
WHERE us.user_id = users.id
|
||||||
|
AND us.status = 'ACTIVE'
|
||||||
|
AND us.expires_at > CURRENT_TIMESTAMP
|
||||||
|
)
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM user_subscriptions us
|
||||||
|
WHERE us.user_id = users.id AND us.status = 'PENDING'
|
||||||
|
)
|
||||||
|
AND EXISTS (
|
||||||
|
SELECT 1 FROM user_subscriptions us
|
||||||
|
WHERE us.user_id = users.id
|
||||||
|
AND (
|
||||||
|
us.status = 'EXPIRED'
|
||||||
|
OR us.status = 'CANCELLED'
|
||||||
|
OR (us.expires_at <= CURRENT_TIMESTAMP AND us.status <> 'PENDING')
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
LIMIT $10::INT
|
LIMIT $33::INT
|
||||||
OFFSET $9::INT
|
OFFSET $32::INT
|
||||||
`
|
`
|
||||||
|
|
||||||
type GetAllUsersParams struct {
|
type GetAllUsersParams struct {
|
||||||
Role pgtype.Text `json:"role"`
|
Role pgtype.Text `json:"role"`
|
||||||
Status pgtype.Text `json:"status"`
|
Status pgtype.Text `json:"status"`
|
||||||
Query pgtype.Text `json:"query"`
|
Query pgtype.Text `json:"query"`
|
||||||
CreatedAfter pgtype.Timestamptz `json:"created_after"`
|
CreatedAfter pgtype.Timestamptz `json:"created_after"`
|
||||||
CreatedBefore pgtype.Timestamptz `json:"created_before"`
|
CreatedBefore pgtype.Timestamptz `json:"created_before"`
|
||||||
Country pgtype.Text `json:"country"`
|
Country pgtype.Text `json:"country"`
|
||||||
Region pgtype.Text `json:"region"`
|
Region pgtype.Text `json:"region"`
|
||||||
SubscriptionStatus pgtype.Text `json:"subscription_status"`
|
EducationLevel pgtype.Text `json:"education_level"`
|
||||||
Offset pgtype.Int4 `json:"offset"`
|
Occupation pgtype.Text `json:"occupation"`
|
||||||
Limit pgtype.Int4 `json:"limit"`
|
AgeGroup pgtype.Text `json:"age_group"`
|
||||||
|
FavouriteTopic pgtype.Text `json:"favourite_topic"`
|
||||||
|
LanguageGoal pgtype.Text `json:"language_goal"`
|
||||||
|
LearningGoal pgtype.Text `json:"learning_goal"`
|
||||||
|
KnowledgeLevel pgtype.Text `json:"knowledge_level"`
|
||||||
|
ProfileCompleted pgtype.Bool `json:"profile_completed"`
|
||||||
|
InitialAssessmentCompleted pgtype.Bool `json:"initial_assessment_completed"`
|
||||||
|
MinProfileCompletionPct pgtype.Int4 `json:"min_profile_completion_pct"`
|
||||||
|
LastLoginAfter pgtype.Timestamptz `json:"last_login_after"`
|
||||||
|
LastLoginBefore pgtype.Timestamptz `json:"last_login_before"`
|
||||||
|
DaysSinceLastLoginMin pgtype.Int4 `json:"days_since_last_login_min"`
|
||||||
|
DaysSinceLastLoginMax pgtype.Int4 `json:"days_since_last_login_max"`
|
||||||
|
MinLessonsCompleted pgtype.Int4 `json:"min_lessons_completed"`
|
||||||
|
MinModulesCompleted pgtype.Int4 `json:"min_modules_completed"`
|
||||||
|
HasEverPaid pgtype.Bool `json:"has_ever_paid"`
|
||||||
|
AutoRenew pgtype.Bool `json:"auto_renew"`
|
||||||
|
PlanID pgtype.Int8 `json:"plan_id"`
|
||||||
|
PlanCategory pgtype.Text `json:"plan_category"`
|
||||||
|
ExpiresWithinDays pgtype.Int4 `json:"expires_within_days"`
|
||||||
|
DevicePlatform pgtype.Text `json:"device_platform"`
|
||||||
|
HasActiveDevice pgtype.Bool `json:"has_active_device"`
|
||||||
|
SubscriptionStatus pgtype.Text `json:"subscription_status"`
|
||||||
|
Offset pgtype.Int4 `json:"offset"`
|
||||||
|
Limit pgtype.Int4 `json:"limit"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetAllUsersRow struct {
|
type GetAllUsersRow struct {
|
||||||
|
|
@ -644,6 +823,29 @@ func (q *Queries) GetAllUsers(ctx context.Context, arg GetAllUsersParams) ([]Get
|
||||||
arg.CreatedBefore,
|
arg.CreatedBefore,
|
||||||
arg.Country,
|
arg.Country,
|
||||||
arg.Region,
|
arg.Region,
|
||||||
|
arg.EducationLevel,
|
||||||
|
arg.Occupation,
|
||||||
|
arg.AgeGroup,
|
||||||
|
arg.FavouriteTopic,
|
||||||
|
arg.LanguageGoal,
|
||||||
|
arg.LearningGoal,
|
||||||
|
arg.KnowledgeLevel,
|
||||||
|
arg.ProfileCompleted,
|
||||||
|
arg.InitialAssessmentCompleted,
|
||||||
|
arg.MinProfileCompletionPct,
|
||||||
|
arg.LastLoginAfter,
|
||||||
|
arg.LastLoginBefore,
|
||||||
|
arg.DaysSinceLastLoginMin,
|
||||||
|
arg.DaysSinceLastLoginMax,
|
||||||
|
arg.MinLessonsCompleted,
|
||||||
|
arg.MinModulesCompleted,
|
||||||
|
arg.HasEverPaid,
|
||||||
|
arg.AutoRenew,
|
||||||
|
arg.PlanID,
|
||||||
|
arg.PlanCategory,
|
||||||
|
arg.ExpiresWithinDays,
|
||||||
|
arg.DevicePlatform,
|
||||||
|
arg.HasActiveDevice,
|
||||||
arg.SubscriptionStatus,
|
arg.SubscriptionStatus,
|
||||||
arg.Offset,
|
arg.Offset,
|
||||||
arg.Limit,
|
arg.Limit,
|
||||||
|
|
|
||||||
|
|
@ -164,7 +164,38 @@ type UserFilter struct {
|
||||||
Status string
|
Status string
|
||||||
Country string
|
Country string
|
||||||
Region string
|
Region string
|
||||||
SubscriptionStatus string // display filter: ACTIVE, PENDING, Unsubscribed (same as API subscription_status values)
|
SubscriptionStatus string // display filter: ACTIVE, PENDING, Unsubscribed, EXPIRED
|
||||||
|
|
||||||
|
// Profile
|
||||||
|
EducationLevel string
|
||||||
|
Occupation string
|
||||||
|
AgeGroup string
|
||||||
|
FavouriteTopic string
|
||||||
|
LanguageGoal string
|
||||||
|
LearningGoal string
|
||||||
|
KnowledgeLevel string
|
||||||
|
|
||||||
|
// Subscription targeting (current or derived subscription state)
|
||||||
|
PlanID *int64
|
||||||
|
PlanCategory string
|
||||||
|
AutoRenew *bool
|
||||||
|
HasEverPaid *bool
|
||||||
|
ExpiresWithinDays *int32
|
||||||
|
|
||||||
|
// Engagement / activity
|
||||||
|
LastLoginBefore ValidTime
|
||||||
|
LastLoginAfter ValidTime
|
||||||
|
DaysSinceLastLoginMin *int32
|
||||||
|
DaysSinceLastLoginMax *int32
|
||||||
|
ProfileCompleted *bool
|
||||||
|
InitialAssessmentCompleted *bool
|
||||||
|
MinLessonsCompleted *int32
|
||||||
|
MinModulesCompleted *int32
|
||||||
|
MinProfileCompletionPct *int32
|
||||||
|
|
||||||
|
// Technical / deliverability (device table only; push permission, timezone, app version not stored)
|
||||||
|
DevicePlatform string
|
||||||
|
HasActiveDevice *bool
|
||||||
|
|
||||||
Page int64
|
Page int64
|
||||||
PageSize int64
|
PageSize int64
|
||||||
|
|
|
||||||
|
|
@ -58,14 +58,7 @@ type UserStore interface {
|
||||||
) (domain.User, error)
|
) (domain.User, error)
|
||||||
GetAllUsers(
|
GetAllUsers(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
role *string,
|
filter domain.UserFilter,
|
||||||
status *string,
|
|
||||||
query *string,
|
|
||||||
createdBefore, createdAfter *time.Time,
|
|
||||||
country *string,
|
|
||||||
region *string,
|
|
||||||
subscriptionStatus *string,
|
|
||||||
limit, offset int32,
|
|
||||||
) ([]domain.User, int64, error)
|
) ([]domain.User, int64, error)
|
||||||
ListAccountDeletionRequests(ctx context.Context, filter domain.AccountDeletionRequestFilter) ([]domain.AccountDeletionRequest, int64, error)
|
ListAccountDeletionRequests(ctx context.Context, filter domain.AccountDeletionRequestFilter) ([]domain.AccountDeletionRequest, int64, error)
|
||||||
GetTotalUsers(ctx context.Context, role *string) (int64, error)
|
GetTotalUsers(ctx context.Context, role *string) (int64, error)
|
||||||
|
|
|
||||||
|
|
@ -504,73 +504,48 @@ func (s *Store) GetUserByGoogleID(
|
||||||
// GetAllUsers retrieves users with optional filters
|
// GetAllUsers retrieves users with optional filters
|
||||||
func (s *Store) GetAllUsers(
|
func (s *Store) GetAllUsers(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
role *string,
|
filter domain.UserFilter,
|
||||||
status *string,
|
|
||||||
query *string,
|
|
||||||
createdBefore, createdAfter *time.Time,
|
|
||||||
country *string,
|
|
||||||
region *string,
|
|
||||||
subscriptionStatus *string,
|
|
||||||
limit, offset int32,
|
|
||||||
) ([]domain.User, int64, error) {
|
) ([]domain.User, int64, error) {
|
||||||
|
pageSize := filter.PageSize
|
||||||
var roleParam pgtype.Text
|
if pageSize <= 0 {
|
||||||
if role != nil && *role != "" {
|
pageSize = 10
|
||||||
roleParam = pgtype.Text{String: *role, Valid: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
var statusParam pgtype.Text
|
|
||||||
if status != nil && *status != "" {
|
|
||||||
statusParam = pgtype.Text{String: *status, Valid: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
var queryParam pgtype.Text
|
|
||||||
if query != nil && *query != "" {
|
|
||||||
queryParam = pgtype.Text{String: *query, Valid: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
var createdAfterParam pgtype.Timestamptz
|
|
||||||
if createdAfter != nil {
|
|
||||||
createdAfterParam = pgtype.Timestamptz{Time: *createdAfter, Valid: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
var createdBeforeParam pgtype.Timestamptz
|
|
||||||
if createdBefore != nil {
|
|
||||||
createdBeforeParam = pgtype.Timestamptz{Time: *createdBefore, Valid: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
var countryParam pgtype.Text
|
|
||||||
if country != nil && *country != "" {
|
|
||||||
countryParam = pgtype.Text{String: *country, Valid: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
var regionParam pgtype.Text
|
|
||||||
if region != nil && *region != "" {
|
|
||||||
regionParam = pgtype.Text{String: *region, Valid: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
var subscriptionStatusParam pgtype.Text
|
|
||||||
if subscriptionStatus != nil && *subscriptionStatus != "" {
|
|
||||||
subscriptionStatusParam = pgtype.Text{String: *subscriptionStatus, Valid: true}
|
|
||||||
}
|
}
|
||||||
|
offset := int32(filter.Page * pageSize)
|
||||||
|
|
||||||
params := dbgen.GetAllUsersParams{
|
params := dbgen.GetAllUsersParams{
|
||||||
Role: roleParam,
|
Role: userFilterText(filter.Role),
|
||||||
Status: statusParam,
|
Status: userFilterText(filter.Status),
|
||||||
Query: queryParam,
|
Query: userFilterText(filter.Query),
|
||||||
CreatedAfter: createdAfterParam,
|
CreatedAfter: userFilterTime(filter.CreatedAfter),
|
||||||
CreatedBefore: createdBeforeParam,
|
CreatedBefore: userFilterTime(filter.CreatedBefore),
|
||||||
Country: countryParam,
|
Country: userFilterText(filter.Country),
|
||||||
Region: regionParam,
|
Region: userFilterText(filter.Region),
|
||||||
SubscriptionStatus: subscriptionStatusParam,
|
SubscriptionStatus: userFilterText(filter.SubscriptionStatus),
|
||||||
Limit: pgtype.Int4{
|
EducationLevel: userFilterText(filter.EducationLevel),
|
||||||
Int32: limit,
|
Occupation: userFilterText(filter.Occupation),
|
||||||
Valid: true,
|
AgeGroup: userFilterText(filter.AgeGroup),
|
||||||
},
|
FavouriteTopic: userFilterText(filter.FavouriteTopic),
|
||||||
Offset: pgtype.Int4{
|
LanguageGoal: userFilterText(filter.LanguageGoal),
|
||||||
Int32: offset,
|
LearningGoal: userFilterText(filter.LearningGoal),
|
||||||
Valid: true,
|
KnowledgeLevel: userFilterText(filter.KnowledgeLevel),
|
||||||
},
|
PlanCategory: userFilterText(filter.PlanCategory),
|
||||||
|
DevicePlatform: userFilterText(filter.DevicePlatform),
|
||||||
|
ProfileCompleted: userFilterBool(filter.ProfileCompleted),
|
||||||
|
InitialAssessmentCompleted: userFilterBool(filter.InitialAssessmentCompleted),
|
||||||
|
HasEverPaid: userFilterBool(filter.HasEverPaid),
|
||||||
|
AutoRenew: userFilterBool(filter.AutoRenew),
|
||||||
|
HasActiveDevice: userFilterBool(filter.HasActiveDevice),
|
||||||
|
PlanID: userFilterInt64(filter.PlanID),
|
||||||
|
ExpiresWithinDays: userFilterInt32(filter.ExpiresWithinDays),
|
||||||
|
DaysSinceLastLoginMin: userFilterInt32(filter.DaysSinceLastLoginMin),
|
||||||
|
DaysSinceLastLoginMax: userFilterInt32(filter.DaysSinceLastLoginMax),
|
||||||
|
MinLessonsCompleted: userFilterInt32(filter.MinLessonsCompleted),
|
||||||
|
MinModulesCompleted: userFilterInt32(filter.MinModulesCompleted),
|
||||||
|
MinProfileCompletionPct: userFilterInt32(filter.MinProfileCompletionPct),
|
||||||
|
LastLoginAfter: userFilterTime(filter.LastLoginAfter),
|
||||||
|
LastLoginBefore: userFilterTime(filter.LastLoginBefore),
|
||||||
|
Limit: pgtype.Int4{Int32: int32(pageSize), Valid: true},
|
||||||
|
Offset: pgtype.Int4{Int32: offset, Valid: true},
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := s.queries.GetAllUsers(ctx, params)
|
rows, err := s.queries.GetAllUsers(ctx, params)
|
||||||
|
|
@ -1232,3 +1207,39 @@ func mapDBUser(
|
||||||
UpdatedAt: updatedAt,
|
UpdatedAt: updatedAt,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func userFilterText(value string) pgtype.Text {
|
||||||
|
trimmed := strings.TrimSpace(value)
|
||||||
|
if trimmed == "" {
|
||||||
|
return pgtype.Text{}
|
||||||
|
}
|
||||||
|
return pgtype.Text{String: trimmed, Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
func userFilterTime(value domain.ValidTime) pgtype.Timestamptz {
|
||||||
|
if !value.Valid {
|
||||||
|
return pgtype.Timestamptz{}
|
||||||
|
}
|
||||||
|
return pgtype.Timestamptz{Time: value.Value, Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
func userFilterBool(value *bool) pgtype.Bool {
|
||||||
|
if value == nil {
|
||||||
|
return pgtype.Bool{}
|
||||||
|
}
|
||||||
|
return pgtype.Bool{Bool: *value, Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
func userFilterInt32(value *int32) pgtype.Int4 {
|
||||||
|
if value == nil {
|
||||||
|
return pgtype.Int4{}
|
||||||
|
}
|
||||||
|
return pgtype.Int4{Int32: *value, Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
func userFilterInt64(value *int64) pgtype.Int8 {
|
||||||
|
if value == nil {
|
||||||
|
return pgtype.Int8{}
|
||||||
|
}
|
||||||
|
return pgtype.Int8{Int64: *value, Valid: true}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,69 +67,25 @@ func (s *Service) GetAllUsers(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
filter domain.UserFilter,
|
filter domain.UserFilter,
|
||||||
) ([]domain.User, int64, error) {
|
) ([]domain.User, int64, error) {
|
||||||
|
normalized := filter
|
||||||
var before *time.Time
|
if normalized.Role != "" {
|
||||||
if filter.CreatedBefore.Valid {
|
normalized.Role = strings.ToUpper(strings.TrimSpace(normalized.Role))
|
||||||
before = &filter.CreatedBefore.Value
|
}
|
||||||
|
if normalized.Status != "" {
|
||||||
|
normalized.Status = strings.ToUpper(strings.TrimSpace(normalized.Status))
|
||||||
|
}
|
||||||
|
if normalized.PlanCategory != "" {
|
||||||
|
normalized.PlanCategory = strings.ToUpper(strings.TrimSpace(normalized.PlanCategory))
|
||||||
|
}
|
||||||
|
if normalized.DevicePlatform != "" {
|
||||||
|
normalized.DevicePlatform = strings.ToLower(strings.TrimSpace(normalized.DevicePlatform))
|
||||||
}
|
}
|
||||||
|
|
||||||
var after *time.Time
|
if normalized.PageSize <= 0 {
|
||||||
if filter.CreatedAfter.Valid {
|
normalized.PageSize = defaultUnpaginatedUserLimit
|
||||||
after = &filter.CreatedAfter.Value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var role *string
|
return s.userStore.GetAllUsers(ctx, normalized)
|
||||||
if filter.Role != "" {
|
|
||||||
normalized := strings.ToUpper(strings.TrimSpace(filter.Role))
|
|
||||||
role = &normalized
|
|
||||||
}
|
|
||||||
|
|
||||||
var status *string
|
|
||||||
if filter.Status != "" {
|
|
||||||
normalized := strings.ToUpper(strings.TrimSpace(filter.Status))
|
|
||||||
status = &normalized
|
|
||||||
}
|
|
||||||
|
|
||||||
var query *string
|
|
||||||
if filter.Query != "" {
|
|
||||||
query = &filter.Query
|
|
||||||
}
|
|
||||||
|
|
||||||
var country *string
|
|
||||||
if filter.Country != "" {
|
|
||||||
country = &filter.Country
|
|
||||||
}
|
|
||||||
|
|
||||||
var region *string
|
|
||||||
if filter.Region != "" {
|
|
||||||
region = &filter.Region
|
|
||||||
}
|
|
||||||
|
|
||||||
var subscriptionStatus *string
|
|
||||||
if filter.SubscriptionStatus != "" {
|
|
||||||
subscriptionStatus = &filter.SubscriptionStatus
|
|
||||||
}
|
|
||||||
|
|
||||||
pageSize := filter.PageSize
|
|
||||||
if pageSize <= 0 {
|
|
||||||
pageSize = defaultUnpaginatedUserLimit
|
|
||||||
}
|
|
||||||
|
|
||||||
offset := int32(filter.Page * pageSize)
|
|
||||||
|
|
||||||
return s.userStore.GetAllUsers(
|
|
||||||
ctx,
|
|
||||||
role,
|
|
||||||
status,
|
|
||||||
query,
|
|
||||||
before,
|
|
||||||
after,
|
|
||||||
country,
|
|
||||||
region,
|
|
||||||
subscriptionStatus,
|
|
||||||
int32(pageSize),
|
|
||||||
offset,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetUserSummary(ctx context.Context) (domain.UserSummary, error) {
|
func (s *Service) GetUserSummary(ctx context.Context) (domain.UserSummary, error) {
|
||||||
|
|
|
||||||
|
|
@ -124,6 +124,26 @@ func parseUserListFilter(c *fiber.Ctx) (domain.UserFilter, error) {
|
||||||
createdAfter = domain.ValidTime{Value: parsed, Valid: true}
|
createdAfter = domain.ValidTime{Value: parsed, Valid: true}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastLoginBeforeQuery := c.Query("last_login_before")
|
||||||
|
var lastLoginBefore domain.ValidTime
|
||||||
|
if lastLoginBeforeQuery != "" {
|
||||||
|
parsed, err := time.Parse(time.RFC3339, lastLoginBeforeQuery)
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, fmt.Errorf("invalid last_login_before format")
|
||||||
|
}
|
||||||
|
lastLoginBefore = domain.ValidTime{Value: parsed, Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastLoginAfterQuery := c.Query("last_login_after")
|
||||||
|
var lastLoginAfter domain.ValidTime
|
||||||
|
if lastLoginAfterQuery != "" {
|
||||||
|
parsed, err := time.Parse(time.RFC3339, lastLoginAfterQuery)
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, fmt.Errorf("invalid last_login_after format")
|
||||||
|
}
|
||||||
|
lastLoginAfter = domain.ValidTime{Value: parsed, Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
subscriptionStatusQuery := strings.TrimSpace(c.Query("subscription_status"))
|
subscriptionStatusQuery := strings.TrimSpace(c.Query("subscription_status"))
|
||||||
var subscriptionStatusFilter string
|
var subscriptionStatusFilter string
|
||||||
if subscriptionStatusQuery != "" {
|
if subscriptionStatusQuery != "" {
|
||||||
|
|
@ -134,23 +154,155 @@ func parseUserListFilter(c *fiber.Ctx) (domain.UserFilter, error) {
|
||||||
subscriptionStatusFilter = "PENDING"
|
subscriptionStatusFilter = "PENDING"
|
||||||
case "UNSUBSCRIBED":
|
case "UNSUBSCRIBED":
|
||||||
subscriptionStatusFilter = "Unsubscribed"
|
subscriptionStatusFilter = "Unsubscribed"
|
||||||
|
case "EXPIRED":
|
||||||
|
subscriptionStatusFilter = "EXPIRED"
|
||||||
default:
|
default:
|
||||||
return domain.UserFilter{}, fmt.Errorf("invalid subscription_status; use ACTIVE, PENDING, or Unsubscribed")
|
return domain.UserFilter{}, fmt.Errorf("invalid subscription_status; use ACTIVE, PENDING, Unsubscribed, or EXPIRED")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
planID, err := parseOptionalInt64Query(c, "plan_id")
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, err
|
||||||
|
}
|
||||||
|
autoRenew, err := parseOptionalBoolQuery(c, "auto_renew")
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, err
|
||||||
|
}
|
||||||
|
hasEverPaid, err := parseOptionalBoolQuery(c, "has_ever_paid")
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, err
|
||||||
|
}
|
||||||
|
expiresWithinDays, err := parseOptionalInt32Query(c, "expires_within_days")
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, err
|
||||||
|
}
|
||||||
|
daysSinceLastLoginMin, err := parseOptionalInt32Query(c, "days_since_last_login_min")
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, err
|
||||||
|
}
|
||||||
|
daysSinceLastLoginMax, err := parseOptionalInt32Query(c, "days_since_last_login_max")
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, err
|
||||||
|
}
|
||||||
|
profileCompleted, err := parseOptionalBoolQuery(c, "profile_completed")
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, err
|
||||||
|
}
|
||||||
|
initialAssessmentCompleted, err := parseOptionalBoolQuery(c, "initial_assessment_completed")
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, err
|
||||||
|
}
|
||||||
|
minLessonsCompleted, err := parseOptionalInt32Query(c, "min_lessons_completed")
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, err
|
||||||
|
}
|
||||||
|
minModulesCompleted, err := parseOptionalInt32Query(c, "min_modules_completed")
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, err
|
||||||
|
}
|
||||||
|
minProfileCompletionPct, err := parseOptionalInt32Query(c, "min_profile_completion_pct")
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, err
|
||||||
|
}
|
||||||
|
hasActiveDevice, err := parseOptionalBoolQuery(c, "has_active_device")
|
||||||
|
if err != nil {
|
||||||
|
return domain.UserFilter{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
planCategory := strings.TrimSpace(c.Query("plan_category"))
|
||||||
|
if planCategory != "" {
|
||||||
|
cat := strings.ToUpper(planCategory)
|
||||||
|
if cat != string(domain.SubscriptionCategoryLearnEnglish) &&
|
||||||
|
cat != string(domain.SubscriptionCategoryIELTS) &&
|
||||||
|
cat != string(domain.SubscriptionCategoryDuolingo) {
|
||||||
|
return domain.UserFilter{}, fmt.Errorf("invalid plan_category")
|
||||||
|
}
|
||||||
|
planCategory = cat
|
||||||
|
}
|
||||||
|
|
||||||
|
devicePlatform := strings.ToLower(strings.TrimSpace(c.Query("device_platform")))
|
||||||
|
if devicePlatform != "" && devicePlatform != "ios" && devicePlatform != "android" && devicePlatform != "web" {
|
||||||
|
return domain.UserFilter{}, fmt.Errorf("invalid device_platform; use ios, android, or web")
|
||||||
|
}
|
||||||
|
|
||||||
return domain.UserFilter{
|
return domain.UserFilter{
|
||||||
Role: c.Query("role"),
|
Role: c.Query("role"),
|
||||||
Status: c.Query("status"),
|
Status: c.Query("status"),
|
||||||
Country: strings.TrimSpace(c.Query("country")),
|
Country: strings.TrimSpace(c.Query("country")),
|
||||||
Region: strings.TrimSpace(c.Query("region")),
|
Region: strings.TrimSpace(c.Query("region")),
|
||||||
SubscriptionStatus: subscriptionStatusFilter,
|
SubscriptionStatus: subscriptionStatusFilter,
|
||||||
Query: c.Query("query"),
|
EducationLevel: strings.TrimSpace(c.Query("education_level")),
|
||||||
CreatedBefore: createdBefore,
|
Occupation: strings.TrimSpace(c.Query("occupation")),
|
||||||
CreatedAfter: createdAfter,
|
AgeGroup: strings.TrimSpace(c.Query("age_group")),
|
||||||
|
FavouriteTopic: strings.TrimSpace(c.Query("favourite_topic")),
|
||||||
|
LanguageGoal: strings.TrimSpace(c.Query("language_goal")),
|
||||||
|
LearningGoal: strings.TrimSpace(c.Query("learning_goal")),
|
||||||
|
KnowledgeLevel: strings.TrimSpace(c.Query("knowledge_level")),
|
||||||
|
PlanID: planID,
|
||||||
|
PlanCategory: planCategory,
|
||||||
|
AutoRenew: autoRenew,
|
||||||
|
HasEverPaid: hasEverPaid,
|
||||||
|
ExpiresWithinDays: expiresWithinDays,
|
||||||
|
LastLoginBefore: lastLoginBefore,
|
||||||
|
LastLoginAfter: lastLoginAfter,
|
||||||
|
DaysSinceLastLoginMin: daysSinceLastLoginMin,
|
||||||
|
DaysSinceLastLoginMax: daysSinceLastLoginMax,
|
||||||
|
ProfileCompleted: profileCompleted,
|
||||||
|
InitialAssessmentCompleted: initialAssessmentCompleted,
|
||||||
|
MinLessonsCompleted: minLessonsCompleted,
|
||||||
|
MinModulesCompleted: minModulesCompleted,
|
||||||
|
MinProfileCompletionPct: minProfileCompletionPct,
|
||||||
|
DevicePlatform: devicePlatform,
|
||||||
|
HasActiveDevice: hasActiveDevice,
|
||||||
|
Query: c.Query("query"),
|
||||||
|
CreatedBefore: createdBefore,
|
||||||
|
CreatedAfter: createdAfter,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseOptionalBoolQuery(c *fiber.Ctx, key string) (*bool, error) {
|
||||||
|
raw := strings.TrimSpace(c.Query(key))
|
||||||
|
if raw == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
switch strings.ToLower(raw) {
|
||||||
|
case "true", "1", "yes":
|
||||||
|
v := true
|
||||||
|
return &v, nil
|
||||||
|
case "false", "0", "no":
|
||||||
|
v := false
|
||||||
|
return &v, nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("invalid %s; use true or false", key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOptionalInt32Query(c *fiber.Ctx, key string) (*int32, error) {
|
||||||
|
raw := strings.TrimSpace(c.Query(key))
|
||||||
|
if raw == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
parsed, err := strconv.ParseInt(raw, 10, 32)
|
||||||
|
if err != nil || parsed < 0 {
|
||||||
|
return nil, fmt.Errorf("invalid %s", key)
|
||||||
|
}
|
||||||
|
v := int32(parsed)
|
||||||
|
return &v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOptionalInt64Query(c *fiber.Ctx, key string) (*int64, error) {
|
||||||
|
raw := strings.TrimSpace(c.Query(key))
|
||||||
|
if raw == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
parsed, err := strconv.ParseInt(raw, 10, 64)
|
||||||
|
if err != nil || parsed <= 0 {
|
||||||
|
return nil, fmt.Errorf("invalid %s", key)
|
||||||
|
}
|
||||||
|
return &parsed, nil
|
||||||
|
}
|
||||||
|
|
||||||
func parseActivityLogFilterFields(c *fiber.Ctx) (domain.ActivityLogFilter, error) {
|
func parseActivityLogFilterFields(c *fiber.Ctx) (domain.ActivityLogFilter, error) {
|
||||||
var filter domain.ActivityLogFilter
|
var filter domain.ActivityLogFilter
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -445,61 +445,14 @@ func (h *Handler) CheckUserPending(c *fiber.Ctx) error {
|
||||||
// @Failure 500 {object} response.APIResponse
|
// @Failure 500 {object} response.APIResponse
|
||||||
// @Router /api/v1/users [get]
|
// @Router /api/v1/users [get]
|
||||||
func (h *Handler) GetAllUsers(c *fiber.Ctx) error {
|
func (h *Handler) GetAllUsers(c *fiber.Ctx) error {
|
||||||
searchQuery := c.Query("query")
|
filter, err := parseUserListFilter(c)
|
||||||
searchString := domain.ValidString{
|
if err != nil {
|
||||||
Value: searchQuery,
|
h.logger.Info("invalid filter values in GetAllUsers request", "error", err)
|
||||||
// Valid: searchQuery != "",
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
createdBeforeQuery := c.Query("created_before")
|
filter.Page = int64(c.QueryInt("page", 1) - 1)
|
||||||
var createdBefore domain.ValidTime
|
filter.PageSize = int64(c.QueryInt("page_size", 10))
|
||||||
if createdBeforeQuery != "" {
|
|
||||||
parsed, err := time.Parse(time.RFC3339, createdBeforeQuery)
|
|
||||||
if err != nil {
|
|
||||||
h.logger.Info("invalid created_before format", "error", err)
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid created_before format")
|
|
||||||
}
|
|
||||||
createdBefore = domain.ValidTime{Value: parsed, Valid: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
createdAfterQuery := c.Query("created_after")
|
|
||||||
var createdAfter domain.ValidTime
|
|
||||||
if createdAfterQuery != "" {
|
|
||||||
parsed, err := time.Parse(time.RFC3339, createdAfterQuery)
|
|
||||||
if err != nil {
|
|
||||||
h.logger.Info("invalid created_after format", "error", err)
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid created_after format")
|
|
||||||
}
|
|
||||||
createdAfter = domain.ValidTime{Value: parsed, Valid: true}
|
|
||||||
}
|
|
||||||
|
|
||||||
subscriptionStatusQuery := strings.TrimSpace(c.Query("subscription_status"))
|
|
||||||
var subscriptionStatusFilter string
|
|
||||||
if subscriptionStatusQuery != "" {
|
|
||||||
switch strings.ToUpper(subscriptionStatusQuery) {
|
|
||||||
case "ACTIVE":
|
|
||||||
subscriptionStatusFilter = "ACTIVE"
|
|
||||||
case "PENDING":
|
|
||||||
subscriptionStatusFilter = "PENDING"
|
|
||||||
case "UNSUBSCRIBED":
|
|
||||||
subscriptionStatusFilter = "Unsubscribed"
|
|
||||||
default:
|
|
||||||
return fiber.NewError(fiber.StatusBadRequest, `Invalid subscription_status; use ACTIVE, PENDING, or Unsubscribed`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
filter := domain.UserFilter{
|
|
||||||
Role: c.Query("role"),
|
|
||||||
Status: c.Query("status"),
|
|
||||||
Country: strings.TrimSpace(c.Query("country")),
|
|
||||||
Region: strings.TrimSpace(c.Query("region")),
|
|
||||||
SubscriptionStatus: subscriptionStatusFilter,
|
|
||||||
Page: int64(c.QueryInt("page", 1) - 1),
|
|
||||||
PageSize: int64(c.QueryInt("page_size", 10)),
|
|
||||||
Query: searchString.Value,
|
|
||||||
CreatedBefore: createdBefore,
|
|
||||||
CreatedAfter: createdAfter,
|
|
||||||
}
|
|
||||||
|
|
||||||
if valErrs, ok := h.validator.Validate(c, filter); !ok {
|
if valErrs, ok := h.validator.Validate(c, filter); !ok {
|
||||||
var errMsg string
|
var errMsg string
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user