updated managment frontedn
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import { appendApplicationToService } from "@/apicalls/custom/application/apicall";
|
||||
import { NextRequest } from "next/server";
|
||||
import { withErrorHandling } from "@/app/api/utils/requestHandlers";
|
||||
import {
|
||||
successResponse,
|
||||
errorResponse,
|
||||
} from "@/app/api/utils/responseHandlers";
|
||||
|
||||
export const POST = withErrorHandling(
|
||||
async (request: NextRequest, body: any) => {
|
||||
const payload = {
|
||||
application_uu_id: body.application_uu_id,
|
||||
service_uu_id: body.service_uu_id,
|
||||
};
|
||||
const result = await appendApplicationToService(payload);
|
||||
if (!result) {
|
||||
return errorResponse("Error appending application to service");
|
||||
} else {
|
||||
return successResponse(result);
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -0,0 +1,22 @@
|
||||
import { removeApplicationFromService } from "@/apicalls/custom/application/apicall";
|
||||
import { NextRequest } from "next/server";
|
||||
import { withErrorHandling } from "@/app/api/utils/requestHandlers";
|
||||
import {
|
||||
successResponse,
|
||||
errorResponse,
|
||||
} from "@/app/api/utils/responseHandlers";
|
||||
|
||||
export const POST = withErrorHandling(
|
||||
async (request: NextRequest, body: any) => {
|
||||
const payload = {
|
||||
application_uu_id: body.application_uu_id,
|
||||
service_uu_id: body.service_uu_id,
|
||||
};
|
||||
const result = await removeApplicationFromService(payload);
|
||||
if (!result) {
|
||||
return errorResponse("Error removing application from service");
|
||||
} else {
|
||||
return successResponse(result);
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -0,0 +1,4 @@
|
||||
import { listApplicationsAppended } from "@/apicalls/custom/application/apicall";
|
||||
import { createListHandler } from "@/app/api/utils";
|
||||
|
||||
export const POST = createListHandler(listApplicationsAppended);
|
||||
@@ -0,0 +1,22 @@
|
||||
import { appendEventToService } from "@/apicalls/custom/events/endpoints";
|
||||
import { NextRequest } from "next/server";
|
||||
import { withErrorHandling } from "@/app/api/utils/requestHandlers";
|
||||
import {
|
||||
successResponse,
|
||||
errorResponse,
|
||||
} from "@/app/api/utils/responseHandlers";
|
||||
|
||||
export const POST = withErrorHandling(
|
||||
async (request: NextRequest, body: any) => {
|
||||
const payload = {
|
||||
event_uu_id: body.event_uu_id,
|
||||
service_uu_id: body.service_uu_id,
|
||||
};
|
||||
const result = await appendEventToService(payload);
|
||||
if (!result) {
|
||||
return errorResponse("Error appending event to service");
|
||||
} else {
|
||||
return successResponse(result);
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -0,0 +1,22 @@
|
||||
import { removeEventFromService } from "@/apicalls/custom/events/endpoints";
|
||||
import { NextRequest } from "next/server";
|
||||
import { withErrorHandling } from "@/app/api/utils/requestHandlers";
|
||||
import {
|
||||
successResponse,
|
||||
errorResponse,
|
||||
} from "@/app/api/utils/responseHandlers";
|
||||
|
||||
export const POST = withErrorHandling(
|
||||
async (request: NextRequest, body: any) => {
|
||||
const payload = {
|
||||
event_uu_id: body.event_uu_id,
|
||||
service_uu_id: body.service_uu_id,
|
||||
};
|
||||
const result = await removeEventFromService(payload);
|
||||
if (!result) {
|
||||
return errorResponse("Error removing event from service");
|
||||
} else {
|
||||
return successResponse(result);
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -0,0 +1,4 @@
|
||||
import { list_events_appended } from "@/apicalls/custom/events/endpoints";
|
||||
import { createListHandler } from "@/app/api/utils";
|
||||
|
||||
export const POST = createListHandler(list_events_appended);
|
||||
@@ -0,0 +1,4 @@
|
||||
import { createCreateHandler } from "@/app/api/utils";
|
||||
import { appendApplicationToService } from "@/apicalls/custom/application/apicall";
|
||||
|
||||
export const POST = createCreateHandler(appendApplicationToService);
|
||||
@@ -0,0 +1,4 @@
|
||||
import { createCreateHandler } from "@/app/api/utils";
|
||||
import { createApplication } from "@/apicalls/custom/application/apicall";
|
||||
|
||||
export const POST = createCreateHandler(createApplication);
|
||||
@@ -0,0 +1,4 @@
|
||||
import { listApplicationsAvailable } from "@/apicalls/custom/application/apicall";
|
||||
import { createListHandler } from "@/app/api/utils";
|
||||
|
||||
export const POST = createListHandler(listApplicationsAvailable);
|
||||
@@ -0,0 +1,4 @@
|
||||
import { listAllApplications } from "@/apicalls/custom/application/apicall";
|
||||
import { createListHandler } from "@/app/api/utils";
|
||||
|
||||
export const POST = createListHandler(listAllApplications);
|
||||
@@ -0,0 +1,4 @@
|
||||
import { createCreateHandler } from "@/app/api/utils";
|
||||
import { removeApplicationFromService } from "@/apicalls/custom/application/apicall";
|
||||
|
||||
export const POST = createCreateHandler(removeApplicationFromService);
|
||||
@@ -0,0 +1,4 @@
|
||||
import { createUpdateHandler } from "../../utils";
|
||||
import { updateApplication } from "@/apicalls/custom/application/apicall";
|
||||
|
||||
export const POST = createUpdateHandler(updateApplication);
|
||||
@@ -0,0 +1,4 @@
|
||||
import { list_events_available } from "@/apicalls/custom/events/endpoints";
|
||||
import { createListHandler } from "@/app/api/utils";
|
||||
|
||||
export const POST = createListHandler(list_events_available);
|
||||
@@ -0,0 +1,4 @@
|
||||
import { listServices } from "@/apicalls/custom/services/endpoints";
|
||||
import { createListHandler } from "@/app/api/utils";
|
||||
|
||||
export const POST = createListHandler(listServices);
|
||||
@@ -0,0 +1,169 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import {
|
||||
successResponse,
|
||||
errorResponse,
|
||||
paginationResponse,
|
||||
createResponse,
|
||||
updateResponse,
|
||||
deleteResponse,
|
||||
} from "./responseHandlers";
|
||||
import { withErrorHandling, validateRequiredFields } from "./requestHandlers";
|
||||
import {
|
||||
ApiHandler,
|
||||
PaginationParams,
|
||||
ListFunction,
|
||||
CreateFunction,
|
||||
UpdateFunction,
|
||||
DeleteFunction,
|
||||
} from "./types";
|
||||
|
||||
/**
|
||||
* Generic list operation handler
|
||||
* @param request NextRequest object
|
||||
* @param body Request body
|
||||
* @param listFunction The function to call to get the list data
|
||||
*/
|
||||
export async function handleListOperation(
|
||||
request: NextRequest,
|
||||
body: any,
|
||||
listFunction: ListFunction
|
||||
) {
|
||||
const page = body.page || 1;
|
||||
const size = body.size || 10;
|
||||
const orderField = body.orderField || ["uu_id"];
|
||||
const orderType = body.orderType || ["asc"];
|
||||
const query = body.query || {};
|
||||
const response = await listFunction({
|
||||
page,
|
||||
size,
|
||||
orderField,
|
||||
orderType,
|
||||
query,
|
||||
} as PaginationParams);
|
||||
return paginationResponse(response.data, response.pagination);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic create operation handler
|
||||
* @param request NextRequest object
|
||||
* @param body Request body
|
||||
* @param createFunction The function to call to create the item
|
||||
* @param requiredFields Array of required field names
|
||||
*/
|
||||
export async function handleCreateOperation(
|
||||
body: any,
|
||||
createFunction?: CreateFunction,
|
||||
requiredFields: string[] = []
|
||||
) {
|
||||
if (requiredFields.length > 0) {
|
||||
const validation = validateRequiredFields(body, requiredFields);
|
||||
if (!validation.valid) {
|
||||
return errorResponse(validation.error as string, 400);
|
||||
}
|
||||
}
|
||||
|
||||
if (createFunction) {
|
||||
console.log("Body:", body);
|
||||
const result = await createFunction(body);
|
||||
return createResponse(result);
|
||||
}
|
||||
|
||||
return createResponse({
|
||||
uuid: Math.floor(Math.random() * 1000),
|
||||
...body,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic update operation handler
|
||||
* @param request NextRequest object
|
||||
* @param body Request body
|
||||
* @param updateFunction The function to call to update the item
|
||||
*/
|
||||
export async function handleUpdateOperation(
|
||||
request: NextRequest,
|
||||
body: any,
|
||||
updateFunction?: UpdateFunction
|
||||
) {
|
||||
const uuid = request.nextUrl.searchParams.get("uuid");
|
||||
if (!uuid) {
|
||||
return errorResponse("UUID not found", 400);
|
||||
}
|
||||
if (updateFunction) {
|
||||
console.log("Body:", body);
|
||||
const result = await updateFunction(body, uuid);
|
||||
return updateResponse(result);
|
||||
}
|
||||
return updateResponse(body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic delete operation handler
|
||||
* @param request NextRequest object
|
||||
* @param deleteFunction The function to call to delete the item
|
||||
*/
|
||||
export async function handleDeleteOperation(
|
||||
request: NextRequest,
|
||||
deleteFunction?: DeleteFunction
|
||||
) {
|
||||
const uuid = request.nextUrl.searchParams.get("uuid");
|
||||
if (!uuid) {
|
||||
return errorResponse("UUID not found", 400);
|
||||
}
|
||||
|
||||
if (deleteFunction) {
|
||||
await deleteFunction(uuid);
|
||||
}
|
||||
return deleteResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a wrapped list handler with error handling
|
||||
* @param listFunction The function to call to get the list data
|
||||
*/
|
||||
export function createListHandler(listFunction: ListFunction) {
|
||||
return withErrorHandling((request: NextRequest, body: any) =>
|
||||
handleListOperation(request, body, listFunction)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a wrapped create handler with error handling
|
||||
* @param createFunction The function to call to create the item
|
||||
* @param requiredFields Array of required field names
|
||||
*/
|
||||
export function createCreateHandler(
|
||||
createFunction?: CreateFunction,
|
||||
requiredFields: string[] = []
|
||||
) {
|
||||
console.log("Required fields:", requiredFields);
|
||||
// This handler only takes the body parameter, not the request
|
||||
return withErrorHandling((body: any) => {
|
||||
// Ensure we're only passing the actual body data to the create function
|
||||
if (body && typeof body === 'object' && body.body) {
|
||||
console.log("Extracting body from request body");
|
||||
return handleCreateOperation(body.body, createFunction, requiredFields);
|
||||
}
|
||||
return handleCreateOperation(body, createFunction, requiredFields);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a wrapped update handler with error handling
|
||||
* @param updateFunction The function to call to update the item
|
||||
*/
|
||||
export function createUpdateHandler(updateFunction?: UpdateFunction) {
|
||||
return withErrorHandling((request: NextRequest, body: any) =>
|
||||
handleUpdateOperation(request, body, updateFunction)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a wrapped delete handler with error handling
|
||||
* @param deleteFunction The function to call to delete the item
|
||||
*/
|
||||
export function createDeleteHandler(deleteFunction?: DeleteFunction) {
|
||||
return withErrorHandling((request: NextRequest) =>
|
||||
handleDeleteOperation(request, deleteFunction)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// Export all utility functions from a single entry point
|
||||
export * from './responseHandlers';
|
||||
export * from './requestHandlers';
|
||||
export * from './apiOperations';
|
||||
@@ -0,0 +1,70 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { errorResponse } from "./responseHandlers";
|
||||
import { ValidationResult, ApiHandler, ApiHandlerBodyOnly, ApiHandlerWithRequest } from "./types";
|
||||
|
||||
/**
|
||||
* Safely parse JSON request body with error handling
|
||||
* @param request NextRequest object
|
||||
* @returns Parsed request body or null if parsing fails
|
||||
*/
|
||||
export async function parseRequestBody(request: NextRequest) {
|
||||
try {
|
||||
return await request.json();
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for API route handlers with built-in error handling
|
||||
* @param handler The handler function to wrap
|
||||
*/
|
||||
export function withErrorHandling(
|
||||
handler: ApiHandler
|
||||
) {
|
||||
return async (request: NextRequest) => {
|
||||
try {
|
||||
const body = await parseRequestBody(request);
|
||||
|
||||
if (body === null) {
|
||||
return errorResponse("Invalid request body", 400);
|
||||
}
|
||||
|
||||
// Check handler parameter count to determine if it needs request object
|
||||
// If handler has only 1 parameter, it's likely a create operation that only needs body
|
||||
if (handler.length === 1) {
|
||||
// Cast to the appropriate handler type
|
||||
return await (handler as ApiHandlerBodyOnly)(body);
|
||||
} else {
|
||||
// Otherwise pass both request and body (for list, update, delete operations)
|
||||
return await (handler as ApiHandlerWithRequest)(request, body);
|
||||
}
|
||||
} catch (error: any) {
|
||||
return errorResponse(
|
||||
error.message || "Internal Server Error",
|
||||
error.status || 500
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that required fields are present in the request body
|
||||
* @param body Request body
|
||||
* @param requiredFields Array of required field names
|
||||
* @returns Object with validation result and error message if validation fails
|
||||
*/
|
||||
export function validateRequiredFields(body: any, requiredFields: string[]): ValidationResult {
|
||||
const missingFields = requiredFields.filter(field =>
|
||||
body[field] === undefined || body[field] === null || body[field] === ''
|
||||
);
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
return {
|
||||
valid: false,
|
||||
error: `Missing required fields: ${missingFields.join(', ')}`
|
||||
};
|
||||
}
|
||||
|
||||
return { valid: true };
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { ApiResponse, PaginationResponse, PaginatedApiResponse } from "./types";
|
||||
|
||||
/**
|
||||
* Standard success response handler
|
||||
* @param data The data to return in the response
|
||||
* @param status HTTP status code (default: 200)
|
||||
*/
|
||||
export function successResponse<T>(data: T, status: number = 200) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
data,
|
||||
} as ApiResponse<T>,
|
||||
{ status }
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard error response handler
|
||||
* @param message Error message
|
||||
* @param status HTTP status code (default: 500)
|
||||
*/
|
||||
export function errorResponse(message: string, status: number = 500) {
|
||||
console.error(`API error: ${message}`);
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
error: message
|
||||
} as ApiResponse<never>,
|
||||
{ status }
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard pagination response format
|
||||
* @param data Array of items to return
|
||||
* @param pagination Pagination information
|
||||
*/
|
||||
export function paginationResponse<T>(data: T[], pagination: PaginationResponse | null) {
|
||||
return NextResponse.json({
|
||||
data: data || [],
|
||||
pagination: pagination || {
|
||||
page: 1,
|
||||
size: 10,
|
||||
totalCount: 0,
|
||||
totalItems: 0,
|
||||
totalPages: 0,
|
||||
pageCount: 0,
|
||||
orderField: ["name"],
|
||||
orderType: ["asc"],
|
||||
query: {},
|
||||
next: false,
|
||||
back: false,
|
||||
},
|
||||
} as PaginatedApiResponse<T>);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create response handler
|
||||
* @param data The created item data
|
||||
*/
|
||||
export function createResponse<T>(data: T) {
|
||||
return successResponse(
|
||||
{
|
||||
...data as any,
|
||||
createdAt: new Date().toISOString(),
|
||||
} as T,
|
||||
201
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update response handler
|
||||
* @param data The updated item data
|
||||
*/
|
||||
export function updateResponse<T>(data: T) {
|
||||
return successResponse(
|
||||
{
|
||||
...data as any,
|
||||
updatedAt: new Date().toISOString(),
|
||||
} as T
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete response handler
|
||||
*/
|
||||
export function deleteResponse() {
|
||||
return successResponse({ message: "Item deleted successfully" }, 204);
|
||||
}
|
||||
119
web_services/management_frontend/src/app/api/utils/types.ts
Normal file
119
web_services/management_frontend/src/app/api/utils/types.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* Type definitions for API utilities
|
||||
*/
|
||||
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
/**
|
||||
* Validation result interface
|
||||
*/
|
||||
export interface ValidationResult {
|
||||
valid: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pagination parameters interface
|
||||
*/
|
||||
export interface PaginationParams {
|
||||
page: number;
|
||||
size: number;
|
||||
orderField: string[];
|
||||
orderType: string[];
|
||||
query: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pagination response interface
|
||||
*/
|
||||
export interface PaginationResponse {
|
||||
page: number;
|
||||
size: number;
|
||||
totalCount: number;
|
||||
totalItems: number;
|
||||
totalPages: number;
|
||||
pageCount: number;
|
||||
orderField: string[];
|
||||
orderType: string[];
|
||||
query: Record<string, any>;
|
||||
next: boolean;
|
||||
back: boolean;
|
||||
}
|
||||
|
||||
export const defaultPaginationResponse: PaginationResponse = {
|
||||
page: 1,
|
||||
size: 10,
|
||||
totalCount: 0,
|
||||
totalItems: 0,
|
||||
totalPages: 0,
|
||||
pageCount: 0,
|
||||
orderField: ["uu_id"],
|
||||
orderType: ["asc"],
|
||||
query: {},
|
||||
next: false,
|
||||
back: false,
|
||||
};
|
||||
|
||||
/**
|
||||
* API response interface
|
||||
*/
|
||||
export interface ApiResponse<T> {
|
||||
success: boolean;
|
||||
data?: T;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Paginated API response interface
|
||||
*/
|
||||
export interface PaginatedApiResponse<T> {
|
||||
data: T[];
|
||||
pagination: PaginationResponse;
|
||||
}
|
||||
|
||||
export const collectPaginationFromApiResponse = (
|
||||
response: PaginatedApiResponse<any>
|
||||
): PaginationResponse => {
|
||||
return {
|
||||
page: response.pagination?.page || 1,
|
||||
size: response.pagination?.size || 10,
|
||||
totalCount: response.pagination?.totalCount || 0,
|
||||
totalItems: response.pagination?.totalItems || 0,
|
||||
totalPages: response.pagination?.totalPages || 0,
|
||||
pageCount: response.pagination?.pageCount || 0,
|
||||
orderField: response.pagination?.orderField || ["uu_id"],
|
||||
orderType: response.pagination?.orderType || ["asc"],
|
||||
query: response.pagination?.query || {},
|
||||
next: response.pagination?.next || false,
|
||||
back: response.pagination?.back || false,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* API handler function types
|
||||
*/
|
||||
export type ApiHandlerWithRequest = (request: NextRequest, body: any) => Promise<Response>;
|
||||
export type ApiHandlerBodyOnly = (body: any) => Promise<Response>;
|
||||
export type ApiHandler = ApiHandlerWithRequest | ApiHandlerBodyOnly;
|
||||
|
||||
/**
|
||||
* List function type
|
||||
*/
|
||||
export type ListFunction = (
|
||||
params: PaginationParams
|
||||
) => Promise<PaginatedApiResponse<any>>;
|
||||
|
||||
/**
|
||||
* Create function type
|
||||
*/
|
||||
export type CreateFunction = (data: any) => Promise<any>;
|
||||
|
||||
/**
|
||||
* Update function type
|
||||
*/
|
||||
export type UpdateFunction = (id: any, data: any) => Promise<any>;
|
||||
|
||||
/**
|
||||
* Delete function type
|
||||
*/
|
||||
export type DeleteFunction = (id: any) => Promise<any>;
|
||||
Reference in New Issue
Block a user