Skip to content

Commit

Permalink
Merge pull request #12002 from Budibase/fix/users-column-search-mapping
Browse files Browse the repository at this point in the history
Fix/users column search mapping
  • Loading branch information
adrinr authored Oct 9, 2023
2 parents b47ebd9 + 29686d1 commit 6a39eb6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 50 deletions.
111 changes: 63 additions & 48 deletions packages/server/src/sdk/app/rows/search/tests/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,73 @@ const tableWithUserCol: Table = {
},
}

describe("searchInputMapping", () => {
const globalUserId = dbCore.generateGlobalUserID()
const userMedataId = dbCore.generateUserMetadataID(globalUserId)
const tableWithUsersCol: Table = {
_id: tableId,
name: "table",
schema: {
user: {
name: "user",
type: FieldType.BB_REFERENCE,
subtype: FieldTypeSubtypes.BB_REFERENCE.USERS,
},
},
}

describe.each([tableWithUserCol, tableWithUsersCol])(
"searchInputMapping",
col => {
const globalUserId = dbCore.generateGlobalUserID()
const userMedataId = dbCore.generateUserMetadataID(globalUserId)

it("should be able to map ro_ to global user IDs", () => {
const params: SearchParams = {
tableId,
query: {
equal: {
"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(tableWithUserCol, params)
expect(output.query.equal!["1:user"]).toBe(globalUserId)
})
}
const output = searchInputMapping(col, params)
expect(output.query.equal!["1:user"]).toBe(globalUserId)
})

it("should handle array of user IDs", () => {
const params: SearchParams = {
tableId,
query: {
oneOf: {
"1:user": [userMedataId, globalUserId],
it("should handle array of user IDs", () => {
const params: SearchParams = {
tableId,
query: {
oneOf: {
"1:user": [userMedataId, globalUserId],
},
},
},
}
const output = searchInputMapping(tableWithUserCol, params)
expect(output.query.oneOf!["1:user"]).toStrictEqual([
globalUserId,
globalUserId,
])
})
}
const output = searchInputMapping(col, params)
expect(output.query.oneOf!["1:user"]).toStrictEqual([
globalUserId,
globalUserId,
])
})

it("shouldn't change any other input", () => {
const email = "[email protected]"
const params: SearchParams = {
tableId,
query: {
equal: {
"1:user": email,
it("shouldn't change any other input", () => {
const email = "[email protected]"
const params: SearchParams = {
tableId,
query: {
equal: {
"1:user": email,
},
},
},
}
const output = searchInputMapping(tableWithUserCol, params)
expect(output.query.equal!["1:user"]).toBe(email)
})
}
const output = searchInputMapping(col, params)
expect(output.query.equal!["1:user"]).toBe(email)
})

it("shouldn't error if no query supplied", () => {
const params: any = {
tableId,
}
const output = searchInputMapping(tableWithUserCol, params)
expect(output.query).toBeUndefined()
})
})
it("shouldn't error if no query supplied", () => {
const params: any = {
tableId,
}
const output = searchInputMapping(col, params)
expect(output.query).toBeUndefined()
})
}
)
12 changes: 10 additions & 2 deletions packages/server/src/sdk/app/rows/search/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
Table,
DocumentType,
SEPARATOR,
FieldSubtype,
} from "@budibase/types"
import { db as dbCore } from "@budibase/backend-core"
import { utils } from "@budibase/shared-core"

function findColumnInQueries(
column: string,
Expand Down Expand Up @@ -66,8 +68,14 @@ export function searchInputMapping(table: Table, options: SearchParams) {
for (let [key, column] of Object.entries(table.schema)) {
switch (column.type) {
case FieldType.BB_REFERENCE:
if (column.subtype === FieldTypeSubtypes.BB_REFERENCE.USER) {
userColumnMapping(key, options)
const subtype = column.subtype as FieldSubtype
switch (subtype) {
case FieldSubtype.USER:
case FieldSubtype.USERS:
userColumnMapping(key, options)
break
default:
utils.unreachable(subtype)
}
break
}
Expand Down

0 comments on commit 6a39eb6

Please sign in to comment.