Move app enrichment of user sessions to users SDK
This commit is contained in:
parent
1584ee4c95
commit
1a8da1c0af
|
@ -199,31 +199,10 @@ export async function fetch(ctx: UserCtx) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all builder sessions in each app
|
// Enrich apps with all builder user sessions
|
||||||
const sessions = await builderSocket?.getRoomSessions(appIds)
|
const enrichedApps = await sdk.users.sessions.enrichApps(apps)
|
||||||
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
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.body = await checkAppMetadata(apps)
|
ctx.body = await checkAppMetadata(enrichedApps)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchAppDefinition(ctx: UserCtx) {
|
export async function fetchAppDefinition(ctx: UserCtx) {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import * as utils from "./utils"
|
import * as utils from "./utils"
|
||||||
|
import * as sessions from "./sessions"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
...utils,
|
...utils,
|
||||||
|
sessions,
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue