-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-db.sh
executable file
·50 lines (39 loc) · 1.65 KB
/
setup-db.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
#!/usr/bin/env bash
baseUrl="http://localhost:3001"
function curlSilent {
curl -s "$@"
}
function unquote() {
sed 's/^"\(.*\)"$/\1/'
}
function createAdmin() {
# Create a new user
body='{"email": "[email protected]", "name": "Admin", "password": "password"}'
curlSilent -XPOST $baseUrl/users -H "Content-Type: application/json" -d "$body"
# Set admin role directly in the database
body="db.getCollection('users').findOneAndUpdate({email: '[email protected]'}, {\\\$set: {role: 'admin'}})"
cmd="mongosh -u root -p password --authenticationDatabase admin fs3-pokedex --file /tmp/createAdmin.js"
sudo docker compose exec mongo bash -c "echo \"$body\" >/tmp/createAdmin.js && $cmd && rm /tmp/createAdmin.js" >/dev/null
}
function logIn() {
body='{"email":"[email protected]", "password":"password"}'
curlSilent -XPOST $baseUrl/auth/login -H "Content-Type: application/json" -d "$body"
}
function createDefaultChatrooms() {
# Get JWT
jwt=$(logIn | jq .jwt | unquote)
# Get user ID
userId=$(curlSilent $baseUrl/users/@me -H "Authorization: Bearer $jwt" | jq .id | unquote)
# Create default chatrooms
body="{\"name\": \"general\", \"members\": [\"$userId\"]}"
curlSilent -X POST "$baseUrl/chatrooms" -H "Content-Type: application/json" -H "Authorization: Bearer $jwt" -d "$body"
body="{\"name\": \"private\", \"members\": [\"$userId\"]}"
curlSilent -X POST "$baseUrl/chatrooms" -H "Content-Type: application/json" -H "Authorization: Bearer $jwt" -d "$body"
}
command -v jq >/dev/null 2>&1 || {
echo >&2 "jq is required but not installed. Aborting."
echo "Install jq with: sudo apt install jq"
exit 1
}
createAdmin
createDefaultChatrooms