Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(3165): enhance search, filter, and sort on private products #3833

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions app/app/api/private-cloud/products/_operations/search.ts

This file was deleted.

12 changes: 6 additions & 6 deletions app/app/api/private-cloud/products/download/route.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@jest/globals';
import { DecisionStatus, Ministry, Cluster } from '@prisma/client';
import { DecisionStatus, Ministry, Cluster, ProjectStatus } from '@prisma/client';
import { parse } from 'csv-parse/sync';
import prisma from '@/core/prisma';
import { createSamplePrivateCloudProductData } from '@/helpers/mock-resources';
Expand Down Expand Up @@ -289,9 +289,9 @@ describe('Download Private Cloud Products - Validations', () => {
await mockSessionByRole('admin');

const res1 = await downloadPrivateCloudProjects({
ministry: Ministry.AEST,
cluster: Cluster.CLAB,
includeInactive: false,
ministries: [Ministry.AEST],
clusters: [Cluster.CLAB],
status: [ProjectStatus.ACTIVE],
});

expect(res1.status).toBe(200);
Expand Down Expand Up @@ -331,7 +331,7 @@ describe('Download Private Cloud Products - Validations', () => {
await mockSessionByRole('admin');

const res1 = await downloadPrivateCloudProjects({
cluster: 'INVALID' as Cluster,
clusters: ['INVALID' as Cluster],
});

expect(res1.status).toBe(400);
Expand All @@ -341,7 +341,7 @@ describe('Download Private Cloud Products - Validations', () => {
await mockSessionByRole('admin');

const res1 = await downloadPrivateCloudProjects({
ministry: 'INVALID' as Ministry,
ministries: ['INVALID' as Ministry],
});

expect(res1.status).toBe(400);
Expand Down
17 changes: 6 additions & 11 deletions app/app/api/private-cloud/products/download/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,25 @@ import { NoContent, CsvResponse } from '@/core/responses';
import { ministryKeyToName, getTotalQuotaStr } from '@/helpers/product';
import { formatFullName } from '@/helpers/user';
import { createEvent } from '@/mutations/events';
import { searchPrivateCloudProducts } from '@/queries/private-cloud-products';
import { PrivateProductCsvRecord } from '@/types/csv';
import { formatDateSimple } from '@/utils/date';
import { privateCloudProductSearchNoPaginationBodySchema } from '@/validation-schemas/private-cloud';
import searchOp from '../_operations/search';

export const POST = createApiHandler({
roles: ['user'],
validations: { body: privateCloudProductSearchNoPaginationBodySchema },
})(async ({ session, body }) => {
const { search = '', ministry, cluster, includeInactive = false, showTest = false, sortKey, sortOrder } = body;

const searchProps = {
search,
page: 1,
pageSize: 10000,
ministry,
cluster,
status: includeInactive ? undefined : ProjectStatus.ACTIVE,
sortKey: sortKey || undefined,
sortOrder,
isTest: showTest,
...body,
};

const { docs, totalCount } = await searchOp({ ...searchProps, session });
const { docs, totalCount } = await searchPrivateCloudProducts({
session,
...searchProps,
});

if (docs.length === 0) {
return NoContent();
Expand Down
8 changes: 4 additions & 4 deletions app/app/api/private-cloud/products/search/route.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@jest/globals';
import { DecisionStatus, Ministry, Cluster } from '@prisma/client';
import { DecisionStatus, Ministry, Cluster, ProjectStatus } from '@prisma/client';
import prisma from '@/core/prisma';
import { createSamplePrivateCloudProductData } from '@/helpers/mock-resources';
import { mockNoRoleUsers, findMockUserByIdr, findOtherMockUsers } from '@/helpers/mock-users';
Expand Down Expand Up @@ -211,9 +211,9 @@ describe('Search Private Cloud Products - Validations', () => {
await mockSessionByRole('admin');

const res1 = await searchPrivateCloudProjects({
ministry: Ministry.AEST,
cluster: Cluster.CLAB,
includeInactive: false,
ministries: [Ministry.AEST],
clusters: [Cluster.CLAB],
status: [ProjectStatus.ACTIVE],
});

expect(res1.status).toBe(200);
Expand Down
29 changes: 4 additions & 25 deletions app/app/api/private-cloud/products/search/route.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
import { ProjectStatus } from '@prisma/client';
import _isString from 'lodash-es/isString';
import createApiHandler from '@/core/api-handler';
import { OkResponse } from '@/core/responses';
import { searchPrivateCloudProducts } from '@/queries/private-cloud-products';
import { privateCloudProductSearchBodySchema } from '@/validation-schemas/private-cloud';
import searchOp from '../_operations/search';

export const POST = createApiHandler({
roles: ['user'],
validations: { body: privateCloudProductSearchBodySchema },
})(async ({ session, body }) => {
const {
search = '',
page = 1,
pageSize = 5,
ministry,
cluster,
includeInactive = false,
sortKey,
sortOrder,
showTest,
} = body;

const data = await searchOp({
const { docs, totalCount } = await searchPrivateCloudProducts({
session,
search,
page,
pageSize,
ministry,
cluster,
status: includeInactive ? undefined : ProjectStatus.ACTIVE,
sortKey: sortKey || undefined,
sortOrder,
isTest: showTest,
...body,
});

return OkResponse(data);
return OkResponse({ docs, totalCount });
});
11 changes: 5 additions & 6 deletions app/app/api/v1/private-cloud/products/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ProjectStatus, Ministry, Cluster } from '@prisma/client';
import { Session } from 'next-auth';
import { z } from 'zod';
import createApiHandler from '@/core/api-handler';
import { OkResponse, BadRequestResponse } from '@/core/responses';
Expand Down Expand Up @@ -33,13 +32,13 @@ export const GET = apiHandler(async ({ queryParams, session }) => {
const { skip, take, page } = parsePaginationParams(_page ?? defaultPage, _pageSize ?? defaultPageSize, 10);

const { docs, totalCount } = await searchPrivateCloudProducts({
session: session as Session,
session,
skip,
take,
ministry,
cluster,
status,
isTest: false,
ministries: ministry ? [ministry] : [],
clusters: cluster ? [cluster] : [],
status: status ? [status] : [],
temporary: [],
});

const data = docs.map((doc) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Prisma } from '@prisma/client';
import { Cluster, Ministry, Prisma } from '@prisma/client';
import { useEffect, useRef, useState } from 'react';
import { useSnapshot, subscribe } from 'valtio';
import FormToggle from '@/components/generic/checkbox/FormToggle';
import FormMultiSelect from '@/components/generic/select/FormMultiSelect';
import FormSelect from '@/components/generic/select/FormSelect';
import { clusters, productSorts, ministryOptions } from '@/constants';
import { pageState } from './state';
Expand Down Expand Up @@ -33,11 +34,11 @@ export default function FilterPanel() {
};

const handleClusterChange = (value: string) => {
pageState.cluster = value;
pageState.cluster = value as Cluster;
};

const handleMinistryChange = (value: string) => {
pageState.ministry = value;
pageState.ministry = value as Ministry;
};

const clearFilters = () => {
Expand All @@ -51,10 +52,10 @@ export default function FilterPanel() {
sortRef.current.value = '';
}

pageState.cluster = '';
pageState.ministry = '';
pageState.cluster = undefined;
pageState.ministry = undefined;
pageState.sortKey = '';
pageState.sortOrder = '';
pageState.sortOrder = Prisma.SortOrder.asc;
pageState.includeInactive = false;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export default privateCloudProductRequests(({ pathParams, queryParams, session }
return (
<Table
totalCount={totalCount}
page={snap.page}
pageSize={snap.pageSize}
page={snap.page ?? 1}
pageSize={snap.pageSize ?? 10}
search={snap.search}
onPagination={(page: number, pageSize: number) => {
pageState.page = page;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Prisma } from '@prisma/client';
import { Prisma, ProjectStatus } from '@prisma/client';
import { proxy, useSnapshot } from 'valtio';
import { PrivateCloudProductSearchCriteria } from '@/services/backend/private-cloud/products';
import { productSorts } from '@/constants';
import { PrivateCloudRequestSearchBody } from '@/validation-schemas/private-cloud';

export const pageState = proxy<Omit<PrivateCloudProductSearchCriteria, 'licencePlate'>>({
export const pageState = proxy<PrivateCloudRequestSearchBody>({
search: '',
page: 1,
pageSize: 10,
ministry: '',
cluster: '',
ministry: undefined,
cluster: undefined,
includeInactive: true,
sortKey: '',
sortOrder: Prisma.SortOrder.desc,
showTest: false,
showTest: true,
sortKey: productSorts[0].sortKey,
sortOrder: productSorts[0].sortOrder,
});
Loading
Loading