From b6c6e6ec6edc1f18e2e926b615d604bed7841814 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 23 Nov 2023 17:35:55 +0000 Subject: [PATCH] Clearing out per-app builders when app is deleted. --- packages/backend-core/src/users/db.ts | 4 ++-- .../worker/src/api/controllers/global/roles.ts | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/backend-core/src/users/db.ts b/packages/backend-core/src/users/db.ts index 2b6fd52d44..25b414c8da 100644 --- a/packages/backend-core/src/users/db.ts +++ b/packages/backend-core/src/users/db.ts @@ -146,12 +146,12 @@ export class UserDB { static async allUsers() { const db = getGlobalDB() - const response = await db.allDocs( + const response = await db.allDocs( dbUtils.getGlobalUserParams(null, { include_docs: true, }) ) - return response.rows.map((row: any) => row.doc) + return response.rows.map(row => row.doc!) } static async countUsersByApp(appId: string) { diff --git a/packages/worker/src/api/controllers/global/roles.ts b/packages/worker/src/api/controllers/global/roles.ts index 7b9ce74e3e..e1a3130924 100644 --- a/packages/worker/src/api/controllers/global/roles.ts +++ b/packages/worker/src/api/controllers/global/roles.ts @@ -51,10 +51,22 @@ export async function removeAppRole(ctx: Ctx) { const users = await sdk.users.db.allUsers() const bulk = [] const cacheInvalidations = [] + const prodAppId = dbCore.getProdAppID(appId) for (let user of users) { - if (user.roles[appId]) { - cacheInvalidations.push(cache.user.invalidateUser(user._id)) - delete user.roles[appId] + let updated = false + if (user.roles[prodAppId]) { + cacheInvalidations.push(cache.user.invalidateUser(user._id!)) + delete user.roles[prodAppId] + updated = true + } + if (user.builder && Array.isArray(user.builder?.apps)) { + const idx = user.builder.apps.indexOf(prodAppId) + if (idx !== -1) { + user.builder.apps.splice(idx, 1) + updated = true + } + } + if (updated) { bulk.push(user) } }