Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core events #324

Merged
merged 52 commits into from
May 11, 2021
Merged

Core events #324

merged 52 commits into from
May 11, 2021

Conversation

Mikearaya
Copy link
Contributor

No description provided.

@Mikearaya
Copy link
Contributor Author

@schmidsi this is the branch that one had a conflict plus it's not the module approach so i created a new branch. that one is deleted now tho

package.json Outdated Show resolved Hide resolved
@Mikearaya
Copy link
Contributor Author

Mikearaya commented Feb 17, 2021

@schmidsi @pozylon
so far implementing order event emitting I've identified this event and its corresponding payloads

event payload

ORDER_CHECKOUT =>  current order object
ORDER_ADD_PRODUCT => Order position
ORDER_ADD_DISCOUNT => reservedDiscount
ORDER_CONFIRMED => current order object
ORDER_FULLFILLED => current order object
ORDER_UPDATE_PAYMENT => current order payment object
ORDER_UPDATE_DELIVERY => current order delivery object
ORDER_SIGN_PAYMENT => { orderPayment,  transactionContext }
ORDER_REMOVE => { orderId }
ASSORTMENT_CREATE => { assortment }
ASSORTMENT_ADD_FILTER => { assortmentFilter }
ASSORTMENT_ADD_LINK => { parentAssortmentId, childAssortmentId }
ASSORTMENT_ADD_PRODUCT => { assortmentProduct }
ASSORTMENT_REMOVE => { assortmentId }
ASSORTMENT_REMOVE_FILTER => { assortmentFilterId }
ASSORTMENT_REMOVE_LINK => { assortmentLinkId }
ASSORTMENT_REORDER_PRODUCTS => {assortmentProducts}
ASSORTMENT_REORDER_FILTERS => {assortmentFilters}
ASSORTMENT_REORDER_LINKS => {assortmentLinks}
ASSORTMENT_SET_BASE => {assortmentId}
ASSORTMENT_UPDATE => {assortmentId}
ASSORTMENT_UPDATE_TEXTS => {assortmentId, assortmentTexts }
PRODUCT_ADD_ASSIGNMENT => {productId, proxyId}
PRODUCT_ADD_MEDIA => {productMedia}
PRODUCT_REVIEW_ADD_VOTE => {productReview}
PRODUCT_CREATE => {product}
PRODUCT_CREATE_BUNDLE_ITEM => {productId}
PRODUCT_REVIEW_CREATE => {prodcutReview} 
PRODUCT_CREATE_VARIATION => {productVariation}
PRODUCT_VARIATION_OPTION_CREATE => {productVariation}
PRODUCT_REMOVE_BUNDLE_ITEM => {productId, item}
PRODUCT_REMOVE => {productId}
PRODUCT_REMOVE_ASSIGNMENT => {productId}
PRODUCT_REMOVE_MEDIA => {productMediaId}
PRODUCT_REMOVE_REVIEW => {productReviewId}
PRODUCT_REMOVE_REVIEW_VOTE => {productReviewId, type, userId}
PRODUCT_REMOVE_VARIATION => {productVariationId}
PRODUCT_REMOVE_VARIATION_OPTION => { productVariationId, productVariationOptionValue }
PRODUCT_REORDER_MEDIA => {productMedias}
PRODUCT_UNPUBLISH => {product}
PRODUCT_PUBLISH => {product}
PRODUCT_UPDATE => {productId, type, ...rest}
PRODUCT_UPDATE_MEDIA_TEXT => {productMedia, mediaTexts}
PRODUCT_UPDATE_REVIEW => {productReview}
PRODUCT_UPDATE_TEXTS => {product, productTexts}
PRODUCT_UPDATE_VARIATION_TEXTS => {productVariation, productVariationTexts}
BOOKMARK_CREATE => {bookmarkId}
BOOKMARK_REMOVE => {bookmarkId}

inputs....looks like there needs to be a lot more decision-making required from the group per module on what the emitted format should be and the content of the event.
I've implemented the above events and we can discuss them over this

@Mikearaya Mikearaya marked this pull request as ready for review February 17, 2021 15:08
@pozylon
Copy link
Member

pozylon commented Feb 17, 2021

ORDER_UPDATE in general is missing (changed contact, billing, meta, ...) + ORDER_SET_PAYMENT_PROVIDER + ORDER_SET_DELIVERY_PROVIDER

@pozylon
Copy link
Member

pozylon commented Feb 17, 2021

I wonder what's better when naming events:
ORDER_UPDATED or ORDER_UPDATE?

I think the Event Payload for orders should mostly just contain id's rather than actual objects. The code that hooks into an event must have access to the unchained "context" that is also passed to apollo.

So it's like onEvent({ orderId }, futureSuperContext) => futureSuperContext.modules.orders.findOrder({ orderId })... to get the object

@schmidsi
Copy link
Member

I wonder what's better when naming events:
ORDER_UPDATED or ORDER_UPDATE?

Usually, event names are present tense. See: https://nodejs.org/api/http.html#http_class_http_clientrequest

I think the Event Payload for orders should mostly just contain id's rather than actual objects. The code that hooks into an event must have access to the unchained "context" that is also passed to apollo.

So it's like onEvent({ orderId }, futureSuperContext) => futureSuperContext.modules.orders.findOrder({ orderId })... to get the object

