Handle ids
This commit is contained in:
parent
4c4c2e118b
commit
1ff3f5db80
|
@ -1,12 +1,13 @@
|
|||
import { User } from "@budibase/types"
|
||||
import { generator } from "./generator"
|
||||
import { uuid } from "./common"
|
||||
import { tenant } from "."
|
||||
|
||||
export const newEmail = () => {
|
||||
return `${uuid()}@test.com`
|
||||
}
|
||||
|
||||
export const user = (userProps?: any): User => {
|
||||
export const user = (userProps?: Partial<User>): User => {
|
||||
return {
|
||||
email: newEmail(),
|
||||
password: "test",
|
||||
|
@ -14,6 +15,7 @@ export const user = (userProps?: any): User => {
|
|||
firstName: generator.first(),
|
||||
lastName: generator.last(),
|
||||
pictureUrl: "http://test.com",
|
||||
tenantId: tenant.id(),
|
||||
...userProps,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,27 @@
|
|||
import { cache } from "@budibase/backend-core"
|
||||
import { utils } from "@budibase/shared-core"
|
||||
import { FieldSubtype } from "@budibase/types"
|
||||
|
||||
export async function processOutputBBReferences(
|
||||
export async function processInputBBReferences(
|
||||
value: string,
|
||||
subtype: FieldSubtype
|
||||
) {}
|
||||
) {
|
||||
const result = []
|
||||
const ids = value.split(",").map((id: string) => id.trim())
|
||||
|
||||
switch (subtype) {
|
||||
case FieldSubtype.USER:
|
||||
for (const id of ids) {
|
||||
result.push(await cache.user.getUser(id))
|
||||
}
|
||||
break
|
||||
default:
|
||||
utils.unreachable(subtype)
|
||||
}
|
||||
|
||||
if (result.length > 1) {
|
||||
return result
|
||||
}
|
||||
|
||||
return result[0]
|
||||
}
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
import * as backendCore from "@budibase/backend-core"
|
||||
import { FieldSubtype, User } from "@budibase/types"
|
||||
import { processInputBBReferences } from "../bbReferenceProcessor"
|
||||
import { generator, structures } from "@budibase/backend-core/tests"
|
||||
|
||||
jest.mock("@budibase/backend-core", (): typeof backendCore => {
|
||||
const actual = jest.requireActual("@budibase/backend-core")
|
||||
return {
|
||||
...actual,
|
||||
cache: {
|
||||
...actual.cache,
|
||||
user: {
|
||||
getUser: jest.fn(),
|
||||
invalidateUser: jest.fn(),
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
describe("bbReferenceProcessor", () => {
|
||||
const mockedCacheGetUser = backendCore.cache.user.getUser as jest.Mock
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks()
|
||||
})
|
||||
|
||||
describe("processInputBBReferences", () => {
|
||||
describe("subtype user", () => {
|
||||
it("fetches by user id", async () => {
|
||||
const input = generator.guid()
|
||||
|
||||
const userFromCache = structures.users.user()
|
||||
mockedCacheGetUser.mockResolvedValueOnce(userFromCache)
|
||||
|
||||
const result = await processInputBBReferences(input, FieldSubtype.USER)
|
||||
|
||||
expect(result).toEqual(userFromCache)
|
||||
expect(mockedCacheGetUser).toBeCalledTimes(1)
|
||||
expect(mockedCacheGetUser).toBeCalledWith(input)
|
||||
})
|
||||
|
||||
it("fetches by user id when send as csv", async () => {
|
||||
const users: Record<string, User> = {}
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const userId = generator.guid()
|
||||
const user = structures.users.user({ _id: userId, userId })
|
||||
mockedCacheGetUser.mockResolvedValueOnce(user)
|
||||
users[userId] = user
|
||||
}
|
||||
|
||||
const input = Object.keys(users).join(" , ")
|
||||
const result = await processInputBBReferences(input, FieldSubtype.USER)
|
||||
|
||||
expect(result).toEqual(Object.values(users))
|
||||
expect(mockedCacheGetUser).toBeCalledTimes(5)
|
||||
Object.keys(users).forEach(userId => {
|
||||
expect(mockedCacheGetUser).toBeCalledWith(userId)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue