Merge pull request #12002 from Budibase/fix/users-column-search-mapping

Fix/users column search mapping
This commit is contained in:
Adria Navarro 2023-10-09 13:54:54 +02:00 committed by GitHub
commit afedd560fd
2 changed files with 73 additions and 50 deletions

View File

@ -20,58 +20,73 @@ const tableWithUserCol: Table = {
}, },
} }
describe("searchInputMapping", () => { const tableWithUsersCol: Table = {
const globalUserId = dbCore.generateGlobalUserID() _id: tableId,
const userMedataId = dbCore.generateUserMetadataID(globalUserId) name: "table",
schema: {
user: {
name: "user",
type: FieldType.BB_REFERENCE,
subtype: FieldTypeSubtypes.BB_REFERENCE.USERS,
},
},
}
it("should be able to map ro_ to global user IDs", () => { describe.each([tableWithUserCol, tableWithUsersCol])(
const params: SearchParams = { "searchInputMapping",
tableId, col => {
query: { const globalUserId = dbCore.generateGlobalUserID()
equal: { const userMedataId = dbCore.generateUserMetadataID(globalUserId)
"1:user": userMedataId,
it("should be able to map ro_ to global user IDs", () => {
const params: SearchParams = {
tableId,
query: {
equal: {
"1:user": userMedataId,
},
}, },
}, }
} const output = searchInputMapping(col, params)
const output = searchInputMapping(tableWithUserCol, params) expect(output.query.equal!["1:user"]).toBe(globalUserId)
expect(output.query.equal!["1:user"]).toBe(globalUserId) })
})
it("should handle array of user IDs", () => { it("should handle array of user IDs", () => {
const params: SearchParams = { const params: SearchParams = {
tableId, tableId,
query: { query: {
oneOf: { oneOf: {
"1:user": [userMedataId, globalUserId], "1:user": [userMedataId, globalUserId],
},
}, },
}, }
} const output = searchInputMapping(col, params)
const output = searchInputMapping(tableWithUserCol, params) expect(output.query.oneOf!["1:user"]).toStrictEqual([
expect(output.query.oneOf!["1:user"]).toStrictEqual([ globalUserId,
globalUserId, globalUserId,
globalUserId, ])
]) })
})
it("shouldn't change any other input", () => { it("shouldn't change any other input", () => {
const email = "test@test.com" const email = "test@test.com"
const params: SearchParams = { const params: SearchParams = {
tableId, tableId,
query: { query: {
equal: { equal: {
"1:user": email, "1:user": email,
},
}, },
}, }
} const output = searchInputMapping(col, params)
const output = searchInputMapping(tableWithUserCol, params) expect(output.query.equal!["1:user"]).toBe(email)
expect(output.query.equal!["1:user"]).toBe(email) })
})
it("shouldn't error if no query supplied", () => { it("shouldn't error if no query supplied", () => {
const params: any = { const params: any = {
tableId, tableId,
} }
const output = searchInputMapping(tableWithUserCol, params) const output = searchInputMapping(col, params)
expect(output.query).toBeUndefined() expect(output.query).toBeUndefined()
}) })
}) }
)

View File

@ -5,8 +5,10 @@ import {
Table, Table,
DocumentType, DocumentType,
SEPARATOR, SEPARATOR,
FieldSubtype,
} from "@budibase/types" } from "@budibase/types"
import { db as dbCore } from "@budibase/backend-core" import { db as dbCore } from "@budibase/backend-core"
import { utils } from "@budibase/shared-core"
function findColumnInQueries( function findColumnInQueries(
column: string, column: string,
@ -66,8 +68,14 @@ export function searchInputMapping(table: Table, options: SearchParams) {
for (let [key, column] of Object.entries(table.schema)) { for (let [key, column] of Object.entries(table.schema)) {
switch (column.type) { switch (column.type) {
case FieldType.BB_REFERENCE: case FieldType.BB_REFERENCE:
if (column.subtype === FieldTypeSubtypes.BB_REFERENCE.USER) { const subtype = column.subtype as FieldSubtype
userColumnMapping(key, options) switch (subtype) {
case FieldSubtype.USER:
case FieldSubtype.USERS:
userColumnMapping(key, options)
break
default:
utils.unreachable(subtype)
} }
break break
} }