Handle arrays and fix issue brought up by REST testcase.

This commit is contained in:
mike12345567 2023-10-06 12:31:52 +01:00
parent cfe261a8f6
commit 6e6c5bc776
2 changed files with 51 additions and 8 deletions

View File

@ -21,9 +21,10 @@ const tableWithUserCol: Table = {
}
describe("searchInputMapping", () => {
const globalUserId = dbCore.generateGlobalUserID()
const userMedataId = dbCore.generateUserMetadataID(globalUserId)
it("should be able to map ro_ to global user IDs", () => {
const globalUserId = dbCore.generateGlobalUserID()
const userMedataId = dbCore.generateUserMetadataID(globalUserId)
const params: SearchParams = {
tableId,
query: {
@ -36,6 +37,22 @@ describe("searchInputMapping", () => {
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],
},
},
}
const output = searchInputMapping(tableWithUserCol, params)
expect(output.query.oneOf!["1:user"]).toStrictEqual([
globalUserId,
globalUserId,
])
})
it("shouldn't change any other input", () => {
const email = "test@test.com"
const params: SearchParams = {
@ -49,4 +66,12 @@ describe("searchInputMapping", () => {
const output = searchInputMapping(tableWithUserCol, 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()
})
})

View File

@ -13,6 +13,9 @@ function findColumnInQueries(
options: SearchParams,
callback: (filter: any) => any
) {
if (!options.query) {
return
}
for (let filterBlock of Object.values(options.query)) {
if (typeof filterBlock !== "object") {
continue
@ -27,15 +30,30 @@ function findColumnInQueries(
function userColumnMapping(column: string, options: SearchParams) {
findColumnInQueries(column, options, (filterValue: any): string => {
if (typeof filterValue !== "string") {
const isArray = Array.isArray(filterValue),
isString = typeof filterValue === "string"
if (!isString && !isArray) {
return filterValue
}
const rowPrefix = DocumentType.ROW + SEPARATOR
// TODO: at some point in future might want to handle mapping emails to IDs
if (filterValue.startsWith(rowPrefix)) {
return dbCore.getGlobalIDFromUserMetadataID(filterValue)
const processString = (input: string) => {
const rowPrefix = DocumentType.ROW + SEPARATOR
if (input.startsWith(rowPrefix)) {
return dbCore.getGlobalIDFromUserMetadataID(input)
} else {
return input
}
}
if (isArray) {
return filterValue.map(el => {
if (typeof el === "string") {
return processString(el)
} else {
return el
}
})
} else {
return processString(filterValue)
}
return filterValue
})
}