build-address added tested
This commit is contained in:
39
frontend/app/api/people/add/route.ts
Normal file
39
frontend/app/api/people/add/route.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
'use server';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { GraphQLClient, gql } from 'graphql-request';
|
||||
import { personAddSchema } from './schema';
|
||||
|
||||
const endpoint = "http://localhost:3001/graphql";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const body = await request.json();
|
||||
const validatedBody = personAddSchema.parse(body);
|
||||
try {
|
||||
const client = new GraphQLClient(endpoint);
|
||||
const query = gql`
|
||||
mutation CreatePerson($input: CreatePersonInput!) {
|
||||
createPerson(input: $input) {
|
||||
uuid
|
||||
firstName
|
||||
surname
|
||||
birthDate
|
||||
}
|
||||
}
|
||||
`;
|
||||
const variables = { input: validatedBody };
|
||||
const data = await client.request(query, variables);
|
||||
return NextResponse.json({ data: data.createPerson, 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 })
|
||||
// }
|
||||
22
frontend/app/api/people/add/schema.ts
Normal file
22
frontend/app/api/people/add/schema.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const personAddSchema = z.object({
|
||||
firstName: z.string(),
|
||||
surname: z.string(),
|
||||
middleName: z.string().optional(),
|
||||
sexCode: z.string(),
|
||||
personRef: z.string().optional(),
|
||||
personTag: z.string().optional(),
|
||||
fatherName: z.string().optional(),
|
||||
motherName: z.string().optional(),
|
||||
countryCode: z.string(),
|
||||
nationalIdentityId: z.string(),
|
||||
birthPlace: z.string(),
|
||||
birthDate: z.string(),
|
||||
taxNo: z.string().optional(),
|
||||
birthname: z.string().optional(),
|
||||
expiryStarts: z.string().optional(),
|
||||
expiryEnds: z.string().optional(),
|
||||
});
|
||||
|
||||
export type PeopleAdd = z.infer<typeof personAddSchema>;
|
||||
23
frontend/app/api/people/delete/route.ts
Normal file
23
frontend/app/api/people/delete/route.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
'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 DeletePerson($uuid: String!) { deletePerson(uuid: $uuid)}`;
|
||||
const variables = { uuid: uuid };
|
||||
const data = await client.request(query, variables);
|
||||
return NextResponse.json({ data: data.deletePerson, status: 200 });
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
return NextResponse.json({ error: err.message }, { status: 500 });
|
||||
}
|
||||
|
||||
}
|
||||
48
frontend/app/api/people/list/route.ts
Normal file
48
frontend/app/api/people/list/route.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
'use server';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { GraphQLClient, gql } from 'graphql-request';
|
||||
|
||||
const endpoint = "http://localhost:3001/graphql";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const body = await request.json();
|
||||
const { limit, skip, sort, filters } = body;
|
||||
try {
|
||||
const client = new GraphQLClient(endpoint);
|
||||
const query = gql`
|
||||
query GetPeople($input: ListArguments!) {
|
||||
people(input: $input) {
|
||||
totalCount
|
||||
data {
|
||||
_id
|
||||
uuid
|
||||
firstName
|
||||
surname
|
||||
middleName
|
||||
sexCode
|
||||
personRef
|
||||
personTag
|
||||
fatherName
|
||||
motherName
|
||||
countryCode
|
||||
nationalIdentityId
|
||||
birthPlace
|
||||
birthDate
|
||||
taxNo
|
||||
birthname
|
||||
expiryStarts
|
||||
expiryEnds
|
||||
createdAt
|
||||
updatedAt
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
const variables = { input: { limit, skip, sort, filters } };
|
||||
const data = await client.request(query, variables);
|
||||
return NextResponse.json({ data: data.people.data, totalCount: data.people.totalCount });
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
return NextResponse.json({ error: err.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
34
frontend/app/api/people/update/route.ts
Normal file
34
frontend/app/api/people/update/route.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
'use server';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { GraphQLClient, gql } from 'graphql-request';
|
||||
import { personUpdateSchema } 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 = personUpdateSchema.parse(body);
|
||||
console.dir({ validatedBody });
|
||||
if (uuid === "") { return NextResponse.json({ error: "UUID is required" }, { status: 400 }) }
|
||||
try {
|
||||
const client = new GraphQLClient(endpoint);
|
||||
const query = gql`
|
||||
mutation updatePerson($uuid: String! $input: UpdatePersonInput!) {
|
||||
updatePerson(uuid: $uuid, input: $input) {
|
||||
uuid
|
||||
firstName
|
||||
surname
|
||||
sexCode
|
||||
}
|
||||
}
|
||||
`;
|
||||
const variables = { uuid: uuid, input: validatedBody };
|
||||
const data = await client.request(query, variables);
|
||||
return NextResponse.json({ data: data.updatePerson, status: 200 });
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
return NextResponse.json({ error: err.message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
22
frontend/app/api/people/update/schema.ts
Normal file
22
frontend/app/api/people/update/schema.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const personUpdateSchema = z.object({
|
||||
firstName: z.string().optional(),
|
||||
surname: z.string().optional(),
|
||||
middleName: z.string().optional(),
|
||||
sexCode: z.string().optional(),
|
||||
personRef: z.string().optional(),
|
||||
personTag: z.string().optional(),
|
||||
fatherName: z.string().optional(),
|
||||
motherName: z.string().optional(),
|
||||
countryCode: z.string().optional(),
|
||||
nationalIdentityId: z.string().optional(),
|
||||
birthPlace: z.string().optional(),
|
||||
birthDate: z.string().optional(),
|
||||
taxNo: z.string().optional().optional(),
|
||||
birthname: z.string().optional().optional(),
|
||||
expiryStarts: z.string().optional().optional(),
|
||||
expiryEnds: z.string().optional().optional(),
|
||||
});
|
||||
|
||||
export type PeopleUpdate = z.infer<typeof personUpdateSchema>;
|
||||
Reference in New Issue
Block a user