diff --git a/packages/js/api/src/models/orders/orders.ts b/packages/js/api/src/models/orders/orders.ts index d23c4ca7611..400b4124fa9 100644 --- a/packages/js/api/src/models/orders/orders.ts +++ b/packages/js/api/src/models/orders/orders.ts @@ -9,7 +9,8 @@ import { DeletesModels, } from '../../framework'; import { - OrderAddressUpdateParams, + BillingOrderAddressUpdateParams, + ShippingOrderAddressUpdateParams, OrderCouponUpdateParams, OrderDataUpdateParams, OrderFeeUpdateParams, @@ -19,7 +20,8 @@ import { OrderTaxUpdateParams, OrderTotalUpdateParams, OrderItemMeta, - OrderAddress, + BillingOrderAddress, + ShippingOrderAddress, OrderCouponLine, OrderFeeLine, OrderLineItem, @@ -33,7 +35,8 @@ import { ObjectLinks } from '../shared-types'; /** * The parameters that orders can update. */ -type OrderUpdateParams = OrderAddressUpdateParams +type OrderUpdateParams = BillingOrderAddressUpdateParams + & ShippingOrderAddressUpdateParams & OrderCouponUpdateParams & OrderDataUpdateParams & OrderFeeUpdateParams @@ -194,16 +197,16 @@ export class Order extends OrderItemMeta { /** * The billing address. * - * @type {OrderAddress} + * @type {BillingOrderAddress} */ - public readonly billing: OrderAddress | null = null; + public readonly billing: BillingOrderAddress | null = null; /** * The shipping address. * - * @type {OrderAddress} + * @type {ShippingOrderAddress} */ - public readonly shipping: OrderAddress | null = null; + public readonly shipping: ShippingOrderAddress | null = null; /** * Name of the payment method. diff --git a/packages/js/api/src/models/orders/shared/classes.ts b/packages/js/api/src/models/orders/shared/classes.ts index 540c543d4ae..49e55f2eea4 100644 --- a/packages/js/api/src/models/orders/shared/classes.ts +++ b/packages/js/api/src/models/orders/shared/classes.ts @@ -36,7 +36,7 @@ export class OrderItemTax extends Model { /** * An order address. */ -export class OrderAddress extends Model { +export class ShippingOrderAddress extends Model { /** * The first name of the person in the address. * @@ -56,7 +56,7 @@ export class OrderAddress extends Model { * * @type {string} */ - public readonly companyName: string = ''; + public readonly company: string = ''; /** * The first address line in the address. @@ -98,21 +98,61 @@ export class OrderAddress extends Model { * * @type {string} */ - public readonly countryCode: string = ''; + public readonly country: string = ''; + /** + * Adapter to keep backward compatibility with renamed property. + * + * @type {string|null} + */ + get companyName() { + return this.company; + } + + /** + * Adapter to keep backward compatibility with renamed property. + * + * @type {string|null} + */ + get countryCode() { + return this.country; + } + + /** + * Creates a new order instance with the given properties + * + * @param {Object} properties The properties to set in the object. + */ + public constructor( properties?: Partial< ShippingOrderAddress > ) { + super(); + Object.assign( this, properties ); + } +} + +export class BillingOrderAddress extends ShippingOrderAddress { /** * The email address of the person in the address. * - * @type {string} + * @type {string|null} */ - public readonly email: string = ''; + public readonly email: undefined | string = ''; /** * The phone number of the person in the address. * - * @type {string} + * @type {string|null} */ - public readonly phone: string = ''; + public readonly phone: undefined | string = ''; + + /** + * Creates a new order instance with the given properties + * + * @param {Object} properties The properties to set in the object. + */ + public constructor( properties?: Partial< BillingOrderAddress > ) { + super(); + Object.assign( this, properties ); + } } /** diff --git a/packages/js/api/src/models/orders/shared/types.ts b/packages/js/api/src/models/orders/shared/types.ts index adeba0128c4..cb5454b755b 100644 --- a/packages/js/api/src/models/orders/shared/types.ts +++ b/packages/js/api/src/models/orders/shared/types.ts @@ -28,11 +28,17 @@ export type OrderDataUpdateParams = 'id' | 'parentId' | 'status' | 'currency' | export type OrderTotalUpdateParams = 'total' | 'totalTax'; /** - * Order address properties + * Billing address properties */ -export type OrderAddressUpdateParams = 'firstName' | 'lastName' | 'companyName' | 'address1' +export type BillingOrderAddressUpdateParams = 'firstName' | 'lastName' | 'companyName' | 'address1' | 'address2' | 'city' | 'state' | 'postCode' | 'countryCode' | 'email' | 'phone'; +/** + * Shipping address properties + */ +export type ShippingOrderAddressUpdateParams = 'firstName' | 'lastName' | 'companyName' | 'address1' + | 'address2' | 'city' | 'state' | 'postCode' | 'countryCode'; + /** * Line item properties */ diff --git a/packages/js/api/src/repositories/rest/orders/transformer.ts b/packages/js/api/src/repositories/rest/orders/transformer.ts index 5cd3c0e2b6f..5206c324609 100644 --- a/packages/js/api/src/repositories/rest/orders/transformer.ts +++ b/packages/js/api/src/repositories/rest/orders/transformer.ts @@ -8,7 +8,8 @@ import { } from '../../../framework'; import { Order, - OrderAddress, + BillingOrderAddress, + ShippingOrderAddress, OrderCouponLine, OrderFeeLine, OrderLineItem, @@ -26,7 +27,8 @@ export function createOrderTransformer(): ModelTransformer< Order > { return new ModelTransformer( [ new IgnorePropertyTransformation( [ 'date_created', 'date_modified' ] ), - new ModelTransformerTransformation( 'billing', OrderAddress, createOrderAddressTransformer() ), + new ModelTransformerTransformation( 'billing', BillingOrderAddress, createBillingAddressTransformer() ), + new ModelTransformerTransformation( 'shipping', ShippingOrderAddress, createShippingAddressTransformer() ), new ModelTransformerTransformation( 'tax_lines', OrderTaxRate, createOrderTaxRateTransformer() ), new ModelTransformerTransformation( 'refunds', OrderRefundLine, createOrderRefundLineTransformer() ), new ModelTransformerTransformation( 'coupon_lines', OrderCouponLine, createOrdeCouponLineTransformer() ), @@ -78,7 +80,7 @@ export function createOrderTransformer(): ModelTransformer< Order > { * * @return {ModelTransformer} The created transformer. */ -export function createOrderAddressTransformer(): ModelTransformer< OrderAddress > { +export function createBillingAddressTransformer(): ModelTransformer< BillingOrderAddress > { return new ModelTransformer( [ new PropertyTypeTransformation( @@ -92,9 +94,45 @@ export function createOrderAddressTransformer(): ModelTransformer< OrderAddress state: PropertyType.String, postCode: PropertyType.String, country: PropertyType.String, + phone: PropertyType.String, + email: PropertyType.String, }, ), - new KeyChangeTransformation< OrderAddress >( + new KeyChangeTransformation< BillingOrderAddress >( + { + firstName: 'first_name', + lastName: 'last_name', + address1: 'address_1', + address2: 'address_2', + postCode: 'postcode', + }, + ), + ], + ); +} + +/** + * Creates a transformer for an order address object. + * + * @return {ModelTransformer} The created transformer. + */ +export function createShippingAddressTransformer(): ModelTransformer< ShippingOrderAddress > { + return new ModelTransformer( + [ + new PropertyTypeTransformation( + { + firstName: PropertyType.String, + lastName: PropertyType.String, + company: PropertyType.String, + address1: PropertyType.String, + address2: PropertyType.String, + city: PropertyType.String, + state: PropertyType.String, + postCode: PropertyType.String, + country: PropertyType.String + }, + ), + new KeyChangeTransformation< ShippingOrderAddress >( { firstName: 'first_name', lastName: 'last_name',