link-stack/packages/bridge-ui/lib/whatsapp.ts

34 lines
1,019 B
TypeScript
Raw Permalink Normal View History

2024-05-16 18:22:10 +02:00
import { NextResponse } from "next/server";
2024-06-05 08:52:41 +02:00
import { db } from "@link-stack/bridge-common";
2024-05-16 18:22:10 +02:00
import { revalidatePath } from "next/cache";
import { Service, ServiceParams } from "./service";
2024-04-30 11:39:16 +02:00
2024-05-16 18:22:10 +02:00
export class Whatsapp extends Service {
async getBot({ params: { token } }: ServiceParams) {
const row = await db
.selectFrom("WhatsappBot")
.selectAll()
.where("token", "=", token as string)
.executeTakeFirstOrThrow();
const id = row.id;
const url = `${process.env.BRIDGE_WHATSAPP_URL}/api/bots/${id}`;
const result = await fetch(url, { cache: "no-store" });
const json = await result.json();
await db
.updateTable("WhatsappBot")
.set({ verified: json.verified })
.where("id", "=", id)
.execute();
if (!json.verified) {
const url = `${process.env.BRIDGE_WHATSAPP_URL}/api/bots/${id}/register`;
2024-05-17 09:20:00 +02:00
const result = await fetch(url, { method: "POST" });
2024-05-16 18:22:10 +02:00
}
2024-05-17 09:20:00 +02:00
revalidatePath(`/whatsapp/${id}`);
2024-05-16 18:22:10 +02:00
return NextResponse.json(json);
}
}