( notification: CreateNotificationInput, )
| 58 | }; |
| 59 | |
| 60 | export async function createNotification( |
| 61 | notification: CreateNotificationInput, |
| 62 | ) { |
| 63 | try { |
| 64 | const [videoExists] = await db() |
| 65 | .select({ id: videos.id, ownerId: videos.ownerId }) |
| 66 | .from(videos) |
| 67 | .where(eq(videos.id, Video.VideoId.make(notification.videoId))) |
| 68 | .limit(1); |
| 69 | |
| 70 | if (!videoExists) { |
| 71 | console.error("Video not found for videoId:", notification.videoId); |
| 72 | throw new Error(`Video not found for videoId: ${notification.videoId}`); |
| 73 | } |
| 74 | |
| 75 | const [ownerResult] = await db() |
| 76 | .select({ |
| 77 | id: users.id, |
| 78 | activeOrganizationId: users.activeOrganizationId, |
| 79 | preferences: users.preferences, |
| 80 | }) |
| 81 | .from(users) |
| 82 | .where(eq(users.id, videoExists.ownerId)) |
| 83 | .limit(1); |
| 84 | |
| 85 | if (!ownerResult) { |
| 86 | console.warn( |
| 87 | "Owner not found for videoId:", |
| 88 | notification.videoId, |
| 89 | "ownerId:", |
| 90 | videoExists.ownerId, |
| 91 | "- skipping notification creation", |
| 92 | ); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | const videoResult = { |
| 97 | videoId: videoExists.id, |
| 98 | ownerId: ownerResult.id, |
| 99 | activeOrganizationId: ownerResult.activeOrganizationId, |
| 100 | preferences: ownerResult.preferences, |
| 101 | }; |
| 102 | |
| 103 | const { type, ...data } = notification; |
| 104 | |
| 105 | if (type === "reply" && notification.parentCommentId) { |
| 106 | const [parentComment] = await db() |
| 107 | .select({ authorId: comments.authorId }) |
| 108 | .from(comments) |
| 109 | .where(eq(comments.id, notification.parentCommentId)) |
| 110 | .limit(1); |
| 111 | |
| 112 | const recipientId = parentComment?.authorId; |
| 113 | if (!recipientId) return; |
| 114 | if (recipientId === videoResult.ownerId) return; |
| 115 | |
| 116 | const [recipientUser] = await db() |
| 117 | .select({ |
no test coverage detected