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

Test more things and refactoring #288

Merged
merged 11 commits into from
May 6, 2021
Merged
491 changes: 491 additions & 0 deletions api/src/paths/draft.test.ts

Large diffs are not rendered by default.

113 changes: 66 additions & 47 deletions api/src/paths/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { logRequest } from '../utils/path-utils';

const defaultLog = getLogger('paths/draft');

export const PUT: Operation = [logRequest('paths/draft', 'PUT'), updateDraft()];
export const POST: Operation = [logRequest('paths/draft', 'POST'), createDraft()];

const postPutResponses = {
Expand Down Expand Up @@ -76,12 +77,52 @@ POST.apiDoc = {
}
};

PUT.apiDoc = {
description: 'Update a Draft.',
tags: ['draft'],
security: [
{
Bearer: [SYSTEM_ROLE.SYSTEM_ADMIN, SYSTEM_ROLE.PROJECT_ADMIN]
}
],
requestBody: {
description: 'Draft put request object.',
content: {
'application/json': {
schema: {
title: 'Draft request object',
type: 'object',
required: ['name', 'data'],
properties: {
id: {
title: 'Draft record ID',
type: 'number'
},
name: {
title: 'Draft name',
type: 'string'
},
data: {
title: 'Draft json data',
type: 'object',
properties: {}
}
}
}
}
}
},
responses: {
...postPutResponses
}
};

