diff --git a/functions/src/index.ts b/functions/src/index.ts index f9d6dd4..e56f6df 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -262,6 +262,11 @@ export const dispatchNotification = onCall( body: `${currentUserFamiliarName} právě zapsal/a první pivo.`, }, tokens: subscribedNotificationTokens, + webpush: { + fcmOptions: { + link: `https://check.beer/misto/${placeDoc.id}`, + }, + }, }); } case NotificationEvent.freshKeg: { @@ -289,6 +294,11 @@ export const dispatchNotification = onCall( body: `Právě bylo vytočeno první pivo ze sudu ${kegData.serial}, ${kegData.beer}.`, }, tokens: subscribedNotificationTokens, + webpush: { + fcmOptions: { + link: `https://check.beer/misto/${placeDoc.id}`, + }, + }, }); } } diff --git a/src/backend/Firebase.res b/src/backend/Firebase.res index b68045a..a7e4458 100644 --- a/src/backend/Firebase.res +++ b/src/backend/Firebase.res @@ -117,12 +117,12 @@ external arrayUnion: array<'a> => {..} = "arrayUnion" external incrementInt: int => {..} = "increment" @genType.import("firebase/firestore") @genType.as("DocumentReference") -type documentReference<'a> = {id: string} +type rec documentReference<'a> = {id: string, parent: option>, path: string} +@genType.import("firebase/firestore") @genType.as("CollectionReference") and collectionReference<'a> + @module("firebase/firestore") external doc: (firestore, ~path: string) => documentReference<'a> = "doc" -@genType.import("firebase/firestore") @genType.as("CollectionReference") -type collectionReference<'a> @module("firebase/firestore") external collection: (firestore, ~path: string) => collectionReference<'a> = "collection" diff --git a/src/backend/NotificationEvents.res b/src/backend/NotificationEvents.res index 215f9ab..cb02f15 100644 --- a/src/backend/NotificationEvents.res +++ b/src/backend/NotificationEvents.res @@ -24,16 +24,18 @@ type _freshKegMessage = {tag: notificationEvent, keg: string} @module("./NotificationEvents.ts") external freshKegMessage: _freshKegMessage = "FreshKegMessage" -let useDispatchFreeTable = () => { +let useDispatchFreeTableNotification = () => { let functions = Reactfire.useFunctions() let dispatchNotification = Firebase.Functions.httpsCallable(functions, "dispatchNotification") - (place: string) => dispatchNotification({tag: FreeTable, place}) + (placeRef: Firebase.documentReference) => + dispatchNotification({tag: FreeTable, place: placeRef.path}) } -let useDispatchFreshKeg = () => { +let useDispatchFreshKegNotification = () => { let functions = Reactfire.useFunctions() let dispatchNotification = Firebase.Functions.httpsCallable(functions, "dispatchNotification") - (keg: string) => dispatchNotification({tag: FreshKeg, keg}) + (kegRef: Firebase.documentReference) => + dispatchNotification({tag: FreshKeg, keg: kegRef.path}) } let useUpdateNotificationToken = () => { diff --git a/src/pages/Place/Place.res b/src/pages/Place/Place.res index 677f74c..c58d406 100644 --- a/src/pages/Place/Place.res +++ b/src/pages/Place/Place.res @@ -123,6 +123,8 @@ let make = (~placeId) => { let (currentUserRole, _) = place.accounts->Dict.get(currentUser.uid)->Option.getExn let isUserAuthorized = UserRoles.isAuthorized(currentUserRole, ...) let formatConsumption = BackendUtils.getFormatConsumption(place.consumptionSymbols) + let dispatchFreeTableNotification = NotificationEvents.useDispatchFreeTableNotification() + let dispatchFreshKegNotification = NotificationEvents.useDispatchFreshKegNotification()
@@ -174,7 +176,36 @@ let make = (~placeId) => { }} onDismiss={hideDialog} onSubmit={values => { - let kegRef = place.taps->Dict.getUnsafe(values.tap)->Null.getExn + let keg = tapsWithKegs->Dict.getUnsafe(values.tap) + let kegRef = Db.kegDoc(firestore, placeId, Db.getUid(keg)) + let firstBeerFromKeg = keg.consumptions->Dict.keysToArray->Array.length === 0 + if firstBeerFromKeg { + dispatchFreshKegNotification(kegRef) + ->Promise.then(_ => Promise.resolve()) + ->Promise.catch(error => { + let exn = Js.Exn.asJsExn(error)->Option.getExn + LogUtils.captureException(exn) + Promise.resolve() + }) + ->ignore + } else { + let freeTable = + recentConsumptionsByUser + ->Map.values + ->Array.fromIterator + ->Array.every(consumptions => consumptions->Array.length === 0) + if freeTable { + dispatchFreeTableNotification(Db.placeDocument(firestore, placeId)) + ->Promise.then(_ => Promise.resolve()) + ->Promise.catch(error => { + let exn = Js.Exn.asJsExn(error)->Option.getExn + LogUtils.captureException(exn) + Promise.resolve() + }) + ->ignore + } + } + Db.Keg.addConsumption( firestore, ~consumption={