Browse Source

docs(core): Add docs for service helpers

Relates to #1707
Michael Bromley 3 years ago
parent
commit
834c05c514

+ 18 - 0
packages/core/src/service/helpers/order-calculator/order-calculator.ts

@@ -20,6 +20,17 @@ import { ShippingCalculator } from '../shipping-calculator/shipping-calculator';
 
 
 import { prorate } from './prorate';
 import { prorate } from './prorate';
 
 
+/**
+ * @description
+ * This helper is used when making changes to an Order, to apply all applicable price adjustments to that Order,
+ * including:
+ *
+ * - Promotions
+ * - Taxes
+ * - Shipping
+ *
+ * @docsCategory service-helpers
+ */
 @Injectable()
 @Injectable()
 export class OrderCalculator {
 export class OrderCalculator {
     constructor(
     constructor(
@@ -32,6 +43,7 @@ export class OrderCalculator {
     ) {}
     ) {}
 
 
     /**
     /**
+     * @description
      * Applies taxes and promotions to an Order. Mutates the order object.
      * Applies taxes and promotions to an Order. Mutates the order object.
      * Returns an array of any OrderItems which had new adjustments
      * Returns an array of any OrderItems which had new adjustments
      * applied, either tax or promotions.
      * applied, either tax or promotions.
@@ -95,6 +107,7 @@ export class OrderCalculator {
     }
     }
 
 
     /**
     /**
+     * @description
      * Applies the correct TaxRate to each OrderItem in the order.
      * Applies the correct TaxRate to each OrderItem in the order.
      */
      */
     private async applyTaxes(ctx: RequestContext, order: Order, activeZone: Zone) {
     private async applyTaxes(ctx: RequestContext, order: Order, activeZone: Zone) {
@@ -106,6 +119,7 @@ export class OrderCalculator {
     }
     }
 
 
     /**
     /**
+     * @description
      * Applies the correct TaxRate to an OrderLine
      * Applies the correct TaxRate to an OrderLine
      */
      */
     private async applyTaxesToOrderLine(
     private async applyTaxesToOrderLine(
@@ -129,6 +143,7 @@ export class OrderCalculator {
     }
     }
 
 
     /**
     /**
+     * @description
      * Returns a memoized function for performing an efficient
      * Returns a memoized function for performing an efficient
      * lookup of the correct TaxRate for a given TaxCategory.
      * lookup of the correct TaxRate for a given TaxCategory.
      */
      */
@@ -150,6 +165,7 @@ export class OrderCalculator {
     }
     }
 
 
     /**
     /**
+     * @description
      * Applies any eligible promotions to each OrderItem in the order. Returns an array of
      * Applies any eligible promotions to each OrderItem in the order. Returns an array of
      * any OrderItems which had their Adjustments modified.
      * any OrderItems which had their Adjustments modified.
      */
      */
@@ -168,6 +184,7 @@ export class OrderCalculator {
     }
     }
 
 
     /**
     /**
+     * @description
      * Applies promotions to OrderItems. This is a quite complex function, due to the inherent complexity
      * Applies promotions to OrderItems. This is a quite complex function, due to the inherent complexity
      * of applying the promotions, and also due to added complexity in the name of performance
      * of applying the promotions, and also due to added complexity in the name of performance
      * optimization. Therefore it is heavily annotated so that the purpose of each step is clear.
      * optimization. Therefore it is heavily annotated so that the purpose of each step is clear.
@@ -253,6 +270,7 @@ export class OrderCalculator {
     }
     }
 
 
     /**
     /**
+     * @description
      * An OrderLine may have promotion adjustments from Promotions which are no longer applicable.
      * An OrderLine may have promotion adjustments from Promotions which are no longer applicable.
      * For example, a coupon code might have caused a discount to be applied, and now that code has
      * For example, a coupon code might have caused a discount to be applied, and now that code has
      * been removed from the order. The adjustment will still be there on each OrderItem it was applied
      * been removed from the order. The adjustment will still be there on each OrderItem it was applied

+ 10 - 0
packages/core/src/service/helpers/order-modifier/order-modifier.ts

@@ -61,6 +61,8 @@ import { translateDeep } from '../utils/translate-entity';
  * So this helper was mainly extracted to isolate the huge `modifyOrder` method since the
  * So this helper was mainly extracted to isolate the huge `modifyOrder` method since the
  * OrderService was just growing too large. Future refactoring could improve the organization
  * OrderService was just growing too large. Future refactoring could improve the organization
  * of these Order-related methods into a more clearly-delineated set of classes.
  * of these Order-related methods into a more clearly-delineated set of classes.
+ *
+ * @docsCategory service-helpers
  */
  */
 @Injectable()
 @Injectable()
 export class OrderModifier {
 export class OrderModifier {
@@ -80,6 +82,7 @@ export class OrderModifier {
     ) {}
     ) {}
 
 
     /**
     /**
+     * @description
      * Ensure that the ProductVariant has sufficient saleable stock to add the given
      * Ensure that the ProductVariant has sufficient saleable stock to add the given
      * quantity to an Order.
      * quantity to an Order.
      */
      */
@@ -97,6 +100,11 @@ export class OrderModifier {
         return correctedQuantity;
         return correctedQuantity;
     }
     }
 
 
+    /**
+     * @description
+     * Given a ProductVariant ID and optional custom fields, this method will return an existing OrderLine that
+     * matches, or `undefined` if no match is found.
+     */
     async getExistingOrderLine(
     async getExistingOrderLine(
         ctx: RequestContext,
         ctx: RequestContext,
         order: Order,
         order: Order,
@@ -114,6 +122,7 @@ export class OrderModifier {
     }
     }
 
 
     /**
     /**
+     * @description
      * Returns the OrderLine to which a new OrderItem belongs, creating a new OrderLine
      * Returns the OrderLine to which a new OrderItem belongs, creating a new OrderLine
      * if no existing line is found.
      * if no existing line is found.
      */
      */
@@ -162,6 +171,7 @@ export class OrderModifier {
     }
     }
 
 
     /**
     /**
+     * @description
      * Updates the quantity of an OrderLine, taking into account the available saleable stock level.
      * Updates the quantity of an OrderLine, taking into account the available saleable stock level.
      * Returns the actual quantity that the OrderLine was updated to (which may be less than the
      * Returns the actual quantity that the OrderLine was updated to (which may be less than the
      * `quantity` argument if insufficient stock was available.
      * `quantity` argument if insufficient stock was available.

+ 24 - 1
packages/core/src/service/helpers/product-price-applicator/product-price-applicator.ts

@@ -13,7 +13,30 @@ import { ZoneService } from '../../services/zone.service';
 /**
 /**
  * @description
  * @description
  * This helper is used to apply the correct price to a ProductVariant based on the current context
  * This helper is used to apply the correct price to a ProductVariant based on the current context
- * including active Channel, any current Order, etc.
+ * including active Channel, any current Order, etc. If you use the {@link TransactionalConnection} to
+ * directly query ProductVariants, you will find that the `price` and `priceWithTax` properties will
+ * always be `0` until you use the `applyChannelPriceAndTax()` method:
+ *
+ * @example
+ * ```TypeScript
+ * export class MyCustomService {
+ *   constructor(private connection: TransactionalConnection,
+ *               private productPriceApplicator: ProductPriceApplicator) {}
+ *
+ *   getVariant(ctx: RequestContext, id: ID) {
+ *     const productVariant = await this.connection
+ *       .getRepository(ctx, ProductVariant)
+ *       .findOne(id);
+ *
+ *     await this.productPriceApplicator
+ *       .applyChannelPriceAndTax(productVariant, ctx);
+ *
+ *     return productVariant;
+ *   }
+ * }
+ * ```
+ *
+ * @docsCategory service-helpers
  */
  */
 @Injectable()
 @Injectable()
 export class ProductPriceApplicator {
 export class ProductPriceApplicator {

+ 17 - 1
packages/core/src/service/helpers/slug-validator/slug-validator.ts

@@ -8,6 +8,10 @@ import { TransactionalConnection } from '../../../connection/transactional-conne
 import { VendureEntity } from '../../../entity/base/base.entity';
 import { VendureEntity } from '../../../entity/base/base.entity';
 import { ProductOptionGroup } from '../../../entity/product-option-group/product-option-group.entity';
 import { ProductOptionGroup } from '../../../entity/product-option-group/product-option-group.entity';
 
 
+/**
+ * @docsCategory service-helpers
+ * @docsPage SlugValidator
+ */
 export type InputWithSlug = {
 export type InputWithSlug = {
     id?: ID | null;
     id?: ID | null;
     translations?: Array<{
     translations?: Array<{
@@ -17,6 +21,10 @@ export type InputWithSlug = {
     }> | null;
     }> | null;
 };
 };
 
 
+/**
+ * @docsCategory service-helpers
+ * @docsPage SlugValidator
+ */
 export type TranslationEntity = VendureEntity & {
 export type TranslationEntity = VendureEntity & {
     id: ID;
     id: ID;
     languageCode: LanguageCode;
     languageCode: LanguageCode;
@@ -24,6 +32,14 @@ export type TranslationEntity = VendureEntity & {
     base: any;
     base: any;
 };
 };
 
 
+/**
+ * @description
+ * Used to validate slugs to ensure they are URL-safe and unique. Designed to be used with translatable
+ * entities such as {@link Product} and {@link Collection}.
+ *
+ * @docsCategory service-helpers
+ * @docsWeight 0
+ */
 @Injectable()
 @Injectable()
 export class SlugValidator {
 export class SlugValidator {
     constructor(private connection: TransactionalConnection) {}
     constructor(private connection: TransactionalConnection) {}
@@ -53,7 +69,7 @@ export class SlugValidator {
                             .where(`translation.slug = :slug`, { slug: t.slug })
                             .where(`translation.slug = :slug`, { slug: t.slug })
                             .andWhere(`translation.languageCode = :languageCode`, {
                             .andWhere(`translation.languageCode = :languageCode`, {
                                 languageCode: t.languageCode,
                                 languageCode: t.languageCode,
-                            })
+                            });
                         if (input.id) {
                         if (input.id) {
                             qb.andWhere(`translation.base != :id`, { id: input.id });
                             qb.andWhere(`translation.base != :id`, { id: input.id });
                         }
                         }

+ 28 - 1
packages/core/src/service/helpers/translatable-saver/translatable-saver.ts

@@ -25,13 +25,39 @@ export interface UpdateTranslatableOptions<T extends Translatable> extends Creat
 }
 }
 
 
 /**
 /**
- * A helper which contains methods for creating and updating entities which implement the Translatable interface.
+ * @description
+ * A helper which contains methods for creating and updating entities which implement the {@link Translatable} interface.
+ *
+ * @example
+ * ```TypeScript
+ * export class MyService {
+ *   constructor(private translatableSaver: TranslatableSaver) {}
+ *
+ *   async create(ctx: RequestContext, input: CreateFacetInput): Promise<Translated<Facet>> {
+ *     const facet = await this.translatableSaver.create({
+ *       ctx,
+ *       input,
+ *       entityType: Facet,
+ *       translationType: FacetTranslation,
+ *       beforeSave: async f => {
+ *           f.code = await this.ensureUniqueCode(ctx, f.code);
+ *       },
+ *     });
+ *     return facet;
+ *   }
+ *
+ *   // ...
+ * }
+ * ```
+ *
+ * @docsCategory service-helpers
  */
  */
 @Injectable()
 @Injectable()
 export class TranslatableSaver {
 export class TranslatableSaver {
     constructor(private connection: TransactionalConnection) {}
     constructor(private connection: TransactionalConnection) {}
 
 
     /**
     /**
+     * @description
      * Create a translatable entity, including creating any translation entities according
      * Create a translatable entity, including creating any translation entities according
      * to the `translations` array.
      * to the `translations` array.
      */
      */
@@ -59,6 +85,7 @@ export class TranslatableSaver {
     }
     }
 
 
     /**
     /**
+     * @description
      * Update a translatable entity. Performs a diff of the `translations` array in order to
      * Update a translatable entity. Performs a diff of the `translations` array in order to
      * perform the correct operation on the translations.
      * perform the correct operation on the translations.
      */
      */