Skip to content

Commit

Permalink
feat(notifications): handle kyc approved notification (#3041)
Browse files Browse the repository at this point in the history
  • Loading branch information
satish-ravi authored Oct 27, 2022
1 parent 2349077 commit a749ea9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 50 deletions.
32 changes: 32 additions & 0 deletions src/firebase/notifications.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { KycSchema } from '@fiatconnect/fiatconnect-types'
import { FirebaseMessagingTypes } from '@react-native-firebase/messaging'
import BigNumber from 'bignumber.js'
import { expectSaga } from 'redux-saga-test-plan'
Expand Down Expand Up @@ -179,4 +180,35 @@ describe(handleNotification, () => {
})
})
})

describe('with a kyc approved notification', () => {
const message = {
notification: { title: 'KYC', body: 'Kyc Approved' },
data: {
type: NotificationTypes.FIAT_CONNECT_KYC_APPROVED,
kycSchema: KycSchema.PersonalDataAndDocuments,
providerId: 'test-provider',
},
}

it('shows the in-app message when the app is already in the foreground', async () => {
await expectSaga(handleNotification, message, NotificationReceiveState.AppAlreadyOpen)
.put(showMessage('Kyc Approved', undefined, null, null, 'KYC'))
.run()

expect(navigate).not.toHaveBeenCalled()
})

it('navigates to the review screen if the app is not already in the foreground', async () => {
await expectSaga(handleNotification, message, NotificationReceiveState.AppColdStart)
.provide([[select(recipientInfoSelector), mockRecipientInfo]])
.run()

expect(navigate).toHaveBeenCalledWith(Screens.FiatConnectRefetchQuote, {
kycSchema: KycSchema.PersonalDataAndDocuments,
providerId: 'test-provider',
type: NotificationTypes.FIAT_CONNECT_KYC_APPROVED,
})
})
})
})
89 changes: 39 additions & 50 deletions src/firebase/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { navigate } from 'src/navigator/NavigationService'
import { Screens } from 'src/navigator/Screens'
import {
FiatConnectKycApprovedData,
NotificationReceiveState,
NotificationTypes,
TransferNotificationData,
Expand All @@ -27,14 +28,7 @@ import Logger from 'src/utils/Logger'

const TAG = 'FirebaseNotifications'

function* handlePaymentRequested(
paymentRequest: PaymentRequest,
notificationState: NotificationReceiveState
) {
if (notificationState === NotificationReceiveState.AppAlreadyOpen) {
return
}

function* handlePaymentRequested(paymentRequest: PaymentRequest) {
if (!paymentRequest.requesterAddress) {
Logger.error(TAG, 'Payment request must specify a requester address')
return
Expand All @@ -53,34 +47,29 @@ function* handlePaymentRequested(
})
}

function* handlePaymentReceived(
transferNotification: TransferNotificationData,
notificationState: NotificationReceiveState
) {
if (notificationState !== NotificationReceiveState.AppAlreadyOpen) {
const address = transferNotification.sender.toLowerCase()
function* handlePaymentReceived(transferNotification: TransferNotificationData) {
const address = transferNotification.sender.toLowerCase()

yield call(navigate, Screens.TransactionDetailsScreen, {
transaction: {
__typename: 'TokenTransferV2',
type: TokenTransactionTypeV2.Received,
transactionHash: transferNotification.txHash,
timestamp: new BigNumber(transferNotification.timestamp).toNumber(),
block: transferNotification.blockNumber,
address,
amount: {
value: transferNotification.value,
tokenAddress: transferNotification.tokenAddress,
},
metadata: {
title: transferNotification.name,
image: transferNotification.imageUrl,
comment: transferNotification.comment,
},
fees: [],
yield call(navigate, Screens.TransactionDetailsScreen, {
transaction: {
__typename: 'TokenTransferV2',
type: TokenTransactionTypeV2.Received,
transactionHash: transferNotification.txHash,
timestamp: new BigNumber(transferNotification.timestamp).toNumber(),
block: transferNotification.blockNumber,
address,
amount: {
value: transferNotification.value,
tokenAddress: transferNotification.tokenAddress,
},
})
}
metadata: {
title: transferNotification.name,
image: transferNotification.imageUrl,
comment: transferNotification.comment,
},
fees: [],
},
})
}

export function* handleNotification(
Expand All @@ -105,29 +94,29 @@ export function* handleNotification(
if (title) {
yield put(showMessage(body || title, undefined, null, openUrlAction, body ? title : null))
}
} else {
// Notification was received while app wasn't already open (i.e. tapped to act on it)
// So directly handle the action if any
if (openUrlAction) {
yield put(openUrlAction)
return
}
return
}

// Notification was received while app wasn't already open (i.e. tapped to act on it)
// So directly handle the action if any
if (openUrlAction) {
yield put(openUrlAction)
return
}

switch (message.data?.type) {
case NotificationTypes.PAYMENT_REQUESTED:
yield call(
handlePaymentRequested,
message.data as unknown as PaymentRequest,
notificationState
)
yield call(handlePaymentRequested, message.data as unknown as PaymentRequest)
break

case NotificationTypes.PAYMENT_RECEIVED:
yield call(
handlePaymentReceived,
message.data as unknown as TransferNotificationData,
notificationState
yield call(handlePaymentReceived, message.data as unknown as TransferNotificationData)
break

case NotificationTypes.FIAT_CONNECT_KYC_APPROVED:
navigate(
Screens.FiatConnectRefetchQuote,
message.data as unknown as FiatConnectKycApprovedData
)
break

Expand Down
9 changes: 9 additions & 0 deletions src/notifications/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { KycSchema } from '@fiatconnect/fiatconnect-types'

export enum NotificationTypes {
PAYMENT_RECEIVED = 'PAYMENT_RECEIVED',
PAYMENT_REQUESTED = 'PAYMENT_REQUESTED',
FIAT_CONNECT_KYC_APPROVED = 'FIAT_CONNECT_KYC_APPROVED',
}

export interface TransferNotificationData {
Expand All @@ -17,6 +20,12 @@ export interface TransferNotificationData {
imageUrl?: string
}

export interface FiatConnectKycApprovedData {
kycSchema: KycSchema
providerId: string
type: NotificationTypes.FIAT_CONNECT_KYC_APPROVED
}

export enum NotificationReceiveState {
AppAlreadyOpen = 'AppAlreadyOpen',
AppOpenedFromBackground = 'AppOpenedFromBackground',
Expand Down

0 comments on commit a749ea9

Please sign in to comment.