2022-05-26 11:13:26 +02:00
|
|
|
import API from "./api"
|
|
|
|
import env from "../environment"
|
2022-11-16 18:23:12 +01:00
|
|
|
import { Header } from "../constants"
|
2023-02-21 09:23:53 +01:00
|
|
|
import { CloudAccount, HealthStatusResponse } from "@budibase/types"
|
2022-05-26 11:13:26 +02:00
|
|
|
|
|
|
|
const api = new API(env.ACCOUNT_PORTAL_URL)
|
|
|
|
|
2023-02-21 09:23:53 +01:00
|
|
|
/**
|
|
|
|
* This client is intended to be used in a cloud hosted deploy only.
|
|
|
|
* Rather than relying on each consumer to perform the necessary environmental checks
|
|
|
|
* we use the following check to exit early with a undefined response which should be
|
|
|
|
* handled by the caller.
|
|
|
|
*/
|
|
|
|
const EXIT_EARLY = env.SELF_HOSTED || env.DISABLE_ACCOUNT_PORTAL
|
|
|
|
|
2022-05-26 11:13:26 +02:00
|
|
|
export const getAccount = async (
|
|
|
|
email: string
|
|
|
|
): Promise<CloudAccount | undefined> => {
|
2023-02-21 09:23:53 +01:00
|
|
|
if (EXIT_EARLY) {
|
|
|
|
return
|
|
|
|
}
|
2022-05-26 11:13:26 +02:00
|
|
|
const payload = {
|
|
|
|
email,
|
|
|
|
}
|
|
|
|
const response = await api.post(`/api/accounts/search`, {
|
|
|
|
body: payload,
|
|
|
|
headers: {
|
2022-11-16 18:23:12 +01:00
|
|
|
[Header.API_KEY]: env.ACCOUNT_PORTAL_API_KEY,
|
2022-05-26 11:13:26 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if (response.status !== 200) {
|
|
|
|
throw new Error(`Error getting account by email ${email}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const json: CloudAccount[] = await response.json()
|
|
|
|
return json[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getAccountByTenantId = async (
|
|
|
|
tenantId: string
|
|
|
|
): Promise<CloudAccount | undefined> => {
|
2023-02-21 09:23:53 +01:00
|
|
|
if (EXIT_EARLY) {
|
|
|
|
return
|
|
|
|
}
|
2022-05-26 11:13:26 +02:00
|
|
|
const payload = {
|
|
|
|
tenantId,
|
|
|
|
}
|
|
|
|
const response = await api.post(`/api/accounts/search`, {
|
|
|
|
body: payload,
|
|
|
|
headers: {
|
2022-11-16 18:23:12 +01:00
|
|
|
[Header.API_KEY]: env.ACCOUNT_PORTAL_API_KEY,
|
2022-05-26 11:13:26 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if (response.status !== 200) {
|
|
|
|
throw new Error(`Error getting account by tenantId ${tenantId}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const json: CloudAccount[] = await response.json()
|
|
|
|
return json[0]
|
|
|
|
}
|
|
|
|
|
2023-02-21 09:23:53 +01:00
|
|
|
export const getStatus = async (): Promise<
|
|
|
|
HealthStatusResponse | undefined
|
|
|
|
> => {
|
|
|
|
if (EXIT_EARLY) {
|
|
|
|
return
|
|
|
|
}
|
2022-05-26 11:13:26 +02:00
|
|
|
const response = await api.get(`/api/status`, {
|
|
|
|
headers: {
|
2022-11-16 18:23:12 +01:00
|
|
|
[Header.API_KEY]: env.ACCOUNT_PORTAL_API_KEY,
|
2022-05-26 11:13:26 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
const json = await response.json()
|
|
|
|
|
|
|
|
if (response.status !== 200) {
|
|
|
|
throw new Error(`Error getting status`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|