Skip to content

Commit

Permalink
fix: only request new notification signatures (#2575)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook authored Oct 4, 2023
1 parent 759fafe commit 29148a8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { _getSafesToRegister } from '.'
import type { AddedSafesState } from '@/store/addedSafesSlice'
import type { PushNotificationPreferences } from '@/services/push-notifications/preferences'

describe('PushNotificationsBanner', () => {
describe('getSafesToRegister', () => {
it('should return all added safes if no preferences exist', () => {
const addedSafes = {
'1': {
'0x123': {},
'0x456': {},
},
'4': {
'0x789': {},
},
} as unknown as AddedSafesState
const allPreferences = undefined

const result = _getSafesToRegister(addedSafes, allPreferences)

expect(result).toEqual({
'1': ['0x123', '0x456'],
'4': ['0x789'],
})
})

it('should return only newly added safes if preferences exist', () => {
const addedSafes = {
'1': {
'0x123': {},
'0x456': {},
},
'4': {
'0x789': {},
},
} as unknown as AddedSafesState
const allPreferences = {
'1:0x123': {
safeAddress: '0x123',
chainId: '1',
},
'4:0x789': {
safeAddress: '0x789',
chainId: '4',
},
} as unknown as PushNotificationPreferences

const result = _getSafesToRegister(addedSafes, allPreferences)

expect(result).toEqual({
'1': ['0x456'],
})
})

it('should return all added safes if no preferences match', () => {
const addedSafes = {
'1': {
'0x123': {},
'0x456': {},
},
'4': {
'0x789': {},
},
} as unknown as AddedSafesState
const allPreferences = {
'1:0x111': {
safeAddress: '0x111',
chainId: '1',
},
'4:0x222': {
safeAddress: '0x222',
chainId: '4',
},
} as unknown as PushNotificationPreferences

const result = _getSafesToRegister(addedSafes, allPreferences)

expect(result).toEqual({
'1': ['0x123', '0x456'],
'4': ['0x789'],
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ export const useDismissPushNotificationsBanner = () => {
}
}

const getSafesToRegister = (addedSafes: AddedSafesState, allPreferences: PushNotificationPreferences | undefined) => {
export const _getSafesToRegister = (
addedSafes: AddedSafesState,
allPreferences: PushNotificationPreferences | undefined,
) => {
// Regiser all added Safes
if (!allPreferences) {
return transformAddedSafes(addedSafes)
Expand All @@ -77,13 +80,14 @@ const getSafesToRegister = (addedSafes: AddedSafesState, allPreferences: PushNot
const notificationRegistrations = Object.values(allPreferences)

const newlyAddedSafes = addedSafeAddressesOnChain.filter((safeAddress) => {
return (
notificationRegistrations.length === 0 ||
notificationRegistrations.some((registration) => !sameAddress(registration.safeAddress, safeAddress))
return !notificationRegistrations.some(
(registration) => chainId === registration.chainId && sameAddress(registration.safeAddress, safeAddress),
)
})

acc[chainId] = newlyAddedSafes
if (newlyAddedSafes.length > 0) {
acc[chainId] = newlyAddedSafes
}

return acc
}, {})
Expand Down Expand Up @@ -124,7 +128,7 @@ export const PushNotificationsBanner = ({ children }: { children: ReactElement }
trackEvent(PUSH_NOTIFICATION_EVENTS.ENABLE_ALL)

const allPreferences = getAllPreferences()
const safesToRegister = getSafesToRegister(addedSafes, allPreferences)
const safesToRegister = _getSafesToRegister(addedSafes, allPreferences)

try {
await assertWalletChain(onboard, safe.chainId)
Expand Down

0 comments on commit 29148a8

Please sign in to comment.