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(conf = NULL, seed = 123)

Arguments

conf

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: Device users matched, changed, added and removed in MongoDB

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. Updates the device users in MongoDB in place, matched by IMEI, and removes those whose IMEI is no longer in the list

Password generation uses letters (upper/lower), numbers (0-9), and is reproducible when a seed is provided.

MongoDB is never emptied: the tracks app keeps users of its own there (administrators, fishers who registered themselves), and fishers can change their password in the app. A password goes to MongoDB only when the device user is first created, so changing one in Airtable does not reach the app.

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