81 lines
2.3 KiB
TypeScript
81 lines
2.3 KiB
TypeScript
import { NormalizedReaction, ForwardJobData, IndexJobData } from '@tower/types';
|
|
|
|
export interface ApprovalResult {
|
|
forwardJobs: ForwardJobData[];
|
|
indexDoc: IndexJobData;
|
|
}
|
|
|
|
export async function handleStarReaction(
|
|
reaction: NormalizedReaction,
|
|
adminJids: string[],
|
|
prisma: any,
|
|
): Promise<ApprovalResult | null> {
|
|
if (reaction.emoji !== '⭐') return null;
|
|
if (!adminJids.includes(reaction.reactorJid)) return null;
|
|
|
|
const message = await prisma.message.findUnique({
|
|
where: {
|
|
platform_platformMsgId: {
|
|
// TODO: derive platform from NormalizedReaction when multi-platform support is added
|
|
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;
|
|
|
|
let approved = false;
|
|
await prisma.$transaction(async (tx: any) => {
|
|
const updated = await tx.message.updateMany({
|
|
where: { id: message.id, status: 'PENDING' },
|
|
data: { status: 'APPROVED' },
|
|
});
|
|
if (updated.count === 0) return;
|
|
approved = true;
|
|
await tx.approval.create({
|
|
data: {
|
|
messageId: message.id,
|
|
adminId: reaction.reactorJid,
|
|
decision: 'APPROVED',
|
|
},
|
|
});
|
|
});
|
|
|
|
if (!approved) return null;
|
|
|
|
const forwardJobs: ForwardJobData[] = message.sourceGroup.syncRoutesFrom
|
|
.filter((route: any) => route.targetGroup != null)
|
|
.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,
|
|
}));
|
|
|
|
const indexDoc: IndexJobData = {
|
|
messageId: message.id,
|
|
content: message.content,
|
|
senderName: message.senderName ?? null,
|
|
sourceGroupId: message.sourceGroupId,
|
|
sourceGroupName: message.sourceGroup.name,
|
|
tags: message.tags,
|
|
platform: message.platform,
|
|
approvedAt: new Date().toISOString(),
|
|
};
|
|
|
|
return { forwardJobs, indexDoc };
|
|
}
|