users update create delete tested
This commit is contained in:
53
frontend/app/api/users/add/route.ts
Normal file
53
frontend/app/api/users/add/route.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
'use server';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { GraphQLClient, gql } from 'graphql-request';
|
||||
import { userAddSchema } from './schema';
|
||||
|
||||
const endpoint = "http://localhost:3001/graphql";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const body = await request.json();
|
||||
console.log("BODY")
|
||||
console.dir({ body })
|
||||
const validatedBody = userAddSchema.parse(body);
|
||||
validatedBody.person = "6917732face2287b1d901738"
|
||||
try {
|
||||
const client = new GraphQLClient(endpoint);
|
||||
const query = gql`
|
||||
mutation CreateUser($input: CreateUserInput!) {
|
||||
createUser(input: $input) {
|
||||
_id
|
||||
password
|
||||
tag
|
||||
email
|
||||
phone
|
||||
collectionTokens {
|
||||
default
|
||||
tokens {
|
||||
prefix
|
||||
token
|
||||
}
|
||||
}
|
||||
person
|
||||
}
|
||||
}
|
||||
`;
|
||||
const variables = { input: validatedBody };
|
||||
const data = await client.request(query, variables);
|
||||
console.log("DATA")
|
||||
console.dir({ data })
|
||||
return NextResponse.json({ data: data.createUser, status: 200 });
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
return NextResponse.json({ error: err.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// export async function POST(request: Request) {
|
||||
|
||||
// const body = await request.json();
|
||||
// const validatedBody = userAddSchema.parse(body);
|
||||
// console.log("VALIDATED")
|
||||
// console.dir({ validatedBody })
|
||||
// return NextResponse.json({ data: validatedBody })
|
||||
// }
|
||||
29
frontend/app/api/users/add/schema.ts
Normal file
29
frontend/app/api/users/add/schema.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const tokenSchema = z.object({
|
||||
prefix: z.string().min(1, "Prefix is required"),
|
||||
token: z.string().min(1, "Token is required"),
|
||||
})
|
||||
|
||||
export const collectionTokensSchema = z.object({
|
||||
default: z.string().optional(),
|
||||
tokens: z.array(tokenSchema)
|
||||
})
|
||||
|
||||
export const userAddSchema = z.object({
|
||||
expiryStarts: z.string().optional(),
|
||||
expiryEnds: z.string().optional(),
|
||||
|
||||
isConfirmed: z.boolean(),
|
||||
isNotificationSend: z.boolean(),
|
||||
|
||||
password: z.string().min(6),
|
||||
tag: z.string().optional(),
|
||||
email: z.string().email(),
|
||||
phone: z.string().min(5),
|
||||
person: z.string().optional(),
|
||||
|
||||
collectionTokens: collectionTokensSchema,
|
||||
})
|
||||
|
||||
export type UserAdd = z.infer<typeof userAddSchema>
|
||||
22
frontend/app/api/users/delete/route.ts
Normal file
22
frontend/app/api/users/delete/route.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
'use server';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { GraphQLClient, gql } from 'graphql-request';
|
||||
|
||||
const endpoint = "http://localhost:3001/graphql";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
|
||||
const searchParams = new URL(request.url).searchParams;
|
||||
const uuid = searchParams.get('uuid');
|
||||
if (!uuid) { return NextResponse.json({ error: 'UUID not found in search params' }, { status: 400 }) }
|
||||
try {
|
||||
const client = new GraphQLClient(endpoint);
|
||||
const query = gql`mutation DeleteUser($uuid: String!) { deleteUser(uuid: $uuid)}`;
|
||||
const variables = { uuid: uuid };
|
||||
const data = await client.request(query, variables);
|
||||
return NextResponse.json({ data: data.deleteUser, status: 200 });
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
return NextResponse.json({ error: err.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { GraphQLClient, gql } from 'graphql-request';
|
||||
|
||||
const endpoint = process.env.GRAPHQL_URL || "http://localhost:3001/graphql";
|
||||
const endpoint = "http://localhost:3001/graphql";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const body = await request.json();
|
||||
@@ -50,7 +50,6 @@ export async function POST(request: Request) {
|
||||
}
|
||||
}
|
||||
`;
|
||||
console.dir({ limit, skip, sort, filters }, { depth: null });
|
||||
const variables = { input: { limit, skip, sort, filters } };
|
||||
const data = await client.request(query, variables);
|
||||
return NextResponse.json({ data: data.users.data, totalCount: data.users.totalCount });
|
||||
|
||||
35
frontend/app/api/users/update/route.ts
Normal file
35
frontend/app/api/users/update/route.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
'use server';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { GraphQLClient, gql } from 'graphql-request';
|
||||
import { userUpdateSchema } from './schema';
|
||||
|
||||
const endpoint = "http://localhost:3001/graphql";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const searchUrl = new URL(request.url);
|
||||
const uuid = searchUrl.searchParams.get("uuid") || "";
|
||||
const body = await request.json();
|
||||
const validatedBody = userUpdateSchema.parse(body);
|
||||
if (uuid === "") { return NextResponse.json({ error: "UUID is required" }, { status: 400 }) }
|
||||
try {
|
||||
const client = new GraphQLClient(endpoint);
|
||||
const query = gql`
|
||||
mutation UpdateUserTest($uuid: String!, $input: UpdateUserInput!) {
|
||||
updateUser(uuid: $uuid, input: $input) {
|
||||
tag
|
||||
email
|
||||
phone
|
||||
person
|
||||
expiryStarts
|
||||
expiryEnds
|
||||
}
|
||||
}
|
||||
`;
|
||||
const variables = { uuid: uuid, input: validatedBody };
|
||||
const data = await client.request(query, variables);
|
||||
return NextResponse.json({ data: data.updateUser, status: 200 });
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
return NextResponse.json({ error: err.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
28
frontend/app/api/users/update/schema.ts
Normal file
28
frontend/app/api/users/update/schema.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const tokenSchema = z.object({
|
||||
prefix: z.string().min(1, "Prefix is required").optional(),
|
||||
token: z.string().min(1, "Token is required").optional(),
|
||||
})
|
||||
|
||||
export const collectionTokensSchema = z.object({
|
||||
default: z.string().optional(),
|
||||
tokens: z.array(tokenSchema).optional()
|
||||
})
|
||||
|
||||
export const userUpdateSchema = z.object({
|
||||
expiryStarts: z.string().optional(),
|
||||
expiryEnds: z.string().optional(),
|
||||
|
||||
isConfirmed: z.boolean().optional(),
|
||||
isNotificationSend: z.boolean().optional(),
|
||||
|
||||
tag: z.string().optional(),
|
||||
email: z.string().email().optional(),
|
||||
phone: z.string().min(5).optional(),
|
||||
person: z.string().optional(),
|
||||
|
||||
collectionTokens: collectionTokensSchema,
|
||||
})
|
||||
|
||||
export type UserUpdate = z.infer<typeof userUpdateSchema>
|
||||
@@ -1,5 +1,6 @@
|
||||
import DashboardPage from "@/pages/dashboard/page";
|
||||
// import DashboardPage from "@/pages/dashboard/page";
|
||||
|
||||
const PageDashboard = () => { return <DashboardPage /> };
|
||||
// const PageDashboard = () => { return <DashboardPage /> };
|
||||
const PageDashboard = () => { return <></> };
|
||||
|
||||
export default PageDashboard;
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
import "./globals.css";
|
||||
import type { Metadata } from "next";
|
||||
import { Geist, Geist_Mono } from "next/font/google";
|
||||
import "./globals.css";
|
||||
import { SidebarInset, SidebarProvider } from "@/components/ui/sidebar";
|
||||
import { AppSidebar } from "@/components/sidebar/app-sidebar";
|
||||
import { SiteHeader } from "@/components/sidebar/site-header";
|
||||
import Providers from "./providers";
|
||||
|
||||
const geistSans = Geist({
|
||||
variable: "--font-geist-sans",
|
||||
subsets: ["latin"],
|
||||
});
|
||||
const geistSans = Geist({ variable: "--font-geist-sans", subsets: ["latin"] });
|
||||
|
||||
const geistMono = Geist_Mono({
|
||||
variable: "--font-geist-mono",
|
||||
subsets: ["latin"],
|
||||
});
|
||||
const geistMono = Geist_Mono({ variable: "--font-geist-mono", subsets: ["latin"] });
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "Create Next App",
|
||||
@@ -22,7 +19,21 @@ export default function RootLayout({ children }: Readonly<{ children: React.Reac
|
||||
return (
|
||||
<html lang="en">
|
||||
<body className={`${geistSans.variable} ${geistMono.variable} antialiased`}>
|
||||
<Providers>{children}</Providers>
|
||||
<Providers>
|
||||
<SidebarProvider style={{ "--sidebar-width": "calc(var(--spacing) * 72)", "--header-height": "calc(var(--spacing) * 12)" } as React.CSSProperties}>
|
||||
<AppSidebar variant="inset" />
|
||||
<SidebarInset>
|
||||
<SiteHeader />
|
||||
<div className="flex flex-1 flex-col">
|
||||
<div className="@container/main flex flex-1 flex-col gap-2">
|
||||
<div className="flex flex-col gap-4 py-4 md:gap-6 md:py-6">
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</SidebarInset>
|
||||
</SidebarProvider>
|
||||
</Providers>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
|
||||
5
frontend/app/users/add/page.tsx
Normal file
5
frontend/app/users/add/page.tsx
Normal file
@@ -0,0 +1,5 @@
|
||||
import { PageAddUser } from "@/pages/users/add/page";
|
||||
|
||||
const AddUserPage = () => { return <><PageAddUser /></> }
|
||||
|
||||
export default AddUserPage;
|
||||
7
frontend/app/users/update/page.tsx
Normal file
7
frontend/app/users/update/page.tsx
Normal file
@@ -0,0 +1,7 @@
|
||||
import { PageUpdateUser } from '@/pages/users/update/page';
|
||||
|
||||
const UpdateUserPage = async () => {
|
||||
return <PageUpdateUser />
|
||||
}
|
||||
|
||||
export default UpdateUserPage;
|
||||
Reference in New Issue
Block a user