-
-
Notifications
You must be signed in to change notification settings - Fork 685
/
Copy pathapi.ts
103 lines (92 loc) · 2.51 KB
/
api.ts
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
import axios from 'axios'
import { Config } from './config'
import { getDbClient } from './system-db'
export const api = axios.create({
baseURL: Config.API_ENDPOINT,
validateStatus: (status) => {
return status >= 200 && status <= 430
},
})
export async function EnsureTestToken() {
const res = await api.post('/v1/auth/passwd/signin', {
username: Config.TEST_USERNAME,
password: Config.TEST_PASSWORD,
})
const token: string = res.data?.data
if (token) {
return token
}
const res2 = await api.post('/v1/auth/passwd/signup', {
username: Config.TEST_USERNAME,
password: Config.TEST_PASSWORD,
})
const token2: string = res2.data?.data?.token
return token2
}
export async function GetTestApplication() {
const client = await getDbClient()
const db = client.db()
try {
const user = await db
.collection('User')
.findOne({ username: Config.TEST_USERNAME })
const app = await db.collection('Application').findOne({
createdBy: user._id,
state: 'Running',
phase: 'Started',
name: Config.TEST_APP_NAME,
})
return app
} finally {
await client.close()
}
}
export async function ClearTestApplications() {
const client = await getDbClient()
const db = client.db()
const user = await db
.collection('User')
.findOne({ username: Config.TEST_USERNAME })
try {
if (!user) return
await db
.collection('Application')
.updateMany({ createdBy: user._id }, { $set: { state: 'Deleted' } })
} finally {
await client.close()
}
}
export async function ClearTestUser(username: string) {
const client = await getDbClient()
const db = client.db()
try {
const user = await db.collection('User').findOne({ username: username })
if (!user) return
await db.collection('UserPassword').deleteMany({ uid: user._id })
await db.collection('UserProfile').deleteOne({ uid: user._id })
await db.collection('Account').deleteOne({ createdBy: user._id })
await db.collection('User').deleteOne({ _id: user._id })
} finally {
await client.close()
}
}
export async function GetRegion() {
const client = await getDbClient()
const db = client.db()
try {
const region = await db.collection('Region').findOne()
return region
} finally {
await client.close()
}
}
export async function GetRuntime() {
const client = await getDbClient()
const db = client.db()
try {
const runtime = await db.collection('Runtime').findOne()
return runtime
} finally {
await client.close()
}
}