Skip to main content

Event System

::: How to use the built-in Event system :::

Unchained supports the publish-subscribe (pub/sub) event model to keep track of events emitted in each module. By default it uses nodejs EventEmitter module to handle events but the system can be extended to connect to distributed event queues.

You can subscribe to events to trigger any custom logic you can think of. It is especially useful to track data server-side for analytics purposes or to trigger additional e-mails based on special workflows.

The @unchainedshop/core-events module exports three utility functions that can be used to interact with the registered event tracker, or register new custom events.

  • registerEvents: adds custom events that will be tracked. it takes array of event names.
  • emit(eventName, payload): used to emit events, either pre-built events or custom events registered using registerEvents. It takes two arguments, name of the event we want to emit and an object of the data associated with the event.
  • subscribe(eventName: callback): used for subscribing to events emitted by emit function. It takes two arguments, name of the event we want to subscribe to and a call back that will take one argument holding data for the associated event.

Note: to get list of pre-built or custom events registered on unchained use EventType resolver inside GraphQL playground.

Every event emitted from the engine is an object that has one property payload which is also an object that has different properties based on the event. We recommend you follow the same pattern when implementing custom events.

Built-in Events

Below are events tracked under each module under the box:

core-assortments:

Event nameEmited when...payload
ASSORTMENT_CREATEAssortment is created{ assortment: {} }
ASSORTMENT_ADD_FILTERAssortment filter is created{ assortmentFilter: {} }
ASSORTMENT_ADD_LINKAssortment link is created{ assortmentLink: {} }
ASSORTMENT_ADD_PRODUCTProduct is added to an assortment{ assortmentProduct: {} }
ASSORTMENT_REMOVEAssortment is deleted{ assortmentId: string }
ASSORTMENT_REMOVE_FILTERAssortment filter is deleted{ assortmentFilterId: string }
ASSORTMENT_REMOVE_LINKAssortment link is removed{ assortmentLinkId: string }
ASSORTMENT_REORDER_PRODUCTSAssortment product sort order is updated{ assortmentProducts: [] }
ASSORTMENT_REORDER_FILTERSAssortment filters sort order is updated{ assortmentFilters: [] }
ASSORTMENT_REORDER_LINKSAssortment link is updated{ assortmentLinks: [] }
ASSORTMENT_SET_BASEAssortment is set as base assortment{ assortmentId: string }
ASSORTMENT_UPDATEAssortment is updated{ assortmentId: string }
ASSORTMENT_UPDATE_TEXTAssortment text is updated{ assortmentId: string, text: {} }
ASSORTMENT_ADD_MEDIAMedia is added for a product{ assortmentMedia: {} }
ASSORTMENT_REMOVE_MEDIAMedia is deleted from a product{ assortmentMediaId: string }
ASSORTMENT_UPDATE_MEDIA_TEXTProduct media text is updated{ assortmentMediaId: string, text: {} }

core-products

Event nameEmited when...payload
PRODUCT_ADD_ASSIGNMENTCONFIGURABLE_PRODUCT type is assigned proxy product{ productId: string, proxyId: string }
PRODUCT_ADD_MEDIAMedia is added for a product{ productMedia: {} }
PRODUCT_REVIEW_ADD_VOTEProduct review is recieves a vote{ productReview: {} }
PRODUCT_CREATEProduct is created{ product: {} }
PRODUCT_CREATE_BUNDLE_ITEMBUNDLE_PRODUCT is assigned bundle items{ productId: string }
PRODUCT_REVIEW_CREATEProduct review is created{ productReview : {} }
PRODUCT_CREATE_VARIATIONProduct variation is created{ productVariation: {} }
PRODUCT_VARIATION_OPTION_CREATEProduct variation option is created{ productVariation: {} }
PRODUCT_REMOVE_BUNDLE_ITEMBundle item is removed from a BUNDLE_PRODUCT type{ productId: string, item: {} }
PRODUCT_REMOVEProduct is deleted{ productId: string }
PRODUCT_REMOVE_ASSIGNMENTProxy assignment is removed from a configurable product{ productId: string }
PRODUCT_REMOVE_MEDIAMedia is deleted from a product{ productMediaId: string }
PRODUCT_REMOVE_REVIEWProduct review is deleted{ productReviewId: string }
PRODUCT_REMOVE_REVIEW_VOTEProduct review vote is removed{ productReviewId: string, type: string, userId: string }
PRODUCT_REMOVE_VARIATIONProduct variation is removed from a product{ productVariationId: string }
PRODUCT_REMOVE_VARIATION_OPTIONProduct variation option is removed{ productVariationId: string, productVariationOptionValue: {} }
PRODUCT_REORDER_MEDIAProduct media sort order is updated{ productMedias: [] }
PRODUCT_UNPUBLISHProduct is unpublished{ product: {} }
PRODUCT_PUBLISHproduct is published{ product: {} }
PRODUCT_UPDATEproduct is updated{ productId: string, type: string, [commerce,support,warehousing]: {} }
PRODUCT_UPDATE_MEDIA_TEXTProduct media text is updated{ productMediaId: string, text: {} }
PRODUCT_UPDATE_REVIEWProduct review is updated{ productReview: {} }
PRODUCT_UPDATE_TEXTProduct text is updated{ productId: {}, text: [] }
PRODUCT_UPDATE_VARIATION_TEXTproduct variation text is updated{ productVariationId: string, productVariationOptionValue: string, text: {} }

