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: custom fields on basket/orders and line-items #1561

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
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 @@ -97,6 +97,7 @@ describe('Requisition Mapper', () => {
"commonShippingMethod": undefined,
"costCenter": "CostCenter123",
"creationDate": 12345678,
"customFields": {},
"customerNo": "OilCorp",
"dynamicMessages": undefined,
"email": "[email protected]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ <h2>{{ 'approval.detailspage.order_details.heading' | translate }}</h2>
<ish-basket-merchant-message-view [data]="requisition" />
</div>

<ish-basket-custom-fields-view [data]="requisition" />

<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 @@ -13,6 +13,7 @@ import { AddressComponent } from 'ish-shared/components/address/address/address.
import { BasketCostSummaryComponent } from 'ish-shared/components/basket/basket-cost-summary/basket-cost-summary.component';
import { BasketMerchantMessageViewComponent } from 'ish-shared/components/basket/basket-merchant-message-view/basket-merchant-message-view.component';
import { BasketShippingMethodComponent } from 'ish-shared/components/basket/basket-shipping-method/basket-shipping-method.component';
import { BasketCustomFieldsViewComponent } from 'ish-shared/components/checkout/basket-custom-fields-view/basket-custom-fields-view.component';
import { ErrorMessageComponent } from 'ish-shared/components/common/error-message/error-message.component';
import { InfoBoxComponent } from 'ish-shared/components/common/info-box/info-box.component';

Expand All @@ -38,6 +39,7 @@ describe('Requisition Detail Page Component', () => {
declarations: [
MockComponent(AddressComponent),
MockComponent(BasketCostSummaryComponent),
MockComponent(BasketCustomFieldsViewComponent),
MockComponent(BasketMerchantMessageViewComponent),
MockComponent(BasketShippingMethodComponent),
MockComponent(ErrorMessageComponent),
Expand Down Expand Up @@ -81,6 +83,7 @@ describe('Requisition Detail Page Component', () => {
"ish-requisition-summary",
"ish-requisition-cost-center-approval",
"ish-basket-merchant-message-view",
"ish-basket-custom-fields-view",
"ish-info-box",
"ish-address",
"ish-info-box",
Expand Down
16 changes: 15 additions & 1 deletion src/app/core/facades/app.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Store, select } from '@ngrx/store';
import { combineLatest, merge, noop } from 'rxjs';
import { filter, map, sample, shareReplay, startWith, withLatestFrom } from 'rxjs/operators';

import { CustomFieldDefinitionScopes } from 'ish-core/models/server-config/server-config.interface';
import {
getAvailableLocales,
getCurrentCurrency,
Expand All @@ -15,7 +16,12 @@ import {
} from 'ish-core/store/core/configuration';
import { businessError, getGeneralError, getGeneralErrorType } from 'ish-core/store/core/error';
import { selectPath } from 'ish-core/store/core/router';
import { getExtraConfigParameter, getServerConfigParameter } from 'ish-core/store/core/server-config';
import {
getCustomFieldDefinition,
getCustomFieldIdsForScope,
getExtraConfigParameter,
getServerConfigParameter,
} from 'ish-core/store/core/server-config';
import { getBreadcrumbData, getHeaderType, getWrapperClass, isStickyHeader } from 'ish-core/store/core/viewconf';
import { getLoggedInCustomer } from 'ish-core/store/customer/user';
import { getAllCountries, loadCountries } from 'ish-core/store/general/countries';
Expand Down Expand Up @@ -123,6 +129,14 @@ export class AppFacade {
return this.store.pipe(select(getExtraConfigParameter<T>(path)));
}

customFieldsForScope$(scope: CustomFieldDefinitionScopes) {
return this.store.pipe(select(getCustomFieldIdsForScope(scope)));
}

customField$(name: string) {
return this.store.pipe(select(getCustomFieldDefinition(name)));
}

/**
* returns the currency symbol for the currency parameter in the current locale.
* If no parameter is given, the the default currency is taken instead of it.
Expand Down
12 changes: 9 additions & 3 deletions src/app/core/facades/checkout.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { debounceTime, distinctUntilChanged, map, sample, switchMap, take, tap }
import { Address } from 'ish-core/models/address/address.model';
import { Attribute } from 'ish-core/models/attribute/attribute.model';
import { CheckoutStepType } from 'ish-core/models/checkout/checkout-step.type';
import { CustomFields } from 'ish-core/models/custom-field/custom-field.model';
import { LineItemUpdate } from 'ish-core/models/line-item-update/line-item-update.model';
import { PaymentInstrument } from 'ish-core/models/payment-instrument/payment-instrument.model';
import { selectRouteData } from 'ish-core/store/core/router';
Expand Down Expand Up @@ -44,6 +45,7 @@ import {
loadBasketWithId,
removePromotionCodeFromBasket,
setBasketAttribute,
setBasketCustomFields,
setBasketDesiredDeliveryDate,
setBasketPayment,
startCheckout,
Expand Down Expand Up @@ -124,10 +126,10 @@ export class CheckoutFacade {
}

updateBasketItem(update: LineItemUpdate) {
if (update.quantity) {
this.store.dispatch(updateBasketItem({ lineItemUpdate: update }));
} else {
if (update.quantity === 0) {
this.store.dispatch(deleteBasketItem({ itemId: update.itemId }));
} else {
this.store.dispatch(updateBasketItem({ lineItemUpdate: update }));
}
}

Expand Down Expand Up @@ -160,6 +162,10 @@ export class CheckoutFacade {
this.store.dispatch(addMessageToMerchant({ messageToMerchant: messageToMerchant || null }));
}

setBasketCustomFields(customFields: CustomFields) {
this.store.dispatch(setBasketCustomFields({ customFields }));
}

// ORDERS

private ordersError$ = this.store.pipe(select(getOrdersError));
Expand Down
2 changes: 2 additions & 0 deletions src/app/core/models/basket/basket.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BasketInfo } from 'ish-core/models/basket-info/basket-info.model';
import { BasketRebateData } from 'ish-core/models/basket-rebate/basket-rebate.interface';
import { BasketTotalData } from 'ish-core/models/basket-total/basket-total.interface';
import { BasketWarrantyData } from 'ish-core/models/basket-warranty/basket-warranty.interface';
import { CustomFieldData } from 'ish-core/models/custom-field/custom-field.interface';
import { LineItemData } from 'ish-core/models/line-item/line-item.interface';
import { PaymentInstrument } from 'ish-core/models/payment-instrument/payment-instrument.model';
import { PaymentMethodBaseData } from 'ish-core/models/payment-method/payment-method.interface';
Expand Down Expand Up @@ -55,6 +56,7 @@ export interface BasketBaseData {
firstName: string;
lastName: string;
};
customFields?: CustomFieldData[];
}

export interface BasketIncludedData {
Expand Down
2 changes: 2 additions & 0 deletions src/app/core/models/basket/basket.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BasketRebateData } from 'ish-core/models/basket-rebate/basket-rebate.in
import { BasketRebateMapper } from 'ish-core/models/basket-rebate/basket-rebate.mapper';
import { BasketTotal } from 'ish-core/models/basket-total/basket-total.model';
import { BasketBaseData, BasketData } from 'ish-core/models/basket/basket.interface';
import { CustomFieldMapper } from 'ish-core/models/custom-field/custom-field.mapper';
import { LineItemMapper } from 'ish-core/models/line-item/line-item.mapper';
import { PaymentMapper } from 'ish-core/models/payment/payment.mapper';
import { PriceItemMapper } from 'ish-core/models/price-item/price-item.mapper';
Expand Down Expand Up @@ -74,6 +75,7 @@ export class BasketMapper {
user: data.buyer,
externalOrderReference: data.externalOrderReference,
messageToMerchant: data.messageToMerchant,
customFields: CustomFieldMapper.fromData(data.customFields),
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/app/core/models/basket/basket.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BasketApproval } from 'ish-core/models/basket-approval/basket-approval.
import { BasketInfo } from 'ish-core/models/basket-info/basket-info.model';
import { BasketTotal } from 'ish-core/models/basket-total/basket-total.model';
import { BasketValidationResultType } from 'ish-core/models/basket-validation/basket-validation.model';
import { CustomFields } from 'ish-core/models/custom-field/custom-field.model';
import { LineItem, LineItemView } from 'ish-core/models/line-item/line-item.model';
import { Payment } from 'ish-core/models/payment/payment.model';
import { ShippingMethod } from 'ish-core/models/shipping-method/shipping-method.model';
Expand Down Expand Up @@ -36,6 +37,7 @@ export interface AbstractBasket<T> {
};
externalOrderReference?: string;
messageToMerchant?: string;
customFields?: CustomFields;
}

export type Basket = AbstractBasket<LineItem>;
Expand Down
5 changes: 5 additions & 0 deletions src/app/core/models/custom-field/custom-field.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Attribute } from 'ish-core/models/attribute/attribute.model';

export interface CustomFieldData extends Attribute<string> {
type: 'String';
}
59 changes: 59 additions & 0 deletions src/app/core/models/custom-field/custom-field.mapper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { CustomFieldData } from './custom-field.interface';
import { CustomFieldMapper } from './custom-field.mapper';

describe('Custom Field Mapper', () => {
describe('fromData', () => {
it('should return empty object if no data supplied', () => {
const mapped = CustomFieldMapper.fromData();
expect(mapped).toMatchInlineSnapshot(`{}`);
});

it('should return empty object if empty data supplied', () => {
const mapped = CustomFieldMapper.fromData([]);
expect(mapped).toMatchInlineSnapshot(`{}`);
});

it('should map incoming data to model data', () => {
const data: CustomFieldData[] = [
{
name: 'foo',
value: 'foo',
type: 'String',
},
{
name: 'bar',
value: 'bar',
type: 'String',
},
];
const mapped = CustomFieldMapper.fromData(data);
expect(mapped).toMatchInlineSnapshot(`
{
"bar": "bar",
"foo": "foo",
}
`);
});

it('should filter empty data', () => {
const data: CustomFieldData[] = [
{
name: 'foo',
value: '',
type: 'String',
},
{
name: 'bar',
value: 'bar',
type: 'String',
},
];
const mapped = CustomFieldMapper.fromData(data);
expect(mapped).toMatchInlineSnapshot(`
{
"bar": "bar",
}
`);
});
});
});
19 changes: 19 additions & 0 deletions src/app/core/models/custom-field/custom-field.mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { CustomFieldData } from './custom-field.interface';
import { CustomFields } from './custom-field.model';

export class CustomFieldMapper {
static fromData(customFieldData: CustomFieldData[] = []): CustomFields {
return customFieldData.filter(CustomFieldMapper.hasValue).reduce<CustomFields>((customFields, customField) => {
customFields[customField.name] = customField.value;
return customFields;
}, {});
}

private static hasValue(customFieldData: CustomFieldData): boolean {
if (customFieldData.type === 'String') {
return !!customFieldData.value;
}
// eslint-disable-next-line unicorn/no-null
return customFieldData.value !== null;
}
}
9 changes: 9 additions & 0 deletions src/app/core/models/custom-field/custom-field.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { CustomFieldDefinitionScopeType } from 'ish-core/models/server-config/server-config.model';

type CustomFieldValue = string;

export type CustomFields = Record<string, CustomFieldValue>;

export type CustomFieldsComponentInput = CustomFieldDefinitionScopeType & {
value: CustomFieldValue;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { CustomFields } from 'ish-core/models/custom-field/custom-field.model';

export interface LineItemUpdate {
itemId: string;
quantity?: number;
sku?: string;
unit?: string;
customFields?: CustomFields;
warrantySku?: string;
}
2 changes: 2 additions & 0 deletions src/app/core/models/line-item/line-item.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CustomFieldData } from 'ish-core/models/custom-field/custom-field.interface';
import { PriceItemData } from 'ish-core/models/price-item/price-item.interface';
import { PriceData } from 'ish-core/models/price/price.interface';

Expand Down Expand Up @@ -35,5 +36,6 @@ export interface LineItemData {
quantityFixed?: boolean;
quote?: string;
desiredDelivery?: string;
customFields?: CustomFieldData[];
warranty?: string;
}
2 changes: 2 additions & 0 deletions src/app/core/models/line-item/line-item.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BasketRebateData } from 'ish-core/models/basket-rebate/basket-rebate.in
import { BasketRebateMapper } from 'ish-core/models/basket-rebate/basket-rebate.mapper';
import { BasketWarrantyData } from 'ish-core/models/basket-warranty/basket-warranty.interface';
import { BasketWarrantyMapper } from 'ish-core/models/basket-warranty/basket-warranty.mapper';
import { CustomFieldMapper } from 'ish-core/models/custom-field/custom-field.mapper';
import { OrderItemData } from 'ish-core/models/order-item/order-item.interface';
import { OrderLineItem } from 'ish-core/models/order/order.model';
import { PriceItemMapper } from 'ish-core/models/price-item/price-item.mapper';
Expand Down Expand Up @@ -56,6 +57,7 @@ export class LineItemMapper {
editable: !data.quantityFixed,
quote: data.quote ? data.quote : undefined,
desiredDeliveryDate: data.desiredDelivery,
customFields: CustomFieldMapper.fromData(data.customFields),
warranty:
data.warranty && warrantyData ? BasketWarrantyMapper.fromData(warrantyData[data.warranty]) : undefined,
};
Expand Down
3 changes: 2 additions & 1 deletion src/app/core/models/line-item/line-item.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BasketFeedback } from 'ish-core/models/basket-feedback/basket-feedback.model';
import { BasketRebate } from 'ish-core/models/basket-rebate/basket-rebate.model';
import { BasketWarranty } from 'ish-core/models/basket-warranty/basket-warranty.model';
import { CustomFields } from 'ish-core/models/custom-field/custom-field.model';
import { PriceItem } from 'ish-core/models/price-item/price-item.model';
import { Price } from 'ish-core/models/price/price.model';
import { SkuQuantityType } from 'ish-core/models/product/product.model';
Expand Down Expand Up @@ -38,7 +39,7 @@ export interface LineItem {
editable: boolean;
quote?: string;
desiredDeliveryDate?: string;

customFields?: CustomFields;
warranty?: BasketWarranty;
}

Expand Down
2 changes: 2 additions & 0 deletions src/app/core/models/order/order.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AddressMapper } from 'ish-core/models/address/address.mapper';
import { AttributeHelper } from 'ish-core/models/attribute/attribute.helper';
import { BasketMapper } from 'ish-core/models/basket/basket.mapper';
import { CustomFieldMapper } from 'ish-core/models/custom-field/custom-field.mapper';
import { LineItemMapper } from 'ish-core/models/line-item/line-item.mapper';
import { PaymentMapper } from 'ish-core/models/payment/payment.mapper';
import { ShippingMethodMapper } from 'ish-core/models/shipping-method/shipping-method.mapper';
Expand Down Expand Up @@ -91,6 +92,7 @@ export class OrderMapper {
user: data.buyer,
messageToMerchant: data.messageToMerchant,
externalOrderReference: data.externalOrderReference,
customFields: CustomFieldMapper.fromData(data.customFields),
};
}
}
Expand Down
18 changes: 17 additions & 1 deletion src/app/core/models/server-config/server-config.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
export type CustomFieldDefinitionScopes = 'Basket' | 'BasketLineItem' | 'Order' | 'OrderLineItem';

export interface CustomFieldDefinitionsData extends ServerConfigDataEntry {
description: string;
displayName: string;
name: string;
position: number;
type: 'String';
scopes: {
isEditable: boolean;
isVisible: boolean;
name: CustomFieldDefinitionScopes;
}[];
}

export interface ServerConfigDataEntry {
[key: string]: string | boolean | number | string[] | ServerConfigDataEntry;
customFieldDefinitions?: CustomFieldDefinitionsData[];
[key: string]: string | boolean | number | string[] | ServerConfigDataEntry[] | ServerConfigDataEntry;
}

export interface ServerConfigData {
Expand Down
Loading
Loading