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 currency type optimistic cache #3907

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { CurrencyFilter } from '@/object-record/record-filter/types/ObjectRecordQueryFilter';
import { isMatchingCurrencyFilter } from '@/object-record/record-filter/utils/isMatchingCurrencyFilter';

describe('isMatchingCurrencyFilter', () => {
describe('eq', () => {
it('value equals eq filter', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { eq: 10 },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 10 })).toBe(
true,
);
});

it('value does not equal eq filter', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { eq: 10 },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 20 })).toBe(
false,
);
});
});

describe('gt', () => {
it('value is greater than gt filter', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { gt: 10 },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 20 })).toBe(
true,
);
});

it('value is not greater than gt filter', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { gt: 20 },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 10 })).toBe(
false,
);
});
});

describe('gte', () => {
it('value is greater than or equal to gte filter', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { gte: 10 },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 10 })).toBe(
true,
);
});

it('value is not greater than or equal to gte filter', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { gte: 20 },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 10 })).toBe(
false,
);
});
});

describe('in', () => {
it('value is in the array', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { in: [10, 20, 30] },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 20 })).toBe(
true,
);
});

it('value is not in the array', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { in: [10, 30, 40] },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 20 })).toBe(
false,
);
});
});

describe('lt', () => {
it('value is less than lt filter', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { lt: 20 },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 10 })).toBe(
true,
);
});

it('value is not less than lt filter', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { lt: 10 },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 20 })).toBe(
false,
);
});
});

describe('lte', () => {
it('value is less than or equal to lte filter', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { lte: 20 },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 20 })).toBe(
true,
);
});

it('value is not less than or equal to lte filter', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { lte: 10 },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 20 })).toBe(
false,
);
});
});

describe('neq', () => {
it('value does not equal neq filter', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { neq: 10 },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 20 })).toBe(
true,
);
});

it('value equals neq filter', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { neq: 10 },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 10 })).toBe(
false,
);
});
});

describe('is', () => {
it('value is NULL', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { is: 'NULL' },
};
expect(
isMatchingCurrencyFilter({ currencyFilter, value: null as any }),
).toBe(true);
});

it('value is NOT_NULL', () => {
const currencyFilter: CurrencyFilter = {
amountMicros: { is: 'NOT_NULL' },
};
expect(isMatchingCurrencyFilter({ currencyFilter, value: 10 })).toBe(
true,
);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { CurrencyFilter } from '@/object-record/record-filter/types/ObjectRecordQueryFilter';

export const isMatchingCurrencyFilter = ({
currencyFilter,
value,
}: {
currencyFilter: CurrencyFilter;
value: number;
}) => {
if (currencyFilter?.amountMicros !== undefined) {
switch (true) {
case currencyFilter.amountMicros.eq !== undefined: {
return value === currencyFilter.amountMicros.eq;
}
case currencyFilter.amountMicros.neq !== undefined: {
return value !== currencyFilter.amountMicros.neq;
}
case currencyFilter.amountMicros.gt !== undefined: {
return value > currencyFilter.amountMicros.gt;
}
case currencyFilter.amountMicros.gte !== undefined: {
return value >= currencyFilter.amountMicros.gte;
}
case currencyFilter.amountMicros.lt !== undefined: {
return value < currencyFilter.amountMicros.lt;
}
case currencyFilter.amountMicros.lte !== undefined: {
return value <= currencyFilter.amountMicros.lte;
}
case currencyFilter.amountMicros.in !== undefined: {
return currencyFilter.amountMicros.in.includes(value);
}
case currencyFilter.amountMicros.is !== undefined: {
if (currencyFilter.amountMicros.is === 'NULL') {
return value === null;
} else {
return value !== null;
}
}
default: {
throw new Error(
`Unexpected amountMicros for currency filter : ${JSON.stringify(
currencyFilter.amountMicros,
)}`,
);
}
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import {
AndObjectRecordFilter,
BooleanFilter,
CurrencyFilter,
DateFilter,
FloatFilter,
FullNameFilter,
Expand All @@ -15,6 +16,7 @@ import {
UUIDFilter,
} from '@/object-record/record-filter/types/ObjectRecordQueryFilter';
import { isMatchingBooleanFilter } from '@/object-record/record-filter/utils/isMatchingBooleanFilter';
import { isMatchingCurrencyFilter } from '@/object-record/record-filter/utils/isMatchingCurrencyFilter';
import { isMatchingDateFilter } from '@/object-record/record-filter/utils/isMatchingDateFilter';
import { isMatchingFloatFilter } from '@/object-record/record-filter/utils/isMatchingFloatFilter';
import { isMatchingStringFilter } from '@/object-record/record-filter/utils/isMatchingStringFilter';
Expand Down Expand Up @@ -202,6 +204,12 @@ export const isRecordMatchingFilter = ({
value: record[filterKey],
});
}
case FieldMetadataType.Currency: {
return isMatchingCurrencyFilter({
currencyFilter: filterValue as CurrencyFilter,
value: record[filterKey].amountMicros,
});
}
case FieldMetadataType.Relation: {
throw new Error(
`Not implemented yet, use UUID filter instead on the corredponding "${filterKey}Id" field`,
Expand Down
Loading