Hmm, I had in mind that the event payload should be a JSON (no functions) and should contain all relevant data. Relevant is: So that a read only observer that observes all events of the system should be able to rebuild the state of the system at any point in time (time travel).

If you put only the ID and you query for this id later (from next tick up to minutes, hours or days), the information might already have changed.

@pozylon
Copy link
Member

pozylon commented Feb 17, 2021

Yeah I see what you mean. 👍Objects would allow for retrospectivity which is what we need.

@Mikearaya
Copy link
Contributor Author

based on this event names should be CamelCase but I think we are following our own standards when we are using caps. when it comes to the name I also think they should define the action performed (ORDER_UPDATED) rather than the action requested (ORDER_UPDATE).
like Simon said I think limiting the type of user that can read the actual data by only emitting id and letting the user query it from unchained might not be necessary and we could send the actual object the event-related to. the only benefit I see from it is controlling restricted users otherwise anyone can subscribe to an event and get the information they want.

@Mikearaya
Copy link
Contributor Author

forgot to put the link https://basarat.gitbook.io/typescript/main-1/typed-event

@schmidsi
Copy link
Member

schmidsi commented Feb 17, 2021

based on this event names should be CamelCase but I think we are following our own standards when we are using caps. when it comes to the name I also think they should define the action performed (ORDER_UPDATED) rather than the action requested (ORDER_UPDATE).

I think we should follow community standards if possible. But if you have a really good argument, why we should use UPPER_CASE and past tense, go on.

like Simon said I think limiting the type of user that can read the actual data by only emitting id and letting the user query it from unchained might not be necessary and we could send the actual object the event-related to. the only benefit I see from it is controlling restricted users otherwise anyone can subscribe to an event and get the information they want.

Access control happens at the resolver level. Below, everything is open.

... we could send the actual object the event-related to.

... after the change and only the serialisable part (JSON) of it.

@pozylon
Copy link
Member

pozylon commented Feb 17, 2021

Upper case kind of signals to me that it is an enum of some kind and it gets highlighted by the IDE as such. And it's usually the style when you define types and enums in GraphQL. Event names for me are to be handled like enums.

PascalCase is obviously also an option 😂 no just joking I don't care. I could personally live with present style because order_checkedout sucks compared to order_checkout

@pozylon
Copy link
Member

pozylon commented Feb 17, 2021

@schmidsi which community standards about event names do you refer to?

@Mikearaya
Copy link
Contributor Author

@schmidsi
Copy link
Member

@schmidsi which community standards about event names do you refer to?

I just quickly looked over the event names here: https://nodejs.org/api/http.html#http_class_http_clientrequest I don't know if there is a guide. Personally, I like the UPPER_CASE also. Present tense is just shorter.

@Mikearaya
Copy link
Contributor Author

@schmidsi @pozylon 1 & 2 done ready for review and move on to 3

@Mikearaya
Copy link
Contributor Author

Failing test are because of rebase with master

packages/api/resolvers/mutations/pageView.js Outdated Show resolved Hide resolved
packages/core-events/core-events-tests.js Outdated Show resolved Hide resolved
packages/core-events/director.ts Show resolved Hide resolved
@Mikearaya
Copy link
Contributor Author

@schmidsi ? you suggest interface over abstract class ?

@schmidsi
Copy link
Member

schmidsi commented Mar 2, 2021

@schmidsi ? you suggest interface over abstract class ?

I'm not a fan of classes in general, but sometimes they make sense. So I do not have a strong opinion there.

I meant following the node.js standard and use emit instead of publish. And on instead of subscribe and off instead of unsubscribe and once for only get the next event'

Copy link
Member

@pozylon pozylon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I see now, I'd suggest some more tasks. I know that most of the following is not part of the steps in the issue but I think that stuff should be part of such a feature, no?

Admin Features:

  • Queries that allow to query events (only admin by default)
  • Controlpanel Views to view events (only admin by default)
  • (GraphQL subscription to subscribe to the event stream (only admin by default))

Additional plugins:

  • Out-of-the-box redis pub/sub plugin because we already use redis heavily for apollo and thumbor caching

And:

  • EventDirector should log event emit and subscription with verbose or debug log level
  • Matomo should log fetch url's together with the event it subscribed with debug log level

And I'm wondering if PAGE_VIEW also catches the browser version from req?

packages/core-orders/orders.js Outdated Show resolved Hide resolved
@pozylon
Copy link
Member

pozylon commented Mar 12, 2021

Else, structure wise I have nothing more to say, looks very good!

@Mikearaya
Copy link
Contributor Author

Thanks i can work with this

@Mikearaya
Copy link
Contributor Author

I've added events to some of the other modules now. about testing Redis, yes I can get it up and running in CI but the flow is different because it's a fire and forget where do you test for the event? but this can wait for the next round.

@Mikearaya Mikearaya requested a review from pozylon May 7, 2021 21:44
packages/api/schema/query.js Outdated Show resolved Hide resolved
packages/core-orders/db/order-discounts/helpers.js Outdated Show resolved Hide resolved
packages/core-orders/db/orders/helpers.js Show resolved Hide resolved
@pozylon
Copy link
Member

pozylon commented May 11, 2021

@Mikearaya I'll do the last minor fixes and merge it, great work!

@pozylon pozylon merged commit a3a44ba into master May 11, 2021
@pozylon pozylon deleted the core-events branch May 11, 2021 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants