users update create delete tested
This commit is contained in:
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>
|
||||
Reference in New Issue
Block a user