Move app enrichment of user sessions to users SDK

This commit is contained in:
Andrew Kingston 2023-07-17 14:56:09 +01:00
parent 1584ee4c95
commit 1a8da1c0af
3 changed files with 43 additions and 24 deletions

View File

@ -199,31 +199,10 @@ export async function fetch(ctx: UserCtx) {
}
}
// Get all builder sessions in each app
const sessions = await builderSocket?.getRoomSessions(appIds)
if (sessions?.length) {
let appSessionMap: Record<string, SocketSession[]> = {}
sessions.forEach(session => {
const room = session.room
if (!room) {
return
}
if (!appSessionMap[room]) {
appSessionMap[room] = []
}
appSessionMap[room].push(session)
})
apps.forEach(app => {
const sessions = appSessionMap[app.appId]
if (sessions?.length) {
app.sessions = sessions
} else {
delete app.sessions
}
})
}
// Enrich apps with all builder user sessions
const enrichedApps = await sdk.users.sessions.enrichApps(apps)
ctx.body = await checkAppMetadata(apps)
ctx.body = await checkAppMetadata(enrichedApps)
}
export async function fetchAppDefinition(ctx: UserCtx) {

View File

@ -1,5 +1,7 @@
import * as utils from "./utils"
import * as sessions from "./sessions"
export default {
...utils,
sessions,
}

View File

@ -0,0 +1,38 @@
import { builderSocket } from "../../websockets"
import { App, SocketSession } from "@budibase/types"
export const enrichApps = async (apps: App[]) => {
// Sessions can only exist for dev app IDs
const devAppIds = apps
.filter((app: any) => app.status === "development")
.map((app: any) => app.appId)
// Get all sessions for all apps and enrich app list
const sessions = await builderSocket?.getRoomSessions(devAppIds)
if (sessions?.length) {
let appSessionMap: Record<string, SocketSession[]> = {}
sessions.forEach(session => {
const room = session.room
if (!room) {
return
}
if (!appSessionMap[room]) {
appSessionMap[room] = []
}
appSessionMap[room].push(session)
})
return apps.map(app => {
// Shallow clone to avoid mutating original reference
let enriched = { ...app }
const sessions = appSessionMap[app.appId]
if (sessions?.length) {
enriched.sessions = sessions
} else {
delete enriched.sessions
}
return enriched
})
} else {
return apps
}
}