Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -41,8 +41,7 @@ export class CreateAndSendInviteController {
const response = await this.createInviteTokenUseCase.execute(payload)

await this.sendEmailUseCase.execute({
email: payload.email,
organization_id: response.inviteToken.organization_id
invite_token: response.inviteToken.token
})

return reply.status(201).send(response)
Expand Down
7 changes: 7 additions & 0 deletions src/core/domain/exceptions/invites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ControllerError } from "@/adapters/inbound/http/middlewares/general-error-handler";

export class InviteNotFound extends ControllerError {
constructor(public status = 404) {
super('Invite not found.')
}
}
19 changes: 10 additions & 9 deletions src/core/use-cases/email/send-invite/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { AdminRepository } from '@/adapters/outbound/prisma/repositories/admin-repositories'
import { SendInviteUseCasePayload, SendInviteUseCaseReturn } from './types'
import { ResendRepository } from '@/shared/infra/email/resend'
import { InviteNotFound } from '@/core/domain/exceptions/invites'

export class SendInviteUseCase {
constructor(
private readonly adminRepository: AdminRepository,
private readonly resendRepository: ResendRepository
) {}
) { }

async execute({
email,
organization_id
invite_token,
}: SendInviteUseCasePayload): Promise<SendInviteUseCaseReturn> {
const expires_at = new Date(Date.now() + 1000 * 60 * 60 * 72)

const invite = await this.adminRepository.createAndSendInvite({
email,
organization_id
})
const invite = await this.adminRepository.getInviteByToken(invite_token)

if (!invite) {
throw new InviteNotFound()
}

await this.resendRepository.sendInviteEmail({
to: email,
to: invite.email,
token: invite.token,
organization_id,
organization_id: invite.organization_id,
expires_at
})

Expand Down
3 changes: 1 addition & 2 deletions src/core/use-cases/email/send-invite/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export interface SendInviteUseCasePayload {
email: string
organization_id: string
invite_token: string
}

export interface SendInviteUseCaseReturn {
Expand Down
Loading