allow importing accessible foreign athletes
This commit is contained in:
parent
e52056ae60
commit
30542807b8
2 changed files with 99 additions and 10 deletions
35
src/db.rs
35
src/db.rs
|
|
@ -232,6 +232,41 @@ pub async fn upsert_oauth_athlete(
|
|||
Ok(row.try_get("id")?)
|
||||
}
|
||||
|
||||
|
||||
/// Ensure that an athlete accessible through the current session exists
|
||||
/// locally so activities can reference it. The session credential itself is
|
||||
/// intentionally not stored here; callers continue to use their authenticated
|
||||
/// IntervalsClient for API requests.
|
||||
pub async fn ensure_local_athlete(
|
||||
db: &PgPool,
|
||||
intervals_athlete_id: &str,
|
||||
display_name: &str,
|
||||
) -> Result<i64> {
|
||||
let row = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO athletes (
|
||||
intervals_athlete_id,
|
||||
display_name,
|
||||
access_token,
|
||||
scopes
|
||||
)
|
||||
VALUES ($1, $2, '', 'SESSION_ACCESS')
|
||||
ON CONFLICT (intervals_athlete_id)
|
||||
DO UPDATE SET
|
||||
display_name = EXCLUDED.display_name,
|
||||
updated_at = now()
|
||||
RETURNING id
|
||||
"#,
|
||||
)
|
||||
.bind(intervals_athlete_id)
|
||||
.bind(display_name)
|
||||
.fetch_one(db)
|
||||
.await?;
|
||||
|
||||
use sqlx::Row;
|
||||
Ok(row.try_get("id")?)
|
||||
}
|
||||
|
||||
pub async fn ensure_activity(
|
||||
tx: &mut Transaction<'_, Postgres>,
|
||||
athlete_id: i64,
|
||||
|
|
|
|||
Loading…
Reference in a new issue