Skip to content

Commit

Permalink
Created a standardized endpoint generator for the front end. Need to …
Browse files Browse the repository at this point in the history
…add singleton operations, types, intersections, collections and graphs.
  • Loading branch information
buchananwill committed Apr 10, 2024
1 parent 0d16358 commit eb53b76
Show file tree
Hide file tree
Showing 22 changed files with 333 additions and 145 deletions.
6 changes: 6 additions & 0 deletions app/api/actions/carousel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use server';
import { generateBaseEndpointSet } from './template-endpoints';
import { CarouselDto } from '../dtos/CarouselDtoSchema';

export const { getPage, deleteIdList, postList, putList } =
generateBaseEndpointSet<CarouselDto, string>(['carouselGroups', 'carousels']);
96 changes: 9 additions & 87 deletions app/api/actions/curriculum-delivery-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@
import { ActionResponsePromise } from './actionResponse';
import { WorkProjectSeriesSchemaDto } from '../dtos/WorkProjectSeriesSchemaDtoSchema';
import { API_BASE_URL, Page } from '../main';
import { GraphDto, GraphDtoPutRequestBody } from '../zod-mods';
import { WorkSeriesBundleAssignmentDto } from '../dtos/WorkSeriesBundleAssignmentDtoSchema';
import { WorkSeriesSchemaBundleLeanDto } from '../dtos/WorkSeriesSchemaBundleLeanDtoSchema';
import { OrganizationDto } from '../dtos/OrganizationDtoSchema';
import {
deleteEntities,
getDtoListByIds,
getWithoutBody,
postEntities,
postEntitiesWithDifferentReturnType,
putEntities,
putRequestWithDifferentReturnType
putEntities
} from './template-actions';
import { OrganizationTypeDto } from '../dtos/OrganizationTypeDtoSchema';
import { LongLongTuple } from '../dtos/LongLongTupleSchema';

const SCHEMA_URL = `${API_BASE_URL}/workProjectSeriesSchemas`;

