36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
'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 });
|
|
}
|
|
}
|