All files / app/service notifications.ts

23.07% Statements 12/52
0% Branches 0/5
0% Functions 0/9
23.07% Lines 12/52

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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 12080x                   80x   80x                 78x             78x             78x                               78x         78x                       78x       78x                             78x                     78x                                          
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import {
  GetNotificationSettingsReq,
  ListNotificationsReq,
  MarkAllNotificationsSeenReq,
  MarkNotificationSeenReq,
  Notification,
  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());
}
 
export async function listNotifications({
  onlyUnread = false,
}: {
  onlyUnread: boolean;
}) {
  const req = new ListNotificationsReq();
 
  Iif (onlyUnread) {
    req.setOnlyUnread(true);
  }
 
  const res = await client.notifications.listNotifications(req);
  return res.toObject();
}
 
export async function markAllNotificationsSeen(
  lastestNotificationId: Notification.AsObject["notificationId"],
) {
  const req = new MarkAllNotificationsSeenReq();
 
  req.setLatestNotificationId(lastestNotificationId);
 
  const res = await client.notifications.markAllNotificationsSeen(req);
  return res.toObject();
}
 
export async function markNotificationSeen(
  notificationId: Notification.AsObject["notificationId"],
  setSeen: boolean = true,
) {
  const req = new MarkNotificationSeenReq();
 
  Iif (!notificationId) {
    throw new Error(
      "Notification ID is required to mark notification as seen.",
    );
  }
 
  Iif (setSeen) {
    req.setSetSeen(setSeen);
  }
 
  req.setNotificationId(notificationId);
 
  const res = await client.notifications.markNotificationSeen(req);
  return res.toObject();
}