-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathroute.ts
More file actions
158 lines (143 loc) · 4.61 KB
/
route.ts
File metadata and controls
158 lines (143 loc) · 4.61 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { NextRequest, NextResponse } from 'next/server';
import { WhatsAppService } from '@/lib/whatsapp';
import { prisma } from '@/lib/prisma';
interface BroadcastRequest {
userIds: string[];
message: string;
delay?: number; // Delay in ms between each message (default 2000ms)
}
export async function POST(request: NextRequest) {
try {
const body: BroadcastRequest = await request.json();
const { userIds, message, delay = 2000 } = body;
if (!userIds || userIds.length === 0) {
return NextResponse.json(
{ success: false, error: 'No users selected' },
{ status: 400 }
);
}
if (!message) {
return NextResponse.json(
{ success: false, error: 'Message is required' },
{ status: 400 }
);
}
// Fetch users
const users = await prisma.pppoeUser.findMany({
where: {
id: { in: userIds },
},
select: {
id: true,
name: true,
username: true,
phone: true,
profile: {
select: {
name: true,
},
},
},
});
if (users.length === 0) {
return NextResponse.json(
{ success: false, error: 'No valid users found' },
{ status: 400 }
);
}
// Get company info for variables
const company = await prisma.company.findFirst();
// Prepare messages for batch sending with rate limiting
const messagesToSend = users
.filter(user => user.phone) // Only users with phone numbers
.map(user => ({
phone: user.phone!,
message: message
.replace(/\{\{customerName\}\}/g, user.name)
.replace(/\{\{username\}\}/g, user.username)
.replace(/\{\{profileName\}\}/g, user.profile?.name || '-')
.replace(/\{\{companyName\}\}/g, company?.name || '')
.replace(/\{\{companyPhone\}\}/g, company?.phone || ''),
data: {
userId: user.id,
name: user.name,
username: user.username,
}
}));
// Track users without phone numbers
const usersWithoutPhone = users.filter(user => !user.phone);
console.log(`[Broadcast] Sending to ${messagesToSend.length} users (${usersWithoutPhone.length} skipped - no phone)`);
// Send messages with rate limiting (5 msg per 10 seconds)
const { sendWithRateLimit, estimateSendTime, formatEstimatedTime } = await import('@/lib/utils/rateLimiter');
const estimatedTime = estimateSendTime(messagesToSend.length);
console.log(`[Broadcast] Estimated time: ${formatEstimatedTime(estimatedTime)}`);
const result = await sendWithRateLimit(
messagesToSend,
async (msg) => {
// Send message using WhatsApp failover service
const sendResult = await WhatsAppService.sendMessage({
phone: msg.phone,
message: msg.message,
});
return sendResult;
},
{}, // Use default config: 5 msg/10sec
(progress) => {
console.log(`[Broadcast] Progress: ${progress.current}/${progress.total} (Batch ${progress.batch}/${progress.totalBatches})`)
}
);
// Build detailed results
const detailedResults = [
// Successful sends
...result.results
.filter(r => r.success)
.map(r => {
const userData = messagesToSend.find(m => m.phone === r.phone)?.data;
return {
userId: userData?.userId,
name: userData?.name,
username: userData?.username,
phone: r.phone,
success: true,
};
}),
// Failed sends
...result.results
.filter(r => !r.success)
.map(r => {
const userData = messagesToSend.find(m => m.phone === r.phone)?.data;
return {
userId: userData?.userId,
name: userData?.name,
username: userData?.username,
phone: r.phone,
success: false,
error: r.error,
};
}),
// Users without phone
...usersWithoutPhone.map(user => ({
userId: user.id,
name: user.name,
username: user.username,
phone: null,
success: false,
error: 'No phone number',
})),
];
return NextResponse.json({
success: true,
total: users.length,
successCount: result.sent,
failCount: result.failed + usersWithoutPhone.length,
results: detailedResults,
estimatedTime: formatEstimatedTime(estimatedTime),
});
} catch (error: any) {
console.error('Broadcast error:', error);
return NextResponse.json(
{ success: false, error: error.message },
{ status: 500 }
);
}
}