Skip to content

Commit

Permalink
feat : notification routes
Browse files Browse the repository at this point in the history
  • Loading branch information
warmachine028 committed Nov 17, 2024
1 parent 6b96de1 commit 76431e8
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 130 deletions.
34 changes: 34 additions & 0 deletions client/src/app/api/notifications/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { validateRequest } from '@/auth'
import { prisma } from '@/lib'
import { getNotificationDataInclude, NotificationsPage } from '@/types'
import { NextRequest } from 'next/server'

export const GET = async (req: NextRequest) => {
try {
const cursor = req.nextUrl.searchParams.get('cursor') || undefined
const pageSize = 10
const { user } = await validateRequest()

if (!user) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}
const notifications = await prisma.notification.findMany({
where: { recipientId: user.id },
include: getNotificationDataInclude(),
orderBy: { createdAt: 'desc' },
take: pageSize + 1,
cursor: cursor ? { id: cursor } : undefined
})
const nextCursor =
notifications.length > pageSize ? notifications[pageSize].id : null
const data: NotificationsPage = {
notifications: notifications.slice(0, pageSize),
nextCursor
}

return Response.json(data)
} catch (error) {
console.error(error)
return Response.json({ error: 'Internal server error' }, { status: 500 })
}
}
76 changes: 60 additions & 16 deletions client/src/app/api/posts/[postId]/likes/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { validateRequest } from '@/auth'
import { prisma } from '@/lib'
import { LikeInfo } from '@/types'
import { NotificationType } from '@prisma/client'
import { NextRequest } from 'next/server'

export const GET = async (_: NextRequest, { params }: { params: Promise<{ postId: string }> }) => {
Expand Down Expand Up @@ -54,19 +55,43 @@ export const POST = async (_: NextRequest, { params }: { params: Promise<{ postI
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}

await prisma.like.upsert({
where: {
userId_postId: {
const post = await prisma.post.findUnique({
where: { id: postId },
select: {
authorId: true
}
})
if (!post) {
return Response.json({ error: 'Post not found' }, { status: 404 })
}
await prisma.$transaction([
prisma.like.upsert({
where: {
userId_postId: {
userId: currentUser.id,
postId
}
},
create: {
userId: currentUser.id,
postId
}
},
create: {
userId: currentUser.id,
postId
},
update: {}
})
},
update: {}
}),
...(post.authorId !== currentUser.id ?
[
prisma.notification.create({
data: {
isssuerId: currentUser.id,
recipientId: post.authorId,
postId,
type: NotificationType.LIKE
}
})
]
: [])
])

return new Response()
} catch (error) {
console.error(error)
Expand All @@ -82,13 +107,32 @@ export const DELETE = async (_: NextRequest, { params }: { params: Promise<{ pos
if (!currentUser) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}

await prisma.like.deleteMany({
where: {
userId: currentUser.id,
postId
const post = await prisma.post.findUnique({
where: { id: postId },
select: {
authorId: true
}
})
if (!post) {
return Response.json({ error: 'Post not found' }, { status: 404 })
}
await prisma.$transaction([
prisma.like.deleteMany({
where: {
userId: currentUser.id,
postId
}
}),
prisma.notification.deleteMany({
where: {
isssuerId: currentUser.id,
recipientId: post.authorId,
postId,
type: NotificationType.LIKE
}
})
])

return new Response()
} catch (error) {
console.error(error)
Expand Down
58 changes: 39 additions & 19 deletions client/src/app/api/users/[userId]/followers/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { validateRequest } from '@/auth'
import { prisma } from '@/lib'
import { FollowerInfo } from '@/types'
import { NotificationType } from '@prisma/client'
import { NextRequest } from 'next/server'

export const GET = async (req: NextRequest, { params }: { params: Promise<{ userId: string }> }) => {
export const GET = async (_: NextRequest, { params }: { params: Promise<{ userId: string }> }) => {
try {
const { userId } = await params
const { user: currentUser } = await validateRequest()
Expand Down Expand Up @@ -51,20 +52,29 @@ export const POST = async (_: NextRequest, { params }: { params: Promise<{ userI
if (!currentUser) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}

await prisma.follow.upsert({
where: {
followerId_followingId: {
await prisma.$transaction([
prisma.follow.upsert({
where: {
followerId_followingId: {
followerId: currentUser.id,
followingId: userId
}
},
create: {
followerId: currentUser.id,
followingId: userId
},
update: {}
}),
prisma.notification.create({
data: {
isssuerId: currentUser.id,
recipientId: userId,
type: NotificationType.FOLLOW
}
},
create: {
followerId: currentUser.id,
followingId: userId
},
update: {}
})
})
])


return new Response()
} catch (error) {
Expand All @@ -73,20 +83,30 @@ export const POST = async (_: NextRequest, { params }: { params: Promise<{ userI
}
}

export const DELETE = async (req: NextRequest, { params }: { params: Promise<{ userId: string }> }) => {
export const DELETE = async (_: NextRequest, { params }: { params: Promise<{ userId: string }> }) => {
try {
const { userId } = await params
const { user: currentUser } = await validateRequest()

if (!currentUser) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}
await prisma.follow.deleteMany({
where: {
followerId: currentUser.id,
followingId: userId
}
})

await prisma.$transaction([
prisma.follow.deleteMany({
where: {
followerId: currentUser.id,
followingId: userId
}
}),
prisma.notification.deleteMany({
where: {
isssuerId: currentUser.id,
recipientId: userId,
type: NotificationType.FOLLOW
}
})
])

return new Response()
} catch (error) {
Expand Down
1 change: 0 additions & 1 deletion client/src/app/playground/(post)/Post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Textarea } from '@/components/ui/textarea'
import { useSession, useToast } from '@/hooks'
import { Bookmark, Plus, X } from 'lucide-react'
import { cn, formatRelativeDate } from '@/lib/utils'

import { UserTooltip } from '@/components/users'
import { Avatar } from '@/components'
import { LikeButton, ShareDialog, MoreButton } from '.'
Expand Down
34 changes: 25 additions & 9 deletions client/src/components/comments/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PostData, getCommentDataInclude } from '@/types'
import { prisma } from '@/lib'
import { validateRequest } from '@/auth'
import { createCommentSchema } from '@/lib/validation'
import { NotificationType } from '@prisma/client'

export const createComment = async ({ post, content }: { post: PostData; content: string }) => {
const { user } = await validateRequest()
Expand All @@ -12,15 +13,30 @@ export const createComment = async ({ post, content }: { post: PostData; content
}

const { content: validatedContent } = createCommentSchema.parse({ content })

return prisma.comment.create({
data: {
content: validatedContent,
authorId: user.id,
postId: post.id
},
include: getCommentDataInclude(user.id)
})
const [comment] = await prisma.$transaction([
prisma.comment.create({
data: {
content: validatedContent,
authorId: user.id,
postId: post.id
},
include: getCommentDataInclude(user.id)
}),
...(post.authorId !== user.id ?
[
prisma.notification.create({
data: {
isssuerId: user.id,
recipientId: post.authorId,
postId: post.id,
type: NotificationType.COMMENT
}
})
]
: [])
])

return comment
}

export const deleteComment = async (id: string) => {
Expand Down
Loading

1 comment on commit 76431e8

@vercel
Copy link

@vercel vercel bot commented on 76431e8 Nov 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.