-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added notifications for approving new users (#2803)
* Added notifications for approving new users
- Loading branch information
1 parent
52a2c10
commit 7c2aac3
Showing
9 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<databaseChangeLog | ||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog | ||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd"> | ||
|
||
<changeSet id="notification-creation" author="saveourtool-dev"> | ||
<createTable tableName="notification"> | ||
<column name="id" type="bigint" autoIncrement="true"> | ||
<constraints primaryKey="true" nullable="false"/> | ||
</column> | ||
<column name="message" type="TEXT"/> | ||
<column name="user_id" type="bigint"> | ||
<constraints foreignKeyName="fk_notification_user" references="user(id)" | ||
nullable="false" deleteCascade="true"/> | ||
</column> | ||
<column name="create_date" type="DATETIME(3)"> | ||
<constraints nullable="true"/> | ||
</column> | ||
<column name="update_date" type="DATETIME(3)"> | ||
<constraints nullable="true"/> | ||
</column> | ||
</createTable> | ||
</changeSet> | ||
|
||
</databaseChangeLog> |
46 changes: 46 additions & 0 deletions
46
save-backend/src/main/kotlin/com/saveourtool/save/backend/event/UserListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.saveourtool.save.backend.event | ||
|
||
import com.saveourtool.save.backend.service.NotificationService | ||
import com.saveourtool.save.backend.service.UserDetailsService | ||
import com.saveourtool.save.domain.Role | ||
import com.saveourtool.save.entities.Notification | ||
import com.saveourtool.save.entities.User | ||
import com.saveourtool.save.info.UserStatus | ||
import org.springframework.context.event.EventListener | ||
import org.springframework.stereotype.Component | ||
|
||
/** | ||
* A user listener for sending notifications. | ||
*/ | ||
@Component | ||
class UserListener( | ||
private val userDetailsService: UserDetailsService, | ||
private val notificationService: NotificationService, | ||
) { | ||
/** | ||
* @param user new user | ||
*/ | ||
@EventListener | ||
fun createUser(user: User) { | ||
if (user.status == UserStatus.NOT_APPROVED) { | ||
val recipients = userDetailsService.findByRole(Role.SUPER_ADMIN.asSpringSecurityRole()) | ||
val notifications = recipients.map { | ||
Notification( | ||
message = messageNewUser(user), | ||
user = it, | ||
) | ||
} | ||
notificationService.saveAll(notifications) | ||
} | ||
} | ||
|
||
companion object { | ||
/** | ||
* @param user | ||
* @return message | ||
*/ | ||
fun messageNewUser(user: User) = """ | ||
New user: ${user.name} is waiting for approve of his account. | ||
""".trimIndent() | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...backend/src/main/kotlin/com/saveourtool/save/backend/repository/NotificationRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.saveourtool.save.backend.repository | ||
|
||
import com.saveourtool.save.entities.Notification | ||
import com.saveourtool.save.spring.repository.BaseEntityRepository | ||
import org.springframework.stereotype.Repository | ||
|
||
/** | ||
* Repository to access data about user notification | ||
*/ | ||
@Repository | ||
interface NotificationRepository : BaseEntityRepository<Notification> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
save-backend/src/main/kotlin/com/saveourtool/save/backend/service/NotificationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.saveourtool.save.backend.service | ||
|
||
import com.saveourtool.save.backend.repository.NotificationRepository | ||
import com.saveourtool.save.entities.Notification | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
/** | ||
* Service for notifications | ||
*/ | ||
@Service | ||
@Transactional(readOnly = true) | ||
class NotificationService( | ||
private val notificationRepository: NotificationRepository, | ||
) { | ||
/** | ||
* @param notification | ||
* @return saved notification | ||
*/ | ||
@Transactional | ||
fun save(notification: Notification): Notification = notificationRepository.save(notification) | ||
|
||
/** | ||
* @param notifications | ||
* @return saved notifications | ||
*/ | ||
@Transactional | ||
fun saveAll(notifications: List<Notification>): List<Notification> = notificationRepository.saveAll(notifications) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/entities/NotificationDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.saveourtool.save.entities | ||
|
||
import kotlinx.datetime.LocalDateTime | ||
import kotlinx.serialization.Serializable | ||
|
||
/** | ||
* @property message | ||
* @property createDate | ||
*/ | ||
@Serializable | ||
class NotificationDto( | ||
val message: String, | ||
val createDate: LocalDateTime?, | ||
) |
27 changes: 27 additions & 0 deletions
27
save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/Notification.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.saveourtool.save.entities | ||
|
||
import com.saveourtool.save.spring.entity.BaseEntityWithDateAndDto | ||
|
||
import javax.persistence.Entity | ||
import javax.persistence.JoinColumn | ||
import javax.persistence.ManyToOne | ||
|
||
import kotlinx.datetime.toKotlinLocalDateTime | ||
|
||
/** | ||
* @property message | ||
* @property user | ||
*/ | ||
@Entity | ||
class Notification( | ||
var message: String, | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "user_id") | ||
var user: User, | ||
) : BaseEntityWithDateAndDto<NotificationDto>() { | ||
override fun toDto() = NotificationDto( | ||
message = message, | ||
createDate = createDate?.toKotlinLocalDateTime(), | ||
) | ||
} |