ede566eed9
* prep for new resources algo * typescriptifying stuff * minor types fix * migrate to resources col * repo & creation updated, WIP processing/retrieval * WIP invite processing * finished finalization refactor * project invite management * transformed all invites services * fixed up projects & core serverinvites resolvers * test fixes * WIP workspace create GQL & test * basic invite creation test works * a buncha working tests * more tests * cancelation tests * minor invite use refactor * invite retrieval tasks * invite use() works as expected * filtering out broken invites * enabled invite retrieval by token irregardless of who is it for * minor adjustments * tests fix * test config improvements * test env adjustment * extra test case * making resource access limits harder to ignore * linter fixes * eventBus type cleanup * better generic names * refactored serverinvites resource migration * fix(server): better error message in project invite edge case
73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
import { LimitedUserRecord } from '@/modules/core/helpers/types'
|
|
import {
|
|
ProjectInviteResourceType,
|
|
ServerInviteResourceType
|
|
} from '@/modules/serverinvites/domain/constants'
|
|
import {
|
|
InviteResourceTarget,
|
|
ProjectInviteResourceTarget,
|
|
ServerInviteRecord,
|
|
ServerInviteResourceTarget
|
|
} from '@/modules/serverinvites/domain/types'
|
|
import { Nullable } from '@speckle/shared'
|
|
|
|
export type ResolvedTargetData = {
|
|
userId: string | null
|
|
userEmail: string | null
|
|
}
|
|
|
|
/**
|
|
* Resolve target information. The target can either be an email address or a user ID
|
|
* prefixed by an @ character
|
|
*/
|
|
export function resolveTarget(target: string): ResolvedTargetData {
|
|
if (target.startsWith('@')) {
|
|
return {
|
|
userEmail: null,
|
|
userId: target.substring(1)
|
|
}
|
|
} else {
|
|
return {
|
|
userEmail: target,
|
|
userId: null
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Build a valid target value from a user ID
|
|
*/
|
|
export function buildUserTarget(userId: string): string {
|
|
return `@${userId}`
|
|
}
|
|
|
|
/**
|
|
* Resolve a display name for the user being invited.
|
|
* User should be specified if invite targets a registered user.
|
|
*/
|
|
export function resolveInviteTargetTitle(
|
|
invite: ServerInviteRecord,
|
|
user: Nullable<LimitedUserRecord>
|
|
): string {
|
|
const { userId, userEmail } = resolveTarget(invite.target)
|
|
if (userId) {
|
|
// User should be provided otherwise we have to fallback to an ugly ID identifier
|
|
return user ? user.name : `#${userId}`
|
|
}
|
|
|
|
return userEmail!
|
|
}
|
|
|
|
export const isServerResourceTarget = (
|
|
target: InviteResourceTarget
|
|
): target is ServerInviteResourceTarget =>
|
|
target.resourceType === ServerInviteResourceType
|
|
|
|
export const isProjectResourceTarget = (
|
|
target: InviteResourceTarget
|
|
): target is ProjectInviteResourceTarget =>
|
|
target.resourceType === ProjectInviteResourceType
|
|
|
|
export const getPrimaryResourceTarget = (targets: InviteResourceTarget[]) =>
|
|
targets.find((t) => !!t.primary)
|