/**
* Creates a new draft record.
*
* @returns {RequestHandler}
*/
function createDraft(): RequestHandler {
export function createDraft(): RequestHandler {
return async (req, res) => {
const connection = getDBConnection(req['keycloak_token']);

Expand All @@ -94,7 +135,15 @@ function createDraft(): RequestHandler {
throw new HTTP400('Failed to identify system user ID');
}

const postDraftSQLStatement = postDraftSQL(systemUserId, req?.body?.name, req?.body?.data);
if (!req.body.name) {
throw new HTTP400('Missing required param name');
}

if (!req.body.data) {
throw new HTTP400('Missing required param data');
}

const postDraftSQLStatement = postDraftSQL(systemUserId, req.body.name, req.body.data);

if (!postDraftSQLStatement) {
throw new HTTP400('Failed to build SQL insert statement');
Expand All @@ -121,62 +170,32 @@ function createDraft(): RequestHandler {
};
}

export const PUT: Operation = [logRequest('paths/draft', 'PUT'), updateDraft()];

PUT.apiDoc = {
description: 'Update a Draft.',
tags: ['draft'],
security: [
{
Bearer: [SYSTEM_ROLE.SYSTEM_ADMIN, SYSTEM_ROLE.PROJECT_ADMIN]
}
],
requestBody: {
description: 'Draft put request object.',
content: {
'application/json': {
schema: {
title: 'Draft request object',
type: 'object',
required: ['name', 'data'],
properties: {
id: {
title: 'Draft record ID',
type: 'number'
},
name: {
title: 'Draft name',
type: 'string'
},
data: {
title: 'Draft json data',
type: 'object',
properties: {}
}
}
}
}
}
},
responses: {
...postPutResponses
}
};

/**
* Updates an existing draft record.
*
* @returns {RequestHandler}
*/
function updateDraft(): RequestHandler {
export function updateDraft(): RequestHandler {
return async (req, res) => {
const connection = getDBConnection(req['keycloak_token']);

try {
const putDraftSQLStatement = putDraftSQL(req?.body?.id, req?.body?.name, req?.body?.data);
if (!req.body.id) {
throw new HTTP400('Missing required param id');
}

if (!req.body.name) {
throw new HTTP400('Missing required param name');
}

if (!req.body.data) {
throw new HTTP400('Missing required param data');
}

const putDraftSQLStatement = putDraftSQL(req.body.id, req.body.name, req.body.data);

if (!putDraftSQLStatement) {
throw new HTTP400('Failed to build SQL insert statement');
throw new HTTP400('Failed to build SQL update statement');
}

await connection.open();
Expand Down
4 changes: 2 additions & 2 deletions api/src/queries/users/system-role-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('postSystemRolesSQL', () => {
});

it('returns non null response when valid parameters provided', () => {
const response = postSystemRolesSQL(1, [1]);
const response = postSystemRolesSQL(1, [1, 2]);

expect(response).to.not.be.null;
});
Expand All @@ -48,7 +48,7 @@ describe('deleteSystemRolesSQL', () => {
});

it('returns non null response when valid parameters provided', () => {
const response = deleteSystemRolesSQL(1, [1]);
const response = deleteSystemRolesSQL(1, [1, 2]);

expect(response).to.not.be.null;
});
Expand Down
16 changes: 5 additions & 11 deletions app/src/components/attachments/AttachmentsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useBiohubApi } from 'hooks/useBioHubApi';
import { IGetProjectAttachment } from 'interfaces/useProjectApi.interface';
import React, { useState } from 'react';
import { getFormattedDate } from 'utils/Utils';
import { handleChangeRowsPerPage, handleChangePage } from 'utils/tablePaginationUtils';

const useStyles = makeStyles({
table: {
Expand Down Expand Up @@ -51,15 +52,6 @@ const AttachmentsList: React.FC<IAttachmentsListProps> = (props) => {
size: (null as unknown) as number
});

const handleChangePage = (event: unknown, newPage: number) => {
setPage(newPage);
};

const handleChangeRowsPerPage = (event: React.ChangeEvent<HTMLInputElement>) => {
setRowsPerPage(parseInt(event.target.value, 10));
setPage(0);
};

const deleteAttachment = async () => {
if (!attachmentToDelete || !attachmentToDelete.id) {
return;
Expand Down Expand Up @@ -158,8 +150,10 @@ const AttachmentsList: React.FC<IAttachmentsListProps> = (props) => {
count={props.attachmentsList.length}
rowsPerPage={rowsPerPage}
page={page}
onChangePage={handleChangePage}
onChangeRowsPerPage={handleChangeRowsPerPage}
onChangePage={(event: unknown, newPage: number) => handleChangePage(event, newPage, setPage)}
onChangeRowsPerPage={(event: React.ChangeEvent<HTMLInputElement>) =>
handleChangeRowsPerPage(event, setPage, setRowsPerPage)
}
/>
)}
</Paper>
Expand Down
4 changes: 2 additions & 2 deletions app/src/components/surveys/SurveysList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('SurveysList', () => {

const { getByText, queryByText } = render(
<Router history={history}>
<SurveysList surveysList={surveysList} />
<SurveysList projectId={1} surveysList={surveysList} />
</Router>
);

Expand All @@ -42,7 +42,7 @@ describe('SurveysList', () => {
it('renders correctly with no surveys', () => {
const { getByText } = render(
<Router history={history}>
<SurveysList surveysList={[]} />
<SurveysList projectId={1} surveysList={[]} />
</Router>
);

Expand Down
16 changes: 5 additions & 11 deletions app/src/components/surveys/SurveysList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { IGetProjectSurvey } from 'interfaces/useProjectApi.interface';
import React, { useState } from 'react';
import { DATE_FORMAT } from 'constants/dateFormats';
import { getFormattedDateRangeString } from 'utils/Utils';
import { handleChangeRowsPerPage, handleChangePage } from 'utils/tablePaginationUtils';
import { useHistory } from 'react-router';

const useStyles = makeStyles((theme: Theme) => ({
Expand Down Expand Up @@ -53,15 +54,6 @@ const SurveysList: React.FC<ISurveysListProps> = (props) => {
const [rowsPerPage, setRowsPerPage] = useState(5);
const [page, setPage] = useState(0);

const handleChangePage = (event: unknown, newPage: number) => {
setPage(newPage);
};

const handleChangeRowsPerPage = (event: React.ChangeEvent<HTMLInputElement>) => {
setRowsPerPage(parseInt(event.target.value, 10));
setPage(0);
};

const getChipIcon = (status_name: string) => {
let chipLabel;
let chipStatusClass;
Expand Down Expand Up @@ -126,8 +118,10 @@ const SurveysList: React.FC<ISurveysListProps> = (props) => {
count={props.surveysList.length}
rowsPerPage={rowsPerPage}
page={page}
onChangePage={handleChangePage}
onChangeRowsPerPage={handleChangeRowsPerPage}
onChangePage={(event: unknown, newPage: number) => handleChangePage(event, newPage, setPage)}
onChangeRowsPerPage={(event: React.ChangeEvent<HTMLInputElement>) =>
handleChangeRowsPerPage(event, setPage, setRowsPerPage)
}
/>
)}
</Paper>
Expand Down
16 changes: 5 additions & 11 deletions app/src/features/admin/users/ActiveUsersList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import TableRow from '@material-ui/core/TableRow';
import Typography from '@material-ui/core/Typography';
import { IGetUserResponse } from 'interfaces/useUserApi.interface';
import React, { useState } from 'react';
import { handleChangeRowsPerPage, handleChangePage } from 'utils/tablePaginationUtils';

export interface IActiveUsersListProps {
activeUsers: IGetUserResponse[];
Expand All @@ -27,15 +28,6 @@ const ActiveUsersList: React.FC<IActiveUsersListProps> = (props) => {
const [rowsPerPage, setRowsPerPage] = useState(5);
const [page, setPage] = useState(0);

const handleChangePage = (event: unknown, newPage: number) => {
setPage(newPage);
};

const handleChangeRowsPerPage = (event: React.ChangeEvent<HTMLInputElement>) => {
setRowsPerPage(parseInt(event.target.value, 10));
setPage(0);
};

return (
<Paper>
<Box p={2}>
Expand Down Expand Up @@ -82,8 +74,10 @@ const ActiveUsersList: React.FC<IActiveUsersListProps> = (props) => {
count={activeUsers.length}
rowsPerPage={rowsPerPage}
page={page}
onChangePage={handleChangePage}
onChangeRowsPerPage={handleChangeRowsPerPage}
onChangePage={(event: unknown, newPage: number) => handleChangePage(event, newPage, setPage)}
onChangeRowsPerPage={(event: React.ChangeEvent<HTMLInputElement>) =>
handleChangeRowsPerPage(event, setPage, setRowsPerPage)
}
/>
)}
</Paper>
Expand Down
12 changes: 12 additions & 0 deletions app/src/utils/tablePaginationUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const handleChangePage = (event: unknown, newPage: number, setPage: (page: number) => void) => {
setPage(newPage);
};

export const handleChangeRowsPerPage = (
event: React.ChangeEvent<HTMLInputElement>,
setPage: (page: number) => void,
setRowsPerPage: (rowsPerPage: number) => void
) => {
setRowsPerPage(parseInt(event.target.value, 10));
setPage(0);
};