Skip to content

Commit

Permalink
Update 'amountDollars' currency formatting and associated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
danetsao committed Feb 15, 2024
1 parent f07ac99 commit d2bb74f
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 157 deletions.
76 changes: 47 additions & 29 deletions src/api/dto/from/dtoToAggregateCriteria.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
import { IntlShape } from 'react-intl';
import { useStripes } from '@folio/stripes/core';
import getIntl from '../../../test/util/getIntl';
import { ComparisonOperator, CriteriaAggregate, CriteriaAggregateType } from '../../../types/CriteriaTypes';
import { BursarExportFilterAggregate } from '../types';
import dtoToAggregateCriteria from './dtoToAggregateCriteria';

test.each<[BursarExportFilterAggregate | undefined, CriteriaAggregate | undefined]>([
[undefined, undefined],
[
{
type: 'Aggregate',
property: 'NUM_ROWS',
condition: 'LESS_THAN_EQUAL',
amount: 1523,
},
{
type: CriteriaAggregateType.NUM_ROWS,
operator: ComparisonOperator.LESS_THAN_EQUAL,
amount: '1523',
},
],
[
{
type: 'Aggregate',
property: 'TOTAL_AMOUNT',
condition: 'GREATER_THAN',
amount: 1523,
},
{
type: CriteriaAggregateType.TOTAL_AMOUNT,
operator: ComparisonOperator.GREATER_THAN,
amountCurrency: '15.23',
},
],
])('dtoToAggregateCriteria(%s) === %s', (input, expected) => expect(dtoToAggregateCriteria(input)).toEqual(expected));
// United States
let intlEn: IntlShape;

beforeAll(() => {
intlEn = getIntl('en-US', 'EST');
});

jest.mock('@folio/stripes/core');

const mockedUseStripes = useStripes as jest.Mock;

describe('dtoToAggregateCriteria', () => {
const stripes = mockedUseStripes();

test.each<[BursarExportFilterAggregate | undefined, CriteriaAggregate | undefined]>([
[undefined, undefined],
[
{
type: 'Aggregate',
property: 'NUM_ROWS',
condition: 'LESS_THAN_EQUAL',
amount: 1523,
},
{
type: CriteriaAggregateType.NUM_ROWS,
operator: ComparisonOperator.LESS_THAN_EQUAL,
amount: '1523',
},
],
[
{
type: 'Aggregate',
property: 'TOTAL_AMOUNT',
condition: 'GREATER_THAN',
amount: 1523,
},
{
type: CriteriaAggregateType.TOTAL_AMOUNT,
operator: ComparisonOperator.GREATER_THAN,
amountCurrency: '15.23',
},
],
])('dtoToAggregateCriteria(%s) === %s', (input, expected) => expect(dtoToAggregateCriteria(input, stripes, intlEn)).toEqual(expected));
});
6 changes: 5 additions & 1 deletion src/api/dto/from/dtoToAggregateCriteria.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { IntlShape } from 'react-intl';
import { StripesType } from '@folio/stripes/core';
import { ComparisonOperator, CriteriaAggregate, CriteriaAggregateType } from '../../../types/CriteriaTypes';
import { BursarExportFilterAggregate } from '../types';

