Skip to content

Commit

Permalink
Merge pull request #324 from PermanentOrg/PER-9396-ensure-emails-are-…
Browse files Browse the repository at this point in the history
…sent

PEr-9396 Send the email to mixpanel
  • Loading branch information
crisnicandrei authored Nov 16, 2023
2 parents 51d61e2 + 3d0db5d commit 6e32ad6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
68 changes: 67 additions & 1 deletion src/app/shared/services/account/account.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* @format */
import { CookieService } from 'ngx-cookie-service';
import { TestBed, inject } from '@angular/core/testing';
import { Router } from '@angular/router';
import { Shallow } from 'shallow-render';
Expand Down Expand Up @@ -67,6 +68,33 @@ describe('AccountService', () => {
observer.complete();
});
},
logIn: (
email: string,
password: string,
rememberMe: boolean,
keepLoggedIn: boolean
) => {
return new Observable((observer) => {
observer.next(
new AuthResponse({
isSuccessful: true,
Results: [
{
data: [
{
AccountVO: {
primaryEmail: '[email protected]',
fullName: 'Test User',
},
},
],
},
],
})
);
observer.complete();
});
},
},
})
.mock(Router, {
Expand All @@ -89,7 +117,8 @@ describe('AccountService', () => {
})
.mock(EditService, {
deleteItems: (items: any[]) => Promise.resolve(true),
});
})
.mock(CookieService, { set: (key: string, value: string) => {} });
});

it('should be created', () => {
Expand Down Expand Up @@ -137,6 +166,7 @@ describe('AccountService', () => {

it('should handle successful email verification', async () => {
const { instance } = shallow.createService();
const trackerSpy = spyOn(instance, 'trackAuthWithMixpanel');

const account = new AccountVO({
primaryEmail: '[email protected]',
Expand All @@ -150,10 +180,12 @@ describe('AccountService', () => {
await instance.verifyEmail('sampleToken');
expect(instance.getAccount().emailStatus).toBe('status.auth.verified');
expect(instance.getAccount().keepLoggedIn).toBeTrue();
expect(trackerSpy).toHaveBeenCalled();
});

it('should handle successful phone verification', async () => {
const { instance } = shallow.createService();
const trackerSpy = spyOn(instance, 'trackAuthWithMixpanel');

const account = new AccountVO({
primaryEmail: '[email protected]',
Expand All @@ -167,6 +199,7 @@ describe('AccountService', () => {
await instance.verifyEmail('sampleToken');
expect(instance.getAccount().phoneStatus).toBe('status.auth.verified');
expect(instance.getAccount().keepLoggedIn).toBeTrue();
expect(trackerSpy).toHaveBeenCalled();
});
it('should update the account storage when a file is uploaded successfully', async () => {
const { instance, inject } = shallow.createService();
Expand All @@ -182,6 +215,39 @@ describe('AccountService', () => {
await instance.deductAccountStorage(200);
expect(instance.getAccount().spaceLeft).toEqual(99800);
});

it('should send the account data to mixpanel after signing up', async () => {
const { instance, inject } = shallow.createService();
const apiService = inject(ApiService);
const trackerSpy = spyOn(instance, 'trackAuthWithMixpanel');
const account = await instance.signUp(
'[email protected]',
'Test User',
'password123',
'password123',
true,
true,
'',
'',
true
);

expect(trackerSpy).toHaveBeenCalled();
});

it('should send the account data to mixpanel after logging in', async () => {
const { instance, inject } = shallow.createService();
const apiService = inject(ApiService);
const trackerSpy = spyOn(instance, 'trackAuthWithMixpanel');
const account = await instance.logIn(
'[email protected]',
'password123',
true,
true
);

expect(trackerSpy).toHaveBeenCalled();
});
it('should add storage back after deleting an item', async () => {
const { instance, inject } = shallow.createService();
const editService = inject(EditService);
Expand Down
12 changes: 11 additions & 1 deletion src/app/shared/services/account/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ export class AccountService {
newAccount.isNew = currentAccount.isNew;
}
this.setAccount(newAccount);
this.trackAuthWithMixpanel('Sign In', newAccount);
if (response.getArchiveVO()?.archiveId) {
this.setArchive(response.getArchiveVO());
}
Expand Down Expand Up @@ -426,6 +427,7 @@ export class AccountService {
keepLoggedIn,
});
this.setAccount(newAccount);
this.trackAuthWithMixpanel('Verify multi-factor', newAccount);

const authToken = response.getAuthToken()?.value;
if (authToken) {
Expand Down Expand Up @@ -454,6 +456,7 @@ export class AccountService {
keepLoggedIn,
});
this.setAccount(account);
this.trackAuthWithMixpanel('Verify email', account);
return response;
} else {
throw response;
Expand Down Expand Up @@ -567,7 +570,7 @@ export class AccountService {
});
newAccount.isNew = true;
this.setAccount(newAccount);
this.mixpanel.track('Sign up', { accountId: newAccount.accountId });
this.trackAuthWithMixpanel('Sign up', newAccount);
return newAccount;
})
)
Expand Down Expand Up @@ -636,4 +639,11 @@ export class AccountService {
this.setAccount(newAccount);
this.accountStorageUpdate.next(newAccount);
}

public trackAuthWithMixpanel(action: string, accountData: AccountVO): void {
this.mixpanel.track(action, {
accountId: accountData.accountId,
email: accountData.primaryEmail,
});
}
}
1 change: 1 addition & 0 deletions src/app/shared/services/mixpanel/mixpanel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class MixpanelService {
: `${account.accountId}`;
mixpanel.identify(mixpanelIdentifier);
mixpanel.people.set({
$email: account.primaryEmail,
accountId: `${account.accountId}`,
environment: environment.environment,
});
Expand Down

0 comments on commit 6e32ad6

Please sign in to comment.