feat: initial commit
This commit is contained in:
10
src/utilities/extractID.ts
Normal file
10
src/utilities/extractID.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Config } from '@/payload-types'
|
||||
import type { CollectionSlug } from 'payload'
|
||||
|
||||
export const extractID = <T extends Config['collections'][CollectionSlug]>(
|
||||
objectOrID: T | T['id'],
|
||||
): T['id'] => {
|
||||
if (objectOrID && typeof objectOrID === 'object') return objectOrID.id
|
||||
|
||||
return objectOrID
|
||||
}
|
||||
9
src/utilities/getCollectionIDType.ts
Normal file
9
src/utilities/getCollectionIDType.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { CollectionSlug, Payload } from 'payload'
|
||||
|
||||
type Args = {
|
||||
collectionSlug: CollectionSlug
|
||||
payload: Payload
|
||||
}
|
||||
export const getCollectionIDType = ({ collectionSlug, payload }: Args): 'number' | 'text' => {
|
||||
return payload.collections[collectionSlug]?.customIDType ?? payload.db.defaultIDType
|
||||
}
|
||||
31
src/utilities/getUserTenantIDs.ts
Normal file
31
src/utilities/getUserTenantIDs.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import type { Tenant, User } from '../payload-types'
|
||||
import { extractID } from './extractID'
|
||||
|
||||
/**
|
||||
* Returns array of all tenant IDs assigned to a user
|
||||
*
|
||||
* @param user - User object with tenants field
|
||||
* @param role - Optional role to filter by
|
||||
*/
|
||||
export const getUserTenantIDs = (
|
||||
user: null | User,
|
||||
role?: NonNullable<User['tenants']>[number]['roles'][number],
|
||||
): Tenant['id'][] => {
|
||||
if (!user) {
|
||||
return []
|
||||
}
|
||||
|
||||
return (
|
||||
user?.tenants?.reduce<Tenant['id'][]>((acc, { roles, tenant }) => {
|
||||
if (role && !roles.includes(role)) {
|
||||
return acc
|
||||
}
|
||||
|
||||
if (tenant) {
|
||||
acc.push(extractID(tenant))
|
||||
}
|
||||
|
||||
return acc
|
||||
}, []) || []
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user