diff --git a/src/app/api/notifications/triggers/[id]/route.ts b/src/app/api/notifications/triggers/[id]/route.ts index 027d428..eeb582a 100644 --- a/src/app/api/notifications/triggers/[id]/route.ts +++ b/src/app/api/notifications/triggers/[id]/route.ts @@ -2,22 +2,77 @@ import { NextRequest, NextResponse } from "next/server"; import { DeleteNotificationTrigger } from "@/domains/notification-triggers/use-cases/delete-notification-trigger"; import { PrismaClientFactory } from "@/common/database/prisma-factory"; import { UserSessionManager } from "@/domains/auth/services/user-session-maganer"; +import { GetNotificationTrigger } from "@/domains/notification-triggers/use-cases/get-notification-trigger"; +import { ApiErrorHandler } from "@/common/error/api-error-handler"; +import { UpdateNotificationTriggerInput } from "@/domains/notification-triggers/use-cases/update-notification-trigger-types"; +import { UpdateNotificationTrigger } from "@/domains/notification-triggers/use-cases/update-notification-trigger"; +import { GenerateNotificationsByTriggers } from "@/domains/notifications/use-cases/generate-notification-by-triggers"; +export async function GET(request: NextRequest, { params }: { params: RequestParams }) { -export async function DELETE(request: NextRequest, { params }: { params: DeleteParams }) { + try { + if (!params.id) + return NextResponse.json(null); - const useCase = new DeleteNotificationTrigger({ - userLogged: await new UserSessionManager().getUserOrThrow(), - prismaClient: PrismaClientFactory.create() - }); + const useCase = new GetNotificationTrigger({ + userLogged: await new UserSessionManager().getUserOrThrow(), + prismaClient: PrismaClientFactory.create() + }); - await useCase.execute({ - id: params.id - }); + const result = await useCase.execute({ + triggerId: params.id + }); - return new Response(null, { status: 204 }); + return NextResponse.json(result); + } + catch(error) { + return ApiErrorHandler.handler(error); + } } -interface DeleteParams { +export async function PUT(request: NextRequest) { + + try { + const input: UpdateNotificationTriggerInput = await request.json(); + + const useCase = new UpdateNotificationTrigger({ + userLogged: await new UserSessionManager().getUserOrThrow(), + prismaClient: PrismaClientFactory.create() + }); + + const trigger = await useCase.execute(input); + + const generateNotificaion = new GenerateNotificationsByTriggers(); + await generateNotificaion.execute({ + triggerId: trigger.id + }); + + return NextResponse.json(trigger); + } + catch(error) { + return ApiErrorHandler.handler(error); + } +} + +export async function DELETE(request: NextRequest, { params }: { params: RequestParams }) { + + try { + const useCase = new DeleteNotificationTrigger({ + userLogged: await new UserSessionManager().getUserOrThrow(), + prismaClient: PrismaClientFactory.create() + }); + + await useCase.execute({ + id: params.id + }); + + return new Response(null, { status: 204 }); + } + catch(error) { + return ApiErrorHandler.handler(error); + } +} + +interface RequestParams { id: string; } \ No newline at end of file diff --git a/src/app/contacts/[contactId]/notification-triggers/[triggerId]/page.tsx b/src/app/contacts/[contactId]/notification-triggers/[triggerId]/page.tsx new file mode 100644 index 0000000..dfeb2b6 --- /dev/null +++ b/src/app/contacts/[contactId]/notification-triggers/[triggerId]/page.tsx @@ -0,0 +1,33 @@ +import { AppLayout } from "@/components/AppLayout"; +import { AppLayoutBackWithContactTitle } from "@/components/AppLayout/BackWithTitle/WithContact"; +import { AppLayoutHeader } from "@/components/AppLayout/Header"; +import { AppLayoutBody } from "@/components/AppLayout/Body"; +import { AppLayoutPreLoading } from "@/components/AppLayout/PreLoading"; +import { AppNavMenuDefault, AppNavMenuItens } from "@/components/AppLayout/NavMenu"; +import { ValidationInit } from "@/components/ValidationInit"; +import { NotificationView } from "@/components/NotificationView"; + +export default function ViewContactNotification({ params }: PageProps) { + + return ( + + + + + + + + + + + + + ) +} + +interface PageProps { + params: { + contactId: string; + triggerId: string; + } +} \ No newline at end of file diff --git a/src/common/routes/index.tsx b/src/common/routes/index.tsx index ec18b1d..aa964b2 100644 --- a/src/common/routes/index.tsx +++ b/src/common/routes/index.tsx @@ -6,6 +6,7 @@ export abstract class AppRoutes { public static importContacts() { return '/contacts/import'; } public static viewContact(contactId: string) { return '/contacts/' + contactId; } public static newContactNotification(contactId: string) { return `/contacts/${contactId}/notification-triggers/new`; } + public static viewContactNotification(contactId: string, triggerId: string) { return `/contacts/${contactId}/notification-triggers/${triggerId}`; } public static contactNotificationTriggers(contactId: string) { return `/contacts/${contactId}/notification-triggers`; } public static contactNotifications(contactId: string) { return `/contacts/${contactId}/notifications`; } public static home() { return '/'; } diff --git a/src/components/ContactNotificationTriggersView/index.tsx b/src/components/ContactNotificationTriggersView/index.tsx index c9b9d19..554b475 100644 --- a/src/components/ContactNotificationTriggersView/index.tsx +++ b/src/components/ContactNotificationTriggersView/index.tsx @@ -1,7 +1,9 @@ 'use client' +import { useRouter } from "next/navigation"; import { ArrowDownTrayIcon } from "@heroicons/react/24/outline"; import { AppError } from "@/common/error"; +import { AppRoutes } from "@/common/routes"; import { AppToast } from "@/common/ui/toast"; import { NotificationTriggersApi } from "@/domains/notification-triggers/client-api"; import { Button } from "../Form"; @@ -13,6 +15,7 @@ export default function ContactNotificationTriggersView({ contactId }: ContactNo const { data, isLoading: isLoadingTriggers, error, hasMore, loadNextPage, removeItem } = useNotificationTriggers({ contactId }); const { setLoading } = useLoading(); + const router = useRouter(); const isEmpty = data.length == 0 && !isLoadingTriggers && !error; const isFirstLoading = isLoadingTriggers && data.length === 0; @@ -39,13 +42,18 @@ export default function ContactNotificationTriggersView({ contactId }: ContactNo } }; + const handleEditTrigger = (triggerId: string) => async () => { + + router.push(AppRoutes.viewContactNotification(contactId, triggerId)); + }; + return (