Return lastUsage
This commit is contained in:
parent
6e7cd5632a
commit
6612182e33
|
@ -28,9 +28,17 @@ function toFetchOAuth2ConfigsResponse(
|
|||
export async function fetch(ctx: Ctx<void, FetchOAuth2ConfigsResponse>) {
|
||||
const configs = await sdk.oauth2.fetch()
|
||||
|
||||
const timestamps = await sdk.oauth2.getLastUsages(configs.map(c => c.id))
|
||||
|
||||
const response: FetchOAuth2ConfigsResponse = {
|
||||
configs: (configs || []).map(toFetchOAuth2ConfigsResponse),
|
||||
configs: (configs || []).map(c => ({
|
||||
...toFetchOAuth2ConfigsResponse(c),
|
||||
lastUsage: timestamps[c.id]
|
||||
? new Date(timestamps[c.id]).toISOString()
|
||||
: undefined,
|
||||
})),
|
||||
}
|
||||
|
||||
ctx.body = response
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
import fetch, { RequestInit } from "node-fetch"
|
||||
import { HttpError } from "koa"
|
||||
import { get } from "../oauth2"
|
||||
import { OAuth2CredentialsMethod } from "@budibase/types"
|
||||
import { Document, OAuth2CredentialsMethod } from "@budibase/types"
|
||||
import { cache, context, docIds } from "@budibase/backend-core"
|
||||
|
||||
interface OAuth2LogDocument extends Document {
|
||||
lastUsage: number
|
||||
}
|
||||
|
||||
const { DocWritethrough } = cache.docWritethrough
|
||||
|
||||
async function fetchToken(config: {
|
||||
|
@ -44,7 +48,7 @@ async function fetchToken(config: {
|
|||
}
|
||||
|
||||
const trackUsage = async (id: string) => {
|
||||
const writethrough = new DocWritethrough(
|
||||
const writethrough = new DocWritethrough<OAuth2LogDocument>(
|
||||
context.getAppDB(),
|
||||
docIds.generateOAuth2LogID(id)
|
||||
)
|
||||
|
@ -93,3 +97,19 @@ export async function validateConfig(config: {
|
|||
return { valid: false, message: e.message }
|
||||
}
|
||||
}
|
||||
|
||||
export async function getLastUsages(ids: string[]) {
|
||||
const docs = await context
|
||||
.getAppDB()
|
||||
.getMultiple<OAuth2LogDocument>(ids.map(docIds.generateOAuth2LogID), {
|
||||
allowMissing: true,
|
||||
})
|
||||
const result = ids.reduce<Record<string, number>>((acc, id) => {
|
||||
const doc = docs.find(d => d._id === docIds.generateOAuth2LogID(id))
|
||||
if (doc) {
|
||||
acc[id] = doc.lastUsage
|
||||
}
|
||||
return acc
|
||||
}, {})
|
||||
return result
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ export interface OAuth2ConfigResponse {
|
|||
}
|
||||
|
||||
export interface FetchOAuth2ConfigsResponse {
|
||||
configs: OAuth2ConfigResponse[]
|
||||
configs: (OAuth2ConfigResponse & { lastUsage?: string })[]
|
||||
}
|
||||
|
||||
export interface UpsertOAuth2ConfigRequest {
|
||||
|
|
Loading…
Reference in New Issue