summaryrefslogtreecommitdiff
path: root/components/project/dataroom-members.ts
blob: 2512a5cd1ee3abf6ef66140645a267840cd76f99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use server";

import { sendEmail } from "@/lib/mail/sendEmail";
import { getServerSession } from "next-auth/next"
import { authOptions } from "@/app/api/auth/[...nextauth]/route"


interface SendDataRoomInvitationInput {
  email: string;
  name: string;
  dataRoomName: string;
  role: string;
  dataRoomUrl?: string;
}

export async function sendDataRoomInvitation(input: SendDataRoomInvitationInput) {
  try {
    const session = await getServerSession(authOptions)
    if (!session?.user?.id) {
      throw new Error("인증이 필요합니다.")
    }

    const dataRoomUrl = input.dataRoomUrl || `${process.env.NEXT_PUBLIC_APP_URL}/data-rooms`;
    
    const subject = `You've been invited to access "${input.dataRoomName}" Data Room`;

    await sendEmail({
      to: input.email,
      subject,
      template: "data-room-invitation",
      context: {
        name: input.name,
        dataRoomName: input.dataRoomName,
        role: input.role,
        inviterName: session.user.name || "Admin",
        dataRoomUrl,
        loginUrl: `${process.env.NEXT_PUBLIC_APP_URL}/login`,
      },
    });

    return { success: true, message: "Invitation email sent successfully" };
  } catch (error) {
    console.error("Failed to send data room invitation:", error);
    return { success: false, message: "Failed to send invitation email" };
  }
}

// 여러 멤버에게 동시에 이메일 전송
export async function sendBulkDataRoomInvitations(
  members: SendDataRoomInvitationInput[]
) {
  try {
    const session = await getServerSession(authOptions)
    if (!session?.user?.id) {
      throw new Error("인증이 필요합니다.")
    }

    const results = await Promise.allSettled(
      members.map((member) => sendDataRoomInvitation(member))
    );

    const successful = results.filter((r) => r.status === "fulfilled").length;
    const failed = results.filter((r) => r.status === "rejected").length;

    return {
      success: true,
      message: `Sent ${successful} invitations successfully. ${failed} failed.`,
      details: { successful, failed, total: members.length },
    };
  } catch (error) {
    console.error("Failed to send bulk invitations:", error);
    return { success: false, message: "Failed to send invitations" };
  }
}