Skip to content

Commit

Permalink
fix: fix reorder fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Oct 19, 2024
1 parent 7c491f7 commit ff46674
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
10 changes: 6 additions & 4 deletions packages/cqrs/src/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Subject } from "rxjs"
import type { Class } from "type-fest"
import { EVENT_HANDLER_METADATA, EVENT_METADATA } from "./decorators/constants"
import { DefaultEventPubSub } from "./default-event-publisher"
import { EventHandlerNotFoundException } from "./exceptions/event-handler-not-found.exception"
import { InvalidEventHandlerException } from "./exceptions/invalid-event-handler.exception"

export type EventHandlerType = Class<IEventHandler<BaseEvent, any>>
Expand All @@ -17,8 +16,11 @@ export class EventBus<TEvent extends BaseEvent = BaseEvent> implements IEventBus
#handlers = new Map<string, IEventHandler<TEvent, any>>()

async publish(event: TEvent): Promise<void> {
console.log("publish event", event)
const eventId = this.getEventId(event)
if (!eventId) {
return
}

const handler = this.#handlers.get(eventId)
if (!handler) {
return
Expand Down Expand Up @@ -57,11 +59,11 @@ export class EventBus<TEvent extends BaseEvent = BaseEvent> implements IEventBus
return eventMetadata.id
}

private getEventId(event: TEvent): string {
private getEventId(event: TEvent): string | undefined {
const { constructor: eventType } = Object.getPrototypeOf(event)
const eventMetadata: EventMetadata = Reflect.getMetadata(EVENT_METADATA, eventType)
if (!eventMetadata) {
throw new EventHandlerNotFoundException(eventType.name)
return
}
return eventMetadata.id
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,26 @@ export type IViewFields = z.infer<typeof viewFields>

export class ViewFields extends ValueObject<IViewFields> {
constructor(table: TableDo, props: IViewFields) {
const fields = table.schema.fields.map((field) => {
const exists = props.find((f) => f.fieldId === field.id.value)
if (exists) {
return exists
const fields = props.map((prop) => {
const field = table.schema.getFieldByIdOrName(prop.fieldId)
if (field) {
return prop
}
return {
fieldId: field.id.value,
hidden: false,
fieldId: prop.fieldId,
hidden: true,
}
})

table.schema.fields.forEach((field) => {
if (!fields.some((f) => f.fieldId === field.id.value)) {
fields.push({
fieldId: field.id.value,
hidden: false,
})
}
})

super(fields)
}

Expand Down

0 comments on commit ff46674

Please sign in to comment.