// inverse of ../to/aggregateCriteriaToFilterDto
export default function dtoToAggregateCriteria(
filter: BursarExportFilterAggregate | undefined,
stripes: StripesType,
intl: IntlShape,
): CriteriaAggregate | undefined {
switch (filter?.property) {
case 'NUM_ROWS':
Expand All @@ -16,7 +20,7 @@ export default function dtoToAggregateCriteria(
return {
type: CriteriaAggregateType.TOTAL_AMOUNT,
operator: filter.condition as ComparisonOperator,
amountCurrency: (filter.amount / 100).toFixed(2),
amountCurrency: intl.formatNumber(filter.amount / 100, { style: 'currency', currency: stripes.currency }),
};
default:
return undefined;
Expand Down
27 changes: 21 additions & 6 deletions src/api/dto/from/dtoToCriteria.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IntlShape } from 'react-intl';
import { useStripes } from '@folio/stripes/core';
import {
AndOrOperator,
ComparisonOperator,
Expand All @@ -10,14 +12,27 @@ import { FeeFineTypeDTO } from '../../queries/useFeeFineTypes';
import { LocationDTO } from '../../queries/useLocations';
import {
BursarExportFilterDTO,
BursarExportFilterFeeFineOwner,
BursarExportFilterFeeType,
BursarExportFilterLocation,
BursarExportFilterNegation,
} from '../types';
import dtoToCriteria from './dtoToCriteria';
import getIntl from '../../../test/util/getIntl';

// United States
let intlEn: IntlShape;

beforeAll(() => {
intlEn = getIntl('en-US', 'EST');
});

jest.mock('@folio/stripes/core');

const mockedUseStripes = useStripes as jest.Mock;

describe('DTO to criteria conversion for initial values', () => {
const stripes = mockedUseStripes();

it.each<[BursarExportFilterDTO, CriteriaGroup | CriteriaTerminal]>([
[
{ type: 'Age', condition: 'LESS_THAN', numDays: 1 },
Expand Down Expand Up @@ -48,7 +63,7 @@ describe('DTO to criteria conversion for initial values', () => {
{ type: CriteriaTerminalType.SERVICE_POINT, servicePointId: 'sp-id' },
],
[{ type: 'Pass' }, { type: CriteriaTerminalType.PASS }],
])('converts simple criteria %s to %s', (input, expected) => expect(dtoToCriteria(input, [], [])).toEqual(expected));
])('converts simple criteria %s to %s', (input, expected) => expect(dtoToCriteria(input, [], [], stripes, intlEn)).toEqual(expected));

it.each<[BursarExportFilterDTO, CriteriaGroup]>([
[
Expand All @@ -73,7 +88,7 @@ describe('DTO to criteria conversion for initial values', () => {
criteria: [{ type: CriteriaTerminalType.PATRON_GROUP, patronGroupId: 'pg-id' }],
},
],
])('converts condition %s to %s', (input, expected) => expect(dtoToCriteria(input, [], [])).toEqual(expected));
])('converts condition %s to %s', (input, expected) => expect(dtoToCriteria(input, [], [], stripes, intlEn)).toEqual(expected));

it.each<[BursarExportFilterNegation, CriteriaGroup]>([
[
Expand Down Expand Up @@ -124,7 +139,7 @@ describe('DTO to criteria conversion for initial values', () => {
criteria: [{ type: CriteriaTerminalType.PATRON_GROUP, patronGroupId: 'pg-id' }],
},
],
])('converts negation %s to %s', (input, expected) => expect(dtoToCriteria(input, [], [])).toEqual(expected));
])('converts negation %s to %s', (input, expected) => expect(dtoToCriteria(input, [], [], stripes, intlEn)).toEqual(expected));

it.each<[BursarExportFilterFeeType, FeeFineTypeDTO[], CriteriaTerminal]>([
[
Expand Down Expand Up @@ -155,7 +170,7 @@ describe('DTO to criteria conversion for initial values', () => {
},
],
])('converts fee type %s with known types %s to %s', (input, feeFineTypes, expected) =>
expect(dtoToCriteria(input, feeFineTypes, [])).toEqual(expected),
expect(dtoToCriteria(input, feeFineTypes, [], stripes, intlEn)).toEqual(expected),

Check failure on line 173 in src/api/dto/from/dtoToCriteria.test.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected no linebreak before this expression

Check failure on line 173 in src/api/dto/from/dtoToCriteria.test.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected no linebreak before this expression
);

Check failure on line 174 in src/api/dto/from/dtoToCriteria.test.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected newline before ')'

Check failure on line 174 in src/api/dto/from/dtoToCriteria.test.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected newline before ')'

it.each<[BursarExportFilterLocation, LocationDTO[], CriteriaTerminal]>([
Expand Down Expand Up @@ -194,6 +209,6 @@ describe('DTO to criteria conversion for initial values', () => {
},
],
])('converts location %s with known locations %s to %s', (input, locations, expected) =>
expect(dtoToCriteria(input, [], locations)).toEqual(expected),
expect(dtoToCriteria(input, [], locations, stripes, intlEn)).toEqual(expected),

Check failure on line 212 in src/api/dto/from/dtoToCriteria.test.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected no linebreak before this expression

Check failure on line 212 in src/api/dto/from/dtoToCriteria.test.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected no linebreak before this expression
);

Check failure on line 213 in src/api/dto/from/dtoToCriteria.test.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected newline before ')'

Check failure on line 213 in src/api/dto/from/dtoToCriteria.test.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected newline before ')'
});
22 changes: 15 additions & 7 deletions src/api/dto/from/dtoToCriteria.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { StripesType } from '@folio/stripes/core';
import { IntlShape } from 'react-intl';
import {
AndOrOperator,
ComparisonOperator,
Expand All @@ -15,6 +17,8 @@ export default function dtoToCriteria(
filter: BursarExportFilterDTO,
feeFineTypes: FeeFineTypeDTO[],
locations: LocationDTO[],
stripes: StripesType,
intl: IntlShape,
): CriteriaGroup | CriteriaTerminal {
switch (filter.type) {
case CriteriaTerminalType.AGE:
Expand All @@ -27,7 +31,7 @@ export default function dtoToCriteria(
return {
type: CriteriaTerminalType.AMOUNT,
operator: filter.condition as ComparisonOperator,
amountCurrency: (filter.amount / 100).toFixed(2),
amountCurrency: intl.formatNumber(filter.amount / 100, { style: 'currency', currency: stripes.currency }),
};
case CriteriaTerminalType.FEE_FINE_OWNER:
return {
Expand Down Expand Up @@ -58,10 +62,10 @@ export default function dtoToCriteria(
};

case 'Condition':
return dtoToConditionCriteria(filter, feeFineTypes, locations);
return dtoToConditionCriteria(filter, feeFineTypes, locations, stripes, intl);

Check failure on line 65 in src/api/dto/from/dtoToCriteria.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

'dtoToConditionCriteria' was used before it was defined

Check failure on line 65 in src/api/dto/from/dtoToCriteria.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

'dtoToConditionCriteria' was used before it was defined

case 'Negation':
return dtoToNegationCriteria(filter, feeFineTypes, locations);
return dtoToNegationCriteria(filter, feeFineTypes, locations, stripes, intl);

Check failure on line 68 in src/api/dto/from/dtoToCriteria.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

'dtoToNegationCriteria' was used before it was defined

Check failure on line 68 in src/api/dto/from/dtoToCriteria.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

'dtoToNegationCriteria' was used before it was defined

case CriteriaTerminalType.PASS:
default:
Expand All @@ -75,16 +79,18 @@ export function dtoToConditionCriteria(
filter: BursarExportFilterCondition,
feeFineTypes: FeeFineTypeDTO[],
locations: LocationDTO[],
stripes: StripesType,
intl: IntlShape,
): CriteriaGroup {
if (filter.operation === AndOrOperator.AND) {
return {
type: CriteriaGroupType.ALL_OF,
criteria: filter.criteria.map((criteria) => dtoToCriteria(criteria, feeFineTypes, locations)),
criteria: filter.criteria.map((criteria) => dtoToCriteria(criteria, feeFineTypes, locations, stripes, intl)),
};
} else {
return {
type: CriteriaGroupType.ANY_OF,
criteria: filter.criteria.map((criteria) => dtoToCriteria(criteria, feeFineTypes, locations)),
criteria: filter.criteria.map((criteria) => dtoToCriteria(criteria, feeFineTypes, locations, stripes, intl)),
};
}
}
Expand All @@ -93,19 +99,21 @@ export function dtoToNegationCriteria(
filter: BursarExportFilterNegation,
feeFineTypes: FeeFineTypeDTO[],
locations: LocationDTO[],
stripes: StripesType,
intl: IntlShape,
): CriteriaGroup {
// NOR gets displayed as "None of" in the UI, so we flatten the inner OR
if (filter.criteria.type === 'Condition' && filter.criteria.operation === AndOrOperator.OR) {
return {
type: CriteriaGroupType.NONE_OF,
criteria: filter.criteria.criteria.map((criteria) => dtoToCriteria(criteria, feeFineTypes, locations)),
criteria: filter.criteria.criteria.map((criteria) => dtoToCriteria(criteria, feeFineTypes, locations, stripes, intl)),
};
}

// otherwise, just negate the single inner criteria
return {
type: CriteriaGroupType.NONE_OF,
criteria: [dtoToCriteria(filter.criteria, feeFineTypes, locations)],
criteria: [dtoToCriteria(filter.criteria, feeFineTypes, locations, stripes, intl)],
};
}

Expand Down
22 changes: 20 additions & 2 deletions src/api/dto/from/dtoToData.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import { IntlShape } from 'react-intl';
import { useStripes } from '@folio/stripes/core';
import getIntl from '../../../test/util/getIntl';
import { ComparisonOperator, CriteriaTerminalType } from '../../../types/CriteriaTypes';
import { DataToken, DataTokenType, DateFormatType } from '../../../types/TokenTypes';
import { BursarExportDataTokenDTO } from '../types';
import dtoToData, { dtoToDataToken } from './dtoToData';

// United States
let intlEn: IntlShape;

beforeAll(() => {
intlEn = getIntl('en-US', 'EST');
});

jest.mock('@folio/stripes/core');

const mockedUseStripes = useStripes as jest.Mock;

describe('DTO to data conversion for initial values', () => {
const stripes = mockedUseStripes();

it.each<[BursarExportDataTokenDTO, DataToken]>([
[{ type: 'Constant', value: '\n' }, { type: DataTokenType.NEWLINE }],
[{ type: 'Constant', value: '\r\n' }, { type: DataTokenType.NEWLINE_MICROSOFT }],
Expand Down Expand Up @@ -117,9 +133,9 @@ describe('DTO to data conversion for initial values', () => {
else: 'else',
},
],
])('converts token %s to %s', (input, expected) => expect(dtoToDataToken(input, [], [])).toEqual(expected));
])('converts token %s to %s', (input, expected) => expect(dtoToDataToken(input, [], [], stripes, intlEn)).toEqual(expected));

it('converts undefined to empty array', () => expect(dtoToData(undefined, [], [])).toEqual([]));
it('converts undefined to empty array', () => expect(dtoToData(undefined, [], [], stripes, intlEn)).toEqual([]));

it('converts token array to data', () =>
expect(

Check failure on line 141 in src/api/dto/from/dtoToData.test.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected no linebreak before this expression

Check failure on line 141 in src/api/dto/from/dtoToData.test.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected no linebreak before this expression
Expand All @@ -134,6 +150,8 @@ describe('DTO to data conversion for initial values', () => {
],
[],
[],
stripes,
intlEn
),
).toEqual([
{ type: DataTokenType.NEWLINE },
Expand Down
10 changes: 8 additions & 2 deletions src/api/dto/from/dtoToData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { StripesType } from '@folio/stripes/core';
import { IntlShape } from 'react-intl';
import ConvenientConstants from '../../../types/ConvenientConstants';
import { DataToken, DataTokenType, DateFormatType } from '../../../types/TokenTypes';
import { FeeFineTypeDTO } from '../../queries/useFeeFineTypes';
Expand All @@ -11,18 +13,22 @@ export default function dtoToData(
tokens: BursarExportDataTokenDTO[] | undefined,
feeFineTypes: FeeFineTypeDTO[],
locations: LocationDTO[],
stripes: StripesType,
intl: IntlShape,
): DataToken[] {
if (!tokens) {
return [];
}

return tokens.map((token) => dtoToDataToken(token, feeFineTypes, locations));
return tokens.map((token) => dtoToDataToken(token, feeFineTypes, locations, stripes, intl));

Check failure on line 23 in src/api/dto/from/dtoToData.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

'dtoToDataToken' was used before it was defined

Check failure on line 23 in src/api/dto/from/dtoToData.ts

View workflow job for this annotation

GitHub Actions / github-actions-ci

'dtoToDataToken' was used before it was defined
}

export function dtoToDataToken(
token: BursarExportDataTokenDTO,
feeFineTypes: FeeFineTypeDTO[],
locations: LocationDTO[],
stripes: StripesType,
intl: IntlShape,
): DataToken {
switch (token.type) {
case 'Constant':
Expand Down Expand Up @@ -91,7 +97,7 @@ export function dtoToDataToken(
return {
type: DataTokenType.CONSTANT_CONDITIONAL,
conditions: token.conditions.map((condition) => ({
...dtoToCriteria(condition.condition, feeFineTypes, locations),
...dtoToCriteria(condition.condition, feeFineTypes, locations, stripes, intl),
value: condition.value.value,
})),
else: token.else.value,
Expand Down
Loading

0 comments on commit d2bb74f

Please sign in to comment.