-
Notifications
You must be signed in to change notification settings - Fork 587
Expand file tree
/
Copy pathnango.sh
More file actions
executable file
·51 lines (46 loc) · 1.89 KB
/
nango.sh
File metadata and controls
executable file
·51 lines (46 loc) · 1.89 KB
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
#!/usr/bin/env bash
set -euo pipefail
# Usage: scripts/nango.sh {create,refresh,delete} INTEGRATION_ID CONNECTION_ID [USER_ID]
#
# create INTEGRATION_ID CONNECTION_ID USER_ID - upsert connection as connected
# refresh INTEGRATION_ID CONNECTION_ID - mark connection as reconnect_required
# delete INTEGRATION_ID CONNECTION_ID - delete connection
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ENV_FILE="$SCRIPT_DIR/../.env.supabase"
DATABASE_URL=$(grep '^DATABASE_URL=' "$ENV_FILE" | sed "s/^DATABASE_URL=//;s/^['\"]//;s/['\"]$//")
[[ $# -lt 3 ]] && { echo "Usage: $0 {create,refresh,delete} INTEGRATION_ID CONNECTION_ID [USER_ID]"; exit 1; }
OP="$1" INTEGRATION="$2" CONNECTION="$3"
case "$OP" in
create)
USER_ID="${4:?Usage: $0 create INTEGRATION_ID CONNECTION_ID USER_ID}"
psql "$DATABASE_URL" -c "
INSERT INTO nango_connections (user_id, integration_id, connection_id, provider, status, updated_at)
VALUES ('$USER_ID', '$INTEGRATION', '$CONNECTION', '$INTEGRATION', 'connected', now())
ON CONFLICT (integration_id, connection_id) DO UPDATE SET
status = 'connected',
last_error_type = NULL,
last_error_description = NULL,
last_error_at = NULL,
updated_at = now();"
;;
refresh)
psql "$DATABASE_URL" -c "
UPDATE nango_connections SET
status = 'reconnect_required',
last_error_type = 'refresh_token_error',
last_error_description = 'Token expired',
last_error_at = now(),
updated_at = now()
WHERE integration_id = '$INTEGRATION' AND connection_id = '$CONNECTION';"
;;
delete)
psql "$DATABASE_URL" -c "
DELETE FROM nango_connections
WHERE integration_id = '$INTEGRATION' AND connection_id = '$CONNECTION';"
;;
*)
echo "Unknown operation: $OP"
echo "Usage: $0 {create,refresh,delete} INTEGRATION_ID CONNECTION_ID [USER_ID]"
exit 1
;;
esac