-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.sh
executable file
·138 lines (117 loc) · 3.12 KB
/
app.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/sh
set -e
# Required
if [ -z "$SPOND_CLUB_ID" ]; then
echo "Please specify SPOND_CLUB_ID"
exit 1
fi
# Required
if [ -z "$SPOND_EMAIL" ]; then
echo "Please specify SPOND_EMAIL"
exit 1
fi
# Required
if [ -z "$SPOND_PASSWORD" ]; then
echo "Please specify SPOND_PASSWORD"
exit 1
fi
# Required
if [ -z "$PGHOST" ]; then
echo "Please specify PGHOST"
exit 1
fi
# Required
if [ -z "$PGDATABASE" ]; then
echo "Please specify PGDATABASE"
exit 1
fi
# Required
if [ -z "$PGUSER" ]; then
echo "Please specify PGUSER"
exit 1
fi
# Required
if [ -z "$PGPASSWORD" ]; then
echo "Please specify PGPASSWORD"
exit 1
fi
if [ -z "$PGPORT" ]; then
export PGPORT=5432
fi
if [ -z "$PGSSLMODE" ]; then
export PGSSLMODE="require"
fi
# Ensure data directory exists
if [ ! -d "data" ]; then
if [ -e "data" ]; then
echo "Please ensure the 'data' is a directory"
exit 1
fi
mkdir -p data
fi
SPOND_CREDS="{\"email\":\"$SPOND_EMAIL\",\"password\":\"$SPOND_PASSWORD\"}"
API_URL='https://api.spond.com/club/v1'
# Login and store token
echo "Logging in to Spond Using SPOND_EMAIL=$SPOND_EMAIL and SPOND_PASSWORD=<hidden>..."
curl -s -H 'Content-Type: application/json' \
-d $SPOND_CREDS -o data/login.json \
"${API_URL}/login"
SPOND_TOKEN=$(jq -r .loginToken data/login.json)
SPOND_AUTH="Authorization: Bearer $SPOND_TOKEN"
# Fetch member types with token auth
echo "Fetching Spond member types using SPOND_CLUB_ID=$SPOND_CLUB_ID..."
curl -s -H "$SPOND_AUTH" -H "X-Spond-ClubId: $SPOND_CLUB_ID" \
"${API_URL}/memberTypes" | jq . > data/types.json
# Fetch field types with token auth
echo "Fetching Spond member fields using SPOND_CLUB_ID=$SPOND_CLUB_ID..."
curl -s -H "$SPOND_AUTH" -H "X-Spond-ClubId: $SPOND_CLUB_ID" \
"${API_URL}/fields" | jq .clubFields > data/fields.json
# Fetch members with token auth
echo "Fetching Spond members using SPOND_CLUB_ID=$SPOND_CLUB_ID..."
curl -s -H "$SPOND_AUTH" -H "X-Spond-ClubId: $SPOND_CLUB_ID" \
"${API_URL}/members" | jq . > data/members.json
# Import JSON files into PostgreSQL database tables
echo "Importing downloaded Spond JSON data into members database..."
echo "Using PGHOST=$PGHOST"
echo "Using PGPORT=$PGPORT"
echo "Using PGDATABASE=$PGDATABASE"
echo "Using PGUSER=$PGUSER"
echo "Using PGSSLMODE=$PGSSLMODE"
echo "Using PGPASSWORD=<hidden>"
psql -1 <<'EOF'
-- Import member types
\set content `cat data/types.json`
DELETE FROM member_types;
INSERT INTO member_types (
id,
name
) SELECT * FROM json_populate_recordset(null::member_types, :'content');
-- Import member fields
\set content `cat data/fields.json`
DELETE FROM member_fields;
INSERT INTO member_fields (
id,
name,
type
) SELECT * FROM json_populate_recordset(null::member_fields, :'content');
-- Import members
\set content `cat data/members.json`
DELETE FROM members;
INSERT INTO members (
id,
"firstName",
"lastName",
profile,
type,
"fieldValues",
guardians,
nationality,
"joinedDate",
"dateOfBirth",
address,
country,
groups,
gender,
deleted
) SELECT * FROM json_populate_recordset(null::members, :'content');
EOF