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

Add sorting options [gh-1138] #1148

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
1 change: 0 additions & 1 deletion api/controller/expenditures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ export async function getExpenditures(request: IRequest, response: Response, nex
currentUserId: request.currentUser.id
});
await checkDto(getExpendituresDto);

const expenditures = await getExpendituresAsync(getExpendituresDto);
if (expenditures.csv) {
response.type('text/csv');
Expand Down
5 changes: 4 additions & 1 deletion api/models/entity/Contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,12 @@ export async function getContributionsByGovernmentIdAsync(
}
};
if (sort) {
if (!['date', 'status', 'campaignId', 'matchAmount', 'amount'].includes(sort.field)) {
if (!['date', 'status', 'campaignId', 'matchAmount', 'amount', 'oaeType', 'name'].includes(sort.field)) {
throw new Error('Sort.field must be one of date, status, matchAmount, amount or campaignid');
}
if (sort.field === 'campaignId') {
sort.field = 'id';
}

if (!['ASC', 'DESC'].includes(sort.direction)) {
throw new Error('Sort.direction must be one of ASC or DESC');
Expand Down
2 changes: 1 addition & 1 deletion api/models/entity/Expenditure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export async function getExpendituresByGovernmentIdAsync(
};

if (sort) {
if (!['date', 'status', 'campaignId'].includes(sort.field)) {
if (!['date', 'status', 'campaignId', 'amount'].includes(sort.field)) {
throw new Error('Sort.field must be one of date, status or campaignId');
}

Expand Down
30 changes: 17 additions & 13 deletions api/services/contributionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export interface IGetContributionOptions {
from?: string;
to?: string;
sort?: {
field: 'campaignId' | 'status' | 'date';
field: 'campaignId' | 'status' | 'date' | 'id';
direction: 'ASC' | 'DESC';
};
format?: 'json' | 'csv' | 'geoJson' | 'xml';
Expand Down Expand Up @@ -580,18 +580,22 @@ export async function getMatchResultAsync(attrs: GetMatchResultAttrs): Promise<M

const hasPermissions = await isGovernmentAdminAsync(attrs.currentUserId, contribution.government.id);
if (hasPermissions) {
const matchResults: MatchResults = {
matchId: contribution.matchId,
matchStrength: contribution.matchStrength,
results: {
exact: contribution.matchResult.exact,
strong: contribution.matchResult.strong,
weak: contribution.matchResult.weak,
none: crypto.randomBytes(16).toString('hex')
},
inPortland: contribution.matchResult.donor_info.eligible_address
};
return matchResults;
if (contribution.matchId && contribution.matchResult) {
const matchResults: MatchResults = {
matchId: contribution.matchId,
matchStrength: contribution.matchStrength,
results: {
exact: contribution.matchResult.exact,
strong: contribution.matchResult.strong,
weak: contribution.matchResult.weak,
none: crypto.randomBytes(16).toString('hex')
},
inPortland: contribution.matchResult.donor_info.eligible_address
};
return matchResults;
} else {
throw new Error('No match result for contribution');
}
} else {
throw new Error('User does not have permissions');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const columns = isGovAdmin => {
{
field: 'name',
title: 'Name',
sorting: false,
render: rowData => {
if (
rowData.contributorType === 'individual' ||
Expand All @@ -84,7 +83,6 @@ const columns = isGovAdmin => {
{
field: 'amount',
title: 'Contribution Amount',
sorting: false,
type: 'currency',
},
{
Expand All @@ -104,7 +102,6 @@ const columns = isGovAdmin => {
cols.splice(1, 0, {
field: 'campaignId',
title: 'Campaign',
sorting: false,
render: rowData => {
return rowData && rowData.campaign
? rowData.campaign.name
Expand Down
3 changes: 0 additions & 3 deletions app/src/Pages/Portal/Expenses/ExpensesTable/ExpensesTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,11 @@ const columns = isGovAdmin => [
{
field: 'name',
title: 'Name',
sorting: false,
},
{
field: 'amount',
title: 'Amount',
type: 'currency',
sorting: false,
},
{
field: 'paymentMethod',
Expand All @@ -95,7 +93,6 @@ const columns = isGovAdmin => [
? rowData.paymentMethod.replace(/_/g, ' ')
: '';
},
sorting: false,
},
{
field: 'status',
Expand Down
26 changes: 24 additions & 2 deletions app/src/components/ContributorMatchPicker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,30 @@ const Header = props => {
}
// Switch color and symbol based on matchStrength
const matchIcon = getMatchIcon(matchStrength, inPortland);

if (!currentMatchId) {
return (
<h3 css={[sectionStyles.title]}>
Contributor {matchIcon}{' '}
{currentMatchId && (
<span
style={{
fontSize: '.7em',
textTransform: 'capitalize',
display: 'inline-block',
verticalAlign: '5px',
}}
>
{matchSelectedText}
</span>
)}
</h3>
);
}
return (
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
<h3
css={[sectionStyles.title, { cursor: 'pointer' }]}
// Data will auto propigate to props of modal so
onClick={() =>
showModal({
component: 'MatchPickerForm',
Expand Down Expand Up @@ -148,7 +167,10 @@ class contributorMatchPicker extends React.Component {
}

render() {
const inPortland = this.props.matchObj.inPortland;
if (!this.props.matchObj) {
return <div />;
}
const inPortland = (this.props.matchObj || {}).inPortland;
const { totalPages, currentPage, pages } = this.state;
const page = !isEmpty(pages) ? pages[currentPage] : [{}];
const {
Expand Down
11 changes: 11 additions & 0 deletions app/src/state/ducks/contributions.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,17 @@ export function updateFilter(newFilterOptions) {
(filterOptions.page * filterOptions.perPage) / newFilterOptions.perPage
);
}
const existingSortField = filterOptions.sort;
const newSortField = newFilterOptions.sort;
if (
existingSortField &&
newSortField &&
existingSortField.field === newSortField.field &&
existingSortField.direction === newSortField.direction
) {
const isAsc = existingSortField.direction === 'ASC';
newFilterOptions.sort.direction = isAsc ? 'DESC' : 'ASC';
}
Object.entries(filterOptions).forEach(([key, value]) => {
if (Object.prototype.hasOwnProperty.call(newFilterOptions, key))
filterOptions[key] = newFilterOptions[key];
Expand Down
11 changes: 11 additions & 0 deletions app/src/state/ducks/expenditures.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ export function updateFilter(newFilterOptions) {
(filterOptions.page * filterOptions.perPage) / newFilterOptions.perPage
);
}
const existingSortField = filterOptions.sort;
const newSortField = newFilterOptions.sort;
if (
existingSortField &&
newSortField &&
existingSortField.field === newSortField.field &&
existingSortField.direction === newSortField.direction
) {
const isAsc = existingSortField.direction === 'ASC';
newFilterOptions.sort.direction = isAsc ? 'DESC' : 'ASC';
}
Object.entries(filterOptions).forEach(([key, value]) => {
if (Object.prototype.hasOwnProperty.call(newFilterOptions, key))
filterOptions[key] = newFilterOptions[key];
Expand Down
4 changes: 2 additions & 2 deletions app/src/state/ducks/matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const getCurrentContributionMatch = state => {
export const getCurrentMatchResults = state => {
const currentMatches = getCurrentContributionMatch(state);
const matches = [];
const results = currentMatches.results;
const results = (currentMatches || {}).results;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly to satiate my curiosity, why is this like this?

const currentContribution = getCurrentContribution(state);
let match = {};
let selectedMatchId = '';
Expand Down Expand Up @@ -172,7 +172,7 @@ export const getCurrentMatchResults = state => {
}
}
}
if (currentMatches.matchStrength !== 'exact') {
if (results && (currentMatches || {}).matchStrength !== 'exact') {
if (currentContribution.matchStrength === 'none') {
matches.unshift({
id: results.none,
Expand Down
2 changes: 2 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ set -e

export PATH=$PATH:$HOME/.local/bin

echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like something that is used for testing and should be removed?


sh scripts/api-test.sh
sh scripts/app-test.sh