LCOV - code coverage report
Current view: top level - matchscore/domain/player_follow - profile.clj Coverage Total Hit
Test: lcov.info Lines: 98.2 % 111 109
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.domain.player-follow.profile
       2              :   "Profiles of imported players (Rankedin, FSU, PSA, ESF): the name,
       3              :   country and photo we hold, plus a link to the player's page on the source site.
       4              : 
       5              :   A player is addressed by source and id. The id is either the source's own id
       6              :   (Rankedin id, FSU CRM profile id, PSA id or ESF id) or the `rankedin_users.uuid` of an imported draw participant, which
       7              :   is what event pages already hold.
       8              : 
       9              :   The returned `:player-id` is the source's own id; for PSA and ESF it is also
      10              :   the follow key."
      11              :   (:require
      12              :    [clojure.string :as str]
      13              :    [matchscore.domain.country-exclusion :as country-exclusion]
      14              :    [next.jdbc :as jdbc]
      15              :    [next.jdbc.result-set :as rs])
      16              :   (:import
      17              :    (java.text Normalizer Normalizer$Form)))
      18              : 
      19              : 
      20           24 : (def sources #{"rankedin" "fsu" "psa" "esf"})
      21              : 
      22              : 
      23            8 : (def ^:private uuid-re #"(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
      24              : 
      25              : 
      26            4 : (defn parse-id
      27              :  "Returns {:uuid s} or {:id n} for a well-formed id of `source`, else nil."
      28              :  [source raw]
      29           16 :  (let [raw (some->
      30            1 :              raw
      31            1 :              str
      32            1 :              str/trim)]
      33           20 :    (cond (or (not (contains? sources source)) (str/blank? raw)) nil
      34            9 :          (re-matches uuid-re raw) {:uuid (str/lower-case raw)}
      35              :          ;; ESF players without ESF membership get a negative id local to a
      36              :          ;; tournament; ranking pages show "ES662969162".
      37            3 :          (= source "esf")
      38           10 :          (when-let [[_ digits] (re-matches #"(?i)(?:ES)?(-?[0-9]{1,12})" raw)]
      39           12 :            (let [n (parse-long digits)] (when-not (zero? n) {:id n})))
      40              : 
      41            9 :          (re-matches #"[0-9]{1,12}" raw) {:id (parse-long raw)}
      42              :          :else nil)))
      43              : 
      44              : 
      45            4 : (defn slugify
      46              :  "\"Tereza Široká\" → \"tereza-siroka\", the way PSA builds player page slugs."
      47              :  [s]
      48            6 :  (when-not (str/blank? s)
      49            4 :    (let [slug (->
      50            4 :                 (Normalizer/normalize s Normalizer$Form/NFD)
      51            4 :                 (str/replace #"\p{M}" "")
      52            1 :                 str/lower-case
      53            5 :                 (str/replace #"['’]" "")
      54            4 :                 (str/replace #"[^a-z0-9]+" "-")
      55            1 :                 (str/replace #"^-+|-+$" ""))]
      56            3 :      (not-empty slug))))
      57              : 
      58              : 
      59            4 : (defn rankedin-profile-url
      60              :  [{:keys [profile-url username]}]
      61            5 :  (or
      62            3 :   (not-empty profile-url)
      63              :   ;; Older imports stored placeholder handles like "esf_123" for other sources.
      64           14 :   (when (and (not (str/blank? username)) (not (re-find #"^(esf|psa|fsu)_" username)))
      65            4 :     (str "https://www.rankedin.com/en/player/" username))))
      66              : 
      67              : 
      68            4 : (defn psa-profile-url
      69              :  [full-name]
      70            9 :  (some->>
      71            3 :    (slugify full-name)
      72            1 :    (format "https://www.psasquashtour.com/player/%s/")))
      73              : 
      74              : 
      75            4 : (defn fsu-profile-url
      76              :  [crm-profile-id]
      77            4 :  (str "https://crm.squash.ua/squash/profile/" crm-profile-id))
      78              : 
      79              : 
      80           16 : (def ^:private query-options {:builder-fn rs/as-unqualified-kebab-maps})
      81              : 
      82              : 
      83           13 : (defn- one [ds sql & params] (jdbc/execute-one! ds (into [sql] params) query-options))
      84              : 
      85              : 
      86            4 : (defn- draw-user
      87              :   "The imported draw user (`rankedin_users`) of `source`, by uuid or source id."
      88              :   [ds source {:keys [uuid id]}]
      89            2 :   (if uuid
      90            6 :     (one ds "SELECT * FROM rankedin_users WHERE source = ? AND uuid = ?::uuid" source uuid)
      91            6 :     (one ds "SELECT * FROM rankedin_users WHERE source = ? AND rankedin_id = ?" source id)))
      92              : 
      93              : 
      94            4 : (defn- full-name
      95              :   [{:keys [first-name last-name]}]
      96           13 :   (not-empty (str/trim (str/join " " (remove str/blank? [first-name last-name])))))
      97              : 
      98              : 
      99            4 : (defn- rankedin-profile
     100              :   [ds parsed]
     101           11 :   (when-let [user (draw-user ds "rankedin" parsed)]
     102            8 :     {:source       "rankedin"
     103            5 :      :player-id    (str (:rankedin-id user))
     104            7 :      :full-name    (or (full-name user) (:username user))
     105            3 :      :country-code (:country-code user)
     106            3 :      :photo-url    (:photo-url user)
     107            3 :      :profile-url  (rankedin-profile-url user)}))
     108              : 
     109              : 
     110            4 : (defn- fsu-profile
     111              :   [ds {:keys [id]}]
     112            3 :   (when id
     113            6 :     (when-let [player
     114            3 :                (one
     115            1 :                 ds
     116              :                 "SELECT crm_profile_id, full_name, city FROM fsu_players WHERE crm_profile_id = ?"
     117            1 :                 id)]
     118            7 :       {:source      "fsu"
     119            5 :        :player-id   (str (:crm-profile-id player))
     120            3 :        :full-name   (:full-name player)
     121            3 :        :city        (:city player)
     122            5 :        :profile-url (fsu-profile-url (:crm-profile-id player))})))
     123              : 
     124              : 
     125            4 : (defn- psa-profile
     126              :   [ds
     127              :    {:keys [uuid id]
     128              :     :as   parsed}]
     129            1 :   (let
     130              :     [player
     131            2 :      (if uuid
     132            3 :        (one
     133            1 :         ds
     134              :         "SELECT psa_id, full_name, country_code, photo_url FROM psa_players
     135              :                    WHERE rankedin_user_id = ?::uuid"
     136            1 :         uuid)
     137            3 :        (one
     138            1 :         ds
     139              :         "SELECT psa_id, full_name, country_code, photo_url FROM psa_players
     140              :                    WHERE psa_id = ?"
     141            1 :         id))
     142            9 :      user (when-not player (draw-user ds "psa" parsed))
     143           10 :      name (or (:full-name player) (full-name user))]
     144            8 :     (when (or player user)
     145            9 :       {:source       "psa"
     146            9 :        :player-id    (str (or (:psa-id player) (:rankedin-id user)))
     147            1 :        :full-name    name
     148            7 :        :country-code (or (:country-code player) (:country-code user))
     149            9 :        :photo-url    (or (:photo-url player) (:photo-url user))
     150            3 :        :profile-url  (psa-profile-url name)
     151            3 :        :followable   (some? player)})))
     152              : 
     153              : 
     154            4 : (defn- esf-profile
     155              :   [ds parsed]
     156            1 :   (let
     157            5 :     [user (draw-user ds "esf" parsed)
     158           10 :      esid (or (:id parsed) (:rankedin-id user))
     159            7 :      member? (boolean (and esid (pos? esid)))
     160              :      ranking
     161            3 :      (when member?
     162            3 :        (one
     163            1 :         ds
     164              :         "SELECT full_name, country_code, profile_url FROM esf_ranking_entries
     165              :                     WHERE esid = 'ES' || ?::text
     166              :                     ORDER BY publication_id DESC
     167              :                     LIMIT 1"
     168            1 :         esid))]
     169            8 :     (when (or ranking user)
     170            9 :       {:source       "esf"
     171              :        ;; A draw user always has a rankedin_id (NOT NULL); the uuid is a fallback.
     172            7 :        :player-id    (str (or esid (:uuid user)))
     173            8 :        :full-name    (or
     174            3 :                       (:full-name ranking)
     175            3 :                       (full-name user)
     176              :                       (some->>
     177            0 :                         esid
     178            0 :                         (str "ES")))
     179           10 :        :country-code (or (:country-code ranking) (:country-code user))
     180            3 :        :photo-url    (:photo-url user)
     181           14 :        :profile-url  (or (not-empty (:profile-url ranking)) (not-empty (:profile-url user)))
     182            1 :        :followable   member?})))
     183              : 
     184              : 
     185            4 : (defn find-player-profile
     186              :  "Profile of the player `raw-id` on `source`, or nil when unknown, malformed or
     187              :   from an excluded country."
     188              :  [ds source raw-id]
     189           10 :  (when-let [parsed (parse-id source raw-id)]
     190            4 :    (let [profile (case source
     191            4 :                    "rankedin" (rankedin-profile ds parsed)
     192            4 :                    "fsu"      (fsu-profile ds parsed)
     193            4 :                    "psa"      (psa-profile ds parsed)
     194            4 :                    "esf"      (esf-profile ds parsed))]
     195           14 :      (when (and profile (not (country-exclusion/excluded? (:country-code profile))))
     196            6 :        (merge {:followable false} profile)))))
        

Generated by: LCOV version 2.0-1