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

[#173102267] Add a selector to know if a bonus vacanze is requestable #1866

Merged
merged 4 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion ts/features/bonusVacanze/api/backendBonusVacanze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { ProblemJson } from "../../../../definitions/backend/ProblemJson";
import { defaultRetryingFetch } from "../../../utils/fetch";
import { BonusList, BonusListT } from "../types/bonusList";
import { BonusVacanze, BonusVacanzeT } from "../types/bonusVacanze";
import { BonusVacanze, BonusVacanzeT } from "../types/bonusVacanzeActivation";
import {
EligibilityCheck,
EligibilityCheckT,
Expand Down
2 changes: 1 addition & 1 deletion ts/features/bonusVacanze/components/ActiveBonus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import IconFont from "../../../components/ui/IconFont";
import I18n from "../../../i18n";
import customVariables from "../../../theme/variables";
import { formatNumberCentsToAmount } from "../../../utils/stringBuilder";
import { BonusVacanze } from "../types/bonusVacanze";
import { BonusVacanze } from "../types/bonusVacanzeActivation";
import { validityInterval } from "../utils/bonus";

type Props = {
Expand Down
2 changes: 1 addition & 1 deletion ts/features/bonusVacanze/components/RequestBonus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import I18n from "../../../i18n";
import customVariables from "../../../theme/variables";
import { maybeInnerProperty } from "../../../utils/options";
import { BonusList } from "../types/bonusList";
import { BonusVacanze } from "../types/bonusVacanze";
import { BonusVacanze } from "../types/bonusVacanzeActivation";
import { ID_BONUS_VACANZE_TYPE } from "../utils/bonus";
import ActiveBonus from "./ActiveBonus";

Expand Down
2 changes: 1 addition & 1 deletion ts/features/bonusVacanze/mock/mockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
NonNegativeNumber
} from "italia-ts-commons/lib/numbers";
import { FiscalCode, NonEmptyString } from "italia-ts-commons/lib/strings";
import { BonusStatusEnum, BonusVacanze } from "../types/bonusVacanze";
import { BonusStatusEnum, BonusVacanze } from "../types/bonusVacanzeActivation";
import {
EligibilityCheck,
EligibilityCheckStatusEnum
Expand Down
2 changes: 1 addition & 1 deletion ts/features/bonusVacanze/screens/ActiveBonusScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
centsToAmount,
formatNumberAmount
} from "../../../utils/stringBuilder";
import { BonusStatusEnum, BonusVacanze } from "../types/bonusVacanze";
import { BonusStatusEnum, BonusVacanze } from "../types/bonusVacanzeActivation";
import { validityInterval } from "../utils/bonus";

type QRCodeContents = {
Expand Down
4 changes: 2 additions & 2 deletions ts/features/bonusVacanze/screens/AvailableBonusScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import ActiveBonus from "../components/ActiveBonus";
import AvailableBonusItem from "../components/AvailableBonusItem";
import { availableBonusesLoad } from "../store/actions/bonusVacanze";
import { availableBonusesSelector } from "../store/reducers/availableBonuses";
import { bonusVacanzeActivationSelector } from "../store/reducers/bonusVacanze";
import { bonusVacanzeActivationSelector } from "../store/reducers/bonusVacanzeActivation";
import { BonusItem } from "../types/bonusList";
import { BonusVacanze } from "../types/bonusVacanze";
import { BonusVacanze } from "../types/bonusVacanzeActivation";
import { ID_BONUS_VACANZE_TYPE, isBonusActive } from "../utils/bonus";

export type Props = ReturnType<typeof mapStateToProps> &
Expand Down
2 changes: 1 addition & 1 deletion ts/features/bonusVacanze/store/actions/bonusVacanze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
createStandardAction
} from "typesafe-actions";
import { BonusList } from "../../types/bonusList";
import { BonusVacanze } from "../../types/bonusVacanze";
import { BonusVacanze } from "../../types/bonusVacanzeActivation";
import { EligibilityCheck, EligibilityId } from "../../types/eligibility";
import { EligibilityRequestProgressEnum } from "../reducers/eligibility";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { NonNegativeInteger } from "italia-ts-commons/lib/numbers";
import * as pot from "italia-ts-commons/lib/pot";
import { FiscalCode } from "italia-ts-commons/lib/strings";
import {
BonusStatusEnum,
BonusVacanze
} from "../../../types/bonusVacanzeActivation";
import { canBonusVacanzeBeRequestedSelector } from "../bonusVacanzeActivation";

const bonus: BonusVacanze = {
id: "XYZ",
code: "ABCDE123XYZ",
applicant_fiscal_code: "ABCMYY82A12L722R" as FiscalCode,
status: BonusStatusEnum.ACTIVE,
qr_code: [
{
mime_type: "svg+xml",
base64_content: "content"
},
{
mime_type: "image/png",
base64_content: "content"
}
],
max_amount: 50000 as NonNegativeInteger,
max_tax_benefit: 3000 as NonNegativeInteger,
updated_at: new Date("2020-07-04T12:20:00.000Z")
};

describe("canBonusVacanzeBeRequestedSelector selector", () => {
it("should return true when bonus doen't exist", () => {
expect(
canBonusVacanzeBeRequestedSelector().resultFunc(pot.none)
).toBeTruthy();
});

it("should return false when bonus exists and is active", () => {
expect(
canBonusVacanzeBeRequestedSelector().resultFunc(pot.some(bonus))
).toBeFalsy();
});

it("should return false when bonus exists and is consumed", () => {
expect(
canBonusVacanzeBeRequestedSelector().resultFunc(
pot.some({ ...bonus, status: BonusStatusEnum.CONSUMED })
)
).toBeFalsy();
});

it("should return true when bonus exists and is voided", () => {
expect(
canBonusVacanzeBeRequestedSelector().resultFunc(
pot.some({ ...bonus, status: BonusStatusEnum.VOIDED })
)
).toBeTruthy();
});

it("should return true when bonus exists and is failed", () => {
expect(
canBonusVacanzeBeRequestedSelector().resultFunc(
pot.some({ ...bonus, status: BonusStatusEnum.FAILED })
)
).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import * as pot from "italia-ts-commons/lib/pot";
import { combineReducers } from "redux";
import { createSelector } from "reselect";
import { getType } from "typesafe-actions";
import { Action } from "../../../../store/actions/types";
import { GlobalState } from "../../../../store/reducers/types";
import { BonusVacanze } from "../../types/bonusVacanze";
import {
BonusStatusEnum,
BonusVacanze
} from "../../types/bonusVacanzeActivation";
import { loadBonusVacanzeFromId } from "../actions/bonusVacanze";
import availableBonusesReducer, {
AvailableBonusesState
Expand Down Expand Up @@ -43,8 +47,29 @@ const reducer = combineReducers<BonusState, Action>({
});

// Selectors
// return the bonus vacanze activation pot
export const bonusVacanzeActivationSelector = (
state: GlobalState
): pot.Pot<BonusVacanze, Error> => state.bonus.bonusVacanzeActivation;

/* return true if a bonus vacanze
* - doesn't exists (pot.none)
* - exists but its state is VOIDED or FAILED
*/
export const canBonusVacanzeBeRequestedSelector = () =>
createSelector<GlobalState, pot.Pot<BonusVacanze, Error>, boolean>(
bonusVacanzeActivationSelector,
bv => {
return pot.getOrElse(
pot.map(
bv,
v =>
v.status === BonusStatusEnum.VOIDED ||
v.status === BonusStatusEnum.FAILED
),
true
);
}
);

export default reducer;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Timestamp } from "../../../../definitions/backend/Timestamp";

export enum BonusStatusEnum {
"ACTIVE" = "ACTIVE",
"CANCELLED" = "CANCELLED",
"VOIDED" = "VOIDED",
"FAILED" = "FAILED",
"CONSUMED" = "CONSUMED"
}
Expand Down
7 changes: 1 addition & 6 deletions ts/features/bonusVacanze/utils/bonus.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { fromNullable } from "fp-ts/lib/Option";
import { formatDateAsLocal } from "../../../utils/dates";
import { BonusStatusEnum, BonusVacanze } from "../types/bonusVacanze";
import { BonusStatusEnum, BonusVacanze } from "../types/bonusVacanzeActivation";

export const ID_BONUS_VACANZE_TYPE = 1;

// return true if the bonus is active
export const isBonusActive = (bonus: BonusVacanze) =>
bonus.status === BonusStatusEnum.ACTIVE;

// return true if the bonus can be activable
export const isBonusActivable = (bonus: BonusVacanze) =>
bonus.status === BonusStatusEnum.CANCELLED ||
bonus.status === BonusStatusEnum.FAILED;

export const validityInterval = (
bonusValidityFrom?: Date,
bonusValidityTo?: Date
Expand Down
4 changes: 2 additions & 2 deletions ts/screens/wallet/WalletHomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import {
loadBonusVacanzeFromId
} from "../../features/bonusVacanze/store/actions/bonusVacanze";
import { availableBonusesSelector } from "../../features/bonusVacanze/store/reducers/availableBonuses";
import { bonusVacanzeActivationSelector } from "../../features/bonusVacanze/store/reducers/bonusVacanze";
import { BonusVacanze } from "../../features/bonusVacanze/types/bonusVacanze";
import { bonusVacanzeActivationSelector } from "../../features/bonusVacanze/store/reducers/bonusVacanzeActivation";
import { BonusVacanze } from "../../features/bonusVacanze/types/bonusVacanzeActivation";
import I18n from "../../i18n";
import {
navigateBack,
Expand Down
2 changes: 1 addition & 1 deletion ts/store/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { PersistConfig, persistReducer, purgeStoredState } from "redux-persist";
import { isActionOf } from "typesafe-actions";

import AsyncStorage from "@react-native-community/async-storage";
import bonusReducer from "../../features/bonusVacanze/store/reducers/bonusVacanzeActivation";
import { logoutFailure, logoutSuccess } from "../actions/authentication";
import { Action } from "../actions/types";
import createSecureStorage from "../storages/keychain";
import { DateISO8601Transform } from "../transforms/dateISO8601Tranform";
import bonusReducer from "./../../features/bonusVacanze/store/reducers/bonusVacanze";
import appStateReducer from "./appState";
import authenticationReducer, { AuthenticationState } from "./authentication";
import backendInfoReducer from "./backendInfo";
Expand Down
2 changes: 1 addition & 1 deletion ts/store/reducers/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NavigationState } from "react-navigation";
import { PersistPartial } from "redux-persist";

import { BonusState } from "../../features/bonusVacanze/store/reducers/bonusVacanze";
import { BonusState } from "../../features/bonusVacanze/store/reducers/bonusVacanzeActivation";
import { Action } from "../actions/types";
import { AppState } from "./appState";
import { PersistedAuthenticationState } from "./authentication";
Expand Down