Commit 520cafd3 authored by Ryan Diehl's avatar Ryan Diehl

fix(address): extract dialog prompting logic

parent d2e71c21
Pipeline #113662 passed with stages
in 4 minutes and 49 seconds
......@@ -3,6 +3,7 @@ import { select, Store } from '@ngrx/store';
import {
AddressVerificationResult,
concatSelectedAddresses,
handleAddressVerificationResults,
UserAddressVerifierService
} from '@psu/apis/address-verification';
import { ScimAddress } from '@psu/apis/models';
......@@ -76,26 +77,9 @@ export class AddressVerificationFacade {
return this.store.select(selectAddressVerificationCombinedResults).pipe(
filter(combined => !!combined),
take(1),
switchMap(combined => {
// figure out if we need to prompt the user.
// for now, only prompt if prompted items exist.
// TODO prompt for invalid items, all the user can do is cancel those.
if (combined.prompted.length > 0) {
return this.userVerifier.verifyAddresses(...combined.prompted).pipe(
take(1),
filter(dialogResults => !!dialogResults),
map((dialogResults: ScimAddress[]) => this.combineResults(combined, dialogResults))
);
} else {
return of(this.combineResults(combined));
}
})
switchMap(combined => handleAddressVerificationResults(combined, this.userVerifier))
);
}
private combineResults(combined, dialogResults: ScimAddress[] = []): ScimAddress[] {
return [...combined.unmodified, ...combined.functionallyEqual, ...combined.invalid, ...dialogResults];
}
}
// return combineLatest([this.stagedVerificationAddresses$, this.transformedScimAddresses$, this.or]).pipe(
// map(([staged, transformed]) => ({ staged, transformed })),
......
export * from './lib/address-verification.config';
export * from './lib/address-verification.model';
export * from './lib/address-verification.service';
export * from './lib/address-verification.utils';
export * from './lib/user-address-verifier.service';
import { ScimAddress, ScimAddressType } from '@psu/apis/models';
import { Subject } from 'rxjs';
import { Mock } from 'ts-mocks';
import { AddressVerificationCombinedResults } from './address-verification.model';
import { handleAddressVerificationResults } from './address-verification.utils';
import { UserAddressVerifierService } from './user-address-verifier.service';
describe('handleAddressVerificationResults', () => {
const combined: AddressVerificationCombinedResults = {
prompted: [
{
addressTransformed: true,
inputAddress: {
type: ScimAddressType.W4,
streetAddress: '4'
} as any,
outputAddress: { type: ScimAddressType.W4, streetAddress: '4a' } as any
},
{
addressTransformed: true,
inputAddress: {
type: ScimAddressType.Permanent,
streetAddress: '2'
} as any,
outputAddress: { type: ScimAddressType.Permanent, streetAddress: '2a' } as any
}
],
functionallyEqual: [
{
type: ScimAddressType.Work,
streetAddress: '3'
} as any
],
invalid: [],
unmodified: [
{
type: ScimAddressType.Local,
streetAddress: '1'
} as any
]
};
let verifyResult$: Subject<ScimAddress[]>;
let userVerifier: Mock<UserAddressVerifierService>;
beforeEach(() => {
verifyResult$ = new Subject();
userVerifier = new Mock<UserAddressVerifierService>({
verifyAddresses: () => verifyResult$.asObservable()
});
});
it('should emit results if there are no prompted results', () => {
let result: ScimAddress[];
handleAddressVerificationResults({ ...combined, prompted: [] }, userVerifier.Object).subscribe(r => (result = r));
expect(result).toEqual([...combined.unmodified, ...combined.functionallyEqual, ...combined.invalid]);
});
it('should not emit results if the user cancels the verification', () => {
let result: ScimAddress[];
handleAddressVerificationResults(combined, userVerifier.Object).subscribe(r => (result = r));
expect(result).toBeUndefined();
verifyResult$.next();
expect(result).toBeUndefined();
});
it('should not emit results if the user verification errors', () => {
let result: ScimAddress[];
handleAddressVerificationResults(combined, userVerifier.Object).subscribe(r => (result = r));
expect(result).toBeUndefined();
verifyResult$.error('');
expect(result).toBeUndefined();
});
it('should emit combined results with prompted results if user confirms the verification', () => {
let result: ScimAddress[];
handleAddressVerificationResults(combined, userVerifier.Object).subscribe(r => (result = r));
expect(result).toBeUndefined();
verifyResult$.next([
{
type: ScimAddressType.W4,
streetAddress: '4'
} as any,
{ type: ScimAddressType.Permanent, streetAddress: '2a' } as any
]);
expect(result).toEqual([
{
type: ScimAddressType.Local,
streetAddress: '1'
} as any,
{
type: ScimAddressType.Work,
streetAddress: '3'
} as any,
{
type: ScimAddressType.W4,
streetAddress: '4'
} as any,
{ type: ScimAddressType.Permanent, streetAddress: '2a' } as any
]);
});
});
import { ScimAddress } from '@psu/apis/models';
import { Observable, of } from 'rxjs';
import { filter, map, take } from 'rxjs/operators';
import { AddressVerificationCombinedResults } from './address-verification.model';
import { UserAddressVerifierService } from './user-address-verifier.service';
export function handleAddressVerificationResults(
combinedResults: AddressVerificationCombinedResults,
userVerifier: UserAddressVerifierService
): Observable<ScimAddress[]> {
const combineResults = (combined, dialogResults: ScimAddress[] = []): ScimAddress[] => [
...combined.unmodified,
...combined.functionallyEqual,
...combined.invalid,
...dialogResults
];
if (combinedResults.prompted.length > 0) {
return userVerifier.verifyAddresses(...combinedResults.prompted).pipe(
take(1),
filter(dialogResults => !!dialogResults),
map((dialogResults: ScimAddress[]) => combineResults(combinedResults, dialogResults))
);
} else {
return of(combineResults(combinedResults));
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment