Skip to content

Commit

Permalink
feat(3167): enhance search, filter, and sort on private cloud pages
Browse files Browse the repository at this point in the history
  • Loading branch information
junminahn committed Sep 25, 2024
1 parent 5feec87 commit f03a703
Show file tree
Hide file tree
Showing 26 changed files with 350 additions and 627 deletions.
42 changes: 0 additions & 42 deletions app/app/api/public-cloud/products/_operations/search.ts

This file was deleted.

12 changes: 6 additions & 6 deletions app/app/api/public-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, Provider, TaskType, TaskStatus } from '@prisma/client';
import { DecisionStatus, Ministry, Provider, TaskType, TaskStatus, ProjectStatus } from '@prisma/client';
import { parse } from 'csv-parse/sync';
import prisma from '@/core/prisma';
import { createSamplePublicCloudProductData } from '@/helpers/mock-resources';
Expand Down Expand Up @@ -311,9 +311,9 @@ describe('Download Public Cloud Products - Validations', () => {
await mockSessionByRole('admin');

const res1 = await downloadPublicCloudProjects({
ministry: Ministry.AEST,
provider: Provider.AWS,
includeInactive: false,
ministries: [Ministry.AEST],
providers: [Provider.AWS],
status: [ProjectStatus.ACTIVE],
});

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

const res1 = await downloadPublicCloudProjects({
provider: 'INVALID' as Provider,
providers: ['INVALID' as Provider],
});

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

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

expect(res1.status).toBe(400);
Expand Down
16 changes: 6 additions & 10 deletions app/app/api/public-cloud/products/download/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,24 @@ import { NoContent, CsvResponse } from '@/core/responses';
import { ministryKeyToName } from '@/helpers/product';
import { formatFullName } from '@/helpers/user';
import { createEvent } from '@/mutations/events';
import { searchPublicCloudProducts } from '@/queries/public-cloud-products';
import { formatDateSimple } from '@/utils/date';
import { publicCloudProductSearchNoPaginationBodySchema } from '@/validation-schemas/public-cloud';
import searchOp from '../_operations/search';

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

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

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

if (docs.length === 0) {
return NoContent();
Expand Down
8 changes: 4 additions & 4 deletions app/app/api/public-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, Provider, TaskType, TaskStatus } from '@prisma/client';
import { DecisionStatus, Ministry, Provider, TaskType, TaskStatus, ProjectStatus } from '@prisma/client';
import prisma from '@/core/prisma';
import { createSamplePublicCloudProductData } from '@/helpers/mock-resources';
import { mockNoRoleUsers, findMockUserByIdr, findOtherMockUsers } from '@/helpers/mock-users';
Expand Down Expand Up @@ -257,9 +257,9 @@ describe('Search Public Cloud Products - Validations', () => {
await mockSessionByRole('admin');

const res1 = await searchPublicCloudProjects({
ministry: Ministry.AEST,
provider: Provider.AWS,
includeInactive: false,
ministries: [Ministry.AEST],
providers: [Provider.AWS],
status: [ProjectStatus.ACTIVE],
});

expect(res1.status).toBe(200);
Expand Down
18 changes: 4 additions & 14 deletions app/app/api/public-cloud/products/search/route.ts
Original file line number Diff line number Diff line change
@@ -1,27 +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 { searchPublicCloudProducts } from '@/queries/public-cloud-products';
import { publicCloudProductSearchBodySchema } from '@/validation-schemas/public-cloud';
import searchOp from '../_operations/search';

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

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

return OkResponse(data);
return OkResponse({ docs, totalCount });
});
45 changes: 0 additions & 45 deletions app/app/api/public-cloud/requests/_operations/search.ts

This file was deleted.

29 changes: 14 additions & 15 deletions app/app/api/public-cloud/requests/search/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('Search Public Cloud Requests - Permissions', () => {
it('should successfully search 1 request by PO', async () => {
await mockSessionByEmail(PO.email);

const res1 = await searchPublicCloudRequests({ includeInactive: true });
const res1 = await searchPublicCloudRequests({});
expect(res1.status).toBe(200);
const dat1 = await res1.json();

Expand All @@ -117,7 +117,7 @@ describe('Search Public Cloud Requests - Permissions', () => {
it('should successfully search 1 request by TL1', async () => {
await mockSessionByEmail(TL1.email);

const res1 = await searchPublicCloudRequests({ includeInactive: true });
const res1 = await searchPublicCloudRequests({});
expect(res1.status).toBe(200);
const dat1 = await res1.json();

Expand All @@ -127,7 +127,7 @@ describe('Search Public Cloud Requests - Permissions', () => {
it('should successfully search 1 request by TL2', async () => {
await mockSessionByEmail(TL2.email);

const res1 = await searchPublicCloudRequests({ includeInactive: true });
const res1 = await searchPublicCloudRequests({});
expect(res1.status).toBe(200);
const dat1 = await res1.json();

Expand Down Expand Up @@ -160,7 +160,7 @@ describe('Search Public Cloud Requests - Permissions', () => {
it('should successfully search 1 request by the random user', async () => {
await mockSessionByEmail(RANDOM1.email);

const res1 = await searchPublicCloudRequests({ includeInactive: true });
const res1 = await searchPublicCloudRequests({});
expect(res1.status).toBe(200);
const dat1 = await res1.json();

Expand All @@ -170,7 +170,7 @@ describe('Search Public Cloud Requests - Permissions', () => {
it('should successfully search 1 request by PO', async () => {
await mockSessionByEmail(PO.email);

const res1 = await searchPublicCloudRequests({ includeInactive: true });
const res1 = await searchPublicCloudRequests({});
expect(res1.status).toBe(200);
const dat1 = await res1.json();

Expand All @@ -180,7 +180,7 @@ describe('Search Public Cloud Requests - Permissions', () => {
it('should successfully search 1 request by TL1', async () => {
await mockSessionByEmail(TL1.email);

const res1 = await searchPublicCloudRequests({ includeInactive: true });
const res1 = await searchPublicCloudRequests({});
expect(res1.status).toBe(200);
const dat1 = await res1.json();

Expand All @@ -190,7 +190,7 @@ describe('Search Public Cloud Requests - Permissions', () => {
it('should successfully search 1 request by TL2', async () => {
await mockSessionByEmail(TL2.email);

const res1 = await searchPublicCloudRequests({ includeInactive: true });
const res1 = await searchPublicCloudRequests({});
expect(res1.status).toBe(200);
const dat1 = await res1.json();

Expand All @@ -200,7 +200,7 @@ describe('Search Public Cloud Requests - Permissions', () => {
it('should successfully search 2 requests by admin', async () => {
await mockSessionByRole('admin');

const res1 = await searchPublicCloudRequests({ includeInactive: true });
const res1 = await searchPublicCloudRequests({});
expect(res1.status).toBe(200);
const dat1 = await res1.json();

Expand Down Expand Up @@ -298,20 +298,21 @@ describe('Search Public Cloud Requests - Validations', () => {
expect(res.status).toBe(200);
});

it('should successfully search 10 requests by admin', async () => {
it('should successfully search 11 requests by admin', async () => {
await mockSessionByRole('admin');

const res1 = await searchPublicCloudRequests({ includeInactive: true });
const res1 = await searchPublicCloudRequests({});
expect(res1.status).toBe(200);
const dat1 = await res1.json();

// 10 Create requests, 1 Edit request
expect(dat1.totalCount).toBe(11);
});

it('should successfully search 1 requests by admin', async () => {
await mockSessionByRole('admin');

const res1 = await searchPublicCloudRequests({});
const res1 = await searchPublicCloudRequests({ status: [DecisionStatus.APPROVED] });
expect(res1.status).toBe(200);
const dat1 = await res1.json();

Expand All @@ -322,9 +323,8 @@ describe('Search Public Cloud Requests - Validations', () => {
await mockSessionByRole('admin');

const res1 = await searchPublicCloudRequests({
ministry: Ministry.AEST,
provider: Provider.AWS,
includeInactive: true,
ministries: [Ministry.AEST],
providers: [Provider.AWS],
});

expect(res1.status).toBe(200);
Expand All @@ -338,7 +338,6 @@ describe('Search Public Cloud Requests - Validations', () => {

const res1 = await searchPublicCloudRequests({
search: '______name______',
includeInactive: true,
});

expect(res1.status).toBe(200);
Expand Down
28 changes: 4 additions & 24 deletions app/app/api/public-cloud/requests/search/route.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@
import _isString from 'lodash-es/isString';
import createApiHandler from '@/core/api-handler';
import { OkResponse } from '@/core/responses';
import { searchPublicCloudRequests } from '@/queries/public-cloud-requests';
import { publicCloudRequestSearchBodySchema } from '@/validation-schemas/public-cloud';
import searchOp from '../_operations/search';

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

const data = await searchOp({
licencePlate,
const { docs, totalCount } = await searchPublicCloudRequests({
session,
search,
page,
pageSize,
ministry,
provider,
includeInactive,
sortKey: sortKey || undefined,
sortOrder,
...body,
});

return OkResponse(data);
return OkResponse({ docs, totalCount });
});
6 changes: 3 additions & 3 deletions app/app/api/v1/public-cloud/products/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export const GET = apiHandler(async ({ queryParams, session }) => {
session: session as Session,
skip,
take,
ministry,
provider,
status,
ministries: ministry ? [ministry] : [],
providers: provider ? [provider] : [],
status: status ? [status] : [],
});

const data = docs.map((doc) => {
Expand Down
Loading

0 comments on commit f03a703

Please sign in to comment.