Skip to content

Commit

Permalink
feat: dispatch notification from client code
Browse files Browse the repository at this point in the history
  • Loading branch information
czabaj committed Oct 31, 2024
1 parent 2f9112e commit 4ede348
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
10 changes: 10 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ export const dispatchNotification = onCall<FreeTableMessage | FreshKegMessage>(
body: `${currentUserFamiliarName} právě zapsal/a první pivo.`,
},
tokens: subscribedNotificationTokens,
webpush: {
fcmOptions: {
link: `https://check.beer/misto/${placeDoc.id}`,
},
},
});
}
case NotificationEvent.freshKeg: {
Expand Down Expand Up @@ -289,6 +294,11 @@ export const dispatchNotification = onCall<FreeTableMessage | FreshKegMessage>(
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}`,
},
},
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/backend/Firebase.res
Original file line number Diff line number Diff line change
Expand Up @@ -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<collectionReference<'a>>, 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"

Expand Down
10 changes: 6 additions & 4 deletions src/backend/NotificationEvents.res
Original file line number Diff line number Diff line change
Expand Up @@ -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<FirestoreModels.place>) =>
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<FirestoreModels.keg>) =>
dispatchNotification({tag: FreshKeg, keg: kegRef.path})
}

let useUpdateNotificationToken = () => {
Expand Down
33 changes: 32 additions & 1 deletion src/pages/Place/Place.res
Original file line number Diff line number Diff line change
Expand Up @@ -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()

<FormattedCurrency.Provider value={place.currency}>
<div className={`${Styles.page.narrow} ${classes.root}`}>
Expand Down Expand Up @@ -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={
Expand Down

0 comments on commit 4ede348

Please sign in to comment.