|
| 1 | +import { MessageHandlerConfigBuilder } from '@message-queue-toolkit/core' |
| 2 | +import type { ConsumerMessageSchema } from '@message-queue-toolkit/schemas' |
| 3 | +import { AbstractSnsSqsConsumer, type SNSSQSConsumerDependencies } from '@message-queue-toolkit/sns' |
| 4 | +import { UserEvents } from './TestMessages.ts' |
| 5 | +import { userCreatedHandler } from './handlers/UserCreatedHandler.ts' |
| 6 | +import { userUpdatedHandler } from './handlers/UserUpdatedHandler.ts' |
| 7 | + |
| 8 | +type SupportedMessages = ConsumerMessageSchema< |
| 9 | + typeof UserEvents.created | typeof UserEvents.updated |
| 10 | +> |
| 11 | + |
| 12 | +// biome-ignore lint/complexity/noBannedTypes: to be expanded later |
| 13 | +type ExecutionContext = {} |
| 14 | + |
| 15 | +const isTest = true |
| 16 | + |
| 17 | +export class UserConsumer extends AbstractSnsSqsConsumer<SupportedMessages, ExecutionContext> { |
| 18 | + public static readonly CONSUMED_QUEUE_NAME = 'user-my_service' |
| 19 | + public static readonly SUBSCRIBED_TOPIC_NAME = 'user' |
| 20 | + |
| 21 | + constructor(dependencies: SNSSQSConsumerDependencies) { |
| 22 | + super( |
| 23 | + dependencies, |
| 24 | + { |
| 25 | + handlerSpy: true, |
| 26 | + handlers: new MessageHandlerConfigBuilder<SupportedMessages, ExecutionContext>() |
| 27 | + .addConfig(UserEvents.created, userCreatedHandler, {}) |
| 28 | + .addConfig(UserEvents.updated, userUpdatedHandler, {}) |
| 29 | + .build(), |
| 30 | + messageTypeField: 'type', |
| 31 | + // Consumer creates its own queue |
| 32 | + creationConfig: { |
| 33 | + queue: { |
| 34 | + QueueName: UserConsumer.CONSUMED_QUEUE_NAME, |
| 35 | + }, |
| 36 | + }, |
| 37 | + deletionConfig: { |
| 38 | + deleteIfExists: isTest, |
| 39 | + }, |
| 40 | + locatorConfig: { |
| 41 | + // Topic is created by a publisher, consumer relies on it already existing. |
| 42 | + // Note that in order for this to work correctly you need to ensure that |
| 43 | + // publisher gets initialized first. If consumer will initialize first, |
| 44 | + // publisher may delete already existing topic and subscription and break the setup |
| 45 | + topicName: UserConsumer.SUBSCRIBED_TOPIC_NAME, |
| 46 | + }, |
| 47 | + // consumer creates its own subscription |
| 48 | + subscriptionConfig: { |
| 49 | + updateAttributesIfExists: false, |
| 50 | + }, |
| 51 | + }, |
| 52 | + {}, |
| 53 | + ) |
| 54 | + } |
| 55 | +} |
0 commit comments