All files / app/service notifications.ts

28.12% Statements 9/32
100% Branches 0/0
0% Functions 0/6
28.12% Lines 9/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 6968x           68x   68x                 68x             68x             68x                               68x         68x                         68x      
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import {
  GetNotificationSettingsReq,
  RegisterPushNotificationSubscriptionReq,
  SetNotificationSettingsReq,
  SingleNotificationPreference,
} from "proto/notifications_pb";
 
import client from "./client";
 
export interface NotificationPreferenceData {
  topic: string;
  action: string;
  deliveryMethod: "push" | "email";
  enabled: boolean;
}
 
export async function getNotificationSettings() {
  const res = await client.notifications.getNotificationSettings(
    new GetNotificationSettingsReq()
  );
  return res.toObject();
}
 
export async function setNotificationSettings(enableDoNotEmail: boolean) {
  const req = new SetNotificationSettingsReq();
  req.setEnableDoNotEmail(enableDoNotEmail);
  const res = await client.notifications.setNotificationSettings(req);
  return res.toObject();
}
 
export async function setNotificationSettingsPreference(
  preferenceData: NotificationPreferenceData
) {
  const req = new SetNotificationSettingsReq();
  const preference = new SingleNotificationPreference();
  preference.setTopic(preferenceData.topic);
  preference.setAction(preferenceData.action);
  preference.setDeliveryMethod(preferenceData.deliveryMethod);
  preference.setEnabled(preferenceData.enabled);
  req.setPreferencesList([preference]);
 
  const res = await client.notifications.setNotificationSettings(req);
 
  return res.toObject();
}
 
export async function getVapidPublicKey() {
  const res = await client.notifications.getVapidPublicKey(new Empty());
  return res.toObject();
}
 
export async function registerPushNotificationSubscription(
  subscription: PushSubscription
) {
  const req = new RegisterPushNotificationSubscriptionReq();
  req.setFullSubscriptionJson(JSON.stringify(subscription));
  req.setUserAgent(navigator.userAgent);
  const res = await client.notifications.registerPushNotificationSubscription(
    req
  );
 
  return res.toObject();
}
 
export async function sendTestPushNotification() {
  await client.notifications.sendTestPushNotification(new Empty());
}