feat: instant meeting browser notifcations (#16480)
* feat: instant meeting browser notifcations * chore: support multiple notifications * chore: save progress * fix: add test notification and move file * fix: type err * chore: feedback improvements * chore: undo DENY
This commit is contained in:
1 parent
5bc0e1265d
commit
860d173f0a
9 files changed
+220
-37
No files matched your search
+38
-1
@@ -1,6 +1,11 @@
|
||||
import { subscriptionSchema } from "@calcom/features/instant-meeting/schema";
|
||||
import { sendNotification } from "@calcom/features/notifications/sendNotification";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import prisma from "@calcom/prisma";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import type { TAddNotificationsSubscriptionInputSchema } from "./addNotificationsSubscription.schema";
|
||||
|
||||
type AddSecondaryEmailOptions = {
|
||||
@@ -10,20 +15,52 @@ type AddSecondaryEmailOptions = {
|
||||
input: TAddNotificationsSubscriptionInputSchema;
|
||||
};
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["[addNotificationsSubscriptionHandler]"] });
|
||||
|
||||
export const addNotificationsSubscriptionHandler = async ({ ctx, input }: AddSecondaryEmailOptions) => {
|
||||
const { user } = ctx;
|
||||
const { subscription } = input;
|
||||
|
||||
const parsedSubscription = subscriptionSchema.safeParse(JSON.parse(subscription));
|
||||
|
||||
if (!parsedSubscription.success) {
|
||||
log.error("Invalid subscription", parsedSubscription.error, JSON.stringify(subscription));
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Invalid subscription",
|
||||
});
|
||||
}
|
||||
|
||||
const existingSubscription = await prisma.notificationsSubscriptions.findFirst({
|
||||
where: { userId: user.id, subscription },
|
||||
where: { userId: user.id },
|
||||
});
|
||||
|
||||
if (!existingSubscription) {
|
||||
await prisma.notificationsSubscriptions.create({
|
||||
data: { userId: user.id, subscription },
|
||||
});
|
||||
} else {
|
||||
await prisma.notificationsSubscriptions.update({
|
||||
where: { id: existingSubscription.id },
|
||||
data: { userId: user.id, subscription },
|
||||
});
|
||||
}
|
||||
|
||||
// send test notification
|
||||
sendNotification({
|
||||
subscription: {
|
||||
endpoint: parsedSubscription.data.endpoint,
|
||||
keys: {
|
||||
auth: parsedSubscription.data.keys.auth,
|
||||
p256dh: parsedSubscription.data.keys.p256dh,
|
||||
},
|
||||
},
|
||||
title: "Test Notification",
|
||||
body: "Push Notifications activated successfully",
|
||||
url: "https://app.cal.com/",
|
||||
requireInteraction: false,
|
||||
});
|
||||
|
||||
return {
|
||||
message: "Subscription added successfully",
|
||||
};
|
||||
|
||||
+1
-3
@@ -10,15 +10,13 @@ type AddSecondaryEmailOptions = {
|
||||
input: TRemoveNotificationsSubscriptionInputSchema;
|
||||
};
|
||||
|
||||
export const removeNotificationsSubscriptionHandler = async ({ ctx, input }: AddSecondaryEmailOptions) => {
|
||||
export const removeNotificationsSubscriptionHandler = async ({ ctx }: AddSecondaryEmailOptions) => {
|
||||
const { user } = ctx;
|
||||
const { subscription } = input;
|
||||
|
||||
// We just use findFirst because there will only be single unique subscription for a user
|
||||
const subscriptionToDelete = await prisma.notificationsSubscriptions.findFirst({
|
||||
where: {
|
||||
userId: user.id,
|
||||
subscription,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user