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(NOTIFY-1102): add account sync analytics callbacks #4707

Merged
merged 1 commit into from
Sep 16, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,59 @@ describe('user-storage/user-storage-controller - syncInternalAccountsWithUserSto
);
});

it('fires the onAccountAdded callback when adding an account', async () => {
const mockUserStorageAccountsResponse = async () => {
return {
status: 200,
body: await createMockUserStorageEntries(
MOCK_USER_STORAGE_ACCOUNTS.SAME_AS_INTERNAL_ALL,
),
};
};

const arrangeMocksForAccounts = async () => {
return {
messengerMocks: mockUserStorageMessenger({
accounts: {
accountsList: MOCK_INTERNAL_ACCOUNTS.ONE as InternalAccount[],
},
}),
mockAPI: {
mockEndpointGetUserStorage:
await mockEndpointGetUserStorageAllFeatureEntries(
'accounts',
await mockUserStorageAccountsResponse(),
),
},
};
};

const onAccountAdded = jest.fn();

const { messengerMocks, mockAPI } = await arrangeMocksForAccounts();
const controller = new UserStorageController({
messenger: messengerMocks.messenger,
config: {
accountSyncing: {
onAccountAdded,
},
},
env: {
isAccountSyncingEnabled: true,
},
getMetaMetricsState: () => true,
});

await controller.syncInternalAccountsWithUserStorage();

mockAPI.mockEndpointGetUserStorage.done();

expect(onAccountAdded).toHaveBeenCalledTimes(
MOCK_USER_STORAGE_ACCOUNTS.SAME_AS_INTERNAL_ALL.length -
MOCK_INTERNAL_ACCOUNTS.ONE.length,
);
});

it('does not create internal accounts if user storage has less accounts', async () => {
const mockUserStorageAccountsResponse = async () => {
return {
Expand Down Expand Up @@ -893,6 +946,48 @@ describe('user-storage/user-storage-controller - syncInternalAccountsWithUserSto
messengerMocks.mockAccountsUpdateAccountMetadata,
).not.toHaveBeenCalled();
});

it('fires the onAccountNameUpdated callback when renaming an internal account', async () => {
const arrangeMocksForAccounts = async () => {
return {
messengerMocks: mockUserStorageMessenger({
accounts: {
accountsList:
MOCK_INTERNAL_ACCOUNTS.ONE_DEFAULT_NAME as InternalAccount[],
},
}),
mockAPI: {
mockEndpointGetUserStorage:
await mockEndpointGetUserStorageAllFeatureEntries(
'accounts',
await mockUserStorageAccountsResponse(),
),
},
};
};

const onAccountNameUpdated = jest.fn();

const { messengerMocks, mockAPI } = await arrangeMocksForAccounts();
const controller = new UserStorageController({
messenger: messengerMocks.messenger,
config: {
accountSyncing: {
onAccountNameUpdated,
},
},
env: {
isAccountSyncingEnabled: true,
},
getMetaMetricsState: () => true,
});

await controller.syncInternalAccountsWithUserStorage();

mockAPI.mockEndpointGetUserStorage.done();

expect(onAccountNameUpdated).toHaveBeenCalledTimes(1);
});
});

describe('User storage name is a custom name with last updated', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ const metadata: StateMetadata<UserStorageControllerState> = {
},
};

type ControllerConfig = {
accountSyncing?: {
/**
* Callback that fires when account sync adds an account.
* This is used for analytics.
*/
onAccountAdded?: (profileId: string) => void;

/**
* Callback that fires when account sync updates the name of an account.
* This is used for analytics.
*/
onAccountNameUpdated?: (profileId: string) => void;
};
};

// Messenger Actions
type CreateActionsObj<Controller extends keyof UserStorageController> = {
[K in Controller]: {
Expand Down Expand Up @@ -347,6 +363,8 @@ export default class UserStorageController extends BaseController<
},
};

#config?: ControllerConfig;

#notificationServices = {
disableNotificationServices: async () => {
return await this.messagingSystem.call(
Expand Down Expand Up @@ -387,11 +405,13 @@ export default class UserStorageController extends BaseController<
messenger,
state,
env,
config,
getMetaMetricsState,
nativeScryptCrypto,
}: {
messenger: UserStorageControllerMessenger;
state?: UserStorageControllerState;
config?: ControllerConfig;
env?: {
isAccountSyncingEnabled?: boolean;
isNetworkSyncingEnabled?: boolean;
Expand All @@ -406,6 +426,8 @@ export default class UserStorageController extends BaseController<
state: { ...defaultState, ...state },
});

this.#config = config;

this.#accounts.isAccountSyncingEnabled = Boolean(
env?.isAccountSyncingEnabled,
);
Expand Down Expand Up @@ -722,6 +744,8 @@ export default class UserStorageController extends BaseController<
try {
this.#accounts.isAccountSyncingInProgress = true;

const profileId = await this.#auth.getProfileId();

const userStorageAccountsList =
await this.#accounts.getUserStorageAccountsList();

Expand Down Expand Up @@ -752,6 +776,7 @@ export default class UserStorageController extends BaseController<
length: numberOfAccountsToAdd,
}).map(async () => {
await this.messagingSystem.call('KeyringController:addNewAccount');
this.#config?.accountSyncing?.onAccountAdded?.(profileId);
});

await Promise.all(addNewAccountsPromises);
Expand Down Expand Up @@ -791,6 +816,8 @@ export default class UserStorageController extends BaseController<
name: userStorageAccount.n,
},
);

this.#config?.accountSyncing?.onAccountNameUpdated?.(profileId);
}
continue;
}
Expand Down Expand Up @@ -830,6 +857,8 @@ export default class UserStorageController extends BaseController<
},
);

this.#config?.accountSyncing?.onAccountNameUpdated?.(profileId);

continue;
} else if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {
await this.#accounts.saveInternalAccountToUserStorage(
Expand Down
Loading