Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ export const authenticationService = (log: FastifyBaseLogger) => ({
imageUrl: params.imageUrl,
})
}
const existingUser = await userService(log).getOneByIdentityAndPlatform({
identityId: userIdentity.id,
platformId,
})
if (isNil(existingUser)) {
await authenticationUtils(log).assertUserIsInvitedToPlatformOrProject({
email: params.email,
platformId,
})
}
const user = await userService(log).getOrCreateWithProject({
identity: userIdentity,
platformId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ const invitationController: FastifyPluginAsyncZod = async (app) => {
await platformMustBeOwnedByCurrentUser.call(app, request, reply)
break
}
const status = await shouldAutoAcceptInvitation(request.principal, request.body, request.log) ? InvitationStatus.ACCEPTED : InvitationStatus.PENDING
const projectRole = await getProjectRoleAndAssertIfFound(request.principal.platform.id, request.body)
const platformId = request.principal.platform.id
const status = await shouldAutoAcceptInvitation(request.principal, request.body, platformId, request.log) ? InvitationStatus.ACCEPTED : InvitationStatus.PENDING
const projectRole = await getProjectRoleAndAssertIfFound(platformId, request.body)

const invitation = await userInvitationsService(request.log).create({
email,
Expand Down Expand Up @@ -144,21 +144,24 @@ async function getProjectIdAndAssertPermission<R extends Principal>(
return requestQuery.projectId ?? null
}

async function shouldAutoAcceptInvitation(principal: Principal, request: SendUserInvitationRequest, log: FastifyBaseLogger): Promise<boolean> {
async function shouldAutoAcceptInvitation(principal: Principal, request: SendUserInvitationRequest, platformId: string, log: FastifyBaseLogger): Promise<boolean> {
if (principal.type === PrincipalType.SERVICE) {
return true
}

if (request.type === InvitationType.PLATFORM) {
return false
}

const identity = await userIdentityService(log).getIdentityByEmail(request.email)
if (isNil(identity)) {
return false
}

const user = await userService(log).getOneByIdentityIdOnly({ identityId: identity.id })

const user = await userService(log).getOneByIdentityAndPlatform({
identityId: identity.id,
platformId,
})
return !isNil(user)
}

Expand Down
7 changes: 0 additions & 7 deletions packages/server/api/src/app/user/user-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ export const userService = (log: FastifyBaseLogger) => ({
const usersWithMetaInformation = await Promise.all(data.map(this.getMetaInformation))
return paginationHelper.createPage<UserWithMetaInformation>(usersWithMetaInformation, cursor)
},
async getOneByIdentityIdOnly({ identityId }: GetOneByIdentityIdOnlyParams): Promise<User | null> {
return userRepo().findOneBy({ identityId })
},
async getByIdentityId({ identityId }: GetByIdentityId): Promise<UserSchema[]> {
return userRepo().find({ where: { identityId } })
},
Expand Down Expand Up @@ -309,10 +306,6 @@ type ListParams = {
limit?: number
}

type GetOneByIdentityIdOnlyParams = {
identityId: string
}

type GetByIdentityId = {
identityId: string
}
Expand Down
Loading