forked from watermelontools/watermelon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewIntegration.sh
executable file
Β·139 lines (123 loc) Β· 4.08 KB
/
newIntegration.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
139
#!/bin/bash
# Receive a "serviceName" parameter
serviceName=$1
# Create a folder with the serviceName under /app/(loggedIn)
mkdir -p ./app/\(loggedIn\)/${serviceName}
# Create a file "/app/(loggedIn)/${serviceName}/loading.tsx"
cat > ./app/\(loggedIn\)/${serviceName}/loading.tsx <<EOL
import LoadingConnectedService from "../../../components/services/loading";
export default function loadingConnectedService() {
return <LoadingConnectedService />;
}
EOL
# Create a folder with the serviceName under /utils/db
mkdir -p ./utils/db/${serviceName}
# Create a file "saveUser.ts" in that folder
cat > ./utils/db/${serviceName}/saveUser.ts <<EOL
import executeRequest from "../azuredb";
export default async ({
access_token,
id,
avatar_url,
watermelon_user,
refresh_token,
workspace,
name
}) => {
let query = \`EXEC dbo.create_${serviceName} @watermelon_user='\${watermelon_user}', @id='\${id}',
@avatar_url='\${avatar_url}', @name='\${name}', @access_token='\${access_token}', @refresh_token='\${refresh_token}',@workspace='\${workspace}';
\`;
let resp = await executeRequest(query);
return resp;
};
EOL
# Create a file "/app/(loggedIn)/${serviceName}/page.tsx"
cat > ./app/\(loggedIn\)/${serviceName}/page.tsx <<EOL
import { getServerSession } from "next-auth";
//change this to import correctly
import saveUserInfo from "../../../utils/db/${serviceName}/saveUser";
import { authOptions } from "../../api/auth/[...nextauth]/route";
import getAllPublicUserData from "../../../utils/api/getAllUserPublicData";
import ConnectedService from "../../../components/services/page";
import LoginArray from "../../../components/services/loginArray";
export default async function ServicePage({
searchParams,
}: {
searchParams: { [key: string]: string | string[] | undefined };
}) {
const session = await getServerSession(authOptions);
const userEmail = session?.user?.email;
const userName = session?.user?.name;
const { code, state } = searchParams;
let error = "";
// change service name
const serviceName = "${serviceName}";
const [userData, serviceToken] = await Promise.all([
getAllPublicUserData({ email: userEmail }).catch((e) => {
console.error(e);
return null;
}),
// change this fetch
fetch(\`SERVICE_OAUTH_URL\`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
grant_type: "authorization_code",
code: code,
redirect_uri: "https://app.watermelontools.com/${serviceName}",
client_id: process.env.SERVICE_APP_ID,
client_secret: process.env.SERVICE_CLIENT_SECRET,
}),
}),
]);
// the recommended services should not be of the same category as the current one
const nameList = [];
const loginArray = LoginArray({ nameList, userEmail, userData });
const json = await serviceToken.json();
if (json.error) {
error = json.error;
} else {
// get user correctly
let user = await fetch(\`YOUR_USER_API_LINK\`, {
headers: {
Authorization: \`Bearer \${json.access_token}\`,
},
});
let userJson = await user.json();
// save user correctly
await saveUserInfo({
access_token: json.access_token,
refresh_token: json.refresh_token,
scope: json.scope,
username: userJson.username,
id: userJson.id,
avatar_url: userJson.avatar_url,
watermelon_user: state,
name: userJson.name,
organization: userJson.organization,
website: userJson.website,
email: userJson.email,
location: userJson.location,
bio: userJson.bio,
twitter: userJson.twitter,
linkedin: userJson.linkedin,
});
return (
<ConnectedService
serviceName={serviceName}
displayName={userJson.username}
teamName={userJson.organization}
avatarUrl={userJson.avatar_url}
loginArray={loginArray}
error={error}
/>
);
}
}
EOL
# Create a file "/utils/actions/get${serviceName}.ts"
touch ./utils/actions/get${serviceName}.ts
echo "Files and directories created successfully!"