Initial commit: page-manager Next.js app with Coolify deploy support

Replace Vercel-specific infra (Cron, Blob) with self-hosted equivalents
for deployment to Coolify: in-container cron scheduler
(scripts/cron-runner.mjs) and disk-based upload storage served via
/api/uploads/[...path]. Add Dockerfile, .dockerignore, and /api/health
for container health checks.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
chaloemphong
2026-11-22 11:52:17 +07:00
commit 9d32552457
155 changed files with 22174 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
import type { ReplyRule, ReplyRuleKind } from "@prisma/client";
import { prisma } from "@/lib/db";
function normalize(s: string): string {
return s.normalize("NFC").toLowerCase().replace(/\s+/g, " ").trim();
}
/** Highest-priority active rule whose any keyword appears in the text. */
export async function matchRule(pageId: string, kind: ReplyRuleKind, text: string): Promise<ReplyRule | null> {
const rules = await prisma.replyRule.findMany({
where: { pageId, kind, isActive: true },
orderBy: [{ priority: "desc" }, { createdAt: "asc" }],
});
const t = normalize(text);
return rules.find((r) => r.keywords.some((k) => k.trim() && t.includes(normalize(k)))) ?? null;
}
/**
* Claims the (rule, recipient) dedupe slot before sending. Returns false if
* this person already got this rule. Call releaseReplySlot if the send fails
* so a webhook redelivery can retry.
*/
export async function claimReplySlot(ruleId: string, recipientId: string, sourceId: string): Promise<boolean> {
try {
await prisma.replyLog.create({ data: { ruleId, recipientId, sourceId } });
return true;
} catch {
return false;
}
}
export async function releaseReplySlot(ruleId: string, recipientId: string): Promise<void> {
await prisma.replyLog.delete({ where: { ruleId_recipientId: { ruleId, recipientId } } }).catch(() => {});
}
/** "Typing speed" pause so a bot reply doesn't land instantly. */
export function typingDelayMs(text: string): number {
return Math.min(900 + text.length * 35, 3500);
}