Browse Source

feat(core): Publish event when OrderLine cancelled (#2829)

vjung28 1 year ago
parent
commit
213a26be9d

+ 51 - 46
docs/docs/reference/typescript-api/events/event-types.md

@@ -13,7 +13,7 @@ import MemberDescription from '@site/src/components/MemberDescription';
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/account-registration-event.ts" sourceLine="13" packageName="@vendure/core" />
 
-This event is fired when a new user registers an account, either as a stand-alone signup or after
+This event is fired when a new user registers an account, either as a stand-alone signup or after
 placing an order.
 
 ```ts title="Signature"
@@ -41,7 +41,7 @@ class AccountRegistrationEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/account-verified-event.ts" sourceLine="13" packageName="@vendure/core" />
 
-This event is fired when a users email address successfully gets verified after
+This event is fired when a users email address successfully gets verified after
 the `verifyCustomerAccount` mutation was executed.
 
 ```ts title="Signature"
@@ -96,7 +96,7 @@ class AdministratorEvent extends VendureEntityEvent<Administrator, Administrator
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/asset-channel-event.ts" sourceLine="15" packageName="@vendure/core" />
 
-This event is fired whenever an <a href='/reference/typescript-api/entities/asset#asset'>Asset</a> is assigned or removed
+This event is fired whenever an <a href='/reference/typescript-api/entities/asset#asset'>Asset</a> is assigned or removed
 From a channel.
 
 ```ts title="Signature"
@@ -157,8 +157,8 @@ class AssetEvent extends VendureEntityEvent<Asset, AssetInputTypes> {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/attempted-login-event.ts" sourceLine="14" packageName="@vendure/core" />
 
-This event is fired when an attempt is made to log in via the shop or admin API `login` mutation.
-The `strategy` represents the name of the AuthenticationStrategy used in the login attempt.
+This event is fired when an attempt is made to log in via the shop or admin API `login` mutation.
+The `strategy` represents the name of the AuthenticationStrategy used in the login attempt.
 If the "native" strategy is used, the additional `identifier` property will be available.
 
 ```ts title="Signature"
@@ -186,7 +186,7 @@ class AttemptedLoginEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/change-channel-event.ts" sourceLine="17" packageName="@vendure/core" since="1.4" />
 
-This event is fired whenever an <a href='/reference/typescript-api/entities/interfaces#channelaware'>ChannelAware</a> entity is assigned or removed
+This event is fired whenever an <a href='/reference/typescript-api/entities/interfaces#channelaware'>ChannelAware</a> entity is assigned or removed
 from a channel. The entity property contains the value before updating the channels.
 
 ```ts title="Signature"
@@ -268,10 +268,10 @@ class CollectionEvent extends VendureEntityEvent<Collection, CollectionInputType
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/collection-modification-event.ts" sourceLine="18" packageName="@vendure/core" />
 
-This event is fired whenever a Collection is modified in some way. The `productVariantIds`
-argument is an array of ids of all ProductVariants which:
-
-1. were part of this collection prior to modification and are no longer
+This event is fired whenever a Collection is modified in some way. The `productVariantIds`
+argument is an array of ids of all ProductVariants which:
+
+1. were part of this collection prior to modification and are no longer
 2. are now part of this collection after modification but were not before
 
 ```ts title="Signature"
@@ -326,7 +326,7 @@ class CountryEvent extends VendureEntityEvent<Country, CountryInputTypes> {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/coupon-code-event.ts" sourceLine="15" packageName="@vendure/core" since="1.4" />
 
-This event is fired whenever an coupon code of an active <a href='/reference/typescript-api/entities/promotion#promotion'>Promotion</a>
+This event is fired whenever an coupon code of an active <a href='/reference/typescript-api/entities/promotion#promotion'>Promotion</a>
 is assigned or removed to an <a href='/reference/typescript-api/entities/order#order'>Order</a>.
 
 ```ts title="Signature"
@@ -354,7 +354,7 @@ class CouponCodeEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/customer-address-event.ts" sourceLine="22" packageName="@vendure/core" since="1.4" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/address#address'>Address</a> is added, updated
+This event is fired whenever a <a href='/reference/typescript-api/entities/address#address'>Address</a> is added, updated
 or deleted.
 
 ```ts title="Signature"
@@ -388,7 +388,7 @@ class CustomerAddressEvent extends VendureEntityEvent<Address, CustomerAddressIn
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/customer-event.ts" sourceLine="22" packageName="@vendure/core" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/customer#customer'>Customer</a> is added, updated
+This event is fired whenever a <a href='/reference/typescript-api/entities/customer#customer'>Customer</a> is added, updated
 or deleted.
 
 ```ts title="Signature"
@@ -422,7 +422,7 @@ class CustomerEvent extends VendureEntityEvent<Customer, CustomerInputTypes> {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/customer-group-change-event.ts" sourceLine="15" packageName="@vendure/core" since="1.4" />
 
-This event is fired whenever one or more <a href='/reference/typescript-api/entities/customer#customer'>Customer</a> is assigned to or removed from a
+This event is fired whenever one or more <a href='/reference/typescript-api/entities/customer#customer'>Customer</a> is assigned to or removed from a
 <a href='/reference/typescript-api/entities/customer-group#customergroup'>CustomerGroup</a>.
 
 ```ts title="Signature"
@@ -585,7 +585,7 @@ class FulfillmentStateTransitionEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/global-settings-event.ts" sourceLine="16" packageName="@vendure/core" since="1.4" />
 
-This event is fired whenever a {@link GlobalSettings} is added. The type is always `updated`, because it's
+This event is fired whenever a {@link GlobalSettings} is added. The type is always `updated`, because it's
 only created once and never deleted.
 
 ```ts title="Signature"
@@ -646,7 +646,7 @@ class HistoryEntryEvent extends VendureEntityEvent<HistoryEntry, HistoryInput> {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/identifier-change-event.ts" sourceLine="13" packageName="@vendure/core" />
 
-This event is fired when a registered user successfully changes the identifier (ie email address)
+This event is fired when a registered user successfully changes the identifier (ie email address)
 associated with their account.
 
 ```ts title="Signature"
@@ -674,7 +674,7 @@ class IdentifierChangeEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/identifier-change-request-event.ts" sourceLine="13" packageName="@vendure/core" />
 
-This event is fired when a registered user requests to update the identifier (ie email address)
+This event is fired when a registered user requests to update the identifier (ie email address)
 associated with the account.
 
 ```ts title="Signature"
@@ -783,7 +783,7 @@ class LogoutEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/order-event.ts" sourceLine="13" packageName="@vendure/core" />
 
-This event is fired whenever an <a href='/reference/typescript-api/entities/order#order'>Order</a> is added, updated
+This event is fired whenever an <a href='/reference/typescript-api/entities/order#order'>Order</a> is added, updated
 or deleted.
 
 ```ts title="Signature"
@@ -811,12 +811,11 @@ class OrderEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/order-line-event.ts" sourceLine="13" packageName="@vendure/core" />
 
-This event is fired whenever an <a href='/reference/typescript-api/entities/order-line#orderline'>OrderLine</a> is added, updated
-or deleted.
+This event is fired whenever an <a href='/reference/typescript-api/entities/order-line#orderline'>OrderLine</a> is added, updated, deleted or cancelled.
 
 ```ts title="Signature"
 class OrderLineEvent extends VendureEvent {
-    constructor(ctx: RequestContext, order: Order, orderLine: OrderLine, type: 'created' | 'updated' | 'deleted')
+    constructor(ctx: RequestContext, order: Order, orderLine: OrderLine, type: 'created' | 'updated' | 'deleted' | 'cancelled')
 }
 ```
 * Extends: <code><a href='/reference/typescript-api/events/vendure-event#vendureevent'>VendureEvent</a></code>
@@ -839,10 +838,10 @@ class OrderLineEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/order-placed-event.ts" sourceLine="17" packageName="@vendure/core" />
 
-This event is fired whenever an <a href='/reference/typescript-api/entities/order#order'>Order</a> is set as "placed", which by default is
-when it transitions from 'ArrangingPayment' to either 'PaymentAuthorized' or 'PaymentSettled'.
-
-Note that the exact point that it is set as "placed" can be configured according to the
+This event is fired whenever an <a href='/reference/typescript-api/entities/order#order'>Order</a> is set as "placed", which by default is
+when it transitions from 'ArrangingPayment' to either 'PaymentAuthorized' or 'PaymentSettled'.
+
+Note that the exact point that it is set as "placed" can be configured according to the
 <a href='/reference/typescript-api/orders/order-placed-strategy#orderplacedstrategy'>OrderPlacedStrategy</a>.
 
 ```ts title="Signature"
@@ -951,7 +950,7 @@ class PasswordResetVerifiedEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/payment-method-event.ts" sourceLine="18" packageName="@vendure/core" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/payment-method#paymentmethod'>PaymentMethod</a> is added, updated
+This event is fired whenever a <a href='/reference/typescript-api/entities/payment-method#paymentmethod'>PaymentMethod</a> is added, updated
 or deleted.
 
 ```ts title="Signature"
@@ -979,7 +978,7 @@ class PaymentMethodEvent extends VendureEntityEvent<PaymentMethod, PaymentMethod
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/payment-state-transition-event.ts" sourceLine="15" packageName="@vendure/core" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/payment#payment'>Payment</a> transitions from one <a href='/reference/typescript-api/payment/payment-state#paymentstate'>PaymentState</a> to another, e.g.
+This event is fired whenever a <a href='/reference/typescript-api/entities/payment#payment'>Payment</a> transitions from one <a href='/reference/typescript-api/payment/payment-state#paymentstate'>PaymentState</a> to another, e.g.
 a Payment is authorized by the payment provider.
 
 ```ts title="Signature"
@@ -1007,7 +1006,7 @@ class PaymentStateTransitionEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/product-channel-event.ts" sourceLine="15" packageName="@vendure/core" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/product#product'>Product</a> is added, updated
+This event is fired whenever a <a href='/reference/typescript-api/entities/product#product'>Product</a> is added, updated
 or deleted.
 
 ```ts title="Signature"
@@ -1035,7 +1034,7 @@ class ProductChannelEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/product-event.ts" sourceLine="18" packageName="@vendure/core" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/product#product'>Product</a> is added, updated
+This event is fired whenever a <a href='/reference/typescript-api/entities/product#product'>Product</a> is added, updated
 or deleted.
 
 ```ts title="Signature"
@@ -1126,14 +1125,17 @@ class ProductOptionGroupChangeEvent extends VendureEvent {
 This event is fired whenever a <a href='/reference/typescript-api/entities/product-option-group#productoptiongroup'>ProductOptionGroup</a> is added or updated.
 
 ```ts title="Signature"
-class ProductOptionGroupEvent extends VendureEntityEvent<
-    ProductOptionGroup,
-    ProductOptionGroupInputTypes
+class ProductOptionGroupEvent extends VendureEntityEvent<
+    ProductOptionGroup,
+    ProductOptionGroupInputTypes
 > {
     constructor(ctx: RequestContext, entity: ProductOptionGroup, type: 'created' | 'updated' | 'deleted', input?: ProductOptionGroupInputTypes)
 }
 ```
-* Extends: <code><a href='/reference/typescript-api/events/vendure-entity-event#vendureentityevent'>VendureEntityEvent</a>&#60;
     <a href='/reference/typescript-api/entities/product-option-group#productoptiongroup'>ProductOptionGroup</a>,
     ProductOptionGroupInputTypes
 &#62;</code>
+* Extends: <code><a href='/reference/typescript-api/events/vendure-entity-event#vendureentityevent'>VendureEntityEvent</a>&#60;
+     <a href='/reference/typescript-api/entities/product-option-group#productoptiongroup'>ProductOptionGroup</a>,
+     ProductOptionGroupInputTypes
+ &#62;</code>
 
 
 
@@ -1180,7 +1182,7 @@ class ProductVariantChannelEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/product-variant-event.ts" sourceLine="18" packageName="@vendure/core" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/product-variant#productvariant'>ProductVariant</a> is added, updated
+This event is fired whenever a <a href='/reference/typescript-api/entities/product-variant#productvariant'>ProductVariant</a> is added, updated
 or deleted.
 
 ```ts title="Signature"
@@ -1217,14 +1219,17 @@ class ProductVariantEvent extends VendureEntityEvent<ProductVariant[], ProductVa
 This event is fired whenever a <a href='/reference/typescript-api/entities/product-variant-price#productvariantprice'>ProductVariantPrice</a> is added, updated or deleted.
 
 ```ts title="Signature"
-class ProductVariantPriceEvent extends VendureEntityEvent<
-    ProductVariantPrice[],
-    ProductVariantInputTypes
+class ProductVariantPriceEvent extends VendureEntityEvent<
+    ProductVariantPrice[],
+    ProductVariantInputTypes
 > {
     constructor(ctx: RequestContext, entity: ProductVariantPrice[], type: 'created' | 'updated' | 'deleted', input?: ProductVariantInputTypes)
 }
 ```
-* Extends: <code><a href='/reference/typescript-api/events/vendure-entity-event#vendureentityevent'>VendureEntityEvent</a>&#60;
     <a href='/reference/typescript-api/entities/product-variant-price#productvariantprice'>ProductVariantPrice</a>[],
     ProductVariantInputTypes
 &#62;</code>
+* Extends: <code><a href='/reference/typescript-api/events/vendure-entity-event#vendureentityevent'>VendureEntityEvent</a>&#60;
+     <a href='/reference/typescript-api/entities/product-variant-price#productvariantprice'>ProductVariantPrice</a>[],
+     ProductVariantInputTypes
+ &#62;</code>
 
 
 
@@ -1244,7 +1249,7 @@ class ProductVariantPriceEvent extends VendureEntityEvent<
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/promotion-event.ts" sourceLine="18" packageName="@vendure/core" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/promotion#promotion'>Promotion</a> is added, updated
+This event is fired whenever a <a href='/reference/typescript-api/entities/promotion#promotion'>Promotion</a> is added, updated
 or deleted.
 
 ```ts title="Signature"
@@ -1326,7 +1331,7 @@ class RefundStateTransitionEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/role-change-event.ts" sourceLine="16" packageName="@vendure/core" since="1.4" />
 
-This event is fired whenever one <a href='/reference/typescript-api/entities/role#role'>Role</a> is assigned or removed from a user.
+This event is fired whenever one <a href='/reference/typescript-api/entities/role#role'>Role</a> is assigned or removed from a user.
 The property `roleIds` only contains the removed or assigned role ids.
 
 ```ts title="Signature"
@@ -1435,7 +1440,7 @@ class SellerEvent extends VendureEntityEvent<Seller, SellerInputTypes> {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/shipping-method-event.ts" sourceLine="18" packageName="@vendure/core" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/shipping-method#shippingmethod'>ShippingMethod</a> is added, updated
+This event is fired whenever a <a href='/reference/typescript-api/entities/shipping-method#shippingmethod'>ShippingMethod</a> is added, updated
 or deleted.
 
 ```ts title="Signature"
@@ -1463,7 +1468,7 @@ class ShippingMethodEvent extends VendureEntityEvent<ShippingMethod, ShippingMet
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/stock-movement-event.ts" sourceLine="16" packageName="@vendure/core" since="1.1.0" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/stock-movement#stockmovement'>StockMovement</a> entity is created, which occurs when the saleable
+This event is fired whenever a <a href='/reference/typescript-api/entities/stock-movement#stockmovement'>StockMovement</a> entity is created, which occurs when the saleable
 stock level of a ProductVariant is altered due to things like sales, manual adjustments, and cancellations.
 
 ```ts title="Signature"
@@ -1497,7 +1502,7 @@ class StockMovementEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/tax-category-event.ts" sourceLine="18" packageName="@vendure/core" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/tax-category#taxcategory'>TaxCategory</a> is added, updated
+This event is fired whenever a <a href='/reference/typescript-api/entities/tax-category#taxcategory'>TaxCategory</a> is added, updated
 or deleted.
 
 ```ts title="Signature"
@@ -1525,7 +1530,7 @@ class TaxCategoryEvent extends VendureEntityEvent<TaxCategory, TaxCategoryInputT
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/tax-rate-event.ts" sourceLine="18" packageName="@vendure/core" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/tax-rate#taxrate'>TaxRate</a> is added, updated
+This event is fired whenever a <a href='/reference/typescript-api/entities/tax-rate#taxrate'>TaxRate</a> is added, updated
 or deleted.
 
 ```ts title="Signature"
@@ -1580,7 +1585,7 @@ class TaxRateModificationEvent extends VendureEvent {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/zone-event.ts" sourceLine="18" packageName="@vendure/core" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/zone#zone'>Zone</a> is added, updated
+This event is fired whenever a <a href='/reference/typescript-api/entities/zone#zone'>Zone</a> is added, updated
 or deleted.
 
 ```ts title="Signature"
@@ -1608,7 +1613,7 @@ class ZoneEvent extends VendureEntityEvent<Zone, ZoneInputTypes> {
 
 <GenerationInfo sourceFile="packages/core/src/event-bus/events/zone-members-event.ts" sourceLine="15" packageName="@vendure/core" />
 
-This event is fired whenever a <a href='/reference/typescript-api/entities/zone#zone'>Zone</a> gets <a href='/reference/typescript-api/entities/country#country'>Country</a> members assigned or removed
+This event is fired whenever a <a href='/reference/typescript-api/entities/zone#zone'>Zone</a> gets <a href='/reference/typescript-api/entities/country#country'>Country</a> members assigned or removed
 The `entity` property contains the zone with the already updated member field.
 
 ```ts title="Signature"

+ 1 - 1
packages/core/src/event-bus/events/order-line-event.ts

@@ -15,7 +15,7 @@ export class OrderLineEvent extends VendureEvent {
         public ctx: RequestContext,
         public order: Order,
         public orderLine: OrderLine,
-        public type: 'created' | 'updated' | 'deleted',
+        public type: 'created' | 'updated' | 'deleted' | 'cancelled',
     ) {
         super();
     }

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

@@ -341,6 +341,8 @@ export class OrderModifier {
                 await this.connection.getRepository(ctx, OrderLine).update(line.orderLineId, {
                     quantity: orderLine.quantity - line.quantity,
                 });
+
+                await this.eventBus.publish(new OrderLineEvent(ctx, order, orderLine, 'cancelled'));
             }
         }