Handle arrays and fix issue brought up by REST testcase.
This commit is contained in:
parent
cfe261a8f6
commit
6e6c5bc776
|
@ -21,9 +21,10 @@ const tableWithUserCol: Table = {
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("searchInputMapping", () => {
|
describe("searchInputMapping", () => {
|
||||||
|
const globalUserId = dbCore.generateGlobalUserID()
|
||||||
|
const userMedataId = dbCore.generateUserMetadataID(globalUserId)
|
||||||
|
|
||||||
it("should be able to map ro_ to global user IDs", () => {
|
it("should be able to map ro_ to global user IDs", () => {
|
||||||
const globalUserId = dbCore.generateGlobalUserID()
|
|
||||||
const userMedataId = dbCore.generateUserMetadataID(globalUserId)
|
|
||||||
const params: SearchParams = {
|
const params: SearchParams = {
|
||||||
tableId,
|
tableId,
|
||||||
query: {
|
query: {
|
||||||
|
@ -36,6 +37,22 @@ describe("searchInputMapping", () => {
|
||||||
expect(output.query.equal!["1:user"]).toBe(globalUserId)
|
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", () => {
|
it("shouldn't change any other input", () => {
|
||||||
const email = "test@test.com"
|
const email = "test@test.com"
|
||||||
const params: SearchParams = {
|
const params: SearchParams = {
|
||||||
|
@ -49,4 +66,12 @@ describe("searchInputMapping", () => {
|
||||||
const output = searchInputMapping(tableWithUserCol, 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", () => {
|
||||||
|
const params: any = {
|
||||||
|
tableId,
|
||||||
|
}
|
||||||
|
const output = searchInputMapping(tableWithUserCol, params)
|
||||||
|
expect(output.query).toBeUndefined()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -13,6 +13,9 @@ function findColumnInQueries(
|
||||||
options: SearchParams,
|
options: SearchParams,
|
||||||
callback: (filter: any) => any
|
callback: (filter: any) => any
|
||||||
) {
|
) {
|
||||||
|
if (!options.query) {
|
||||||
|
return
|
||||||
|
}
|
||||||
for (let filterBlock of Object.values(options.query)) {
|
for (let filterBlock of Object.values(options.query)) {
|
||||||
if (typeof filterBlock !== "object") {
|
if (typeof filterBlock !== "object") {
|
||||||
continue
|
continue
|
||||||
|
@ -27,15 +30,30 @@ function findColumnInQueries(
|
||||||
|
|
||||||
function userColumnMapping(column: string, options: SearchParams) {
|
function userColumnMapping(column: string, options: SearchParams) {
|
||||||
findColumnInQueries(column, options, (filterValue: any): string => {
|
findColumnInQueries(column, options, (filterValue: any): string => {
|
||||||
if (typeof filterValue !== "string") {
|
const isArray = Array.isArray(filterValue),
|
||||||
|
isString = typeof filterValue === "string"
|
||||||
|
if (!isString && !isArray) {
|
||||||
return filterValue
|
return filterValue
|
||||||
}
|
}
|
||||||
const rowPrefix = DocumentType.ROW + SEPARATOR
|
const processString = (input: string) => {
|
||||||
// TODO: at some point in future might want to handle mapping emails to IDs
|
const rowPrefix = DocumentType.ROW + SEPARATOR
|
||||||
if (filterValue.startsWith(rowPrefix)) {
|
if (input.startsWith(rowPrefix)) {
|
||||||
return dbCore.getGlobalIDFromUserMetadataID(filterValue)
|
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
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue