diff --git a/Backend/dependency-kotlin/inquiry/src/main/kotlin/com/staffinghub/coding/challenges/dependency/inquiry/InquiryCreatedEvent.kt b/Backend/dependency-kotlin/inquiry/src/main/kotlin/com/staffinghub/coding/challenges/dependency/inquiry/InquiryCreatedEvent.kt new file mode 100644 index 0000000..307659a --- /dev/null +++ b/Backend/dependency-kotlin/inquiry/src/main/kotlin/com/staffinghub/coding/challenges/dependency/inquiry/InquiryCreatedEvent.kt @@ -0,0 +1,8 @@ +package com.staffinghub.coding.challenges.dependency.inquiry + +import org.springframework.context.ApplicationEvent + +/** + * This class represents an event that is triggered when an inquiry is created. + */ +class InquiryCreatedEvent(source: Any, val inquiry: Inquiry) : ApplicationEvent(source) \ No newline at end of file diff --git a/Backend/dependency-kotlin/inquiry/src/main/kotlin/com/staffinghub/coding/challenges/dependency/inquiry/InquiryService.kt b/Backend/dependency-kotlin/inquiry/src/main/kotlin/com/staffinghub/coding/challenges/dependency/inquiry/InquiryService.kt index c105fad..91475c1 100644 --- a/Backend/dependency-kotlin/inquiry/src/main/kotlin/com/staffinghub/coding/challenges/dependency/inquiry/InquiryService.kt +++ b/Backend/dependency-kotlin/inquiry/src/main/kotlin/com/staffinghub/coding/challenges/dependency/inquiry/InquiryService.kt @@ -2,15 +2,26 @@ package com.staffinghub.coding.challenges.dependency.inquiry import mu.KotlinLogging import org.springframework.stereotype.Component +import org.springframework.context.ApplicationEventPublisher private val logger = KotlinLogging.logger {} +/** + * This class represents a service responsible for creating inquiries and publishing an event when an inquiry is created. + */ @Component -class InquiryService { +class InquiryService(private val applicationEventPublisher: ApplicationEventPublisher) { + + /** + * Creates an inquiry and publishes an event for the newly created inquiry. + * + * @param inquiry The Inquiry object representing the inquiry to be created. + */ fun create(inquiry: Inquiry) { logger.info { "User sent inquiry: $inquiry" } + applicationEventPublisher.publishEvent(InquiryCreatedEvent(this, inquiry)) } } diff --git a/Backend/dependency-kotlin/notifications/src/main/kotlin/com/staffinghub/coding/challenges/dependency/notifications/InquiryCreatedEventListener.kt b/Backend/dependency-kotlin/notifications/src/main/kotlin/com/staffinghub/coding/challenges/dependency/notifications/InquiryCreatedEventListener.kt new file mode 100644 index 0000000..c104765 --- /dev/null +++ b/Backend/dependency-kotlin/notifications/src/main/kotlin/com/staffinghub/coding/challenges/dependency/notifications/InquiryCreatedEventListener.kt @@ -0,0 +1,30 @@ +package com.staffinghub.coding.challenges.dependency.notifications +import com.staffinghub.coding.challenges.dependency.inquiry.Inquiry +import com.staffinghub.coding.challenges.dependency.inquiry.InquiryCreatedEvent +import org.springframework.context.event.EventListener +import org.springframework.stereotype.Component + +/** + * This class is responsible for handling the creation of inquiries and triggering appropriate actions such as sending + * emails and push notifications. + */ +@Component +class InquiryCreatedEventListener( + private val emailHandler: EmailHandler, + private val pushNotificationHandler: PushNotificationHandler +) { + + /** + * Listens to the InquiryCreatedEvent and handles it by sending an email and a push notification for the newly + * created inquiry. + * + * @param event The InquiryCreatedEvent object representing the event triggered upon inquiry creation. + */ + @EventListener + fun handleInquiryCreatedEvent(event: InquiryCreatedEvent) { + val inq: Inquiry = event.inquiry + + emailHandler.sendEmail(inq) + pushNotificationHandler.sendNotification(inq) + } +}