core-orders

Event nameEmitted when...payload
ORDER_UPDATE_DELIVERYOrder delivery information is updated{ orderDelivery: {} }
ORDER_SIGN_PAYMENTOrder payment provider is signed{ orderPayment: {}, transactionContext: {} }
ORDER_REMOVEOrder is deleted{ orderId: string }
ORDER_ADD_PRODUCTProduct is added to an order{ orderPosition : {} }
ORDER_ADD_DISCOUNTDiscount is added to an order{ discount: {} }
ORDER_CONFIRMEDOrder is confirmed{ order: {} }
ORDER_FULLFILLEDAll requested items are fullfiled for an order{ order: {} }
ORDER_REJECTEDOrder is rejected{ order: {} }
ORDER_UPDATE_PAYMENTOrder payment provider is updated{ orderPayment: {} }
ORDER_CREATENew Order is created{ order: {} }
ORDER_UPDATEOrder information is updated{ order: {}, field: string }
ORDER_SET_PAYMENT_PROVIDERPayment provider is assigned for an order{ order: {}, paymentProviderId: string }
ORDER_SET_DELIVERY_PROVIDERDelivery provider is assigned to an order{ order: {}, deliveryProviderId: string }
ORDER_EMPTY_CARTAll cart items are removed from an order{ orderId: string, count: number }
ORDER_UPDATE_CART_ITEMItems in Order are updated, eg quantity{ orderPosition: {} }
ORDER_REMOVE_CART_ITEMItems are delted from an order{ orderPosition: {} }
ORDER_UPDATE_DISCOUNTOrder discount is updated{ discount: {} }
ORDER_REMOVE_DISCOUNTDiscount associated with Order is removed{ discount: {} }
ORDER_CHECKOUTOrder is checked out successfuly{ order: {} }
ORDER_PAYOrder payment is complete{ orderPayment: {} }
ORDER_DELIVERYOrder delivery status is changed to deliverd{ orderDelivery: {} }

core-bookmarks

Event nameEmitted when...payload
BOOKMARK_REMOVEBookmark is removed{ bookmarkId: string }
BOOKMARK_UPDATEBookmark is updated{ bookmarkId: string }
BOOKMARK_REMOVEBookmark is removed{ bookmarkId: string }

core-country

Event nameEmitted when...payload
COUNTRY_CREATENew country is created{ country: {} }
COUNTRY_UPDATECountry is update{ countryId: string }
COUNTRY_REMOVECountry is removed{ countryId: string }

core-currency

Event nameEmitted when...payload
CURRENCY_CREATENew currency is created{ currency: {} }
CURRENCY_UPDATECurrency is update{ currencyId: string }
CURRENCY_REMOVECurrency is removed{ currencyId: string }

core-language

Event nameEmitted when...payload
LANGUAGE_CREATENew language is created{ language: {} }
LANGUAGE_UPDATELanguage is update{ languageId: string }
LANGUAGE_REMOVELanguage is removed{ languageId: string }

core-filter

Event nameEmitted when...payload
FILTER_CREATENew filter is created{ filter: {} }
FILTER_UPDATEFilter is update{ filter: {} }
FILTER_REMOVEFilter is removed{ filterId: string }
FILTER_UPDATE_TEXTFilter text updated{ filterId: string, filterOptionValue: string, text: {} }

core-payment

Event nameEmitted when...payload
PAYMENT_PROVIDER_CREATENew payment provider is created{ paymentProvider: {} }
PAYMENT_PROVIDER_UPDATEPayment provider is update{ paymentProvider: {} }
PAYMENT_PROVIDER_REMOVEPayment provider is removed{ paymentProvider: {} }

core-payment

Event nameEmitted when...payload
PAYMENT_PROVIDER_CREATENew payment provider is created{ paymentProvider: {} }
PAYMENT_PROVIDER_UPDATEPayment provider is update{ paymentProvider: {} }
PAYMENT_PROVIDER_REMOVEPayment provider is removed{ paymentProvider: {} }

core-warehousing

Event nameEmitted when...payload
WAREHOUSING_PROVIDER_CREATENew warehousing provider is created{ warehousingProvider: {} }
WAREHOUSING_PROVIDER_UPDATEWarehousing provider is update{ warehousingProvider: {} }
WAREHOUSING_PROVIDER_REMOVEWarehousing provider is removed{ warehousingProvider: {} }

core-enrollments

Event nameEmitted when...payload
ENROLLMENT_CREATENew enrollment is created{ enrollment: {} }

Tracking custom events

In addition to the built in event that come with unchained you can register your own custom event easily.

In order to do this, The custom events need to be registered at platform boot time using registerEvents helper function that takes array of event names to be tracked.

import { registerEvents } from '@unchainedshop/core-events';

...
startPlatform({...});
...
registerEvents([
'CUSTOM_EVENT_ONE',
'CUSTOM_EVENT_TWO',
'CUSTOM_EVENT_THREE',
])

After initializing this at system start up, you can emit and subscribe in your code base.

import { registerEvents } from '@unchainedshop/core-events';

subscribe('CUSTOM_EVENT_ONE', ({ payload }) => {
console.log(payload.from);
});

emit('CUSTOM_EVENT_ONE', { from: "fcustom event one"});;


NOTE: before you can subscribe to an event, make sure it's registered first. Otherwise error will be thrown.