Sync Device Users to MongoDB and Update Airtable
sync_device_users.Rd
Retrieves device data from Airtable, joins with user data, generates passwords for new users, updates the Airtable users table with new passwords, and pushes the combined dataset to MongoDB. This function maintains user credentials for devices in Africa timezones with complete synchronization between systems.
Arguments
- pars
List. Configuration parameters (defaults to read_config()). Must contain airtable (token, frame and tracks_app base_ids) and storage (mongodb tracks_app connection_string, database_name, collection) configuration.
- seed
Numeric. Random seed for password generation (default: 123). Set to NULL for random passwords each time.
Value
List containing user data counts, Airtable sync result, and MongoDB push result.
total_users: Number of users synchronized to MongoDB
new_passwords_generated: Number of new passwords created
airtable_sync_result: Result of syncing users to Airtable (updates + creates)
mongodb_result: Result of MongoDB data push
Details
The function performs the following steps:
Retrieves users data from Airtable tracks_app base
Retrieves device data from Airtable frame base (pds_devices table)
Filters devices to Africa timezones only
Joins devices with existing user data by IMEI
Generates 8-character alphanumeric passwords for ALL users without passwords
Removes duplicate IMEI entries (keeps latest)
Syncs complete user dataset to Airtable users table (updates existing, creates new)
Pushes the complete dataset to MongoDB
Password generation uses letters (upper/lower), numbers (0-9), and is reproducible when a seed is provided. The function ensures complete synchronization by updating both Airtable and MongoDB with the same data.
Examples
if (FALSE) { # \dontrun{
# Using default configuration and seed
result <- sync_device_users()
cat("Generated", result$new_passwords_generated, "new passwords")
cat("Synced", result$total_users, "users to MongoDB")
# Using custom configuration and random passwords
custom_config <- read_config("custom_conf.yml")
result <- sync_device_users(custom_config, seed = NULL)
# Check Airtable sync results
if (!is.null(result$airtable_sync_result)) {
cat("Successfully synced users to Airtable")
cat("Updates:", length(result$airtable_sync_result$updates))
cat("Creates:", length(result$airtable_sync_result$creates))
}
} # }