export async function getCurriculumDeliveryModelSchemasByKnowledgeLevel(
page: number = 0,
size: number = 10,
Expand All @@ -37,89 +29,28 @@ export async function getCurriculumDeliveryModelSchemasByKnowledgeLevel(
export async function deleteCurriculumDeliveryModels(schemaIdList: string[]) {
return deleteEntities(schemaIdList, SCHEMA_URL);
}
export async function getBundleDeliveriesByOrgType(
orgType: string
): ActionResponsePromise<WorkSeriesBundleAssignmentDto[]> {
let url = SCHEMA_URL;
url = `${url}/assignments/organizationType/${orgType.replaceAll(' ', '%20')}`;

return await getWithoutBody<WorkSeriesBundleAssignmentDto[]>(url);
}

export async function getSchemasByIdList(idList: string[]) {
const url = `${SCHEMA_URL}/byIdList`;
return await getDtoListByIds<string, WorkProjectSeriesSchemaDto>(idList, url);
}
const url = `${API_BASE_URL}/v2/workProjectSeriesSchemas/listById`;

const organizationGraphEndpoint = `${API_BASE_URL}/v2/organizations/graphs`;
// UUID regex pattern
const uuidRegex =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;

export async function getOrganizationGraph(): ActionResponsePromise<
GraphDto<OrganizationDto>
> {
return getWithoutBody(organizationGraphEndpoint);
}
export async function getOrganizationGraphByRootId(
rootId: number
): ActionResponsePromise<GraphDto<OrganizationDto>> {
return getWithoutBody(`${organizationGraphEndpoint}/byRootId/${rootId}`);
}

export async function getOrganizationGraphByOrganizationType(
yearGroup: string
) {
return getWithoutBody<GraphDto<OrganizationDto>>(
`${organizationGraphEndpoint}/${yearGroup}`
);
}
idList.forEach((uuid) => {
if (!uuidRegex.test(uuid)) console.log(uuid);
});

export async function getOrganizationTypes(): ActionResponsePromise<
OrganizationTypeDto[]
> {
const url = `${organizationGraphEndpoint}/types?parentTypeId=6&depth=1&depthOp=%3D
`;
return getWithoutBody(url);
return await getDtoListByIds<string, WorkProjectSeriesSchemaDto>(idList, url);
}

export async function putOrganizationGraph(
requestBody: GraphDtoPutRequestBody<OrganizationDto>
): ActionResponsePromise<GraphDto<OrganizationDto>> {
return putRequestWithDifferentReturnType<
GraphDtoPutRequestBody<OrganizationDto>,
GraphDto<OrganizationDto>
>(requestBody, organizationGraphEndpoint);
}
export async function getCurriculumDeliveries(
idList: number[]
): ActionResponsePromise<WorkSeriesBundleAssignmentDto[]> {
const urlForDeliveries = `${SCHEMA_URL}/assignments/by-party-id`;
return getDtoListByIds(idList, urlForDeliveries);
}

export async function getBundles(
idList: string[]
): ActionResponsePromise<WorkSeriesSchemaBundleLeanDto[]> {
const urlForBundlesContainingSchemasInList = `${SCHEMA_URL}/bundles/schema-id-list`;
return getDtoListByIds(idList, urlForBundlesContainingSchemasInList);
}

const bundleApiEndpoint = `${SCHEMA_URL}/bundles`;

export async function putBundles(
bundleList: WorkSeriesSchemaBundleLeanDto[]
): ActionResponsePromise<WorkSeriesSchemaBundleLeanDto[]> {
return putEntities(bundleList, bundleApiEndpoint);
}
export async function postBundles(
bundleList: WorkSeriesSchemaBundleLeanDto[]
): ActionResponsePromise<WorkSeriesSchemaBundleLeanDto[]> {
return postEntities(bundleList, bundleApiEndpoint);
}
export async function deleteBundles(
deleteBundleIds: number[]
): ActionResponsePromise<number[]> {
return deleteEntities(deleteBundleIds, bundleApiEndpoint);
}

export async function putModels(
modelList: WorkProjectSeriesSchemaDto[]
): ActionResponsePromise<WorkProjectSeriesSchemaDto[]> {
Expand All @@ -130,12 +61,3 @@ export async function postModels(
): ActionResponsePromise<WorkProjectSeriesSchemaDto[]> {
return postEntities(modelList, SCHEMA_URL);
}
export async function postBundleDeliveries(
bundleAssignments: LongLongTuple[]
): ActionResponsePromise<WorkSeriesBundleAssignmentDto[]> {
const bundlePostUrl = `${SCHEMA_URL}/assignments`;
return postEntitiesWithDifferentReturnType<
LongLongTuple[],
WorkSeriesBundleAssignmentDto[]
>(bundleAssignments, bundlePostUrl);
}
47 changes: 47 additions & 0 deletions app/api/actions/organizations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use server';
import { ActionResponsePromise } from './actionResponse';
import { GraphDto, GraphDtoPutRequestBody } from '../zod-mods';
import { OrganizationDto } from '../dtos/OrganizationDtoSchema';
import {
getWithoutBody,
putRequestWithDifferentReturnType
} from './template-actions';
import { API_BASE_URL } from '../main';
import { OrganizationTypeDto } from '../dtos/OrganizationTypeDtoSchema';

const organizationGraphEndpoint = `${API_BASE_URL}/v2/organizations/graphs`;

export async function getOrganizationGraph(): ActionResponsePromise<
GraphDto<OrganizationDto>
> {
return getWithoutBody(organizationGraphEndpoint);
}

export async function getOrganizationGraphByRootId(
rootId: number
): ActionResponsePromise<GraphDto<OrganizationDto>> {
return getWithoutBody(`${organizationGraphEndpoint}/byRootId/${rootId}`);
}

export async function getOrganizationGraphByOrganizationType(typeId: number) {
return getWithoutBody<GraphDto<OrganizationDto>>(
`${organizationGraphEndpoint}/byOrganizationType/${typeId}`
);
}

export async function getOrganizationTypes(): ActionResponsePromise<
GraphDto<OrganizationTypeDto>
> {
const url = `${API_BASE_URL}/v2/organizations/types/graphs/byRootId/6?depth=0&depthOp=%3E
`;
return getWithoutBody(url);
}

export async function putOrganizationGraph(
requestBody: GraphDtoPutRequestBody<OrganizationDto>
): ActionResponsePromise<GraphDto<OrganizationDto>> {
return putRequestWithDifferentReturnType<
GraphDtoPutRequestBody<OrganizationDto>,
GraphDto<OrganizationDto>
>(requestBody, organizationGraphEndpoint);
}
70 changes: 70 additions & 0 deletions app/api/actions/template-endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { ActionResponsePromise } from './actionResponse';
import {
deleteEntities,
getWithoutBody,
postEntities,
putEntities
} from './template-actions';
import { API_V2_URL, isNotUndefined, Page } from '../main';

function constructUrl(resourceSegments: string[], action?: string) {
const basePath = API_V2_URL;
const resourcePath = resourceSegments.join('/');
return `${basePath}/${resourcePath}${
isNotUndefined(action) ? `/${action}` : ''
}`;
}

export interface PageRequest {
page?: number;
pageSize?: number;
sort?: string;
}

export interface BaseEndpointSet<T, ID_TYPE> {
getPage: (pageRequest: PageRequest) => ActionResponsePromise<Page<T>>;
putList: (dtoList: T[]) => ActionResponsePromise<T[]>;
postList: (dtoList: T[]) => ActionResponsePromise<T[]>;
deleteIdList: (idDeletionList: ID_TYPE[]) => ActionResponsePromise<ID_TYPE[]>;
}

async function getDtoList<T>(
{ page = 0, pageSize = 10 }: PageRequest,
url: string
): ActionResponsePromise<Page<T>> {
return getWithoutBody(`${url}?page=${page}&size=${pageSize}`);
}

async function putDtoList<T>(
dtoList: T[],
url: string
): ActionResponsePromise<T[]> {
return putEntities(dtoList, url);
}

async function postDtoList<T>(
dtoList: T[],
url: string
): ActionResponsePromise<T[]> {
return postEntities(dtoList, url);
}

async function deleteDtoList<ID_TYPE>(
deletionIdList: ID_TYPE[],
url: string
): ActionResponsePromise<ID_TYPE[]> {
return deleteEntities(deletionIdList, url);
}

export function generateBaseEndpointSet<T, ID_TYPE>(
resourceSegments: string[]
): BaseEndpointSet<T, ID_TYPE> {
const generatedUrl = constructUrl(resourceSegments);
return {
getPage: (pageRequest) => getDtoList<T>(pageRequest, generatedUrl),
putList: (dtoList) => putDtoList(dtoList, generatedUrl),
postList: (dtoList) => postDtoList(dtoList, generatedUrl),
deleteIdList: (idDeletionList) =>
deleteDtoList<ID_TYPE>(idDeletionList, generatedUrl)
};
}
29 changes: 29 additions & 0 deletions app/api/actions/work-series-bundle-assignments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use server';
import { ActionResponsePromise } from './actionResponse';
import { WorkSeriesBundleAssignmentDto } from '../dtos/WorkSeriesBundleAssignmentDtoSchema';
import {
getWithoutBody,
postEntitiesWithDifferentReturnType
} from './template-actions';
import { API_V2_URL } from '../main';
import { LongLongTuple } from '../dtos/LongLongTupleSchema';

const ASSIGNMENTS_ENDPOINT = `${API_V2_URL}/workProjectSeriesSchemas/bundleAssignments`;

export async function getBundleAssignmentsByOrgType(
orgType: number
): ActionResponsePromise<WorkSeriesBundleAssignmentDto[]> {
const url = `${ASSIGNMENTS_ENDPOINT}/byOrganizationType/${orgType}`;
console.log(url);
return await getWithoutBody<WorkSeriesBundleAssignmentDto[]>(url);
}

export async function postBundleDeliveries(
bundleAssignments: LongLongTuple[]
): ActionResponsePromise<WorkSeriesBundleAssignmentDto[]> {
const bundlePostUrl = `${ASSIGNMENTS_ENDPOINT}/byLongLongTupleList`;
return postEntitiesWithDifferentReturnType<
LongLongTuple[],
WorkSeriesBundleAssignmentDto[]
>(bundleAssignments, bundlePostUrl);
}
45 changes: 45 additions & 0 deletions app/api/actions/work-series-schema-bundles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { WorkSeriesSchemaBundleLeanDto } from '../dtos/WorkSeriesSchemaBundleLeanDtoSchema';
import { ActionResponsePromise } from './actionResponse';
import {
deleteEntities,
getDtoListByIds,
getWithoutBody,
postEntities,
putEntities
} from './template-actions';
import { API_V2_URL, Page } from '../main';

const BUNDLES_ENDPOINT = `${API_V2_URL}/workProjectSeriesSchemas/bundles`;

export async function getBundlesBySchemaIdList(
idList: string[]
): ActionResponsePromise<WorkSeriesSchemaBundleLeanDto[]> {
const urlForBundlesContainingSchemasInList = `${BUNDLES_ENDPOINT}/withItemSchemaIdInList`;
console.log(urlForBundlesContainingSchemasInList);

return getDtoListByIds(idList, urlForBundlesContainingSchemasInList);
}

export async function getBundles(): ActionResponsePromise<
Page<WorkSeriesSchemaBundleLeanDto>
> {
return getWithoutBody(`${BUNDLES_ENDPOINT}?page=0&size=100`);
}

export async function putBundles(
bundleList: WorkSeriesSchemaBundleLeanDto[]
): ActionResponsePromise<WorkSeriesSchemaBundleLeanDto[]> {
return putEntities(bundleList, BUNDLES_ENDPOINT);
}

export async function postBundles(
bundleList: WorkSeriesSchemaBundleLeanDto[]
): ActionResponsePromise<WorkSeriesSchemaBundleLeanDto[]> {
return postEntities(bundleList, BUNDLES_ENDPOINT);
}

export async function deleteBundles(
deleteBundleIds: number[]
): ActionResponsePromise<number[]> {
return deleteEntities(deleteBundleIds, BUNDLES_ENDPOINT);
}
12 changes: 12 additions & 0 deletions app/api/actions/work-task-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { API_BASE_URL, joinSearchParams } from '../main';
import { WorkTaskTypeDto } from '../dtos/WorkTaskTypeDtoSchema';
import {
getWithoutBody,
postEntitiesWithDifferentReturnType,
putEntities,
putRequestWithDifferentReturnType
} from './template-actions';
Expand Down Expand Up @@ -40,6 +41,17 @@ export async function getWorkTaskTypes(searchParams: {
return getWithoutBody(url);
}

export async function getWorkTaskTypesByWorkProjectSeriesSchemaIdList(
idList: string[]
): ActionResponsePromise<WorkTaskTypeDto[]> {
const url = `${API_BASE_URL}/v2/workTaskTypes/byWorkProjectSeriesSchemaIdList`;

return postEntitiesWithDifferentReturnType<string[], WorkTaskTypeDto[]>(
idList,
url
);
}

export async function putWorkTaskTypes(
modelList: WorkTaskTypeDto[]
): ActionResponsePromise<WorkTaskTypeDto[]> {
Expand Down
3 changes: 2 additions & 1 deletion app/api/dtos/CarouselDtoSchema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { CarouselOptionDtoSchema } from './CarouselOptionDtoSchema';
import { z } from 'zod';
export const CarouselDtoSchema = z.object({
id: z.string().uuid(),
name: z.string(),
workProjectSeriesSchemaId: z.string().uuid(),
workProjectSeriesSchemaName: z.string(),
workProjectSeriesSchemaShortCode: z.string(),
carouselOrdinal: z.number(),
carouselGroupId: z.string().uuid(),
carouselOptions: z.array(CarouselOptionDtoSchema),
});
export type CarouselDto = z.infer<typeof CarouselDtoSchema>;
10 changes: 10 additions & 0 deletions app/api/dtos/CarouselOrderItemDtoSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { z } from 'zod';
export const CarouselOrderItemDtoSchema = z.object({
id: z.number(),
carouselOrderId: z.string().uuid(),
workProjectSeriesSchemaId: z.string().uuid(),
preferencePosition: z.number(),
active: z.boolean(),
carouselOptionId: z.number(),
});
export type CarouselOrderItemDto = z.infer<typeof CarouselOrderItemDtoSchema>;
Loading

0 comments on commit eb53b76

Please sign in to comment.