aboutsummaryrefslogtreecommitdiffstats
path: root/lib/notifications.ts
blob: 023fdeff2e33262295f3ed4ad54a84a8e872cdc6 (plain) (blame)
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
import { emit } from "$lib/sockets";
import { err, warn } from "$lib/logging";
import { db } from "$lib/db";
import mqtt from "$lib/mqtt";
import config from "$config";
import { mail, templates } from "$lib/mail";
import { f, fmt, link, fiat, t } from "$lib/utils";
import webpush from "web-push";

if (config.vapid) {
  webpush.setVapidDetails(
    `mailto:${config.support}`,
    config.vapid.pk,
    config.vapid.sk,
  );
}

export const notify = async (p, user, withdrawal) => {
  emit(user.id, "payment", p);
  let { username } = user;
  let { paymentReceived } = t(user);

  try {
    if (user.verified && user.notify) {
      mail(user, paymentReceived, templates.paymentReceived, {
        ...t(user),
        username,
        payment: {
          amount: fmt(p.amount),
          link: link(p.id),
          tip: p.tip ? fmt(p.tip) : undefined,
          fiat: f(fiat(p.amount, p.rate), p.currency),
          fiatTip: p.tip ? f(fiat(p.tip, p.rate), p.currency) : undefined,
          memo: p.memo,
          items:
            p.items &&
            p.items.map((i) => {
              return {
                quantity: i.quantity,
                name: i.name,
                total: i.quantity * i.price,
                totalFiat: f(i.quantity * i.price, p.currency),
              };
            }),
        },
        withdrawal,
      });
    }
  } catch (e) {
    err("problem emailing", e.message);
  }

  let subscriptions = await db.sMembers(`${user.id}:subscriptions`);

  let payload = {
    title: paymentReceived,
    body: `${fmt(p.amount)} ${f(fiat(p.amount, p.rate), p.currency)}`,
    url: `/payment/${p.id}`,
  };

  for (let s of subscriptions) {
    webpush
      .sendNotification(JSON.parse(s), JSON.stringify(payload))
      .catch((e) => {
        warn("sub failed", e.message);
        db.sRem(`${user.id}:subscriptions`, s);
      });
  }

  if (config.mqtt) {
    if (!mqtt.connected) await mqtt.reconnect();
    mqtt.publish(
      username,
      `pay:${p.amount}:${p.tip}:${p.rate}:${p.created}:${p.id}:${p.memo}:${p.items}`,
    );
  }
};