feat(worker): handleStarReaction approval core with tests
Implement approval core logic for Task 5: when admin reacts to WhatsApp message with ⭐, verify admin status, update message to APPROVED, create Approval record, find active SyncRoutes from source group, and return ForwardJobData[] for each target group. All 7 tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import { NormalizedReaction, ForwardJobData } from '@tower/types';
|
||||
|
||||
export async function handleStarReaction(
|
||||
reaction: NormalizedReaction,
|
||||
adminJids: string[],
|
||||
prisma: any,
|
||||
): Promise<ForwardJobData[] | null> {
|
||||
if (reaction.emoji !== '⭐') return null;
|
||||
if (!adminJids.includes(reaction.reactorJid)) return null;
|
||||
|
||||
const message = await prisma.message.findUnique({
|
||||
where: {
|
||||
platform_platformMsgId: {
|
||||
platform: 'whatsapp',
|
||||
platformMsgId: reaction.targetMsgId,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
approval: true,
|
||||
sourceGroup: {
|
||||
include: {
|
||||
syncRoutesFrom: { where: { isActive: true }, include: { targetGroup: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!message) return null;
|
||||
if (message.status !== 'PENDING') return null;
|
||||
if (message.approval) return null;
|
||||
|
||||
await prisma.message.update({
|
||||
where: { id: message.id },
|
||||
data: { status: 'APPROVED' },
|
||||
});
|
||||
|
||||
await prisma.approval.create({
|
||||
data: {
|
||||
messageId: message.id,
|
||||
adminId: reaction.reactorJid,
|
||||
decision: 'APPROVED',
|
||||
},
|
||||
});
|
||||
|
||||
const jobs: ForwardJobData[] = message.sourceGroup.syncRoutesFrom.map((route: any) => ({
|
||||
messageId: message.id,
|
||||
content: message.content,
|
||||
sourceGroupName: message.sourceGroup.name,
|
||||
senderName: message.senderName ?? undefined,
|
||||
toGroupJid: route.targetGroup.platformId,
|
||||
fromAccountId: route.targetGroup.accountId ?? reaction.accountId,
|
||||
}));
|
||||
|
||||
return jobs;
|
||||
}
|
||||
Reference in New Issue
Block a user