connect-transfer-react-native-sdk
Version:
Connect Transfer React Native SDK for Mastercard Open Banking Connect
201 lines (199 loc) • 7.7 kB
JavaScript
"use strict";
import { useSelector } from 'react-redux';
import { Product } from '@atomicfi/transact-react-native';
import { AtomicEvents, RedirectReason, TransferActionCodes, TransferActionEvents, TransferEventDataName, UserEvents } from '../constants';
// @ts-ignore
export const getTransferProductType = product => {
if (product === 'deposit') {
return Product.DEPOSIT;
}
return null;
};
export const useTransferEventCommonData = () => {
const queryParams = useSelector(state => state.user.queryParamsObject);
if (Object.keys(queryParams).length === 0 || !queryParams.hasOwnProperty('customerId') && !queryParams.hasOwnProperty('partnerId')) {
return {};
}
const commonData = {
[]: queryParams[TransferEventDataName.CUSTOMER_ID],
[]: queryParams[TransferEventDataName.PARTNER_ID],
[]: queryParams[TransferEventDataName.TIMESTAMP],
[]: queryParams[TransferEventDataName.TTL],
[]: queryParams[TransferEventDataName.TYPE],
[]: queryParams.signature
};
if (queryParams?.experience?.id) {
commonData[TransferEventDataName.EXPERIENCE] = queryParams.experience.id;
}
return commonData;
};
export const useTransferEventResponse = () => {
const commonData = useTransferEventCommonData();
const isEmpty = !commonData || Object.keys(commonData).length === 0;
const getResponseForInitializeTransfer = () => {
if (isEmpty) return;
return {
...commonData,
[]: TransferActionEvents.INITIALIZE_TRANSFER
};
};
const getResponseForTermsAndConditionsAccepted = () => {
if (isEmpty) return;
return {
...commonData,
[]: TransferActionEvents.TERMS_ACCEPTED
};
};
const getResponseForInitializeDepositSwitch = productType => {
if (isEmpty) return;
return {
...commonData,
[]: UserEvents.INITIALIZE_DEPOSIT_SWITCH,
...(productType && {
[]: productType
})
};
};
const getResponseForError = errorCode => ({
...commonData,
[]: TransferActionEvents.ERROR,
[]: RedirectReason.ERROR,
[]: errorCode || TransferActionCodes.API_OR_ATOMIC_ERROR
});
const getResponseForClose = (reason, errorCode) => {
if (isEmpty) return;
const isExitReason = reason === RedirectReason.UNKNOWN || reason === RedirectReason.EXIT;
return {
...commonData,
[]: TransferActionEvents.END,
[]: isExitReason ? RedirectReason.EXIT : reason,
[]: isExitReason ? TransferActionCodes.USER_INITIATED_EXIT : errorCode || TransferActionCodes.API_OR_ATOMIC_ERROR
};
};
const getResponseForFinish = responseData => {
if (isEmpty) return;
const {
taskId,
...otherResponseData
} = responseData;
return {
...commonData,
[]: TransferActionEvents.END,
[]: RedirectReason.COMPLETE,
[]: TransferActionCodes.SUCCESS,
...otherResponseData,
switchId: taskId
};
};
return {
getResponseForInitializeTransfer,
getResponseForTermsAndConditionsAccepted,
getResponseForInitializeDepositSwitch,
getResponseForError,
getResponseForClose,
getResponseForFinish
};
};
export const getUserEventMappingForPDS = (interactionResponse, commonData) => {
const {
name: eventName,
value
} = interactionResponse;
const commonResponse = commonData;
switch (eventName) {
case AtomicEvents.SEARCH_BY_COMPANY:
return {
...commonResponse,
[]: UserEvents.SEARCH_PAYROLL_PROVIDER,
[]: value?.query
};
case AtomicEvents.SELECTED_COMPANY_FROM_SEARCH_BY_COMPANY_PAGE:
return {
...commonResponse,
[]: UserEvents.SELECT_PAYROLL_PROVIDER,
[]: value?.company
};
case AtomicEvents.SELECTED_COMPANY_FROM_SEARCH_BY_FRANCHISE_PAGE:
return {
...commonResponse,
[]: UserEvents.SELECTED_COMPANY_THROUGH_FRANCHISE_PAGE,
[]: value?.company
};
case AtomicEvents.SELECTED_COMPANY_FROM_TYPEAHEAD_SEARCH_BY_CONFIGURABLE_CONNECTOR_PAGE:
return {
...commonResponse,
[]: UserEvents.SELECTED_COMPANY_THROUGH_PAYROLL_PROVIDER,
[]: value?.company
};
default:
return getCommonUserEventMapping(interactionResponse, commonData);
}
};
export const getCommonUserEventMapping = (interactionResponse, commonData) => {
const {
name: eventName,
value
} = interactionResponse;
const commonResponse = commonData;
switch (eventName) {
case AtomicEvents.CLICKED_CONTINUE_FROM_FORM_ON_LOGIN_PAGE:
case AtomicEvents.CLICKED_CONTINUE_FROM_FORM_ON_INTERRUPT_PAGE:
return {
...commonResponse,
[]: UserEvents.SUBMIT_CREDENTIALS,
[]: value?.input
};
case AtomicEvents.CLICKED_EXTERNAL_LOGIN_RECOVERY_LINK_FROM_LOGIN_HELP_PAGE:
return {
...commonResponse,
[]: UserEvents.EXTERNAL_LINK,
[]: value?.button
};
case AtomicEvents.CLICKED_CONTINUE_FROM_PERCENTAGE_DEPOSIT_AMOUNT_PAGE:
case AtomicEvents.CLICKED_CONTINUE_FROM_FIXED_DEPOSIT_AMOUNT_PAGE:
return {
...commonResponse,
[]: UserEvents.CHANGE_DEFAULT_ALLOCATION,
[]: value?.distributionType,
...(value?.distributionAmount !== undefined && {
[]: value?.distributionAmount
})
};
case AtomicEvents.CLICKED_DISTRIBUTION_TYPE_FROM_SELECT_FROM_DEPOSIT_OPTIONS_PAGE:
return {
...commonResponse,
[]: UserEvents.CHANGE_DEFAULT_ALLOCATION,
[]: value?.depositOption
};
case AtomicEvents.CLICKED_BUTTON_TO_START_AUTHENTICATION:
return {
...commonResponse,
[]: UserEvents.SUBMIT_ALLOCATION,
[]: value?.distributionType,
...(value?.distributionAmount !== undefined && {
[]: value?.distributionAmount
})
};
case AtomicEvents.VIEWED_TASK_COMPLETED_PAGE:
return {
...commonResponse,
[]: UserEvents.TASK_COMPLETED,
[]: value?.state
};
case AtomicEvents.VIEWED_ACCESS_UNAUTHORIZED_PAGE:
return {
...commonResponse,
[]: UserEvents.UNAUTHORIZED,
[]: false
};
case AtomicEvents.VIEWED_EXPIRED_TOKEN_PAGE:
return {
...commonResponse,
[]: UserEvents.UNAUTHORIZED,
[]: true
};
default:
break;
}
};
//# sourceMappingURL=transferEventHandlers.js.map