ba-cafe-timer

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

route.ts (1604B)


      1 export const runtime = "edge";
      2 
      3 import { NextResponse } from "next/server";
      4 import { notifMessages } from "@/lib/presets";
      5 import { TapKind } from "@/lib/handleTap";
      6 
      7 export async function POST(request: Request) {
      8   try {
      9     const { tapKind, time, userId } = await request.json();
     10     console.log(tapKind, time, userId);
     11     if (
     12       !tapKind ||
     13       !time ||
     14       !userId ||
     15       !process.env.ONESIGNAL_APIKEY ||
     16       !process.env.NEXT_PUBLIC_ONESIGNAL_APP_ID
     17     ) {
     18       return NextResponse.json(
     19         { message: "Missing required fields" },
     20         { status: 400 },
     21       );
     22     }
     23     const tapKindParsed: TapKind = tapKind;
     24     const response = await fetch("https://api.onesignal.com/notifications", {
     25       method: "POST",
     26       headers: {
     27         "Content-Type": "application/json",
     28         Authorization: `Basic ${process.env.ONESIGNAL_APIKEY}`,
     29       },
     30       body: JSON.stringify({
     31         app_id: process.env.NEXT_PUBLIC_ONESIGNAL_APP_ID,
     32         include_external_user_ids: [userId],
     33         contents: {
     34           en: notifMessages[tapKindParsed],
     35         },
     36         send_after: time,
     37       }),
     38     });
     39     console.log(await response.text());
     40     if (response.ok) {
     41       return NextResponse.json({
     42         message: "Notification scheduled successfully",
     43       });
     44     } else {
     45       return NextResponse.json(
     46         { message: "Failed to schedule notification" },
     47         { status: response.status },
     48       );
     49     }
     50   } catch (error) {
     51     console.log(error);
     52     return NextResponse.json(
     53       { message: "Failed to schedule notification" },
     54       { status: 500 },
     55     );
     56   }
     57 }