-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from casesandberg/feature/notifications
Notifications V1
- Loading branch information
Showing
45 changed files
with
1,280 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { NextResponse } from 'next/server' | ||
import type { SchemaResponse } from '@play-money/api-helpers' | ||
import { auth } from '@play-money/auth' | ||
import { getNotifications } from '@play-money/notifications/lib/getNotifications' | ||
import { getUnreadNotificationCount } from '@play-money/notifications/lib/getUnreadNotificationCount' | ||
import { updateNotificationsRead } from '@play-money/notifications/lib/updateNotificationsRead' | ||
import type schema from './schema' | ||
|
||
export const dynamic = 'force-dynamic' | ||
|
||
export async function GET(_req: Request): Promise<SchemaResponse<typeof schema.get.responses>> { | ||
try { | ||
const session = await auth() | ||
|
||
if (!session?.user?.id) { | ||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
} | ||
|
||
const [unreadCount, notifications] = await Promise.all([ | ||
getUnreadNotificationCount({ userId: session.user.id }), | ||
getNotifications({ userId: session.user.id }), | ||
]) | ||
|
||
return NextResponse.json({ notifications, unreadCount }) | ||
} catch (error) { | ||
console.log(error) // eslint-disable-line no-console -- Log error for debugging | ||
|
||
return NextResponse.json({ error: 'Failed to retrieve user session' }, { status: 500 }) | ||
} | ||
} | ||
|
||
export async function POST(_req: Request): Promise<SchemaResponse<typeof schema.post.responses>> { | ||
try { | ||
const session = await auth() | ||
|
||
if (!session?.user?.id) { | ||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
} | ||
|
||
await updateNotificationsRead({ userId: session.user.id }) | ||
|
||
return NextResponse.json({ success: true }) | ||
} catch (error) { | ||
console.log(error) // eslint-disable-line no-console -- Log error for debugging | ||
|
||
return NextResponse.json({ error: 'Failed to retrieve user session' }, { status: 500 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { z } from 'zod' | ||
import { ServerErrorSchema, createSchema } from '@play-money/api-helpers' | ||
import { NotificationGroupSchema } from '@play-money/database' | ||
|
||
export default createSchema({ | ||
get: { | ||
responses: { | ||
200: z.object({ notifications: z.array(NotificationGroupSchema), unreadCount: z.number() }), | ||
404: ServerErrorSchema, | ||
500: ServerErrorSchema, | ||
}, | ||
}, | ||
post: { | ||
responses: { | ||
200: z.object({ success: z.boolean() }), | ||
404: ServerErrorSchema, | ||
500: ServerErrorSchema, | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { NextResponse } from 'next/server' | ||
import { type SchemaResponse } from '@play-money/api-helpers' | ||
import { auth } from '@play-money/auth' | ||
import { updateNotificationsRead } from '@play-money/notifications/lib/updateNotificationsRead' | ||
import schema from './schema' | ||
|
||
export const dynamic = 'force-dynamic' | ||
|
||
export async function POST(req: Request): Promise<SchemaResponse<typeof schema.post.responses>> { | ||
try { | ||
const session = await auth() | ||
|
||
if (!session?.user?.id) { | ||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
} | ||
|
||
const body = (await req.json()) as unknown | ||
const { resourceId, resourceType } = schema.post.requestBody.parse(body) | ||
|
||
if (resourceType === 'MARKET') { | ||
await updateNotificationsRead({ userId: session.user.id, marketId: resourceId }) | ||
} | ||
|
||
return NextResponse.json({ success: true }) | ||
} catch (error: unknown) { | ||
console.log(error) // eslint-disable-line no-console -- Log error for debugging | ||
|
||
return NextResponse.json({ error: 'Failed to update user' }, { status: 500 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { z } from 'zod' | ||
import { ServerErrorSchema, createSchema } from '@play-money/api-helpers' | ||
|
||
export default createSchema({ | ||
post: { | ||
requestBody: z.object({ resourceType: z.string(), resourceId: z.string(), timestamp: z.string() }), | ||
responses: { | ||
200: z.object({ success: z.boolean() }), | ||
404: ServerErrorSchema, | ||
500: ServerErrorSchema, | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/database/migrations/20240718223805_notifications/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
-- CreateEnum | ||
CREATE TYPE "NotificationType" AS ENUM ('MARKET_RESOLVED', 'MARKET_TRADE', 'MARKET_LIQUIDITY_ADDED', 'MARKET_COMMENT', 'COMMENT_REPLY', 'COMMENT_MENTION', 'COMMENT_REACTION'); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Notification" ( | ||
"id" TEXT NOT NULL, | ||
"recipientId" TEXT NOT NULL, | ||
"actorId" TEXT NOT NULL, | ||
"type" "NotificationType" NOT NULL, | ||
"content" JSONB NOT NULL, | ||
"marketId" TEXT, | ||
"marketOptionId" TEXT, | ||
"marketResolutionId" TEXT, | ||
"transactionId" TEXT, | ||
"commentId" TEXT, | ||
"parentCommentId" TEXT, | ||
"commentReactionId" TEXT, | ||
"actionUrl" TEXT NOT NULL, | ||
"readAt" TIMESTAMP(3), | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "Notification_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "NotificationGroup" ( | ||
"id" TEXT NOT NULL, | ||
"recipientId" TEXT NOT NULL, | ||
"type" "NotificationType" NOT NULL, | ||
"count" INTEGER NOT NULL DEFAULT 1, | ||
"lastNotificationId" TEXT NOT NULL, | ||
"groupWindowEnd" TIMESTAMP(3) NOT NULL, | ||
"groupKey" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "NotificationGroup_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "Notification_recipientId_createdAt_idx" ON "Notification"("recipientId", "createdAt"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "NotificationGroup_recipientId_type_groupWindowEnd_groupKey_idx" ON "NotificationGroup"("recipientId", "type", "groupWindowEnd", "groupKey"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "NotificationGroup_recipientId_type_groupWindowEnd_groupKey_key" ON "NotificationGroup"("recipientId", "type", "groupWindowEnd", "groupKey"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Notification" ADD CONSTRAINT "Notification_recipientId_fkey" FOREIGN KEY ("recipientId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Notification" ADD CONSTRAINT "Notification_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Notification" ADD CONSTRAINT "Notification_marketId_fkey" FOREIGN KEY ("marketId") REFERENCES "Market"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Notification" ADD CONSTRAINT "Notification_marketOptionId_fkey" FOREIGN KEY ("marketOptionId") REFERENCES "MarketOption"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Notification" ADD CONSTRAINT "Notification_marketResolutionId_fkey" FOREIGN KEY ("marketResolutionId") REFERENCES "MarketResolution"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Notification" ADD CONSTRAINT "Notification_transactionId_fkey" FOREIGN KEY ("transactionId") REFERENCES "Transaction"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Notification" ADD CONSTRAINT "Notification_commentId_fkey" FOREIGN KEY ("commentId") REFERENCES "Comment"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Notification" ADD CONSTRAINT "Notification_parentCommentId_fkey" FOREIGN KEY ("parentCommentId") REFERENCES "Comment"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Notification" ADD CONSTRAINT "Notification_commentReactionId_fkey" FOREIGN KEY ("commentReactionId") REFERENCES "CommentReaction"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "NotificationGroup" ADD CONSTRAINT "NotificationGroup_recipientId_fkey" FOREIGN KEY ("recipientId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "NotificationGroup" ADD CONSTRAINT "NotificationGroup_lastNotificationId_fkey" FOREIGN KEY ("lastNotificationId") REFERENCES "Notification"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
Oops, something went wrong.