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

task(recovery-phone): Add hasConfirmed method to RecoveryPhoneService #18060

Merged
merged 2 commits into from
Nov 26, 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 @@ -108,6 +108,46 @@ describe('RecoveryPhoneService', () => {
);
});

describe('has confirmed code', () => {
it('can determine confirmed phone number exists', async () => {
mockRecoveryPhoneManager.getConfirmedPhoneNumber.mockReturnValueOnce({
phoneNumber,
});

const result = await service.hasConfirmed(uid);

expect(result.exists).toBeTruthy();
expect(result.phoneNumber).toEqual(phoneNumber);
expect(
mockRecoveryPhoneManager.getConfirmedPhoneNumber
).toHaveBeenCalledWith(uid);
});

it('can determine confirmed phone number does not exist', async () => {
const mockError = new RecoveryNumberNotExistsError(uid);
mockRecoveryPhoneManager.getConfirmedPhoneNumber.mockRejectedValueOnce(
mockError
);

const result = await service.hasConfirmed(uid);

expect(result.exists).toEqual(false);
expect(result.phoneNumber).toBeUndefined();
expect(
mockRecoveryPhoneManager.getConfirmedPhoneNumber
).toHaveBeenCalledWith(uid);
});

it('can propagate unexpected error', () => {
const mockError = new Error(uid);
mockRecoveryPhoneManager.getConfirmedPhoneNumber.mockRejectedValueOnce(
mockError
);

expect(service.hasConfirmed(uid)).rejects.toEqual(mockError);
});
});

describe('confirm code', () => {
it('can confirm valid sms code', async () => {
mockRecoveryPhoneManager.getUnconfirmed.mockReturnValue({});
Expand Down
30 changes: 29 additions & 1 deletion libs/accounts/recovery-phone/src/lib/recovery-phone.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { SmsManager } from './sms.manager';
import { OtpManager } from '@fxa/shared/otp';
import { RecoveryPhoneServiceConfig } from './recovery-phone.service.config';
import { RecoveryPhoneManager } from './recovery-phone.manager';
import { RecoveryNumberNotSupportedError } from './recovery-phone.errors';
import {
RecoveryNumberNotExistsError,
RecoveryNumberNotSupportedError,
RecoveryNumberNotSupportedError
} from './recovery-phone.errors';
import { MessageInstance } from 'twilio/lib/rest/api/v2010/account/message';

@Injectable()
Expand Down Expand Up @@ -93,6 +97,30 @@ export class RecoveryPhoneService {
return await this.recoveryPhoneManager.removePhoneNumber(uid);
}

/**
* Checks if the given uid has confirmed a phone number.
* @param uid Account id
* @returns If the account has confirmed, returns {exists:true, phoneNumber }. If not returns {exists:false}
*/
public async hasConfirmed(uid: string) {
try {
const data = await this.recoveryPhoneManager.getConfirmedPhoneNumber(uid);
return {
exists: true,
phoneNumber: data.phoneNumber,
};
} catch (err) {
if (err instanceof RecoveryNumberNotExistsError) {
// no-op - we handle the error, and just return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽

return {
exists: false,
};
}
// Something unexpected happened...
throw err;
}
}

/**
* Sends an top code to a user
* @param uid Account id
Expand Down