Added transformers for all of the product sub-data

This commit is contained in:
Christopher Allford 2020-11-06 10:44:57 -08:00
parent b4a2316386
commit 45b57d46b9
6 changed files with 131 additions and 14 deletions

View File

@ -56,8 +56,8 @@ export class ModelTransformerTransformation< T extends Model > implements ModelT
* @return {*} The transformed properties. * @return {*} The transformed properties.
*/ */
public fromModel( properties: any ): any { public fromModel( properties: any ): any {
if ( properties.hasOwnProperty( this.property ) ) { const val = properties[ this.property ];
const val = properties[ this.property ]; if ( val ) {
if ( Array.isArray( val ) ) { if ( Array.isArray( val ) ) {
properties[ this.property ] = val.map( ( v ) => this.transformer.fromModel( v ) ); properties[ this.property ] = val.map( ( v ) => this.transformer.fromModel( v ) );
} else { } else {
@ -75,8 +75,8 @@ export class ModelTransformerTransformation< T extends Model > implements ModelT
* @return {*} The transformed properties. * @return {*} The transformed properties.
*/ */
public toModel( properties: any ): any { public toModel( properties: any ): any {
if ( properties.hasOwnProperty( this.property ) ) { const val = properties[ this.property ];
const val = properties[ this.property ]; if ( val ) {
if ( Array.isArray( val ) ) { if ( Array.isArray( val ) ) {
properties[ this.property ] = val.map( ( v ) => this.transformer.toModel( this.modelClass, v ) ); properties[ this.property ] = val.map( ( v ) => this.transformer.toModel( this.modelClass, v ) );
} else { } else {

View File

@ -1,5 +1,5 @@
import { Model } from '../model'; import { Model } from '../model';
import { PostStatus } from '../shared-types'; import { MetaData, PostStatus } from '../shared-types';
import { import {
BackorderStatus, BackorderStatus,
CatalogVisibility, CatalogVisibility,
@ -335,4 +335,11 @@ export abstract class AbstractProduct extends Model {
* @type {number} * @type {number}
*/ */
public readonly numRatings: number = -1; public readonly numRatings: number = -1;
/**
* The extra metadata for the product.
*
* @type {ReadonlyArray.<MetaData>}
*/
public readonly metaData: readonly MetaData[] = [];
} }

View File

@ -133,6 +133,7 @@ export class ProductDownload {
/** /**
* The URL of the downloadable file. * The URL of the downloadable file.
* *
*
* @type {string} * @type {string}
*/ */
public readonly url: string = ''; public readonly url: string = '';
@ -179,6 +180,13 @@ export class ProductAttribute {
*/ */
public readonly isVisibleOnProductPage: boolean = false; public readonly isVisibleOnProductPage: boolean = false;
/**
* Indicates whether or not the attribute should be used in variations.
*
* @type {boolean}
*/
public readonly isForVariations: boolean = false;
/** /**
* The options which are available for the attribute. * The options which are available for the attribute.
* *
@ -242,13 +250,6 @@ export class ProductImage {
*/ */
public readonly altText: string = ''; public readonly altText: string = '';
/**
* The sort order for the image.
*
* @type {number}
*/
public readonly sortOrder: number = -1;
/** /**
* Creates a new product image. * Creates a new product image.
* *

View File

@ -20,7 +20,7 @@ export type PostStatus = 'draft' | 'pending' | 'private' | 'publish' | string;
/** /**
* A metadata object. * A metadata object.
*/ */
export class MetaData { export class MetaData extends Model {
/** /**
* The key of the metadata. * The key of the metadata.
* *
@ -41,6 +41,7 @@ export class MetaData {
* @param {Partial.<MetaData>} properties The properties to set. * @param {Partial.<MetaData>} properties The properties to set.
*/ */
public constructor( properties?: Partial< MetaData > ) { public constructor( properties?: Partial< MetaData > ) {
super();
Object.assign( this, properties ); Object.assign( this, properties );
} }
} }

View File

@ -8,13 +8,98 @@ import {
PropertyTypeTransformation, PropertyTypeTransformation,
} from '../../../framework/transformations/property-type-transformation'; } from '../../../framework/transformations/property-type-transformation';
import { CustomTransformation } from '../../../framework/transformations/custom-transformation'; import { CustomTransformation } from '../../../framework/transformations/custom-transformation';
import { ProductAttribute, ProductDownload, ProductImage, ProductTerm } from '../../../models/products/shared-types';
import { ModelTransformerTransformation } from '../../../framework/transformations/model-transformer-transformation';
import { MetaData } from '../../../models/shared-types';
import { createMetaDataTransformer } from '../shared';
/**
* Creates a transformer for the product term object.
*
* @return {ModelTransformer} The created transformer.
*/
function createProductTermTransformer(): ModelTransformer< ProductTerm > {
return new ModelTransformer(
[
new PropertyTypeTransformation( { id: PropertyType.Integer } ),
],
);
}
/**
* Creates a transformer for the product attribute object.
*
* @return {ModelTransformer} The created transformer.
*/
function createProductAttributeTransformer(): ModelTransformer< ProductAttribute > {
return new ModelTransformer(
[
new PropertyTypeTransformation(
{
id: PropertyType.Integer,
sortOrder: PropertyType.Integer,
isVisibleOnProductPage: PropertyType.Boolean,
isForVariations: PropertyType.Boolean,
},
),
new KeyChangeTransformation< ProductAttribute >(
{
sortOrder: 'position',
isVisibleOnProductPage: 'visible',
isForVariations: 'variation',
},
),
],
);
}
/**
* Creates a transformer for the product image object.
*
* @return {ModelTransformer} The created transformer.
*/
function createProductImageTransformer(): ModelTransformer< ProductImage > {
return new ModelTransformer(
[
new IgnorePropertyTransformation( [ 'date_created', 'date_modified' ] ),
new PropertyTypeTransformation(
{
id: PropertyType.Integer,
created: PropertyType.Date,
modified: PropertyType.Date,
},
),
new KeyChangeTransformation< ProductImage >(
{
created: 'date_created_gmt',
modified: 'date_modified_gmt',
url: 'src',
altText: 'altText',
},
),
],
);
}
/**
* Creates a transformer for the product download object.
*
* @return {ModelTransformer} The created transformer.
*/
function createProductDownloadTransformer(): ModelTransformer< ProductDownload > {
return new ModelTransformer(
[
new KeyChangeTransformation< ProductDownload >( { url: 'file' } ),
],
);
}
/** /**
* Creates a transformer for the shared properties of all products. * Creates a transformer for the shared properties of all products.
* *
* @param {string} type The product type. * @param {string} type The product type.
* @param {Array.<ModelTransformation>} transformations Optional transformers to add to the transformer. * @param {Array.<ModelTransformation>} transformations Optional transformers to add to the transformer.
* @return {ModelTransformer} The created transform. * @return {ModelTransformer} The created transformer.
*/ */
export function createProductTransformer< T extends AbstractProduct >( export function createProductTransformer< T extends AbstractProduct >(
type: string, type: string,
@ -34,6 +119,12 @@ export function createProductTransformer< T extends AbstractProduct >(
'date_on_sale_to', 'date_on_sale_to',
], ],
), ),
new ModelTransformerTransformation( 'categories', ProductTerm, createProductTermTransformer() ),
new ModelTransformerTransformation( 'tags', ProductTerm, createProductTermTransformer() ),
new ModelTransformerTransformation( 'attributes', ProductAttribute, createProductAttributeTransformer() ),
new ModelTransformerTransformation( 'images', ProductImage, createProductImageTransformer() ),
new ModelTransformerTransformation( 'downloads', ProductDownload, createProductDownloadTransformer() ),
new ModelTransformerTransformation( 'metaData', MetaData, createMetaDataTransformer() ),
new CustomTransformation( new CustomTransformation(
TransformationOrder.Normal, TransformationOrder.Normal,
( properties: any ) => { ( properties: any ) => {
@ -121,6 +212,7 @@ export function createProductTransformer< T extends AbstractProduct >(
allowReviews: 'reviews_allowed', allowReviews: 'reviews_allowed',
averageRating: 'average_rating', averageRating: 'average_rating',
numRatings: 'rating_count', numRatings: 'rating_count',
metaData: 'meta_data',
}, },
), ),
); );

View File

@ -0,0 +1,16 @@
import { ModelTransformer } from '../../framework/model-transformer';
import { MetaData } from '../../models/shared-types';
import { IgnorePropertyTransformation } from '../../framework/transformations/ignore-property-transformation';
/**
* Creates a new transformer for metadata models.
*
* @return {ModelTransformer} The created transformer.
*/
export function createMetaDataTransformer(): ModelTransformer< MetaData > {
return new ModelTransformer(
[
new IgnorePropertyTransformation( [ 'id' ] ),
],
);
}