budibase/packages/frontend-core/src/fetch/UserFetch.ts

80 lines
1.7 KiB
TypeScript
Raw Normal View History

import { get } from "svelte/store"
2025-01-08 14:17:10 +01:00
import DataFetch, { DataFetchParams } from "./DataFetch"
import { TableNames } from "../constants"
import { utils } from "@budibase/shared-core"
2025-01-07 11:17:42 +01:00
import {
BasicOperator,
SearchFilters,
SearchUsersRequest,
} from "@budibase/types"
2025-01-07 11:17:42 +01:00
interface UserFetchQuery {
appId: string
paginated: boolean
}
2025-01-08 14:17:10 +01:00
interface UserDatasource {
tableId: string
}
2025-01-07 11:17:42 +01:00
export default class UserFetch extends DataFetch<
2025-01-08 14:17:10 +01:00
UserDatasource,
2025-01-07 11:17:42 +01:00
{},
UserFetchQuery
> {
2025-01-08 14:17:10 +01:00
constructor(opts: DataFetchParams<UserDatasource, UserFetchQuery>) {
super({
...opts,
datasource: {
tableId: TableNames.USERS,
},
})
}
2025-01-08 12:49:17 +01:00
async determineFeatureFlags() {
return {
supportsSearch: true,
supportsSort: false,
supportsPagination: true,
}
}
async getDefinition() {
2025-01-08 13:05:27 +01:00
return { schema: {} }
}
async getData() {
const { limit, paginate } = this.options
const { cursor, query } = get(this.store)
// Convert old format to new one - we now allow use of the lucene format
2025-01-08 13:05:27 +01:00
const { appId, paginated, ...rest } = query
2025-01-07 11:17:42 +01:00
const finalQuery: SearchFilters = utils.isSupportedUserSearch(rest)
? rest
: { [BasicOperator.EMPTY]: { email: true } }
try {
2025-01-07 11:17:42 +01:00
const opts: SearchUsersRequest = {
2025-01-02 15:55:50 +01:00
bookmark: cursor ?? undefined,
query: finalQuery ?? undefined,
appId: appId,
paginate: paginated || paginate,
limit,
}
const res = await this.API.searchUsers(opts)
return {
rows: res?.data || [],
hasNextPage: res?.hasNextPage || false,
cursor: res?.nextPage || null,
}
} catch (error) {
return {
rows: [],
hasNextPage: false,
error,
}
}
}
}