bugfix: clearing sessions on backend update

This commit is contained in:
Michael Shanks 2020-03-28 06:39:22 +00:00
parent 20c624d3c9
commit cb1fc286b1
5 changed files with 29 additions and 7 deletions

View File

@ -3,6 +3,8 @@ import { _deleteRecord } from "../recordApi/delete"
import { getAllIdsIterator } from "../indexing/allIds"
import { permission } from "../authApi/permissions"
import { getCollectionDir } from "../recordApi/recordInfo"
import { ensureCollectionIsInitialised } from "./initialise"
import { getNodeForCollectionPath } from "../templateApi/hierarchy"
export const deleteCollection = (app, disableCleanup = false) => async key =>
apiWrapper(
@ -25,14 +27,19 @@ export const _deleteCollection = async (app, key, disableCleanup) => {
key = safeKey(key)
const collectionDir = getCollectionDir(app.hierarchy, key)
await deleteRecords(app, key)
await deleteCollectionFolder(app, collectionDir)
await deleteCollectionFolder(app, key, collectionDir)
if (!disableCleanup) {
await app.cleanupTransactions()
}
}
const deleteCollectionFolder = async (app, dir) =>
const deleteCollectionFolder = async (app, key, dir) => {
await app.datastore.deleteFolder(dir)
await ensureCollectionIsInitialised(
app.datastore,
getNodeForCollectionPath(app.hierarchy)(key),
dir)
}
const deleteRecords = async (app, key) => {
const iterate = await getAllIdsIterator(app)(key)

View File

@ -6,7 +6,7 @@ import {
} from "../templateApi/hierarchy"
import { $, allTrue, joinKey } from "../common"
const ensureCollectionIsInitialised = async (datastore, node, dir) => {
export const ensureCollectionIsInitialised = async (datastore, node, dir) => {
if (!(await datastore.exists(dir))) {
await datastore.createFolder(dir)
await datastore.createFolder(joinKey(dir, node.nodeId))

View File

@ -8,7 +8,7 @@ import { permission } from "../src/authApi/permissions"
describe("collectionApi > delete", () => {
it("should remove every key in collection's path", async () => {
const { recordApi, collectionApi } = await setupApphierarchy(
const { recordApi, collectionApi, appHierarchy } = await setupApphierarchy(
basicAppHierarchyCreator_WithFields
)
const record1 = recordApi.getNew("/customers", "customer")
@ -31,7 +31,10 @@ describe("collectionApi > delete", () => {
filter(k => splitKey(k)[0] === "customers"),
])
expect(remainingKeys).toEqual([])
expect(remainingKeys).toEqual([
"/customers",
`/customers/${appHierarchy.customerRecord.nodeId}`,
])
})
it("should not delete anything that is not in its path", async () => {
@ -51,7 +54,8 @@ describe("collectionApi > delete", () => {
filter(k => splitKey(k)[0] === "customers"),
])
const expectedRemainingKeys = allKeys.length - customerKeys.length
const expectedRemainingKeys = allKeys.length - customerKeys.length + 2
// +2 because is should keep the collection folders: /customers & /customers/1
await collectionApi.delete("/customers")

View File

@ -6,5 +6,6 @@ module.exports = async ctx => {
accessLevels.version = existingAccessLevels.version
await ctx.instance.authApi.saveAccessLevels(accessLevels)
await ctx.instance.templateApi.upgradeData(ctx.request.body.newHierarchy)
await ctx.master.clearAllSessions(ctx.params.appname)
ctx.response.status = StatusCodes.OK
}

View File

@ -325,6 +325,15 @@ module.exports = async context => {
}
}
const clearAllSessions = async appname => {
if (isMaster(appname)) {
await bb.collectionApi.delete("/mastersessions")
} else {
const app = await getApplication(appname)
await bb.collectionApi.delete(`/applications/${app.id}/sessions`)
}
}
const getApplicationWithInstances = async appname => {
const app = cloneDeep(await getApplication(appname))
app.instances = await bb.indexApi.listItems(
@ -346,7 +355,7 @@ module.exports = async context => {
await bb.recordApi.save(userInMaster)
}
const deleteLatestPackageFromCache = (appname) => {
const deleteLatestPackageFromCache = appname => {
deleteCachedPackage(context, appname, LATEST_VERSIONID)
}
@ -370,5 +379,6 @@ module.exports = async context => {
getFullAccessApiForMaster,
getApplicationWithInstances,
deleteLatestPackageFromCache,
clearAllSessions,
}
}