Track usage
This commit is contained in:
parent
94811ef4a0
commit
cb88465f42
|
@ -1,7 +1,10 @@
|
|||
import fetch, { RequestInit } from "node-fetch"
|
||||
import { HttpError } from "koa"
|
||||
import { get } from "../oauth2"
|
||||
import { OAuth2CredentialsMethod } from "@budibase/types"
|
||||
import { DocumentType, OAuth2CredentialsMethod } from "@budibase/types"
|
||||
import { cache, context } from "@budibase/backend-core"
|
||||
|
||||
const { DocWritethrough } = cache.docWritethrough
|
||||
|
||||
async function fetchToken(config: {
|
||||
url: string
|
||||
|
@ -40,6 +43,14 @@ async function fetchToken(config: {
|
|||
return resp
|
||||
}
|
||||
|
||||
const trackUsage = async (id: string) => {
|
||||
const writethrough = new DocWritethrough(
|
||||
context.getAppDB(),
|
||||
DocumentType.OAUTH2_CONFIG_USAGES
|
||||
)
|
||||
await writethrough.patch({ [id]: Date.now() })
|
||||
}
|
||||
|
||||
// TODO: check if caching is worth
|
||||
export async function generateToken(id: string) {
|
||||
const config = await get(id)
|
||||
|
@ -56,6 +67,7 @@ export async function generateToken(id: string) {
|
|||
throw new Error(`Error fetching oauth2 token: ${message}`)
|
||||
}
|
||||
|
||||
await trackUsage(id)
|
||||
return `${jsonResponse.token_type} ${jsonResponse.access_token}`
|
||||
}
|
||||
|
||||
|
|
|
@ -41,6 +41,7 @@ export enum DocumentType {
|
|||
SCIM_LOG = "scimlog",
|
||||
ROW_ACTIONS = "ra",
|
||||
OAUTH2_CONFIG = "oauth2",
|
||||
OAUTH2_CONFIG_USAGES = "oauth2_usage",
|
||||
}
|
||||
|
||||
// Because DocumentTypes can overlap, we need to make sure that we search
|
||||
|
|
Loading…
Reference in New Issue