diff --git a/services/identity/src/database/device_list.rs b/services/identity/src/database/device_list.rs --- a/services/identity/src/database/device_list.rs +++ b/services/identity/src/database/device_list.rs @@ -3,7 +3,9 @@ use std::collections::HashMap; -use aws_sdk_dynamodb::{client::fluent_builders::Query, model::AttributeValue}; +use aws_sdk_dynamodb::{ + client::fluent_builders::Query, model::AttributeValue, output::GetItemOutput, +}; use chrono::{DateTime, Utc}; use tracing::{error, warn}; @@ -305,6 +307,30 @@ } } +/// Checks if given device exists on user's current device list +pub async fn device_exists( + db: &crate::database::DatabaseClient, + user_id: impl Into, + device_id: impl Into, +) -> Result { + let GetItemOutput { item, .. } = db + .client + .get_item() + .table_name(devices_table::NAME) + .key(ATTR_USER_ID, AttributeValue::S(user_id.into())) + .key(ATTR_ITEM_ID, DeviceIDAttribute(device_id.into()).into()) + // only fetch the primary key, we don't need the rest + .projection_expression(format!("{ATTR_USER_ID}, {ATTR_ITEM_ID}")) + .send() + .await + .map_err(|e| { + error!("Failed to check if device exists: {:?}", e); + Error::AwsSdk(e.into()) + })?; + + Ok(item.is_some()) +} + /// Retrieves user's current devices and their full data pub async fn get_current_devices( db: &crate::database::DatabaseClient,