Skip to contents

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.

Usage

sync_device_users(pars = NULL, seed = 123)

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:

  1. Retrieves users data from Airtable tracks_app base

  2. Retrieves device data from Airtable frame base (pds_devices table)

  3. Filters devices to Africa timezones only

  4. Joins devices with existing user data by IMEI

  5. Generates 8-character alphanumeric passwords for ALL users without passwords

  6. Removes duplicate IMEI entries (keeps latest)

  7. Syncs complete user dataset to Airtable users table (updates existing, creates new)

  8. 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))
}
} # }