pets ranking

This commit is contained in:
2026-08-26 20:00:02 +08:00
parent 1271cd5c8e
commit f15aa87523
2 changed files with 40 additions and 5 deletions
+23
View File
@@ -93,4 +93,27 @@ export async function getHighestPets(limit = 10): Promise<{ userId: string, pets
}));
}
export async function getPetPosition(userId: string): Promise<number | null> {
const result = await client<{ position: number }[]>`
SELECT position
FROM (
SELECT
user_id,
RANK() OVER (ORDER BY pets DESC, user_id ASC) AS position
FROM (
SELECT
user_id,
(data->'statistics'->>'pets')::integer AS pets
FROM users
WHERE
COALESCE((data->'flags'->>'blacklisted')::boolean, false) = false
AND COALESCE((data->'settings'->>'participating')::boolean, true) = true
) ranked
) ranked_users
WHERE user_id = ${userId}
`;
return result[0]?.position ?? null;
}
//#endregion