Split Shipping and Billing Addresses as they have different properties

This commit is contained in:
Lucas Bustamante 2022-01-17 16:25:23 -03:00
parent c6ac637fa6
commit 770bd181ce
4 changed files with 107 additions and 20 deletions

View File

@ -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.

View File

@ -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 );
}
}
/**

View File

@ -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
*/

View File

@ -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',