Skip to content

Commit d211d91

Browse files
committed
feat: ✨ notify friends on new zap uploaded
1 parent c877a9f commit d211d91

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/routes/v1/zaps/ZapController.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import { v7 } from "uuid";
2323
import { fromUUID, typeid } from "typeid-js";
2424
import { Region } from "../../../enums/Region";
25+
import { sendNotifications } from "../../../libs/push-gateway";
2526

2627
interface ZapUploadInfo {
2728
zapId: string;
@@ -121,6 +122,54 @@ export class ZapController extends Controller {
121122
uploaded: true
122123
}
123124
});
125+
126+
const friends = await prisma.user.findMany({
127+
where: {
128+
deviceToken: {
129+
not: null
130+
},
131+
OR: [
132+
{
133+
friendsWith: {
134+
some: {
135+
receiverId: request.user.user.id
136+
}
137+
},
138+
friendsOf: {
139+
some: {
140+
senderId: request.user.user.id
141+
}
142+
}
143+
}
144+
]
145+
}
146+
});
147+
148+
const notifications = friends.map(
149+
(friend) =>
150+
({
151+
id: typeid(Prefix.NOTIFICATION).toString(),
152+
userId: friend.id,
153+
type: "NEW_DAILY_ZAP",
154+
title: "New Zap",
155+
content: `${request.user.user.firstName} has uploaded a new Zap!`,
156+
targetId: id,
157+
deviceToken: friend.deviceToken!
158+
} as const)
159+
);
160+
161+
await prisma.notification.createMany({
162+
data: notifications
163+
});
164+
165+
await sendNotifications({
166+
body: {
167+
notifications: notifications.map((notification) => ({
168+
notificationId: notification.id,
169+
deviceToken: notification.deviceToken
170+
}))
171+
}
172+
});
124173
}
125174

126175
@Post("{id}/repost")

0 commit comments

Comments
 (0)