ba-cafe-timer

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

handleTap.ts (1575B)


      1 import { arrayUnion, doc, setDoc } from "firebase/firestore";
      2 import { auth, db } from "./firebase";
      3 
      4 export type TapKind = "normal" | "ticket1" | "ticket2";
      5 
      6 export async function handleTap(TapKind: TapKind) {
      7   saveTapHistory(TapKind);
      8   await scheduleNotification(TapKind);
      9 }
     10 
     11 export async function saveTapHistory(tapKind: TapKind) {
     12   const user = auth.currentUser;
     13   if (!user) throw new Error("User not authenticated");
     14   const userDocRef = doc(db, "users", user.uid);
     15 
     16   try {
     17     await setDoc(
     18       userDocRef,
     19       {
     20         history: {
     21           [tapKind]: arrayUnion(new Date(Date.now()).toISOString()),
     22         },
     23       },
     24       { merge: true },
     25     );
     26   } catch (error) {
     27     console.error("Error updating tap array:", error);
     28     throw error;
     29   }
     30 }
     31 
     32 export async function notification(tapKind: TapKind, time: string) {
     33   const response = await fetch("api/notif", {
     34     method: "POST",
     35     headers: {
     36       "Content-Type": "application/json",
     37     },
     38     body: JSON.stringify({
     39       tapKind: tapKind,
     40       time: time,
     41       userId: auth.currentUser?.uid,
     42     }),
     43   });
     44   console.log(await response.json());
     45 }
     46 
     47 import { settedTime } from "@/lib/presets";
     48 
     49 export async function scheduleNotification(tapKind: TapKind) {
     50   const response = await fetch("api/notif", {
     51     method: "POST",
     52     headers: {
     53       "Content-Type": "application/json",
     54     },
     55     body: JSON.stringify({
     56       tapKind: tapKind,
     57       time: new Date(Date.now() + settedTime[tapKind]).toISOString(),
     58       userId: auth.currentUser?.uid,
     59     }),
     60   });
     61   console.log(await response.json());
     62 }