Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,9 @@ template.beconf.tfvars
backend.tf
*.iso
.env
.coverage
.coverage

# participants info
create_accounts_local.sh
participants.tsv
generate_tsv.sh
75 changes: 75 additions & 0 deletions create_accounts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env bash
set -euo pipefail

# === DESCRIPTION ==================
# This script creates accounts for a list of participants in a TSV file.
# The TSV file should have the following columns:
# Occupation/Specialty Email Prolific ID.
# The script will randomly select a specified number of participants and create accounts for them.
# The script uses the Prolific API to create accounts.
# The script requires the following environment variables to be set:
# - API_URL: The URL of the admin API for creating accounts.
# - API_KEY: The API key for admin API for creating accounts.
# The script will create accounts for the specified number of participants and print the number of accounts created.
# The script will also print the number of accounts created.
# ==================================

# === CONFIGURATION ===
API_URL="api_url_here"
API_KEY="api_key_here
Comment on lines +18 to +19
Copy link

Copilot AI Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding placeholder values can cause confusion. Instead, read from the environment and validate, e.g.: : "${API_URL:?Please set API_URL}" and remove the default assignment.

Suggested change
API_URL="api_url_here"
API_KEY="api_key_here
: "${API_URL:?Error: Please set API_URL environment variable}"
: "${API_KEY:?Error: Please set API_KEY environment variable}"

Copilot uses AI. Check for mistakes.
INPUT_FILE="participants.tsv"
NUM_SAMPLES=100

# === sanity check ===
if [ ! -f "$INPUT_FILE" ]; then
echo "Error: '$INPUT_FILE' not found. Please save your list as a TSV with columns:" \
"Occupation/Specialty<TAB>Email<TAB>Prolific ID." >&2
exit 1
fi

# === helper to produce randomized lines of "email|id" with valid emails only ===
random_stream() {
awk -F'\t' '
BEGIN { srand() }
NR>1 && NF>=3 {
email = $2
# trim leading/trailing whitespace
gsub(/^[[:space:]]+|[[:space:]]+$/, "", email)
# only keep if looks like [email protected] (no spaces)
if (email ~ /^[^[:space:]]+@[^[:space:]]+\.[^[:space:]]+$/) {
printf "%f\t%s|%s\n", rand(), email, $3
}
}
' "$INPUT_FILE" \
| sort -n
}

# === build the JSON array ===
users_json="["
count=0

while IFS=$'\t' read -r _ pair; do
IFS='|' read -r email id <<< "$pair"
users_json+=$(
printf '{"name":"Prolific ID: %s","email":"%s","position":"Physician Assistant (PA)","employer":"n/a","area_of_clinical_ex":"n/a"},' \
"$id" "$email"
)
count=$((count+1))
[ "$count" -ge "$NUM_SAMPLES" ] && break
done < <(random_stream)
Comment on lines +51 to +59
Copy link

Copilot AI Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script ignores the first column (Occupation/Specialty) and hardcodes position, employer, etc. Unpack all three fields (e.g., read -r occupation email id) and use "$occupation" instead of a fixed string to keep data accurate.

Suggested change
while IFS=$'\t' read -r _ pair; do
IFS='|' read -r email id <<< "$pair"
users_json+=$(
printf '{"name":"Prolific ID: %s","email":"%s","position":"Physician Assistant (PA)","employer":"n/a","area_of_clinical_ex":"n/a"},' \
"$id" "$email"
)
count=$((count+1))
[ "$count" -ge "$NUM_SAMPLES" ] && break
done < <(random_stream)
while IFS=$'\t' read -r occupation email id; do
users_json+=$(
printf '{"name":"Prolific ID: %s","email":"%s","position":"%s","employer":"n/a","area_of_clinical_ex":"n/a"},' \
"$id" "$email" "$occupation"
)
count=$((count+1))
[ "$count" -ge "$NUM_SAMPLES" ] && break
done < <(random_stream | awk -F'|' '{print $1"\t"$2"\t"$3}')

Copilot uses AI. Check for mistakes.

if [ "$count" -eq 0 ]; then
echo "Error: no valid emails found in $INPUT_FILE" >&2
exit 1
fi

# strip trailing comma, close array
users_json="${users_json%,}]"

# === send one bulk request ===
curl --location "$API_URL" \
--header "X-API-KEY: $API_KEY" \
--header 'Content-Type: application/json' \
--data-raw "{\"users\":$users_json}"

echo "✅ Created accounts for $count randomly selected participants."