Skip to content

Commit

Permalink
feat: display buyer widget for B2B customers on checkout and order de…
Browse files Browse the repository at this point in the history
…tails pages (#486)
  • Loading branch information
SGrueber authored and shauke committed Dec 17, 2020
1 parent 6cc98e0 commit 5f046c1
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('Requisition Mapper', () => {
"customerApprovers": undefined,
"status": "APPROVED",
},
"attributes": undefined,
"bucketId": undefined,
"commonShipToAddress": undefined,
"commonShippingMethod": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ <h1>{{ 'account.orderdetails.heading.default' | translate }}</h1>
</div>

<ng-container *ngIf="order">
<div *ishFeature="'businessCustomerRegistration'" class="row">
<!-- Buyer-->
<ish-info-box heading="checkout.widget.buyer.heading" class="infobox-wrapper col-md-6">
<ish-basket-buyer [object]="order"></ish-basket-buyer>
</ish-info-box>
</div>
<div class="row d-flex">
<!-- Invoice Address -->
<ish-info-box heading="checkout.widget.billing-address.heading" class="infobox-wrapper col-md-6">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { FaIconComponent } from '@fortawesome/angular-fontawesome';
import { TranslateModule } from '@ngx-translate/core';
import { MockComponent, MockPipe } from 'ng-mocks';
import { MockComponent, MockDirective, MockPipe } from 'ng-mocks';

import { FeatureToggleDirective } from 'ish-core/directives/feature-toggle.directive';
import { DatePipe } from 'ish-core/pipes/date.pipe';
import { BasketMockData } from 'ish-core/utils/dev/basket-mock-data';
import { AddressComponent } from 'ish-shared/components/address/address/address.component';
import { BasketBuyerComponent } from 'ish-shared/components/basket/basket-buyer/basket-buyer.component';
import { BasketCostSummaryComponent } from 'ish-shared/components/basket/basket-cost-summary/basket-cost-summary.component';
import { InfoBoxComponent } from 'ish-shared/components/common/info-box/info-box.component';
import { LineItemListComponent } from 'ish-shared/components/line-item/line-item-list/line-item-list.component';
Expand All @@ -23,10 +25,12 @@ describe('Account Order Component', () => {
declarations: [
AccountOrderComponent,
MockComponent(AddressComponent),
MockComponent(BasketBuyerComponent),
MockComponent(BasketCostSummaryComponent),
MockComponent(FaIconComponent),
MockComponent(InfoBoxComponent),
MockComponent(LineItemListComponent),
MockDirective(FeatureToggleDirective),
MockPipe(DatePipe),
],
imports: [RouterTestingModule, TranslateModule.forRoot()],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ <h3 class="subheading">{{ 'checkout.addresses.billing_address.heading' | transla
data-testing-id="invoiceAddressForm"
[parentForm]="invoiceAddressForm"
></ish-address-form-container>
<!-- Taxation ID input field -->
<ish-input
*ishFeature="'businessCustomerRegistration'"
[form]="form"
controlName="taxationID"
label="account.address.taxation.label"
></ish-input>
<!-- Email address input field-->
<ish-input
[form]="form"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormBuilder, FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { NgbCollapseModule } from '@ng-bootstrap/ng-bootstrap';
import { TranslateModule } from '@ngx-translate/core';
import { MockComponent } from 'ng-mocks';
import { MockComponent, MockDirective } from 'ng-mocks';
import { anything, instance, mock, verify } from 'ts-mockito';

import { FeatureToggleDirective } from 'ish-core/directives/feature-toggle.directive';
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { makeHttpError } from 'ish-core/utils/dev/api-service-utils';
import { AddressFormContainerComponent } from 'ish-shared/address-forms/components/address-form-container/address-form-container.component';
Expand All @@ -31,6 +32,7 @@ describe('Checkout Address Anonymous Component', () => {
MockComponent(ErrorMessageComponent),
MockComponent(IdentityProviderLoginComponent),
MockComponent(InputComponent),
MockDirective(FeatureToggleDirective),
],
imports: [NgbCollapseModule, ReactiveFormsModule, TranslateModule.forRoot()],
providers: [{ provide: CheckoutFacade, useFactory: () => instance(checkoutFacade) }],
Expand Down Expand Up @@ -118,6 +120,7 @@ describe('Checkout Address Anonymous Component', () => {
it('should create address for valid invoice address form', () => {
component.form = fb.group({
email: new FormControl(''),
taxationID: new FormControl(''),
shipOption: new FormControl('shipToInvoiceAddress'),
});
component.invoiceAddressForm = fb.group({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class CheckoutAddressAnonymousComponent implements OnChanges, OnInit, OnD
ngOnInit() {
// create address form for basket addresses
this.form = this.fb.group({
taxationID: [''],
email: ['', [Validators.required, SpecialValidators.email]],
shipOption: ['shipToInvoiceAddress', [Validators.required]],
});
Expand Down Expand Up @@ -126,6 +127,12 @@ export class CheckoutAddressAnonymousComponent implements OnChanges, OnInit, OnD
? undefined
: this.shippingAddressForm.get('address').value;

if (this.form.get('taxationID').value) {
this.checkoutFacade.setBasketCustomAttribute({ name: 'taxationID', value: this.form.get('taxationID').value });
} else {
this.checkoutFacade.deleteBasketCustomAttribute('taxationID');
}

if (shippingAddress) {
this.checkoutFacade.createBasketAddress(invoiceAddress, 'invoice');
this.checkoutFacade.createBasketAddress(shippingAddress, 'shipping');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
<!-- header as content -->
<ng-content></ng-content>

<div *ishFeature="'businessCustomerRegistration'" class="row">
<!-- Buyer-->
<ish-info-box heading="checkout.widget.buyer.heading" class="infobox-wrapper col-md-6">
<ish-basket-buyer [object]="order"></ish-basket-buyer>
</ish-info-box>
</div>

<div class="row d-flex">
<!-- Invoice Address -->
<ish-info-box heading="checkout.widget.billing-address.heading" class="infobox-wrapper col-md-6">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FaIconComponent } from '@fortawesome/angular-fontawesome';
import { TranslateModule } from '@ngx-translate/core';
import { MockComponent } from 'ng-mocks';
import { MockComponent, MockDirective } from 'ng-mocks';

import { FeatureToggleDirective } from 'ish-core/directives/feature-toggle.directive';
import { BasketMockData } from 'ish-core/utils/dev/basket-mock-data';
import { AddressComponent } from 'ish-shared/components/address/address/address.component';
import { BasketBuyerComponent } from 'ish-shared/components/basket/basket-buyer/basket-buyer.component';
import { BasketCostSummaryComponent } from 'ish-shared/components/basket/basket-cost-summary/basket-cost-summary.component';
import { InfoBoxComponent } from 'ish-shared/components/common/info-box/info-box.component';
import { LineItemListComponent } from 'ish-shared/components/line-item/line-item-list/line-item-list.component';
Expand All @@ -21,10 +23,12 @@ describe('Checkout Receipt Component', () => {
declarations: [
CheckoutReceiptComponent,
MockComponent(AddressComponent),
MockComponent(BasketBuyerComponent),
MockComponent(BasketCostSummaryComponent),
MockComponent(FaIconComponent),
MockComponent(InfoBoxComponent),
MockComponent(LineItemListComponent),
MockDirective(FeatureToggleDirective),
],
imports: [TranslateModule.forRoot()],
}).compileComponents();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ <h1 class="d-flex flex-wrap align-items-baseline">
</p>
</div>
<ish-basket-approval-info *ngIf="basket.approval" [approval]="basket.approval"></ish-basket-approval-info>
<div *ishFeature="'businessCustomerRegistration'" class="row">
<!-- Buyer-->
<ish-info-box heading="checkout.widget.buyer.heading" class="infobox-wrapper col-md-6">
<ish-basket-buyer [object]="basket"></ish-basket-buyer>
</ish-info-box>
</div>
<div class="row d-flex">
<!-- Invoice Address -->
<ish-info-box
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { TranslateModule } from '@ngx-translate/core';
import { MockComponent, MockDirective } from 'ng-mocks';
import { spy, verify } from 'ts-mockito';

import { FeatureToggleDirective } from 'ish-core/directives/feature-toggle.directive';
import { ServerHtmlDirective } from 'ish-core/directives/server-html.directive';
import { BasketApproval } from 'ish-core/models/basket-approval/basket-approval.model';
import { makeHttpError } from 'ish-core/utils/dev/api-service-utils';
import { BasketMockData } from 'ish-core/utils/dev/basket-mock-data';
import { ContentIncludeComponent } from 'ish-shared/cms/components/content-include/content-include.component';
import { AddressComponent } from 'ish-shared/components/address/address/address.component';
import { BasketApprovalInfoComponent } from 'ish-shared/components/basket/basket-approval-info/basket-approval-info.component';
import { BasketBuyerComponent } from 'ish-shared/components/basket/basket-buyer/basket-buyer.component';
import { BasketCostSummaryComponent } from 'ish-shared/components/basket/basket-cost-summary/basket-cost-summary.component';
import { BasketValidationResultsComponent } from 'ish-shared/components/basket/basket-validation-results/basket-validation-results.component';
import { ErrorMessageComponent } from 'ish-shared/components/common/error-message/error-message.component';
Expand All @@ -33,6 +35,7 @@ describe('Checkout Review Component', () => {
CheckoutReviewComponent,
MockComponent(AddressComponent),
MockComponent(BasketApprovalInfoComponent),
MockComponent(BasketBuyerComponent),
MockComponent(BasketCostSummaryComponent),
MockComponent(BasketValidationResultsComponent),
MockComponent(CheckboxComponent),
Expand All @@ -42,6 +45,7 @@ describe('Checkout Review Component', () => {
MockComponent(InfoBoxComponent),
MockComponent(LineItemListComponent),
MockComponent(ModalDialogLinkComponent),
MockDirective(FeatureToggleDirective),
MockDirective(ServerHtmlDirective),
],
imports: [ReactiveFormsModule, TranslateModule.forRoot()],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div *ngIf="object">
<ng-container *ngIf="customer$ | async as customer; else companyFromInvoice">
{{ customer.companyName }} {{ customer.companyName2 }}
</ng-container>
<ng-template #companyFromInvoice>
{{ object.invoiceToAddress.companyName1 }} {{ object.invoiceToAddress.companyName2 }}
</ng-template>
<span *ngIf="taxationID" data-testing-id="taxationID">
<br />{{ 'checkout.widget.buyer.TaxationID' | translate }} {{ taxationID }}
</span>
<p>
{{ userName }}<br />
{{ object.email || object.invoiceToAddress?.email }}
</p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { of } from 'rxjs';
import { instance, mock, when } from 'ts-mockito';

import { AccountFacade } from 'ish-core/facades/account.facade';
import { Customer } from 'ish-core/models/customer/customer.model';
import { User } from 'ish-core/models/user/user.model';
import { BasketMockData } from 'ish-core/utils/dev/basket-mock-data';

import { BasketBuyerComponent } from './basket-buyer.component';

describe('Basket Buyer Component', () => {
let component: BasketBuyerComponent;
let fixture: ComponentFixture<BasketBuyerComponent>;
let element: HTMLElement;
let accountFacade: AccountFacade;

beforeEach(async () => {
accountFacade = mock(AccountFacade);

await TestBed.configureTestingModule({
imports: [TranslateModule.forRoot()],
declarations: [BasketBuyerComponent],
providers: [{ provide: AccountFacade, useFactory: () => instance(accountFacade) }],
}).compileComponents();
});

beforeEach(() => {
accountFacade = mock(AccountFacade);

fixture = TestBed.createComponent(BasketBuyerComponent);
component = fixture.componentInstance;
element = fixture.nativeElement;

component.object = BasketMockData.getBasket();

when(accountFacade.user$).thenReturn(of({ firstName: 'Patricia', lastName: 'Miller' } as User));
when(accountFacade.customer$).thenReturn(of({ companyName: 'OilCorp', taxationID: '1234' } as Customer));
});

it('should be created', () => {
expect(component).toBeTruthy();
expect(element).toBeTruthy();
expect(() => fixture.detectChanges()).not.toThrow();
});

it('should display the taxation id of the customer', () => {
fixture.detectChanges();
expect(element.querySelector('[data-testing-id="taxationID"]')).toBeTruthy();
expect(element.querySelector('[data-testing-id="taxationID"]').innerHTML).toContain('1234');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ChangeDetectionStrategy, Component, Input, OnDestroy, OnInit } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { first, takeUntil } from 'rxjs/operators';

import { AccountFacade } from 'ish-core/facades/account.facade';
import { Basket } from 'ish-core/models/basket/basket.model';
import { Customer } from 'ish-core/models/customer/customer.model';
import { Order } from 'ish-core/models/order/order.model';
import { User } from 'ish-core/models/user/user.model';
import { whenTruthy } from 'ish-core/utils/operators';

@Component({
selector: 'ish-basket-buyer',
templateUrl: './basket-buyer.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BasketBuyerComponent implements OnInit, OnDestroy {
@Input() object: Basket | Order;

customer$: Observable<Customer>;
user$: Observable<User>;

taxationID: string;
userName: string;

private destroy$ = new Subject();

constructor(private accountFacade: AccountFacade) {}

ngOnInit() {
// default values for anonymous users
this.taxationID = this.object.attributes?.find(attr => attr.name === 'taxationID')?.value as string;
this.userName = `${this.object.invoiceToAddress?.firstName} ${this.object.invoiceToAddress?.lastName}`;

this.customer$ = this.accountFacade.customer$;
this.user$ = this.accountFacade.user$;

// values for registered users
this.customer$.pipe(whenTruthy(), first(), takeUntil(this.destroy$)).subscribe(customer => {
this.taxationID = customer?.taxationID;
});

this.user$.pipe(whenTruthy(), first(), takeUntil(this.destroy$)).subscribe(user => {
this.userName = `${user.firstName} ${user.lastName}`;
});
}

ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}
2 changes: 2 additions & 0 deletions src/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { ContentViewcontextComponent } from './cms/components/content-viewcontex
import { AddressComponent } from './components/address/address/address.component';
import { BasketAddressSummaryComponent } from './components/basket/basket-address-summary/basket-address-summary.component';
import { BasketApprovalInfoComponent } from './components/basket/basket-approval-info/basket-approval-info.component';
import { BasketBuyerComponent } from './components/basket/basket-buyer/basket-buyer.component';
import { BasketCostSummaryComponent } from './components/basket/basket-cost-summary/basket-cost-summary.component';
import { BasketInfoComponent } from './components/basket/basket-info/basket-info.component';
import { BasketItemsSummaryComponent } from './components/basket/basket-items-summary/basket-items-summary.component';
Expand Down Expand Up @@ -180,6 +181,7 @@ const exportedComponents = [
AddressComponent,
BasketAddressSummaryComponent,
BasketApprovalInfoComponent,
BasketBuyerComponent,
BasketCostSummaryComponent,
BasketInfoComponent,
BasketInvoiceAddressWidgetComponent,
Expand Down
2 changes: 2 additions & 0 deletions src/assets/i18n/de_DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,8 @@
"checkout.termsandconditions.details.title": "Allgemeine Geschäftsbedingungen",
"checkout.variation.edit.button.label": "Bearbeiten",
"checkout.widget.billing-address.heading": "Rechnungsadresse",
"checkout.widget.buyer.TaxationID": "Steuernummer:",
"checkout.widget.buyer.heading": "Einkäufer",
"checkout.widget.payment_method.heading": "Zahlungsart",
"checkout.widget.promotion.discount": "Rabatt",
"checkout.widget.return_to_cart.link": "Zurück zum Warenkorb",
Expand Down
2 changes: 2 additions & 0 deletions src/assets/i18n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,8 @@
"checkout.termsandconditions.details.title": "Terms & Conditions",
"checkout.variation.edit.button.label": "Edit",
"checkout.widget.billing-address.heading": "Invoice Address",
"checkout.widget.buyer.TaxationID": "Taxation ID:",
"checkout.widget.buyer.heading": "Buyer",
"checkout.widget.payment_method.heading": "Payment Method",
"checkout.widget.promotion.discount": "Discount",
"checkout.widget.return_to_cart.link": "Return to Cart",
Expand Down
2 changes: 2 additions & 0 deletions src/assets/i18n/fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,8 @@
"checkout.termsandconditions.details.title": "Conditions générales",
"checkout.variation.edit.button.label": "Modifier",
"checkout.widget.billing-address.heading": "Adresse de facturation",
"checkout.widget.buyer.TaxationID": "ID de taxation:",
"checkout.widget.buyer.heading": "Acheteur",
"checkout.widget.payment_method.heading": "Mode de paiement",
"checkout.widget.promotion.discount": "Réduction",
"checkout.widget.return_to_cart.link": "Retour au panier",
Expand Down

0 comments on commit 5f046c1

Please sign in to comment.