2023-12-08 10:25:07 +01:00
package gr.cite.notification.service.inappnotification ;
import gr.cite.commons.web.authz.service.AuthorizationService ;
import gr.cite.notification.authorization.Permission ;
import gr.cite.notification.common.enums.NotificationInAppTracking ;
import gr.cite.notification.data.InAppNotificationEntity ;
import gr.cite.notification.data.TenantScopedEntityManager ;
import gr.cite.notification.model.deleter.InAppNotificationDeleter ;
import gr.cite.notification.query.InAppNotificationQuery ;
import gr.cite.tools.data.deleter.DeleterFactory ;
import gr.cite.tools.data.query.QueryFactory ;
import gr.cite.tools.exception.MyForbiddenException ;
import gr.cite.tools.logging.DataLogEntry ;
import gr.cite.tools.logging.LoggerService ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import org.springframework.web.context.annotation.RequestScope ;
import javax.management.InvalidApplicationException ;
2024-01-16 18:13:07 +01:00
import jakarta.persistence.Column ;
import jakarta.persistence.Query ;
2023-12-08 10:25:07 +01:00
import java.time.Instant ;
import java.util.List ;
import java.util.UUID ;
@Service
@RequestScope
public class InAppNotificationServiceImpl implements InAppNotificationService {
private static final LoggerService logger = new LoggerService ( LoggerFactory . getLogger ( InAppNotificationServiceImpl . class ) ) ;
private final QueryFactory queryFactory ;
private final TenantScopedEntityManager entityManager ;
private final AuthorizationService authService ;
private final DeleterFactory deleterFactory ;
@Autowired
public InAppNotificationServiceImpl ( QueryFactory queryFactory , TenantScopedEntityManager entityManager , AuthorizationService authService , DeleterFactory deleterFactory ) {
this . queryFactory = queryFactory ;
this . entityManager = entityManager ;
this . authService = authService ;
this . deleterFactory = deleterFactory ;
}
public void markAsRead ( UUID id )
{
this . markAsRead ( List . of ( id ) ) ;
}
public void markAsRead ( List < UUID > ids )
{
try {
logger . debug ( new DataLogEntry ( " marking as read in-app notifications " , ids ) ) ;
List < InAppNotificationEntity > items = this . queryFactory . query ( InAppNotificationQuery . class )
. ids ( ids )
. collect ( ) ;
Instant now = Instant . now ( ) ;
for ( InAppNotificationEntity item : items ) {
item . setTrackingState ( NotificationInAppTracking . DELIVERED ) ;
item . setUpdatedAt ( now ) ;
item . setReadTime ( now ) ;
this . entityManager . merge ( item ) ;
this . entityManager . persist ( item ) ;
}
} catch ( InvalidApplicationException e ) {
logger . error ( e . getMessage ( ) , e ) ;
}
}
2024-01-11 09:47:57 +01:00
public void markAsReadAllUserNotification ( UUID userId ) {
2024-01-10 14:12:26 +01:00
String entity = InAppNotificationEntity . class . getSimpleName ( ) ;
2023-12-08 10:25:07 +01:00
2024-01-11 09:47:57 +01:00
String sqlQuery = " UPDATE " + entity + " as e SET " + InAppNotificationEntity . Field . _trackingState + " = :trackingState, " + InAppNotificationEntity . Field . _updatedAt + " = :updatedAt, " + InAppNotificationEntity . Field . _readTime + " = :readTime " +
" WHERE e. " + InAppNotificationEntity . Field . _trackingState + " = :trackingStateCondition AND e. " + InAppNotificationEntity . Field . _userId + " = :userId " ;
2024-01-10 14:12:26 +01:00
Query query = this . entityManager . createQuery ( sqlQuery )
2024-01-11 09:47:57 +01:00
. setParameter ( " trackingState " , NotificationInAppTracking . DELIVERED )
2024-01-10 14:12:26 +01:00
. setParameter ( " updatedAt " , Instant . now ( ) )
. setParameter ( " readTime " , Instant . now ( ) )
2024-01-11 09:47:57 +01:00
. setParameter ( " userId " , userId )
. setParameter ( " trackingStateCondition " , NotificationInAppTracking . STORED ) ;
2024-01-10 14:12:26 +01:00
int updateCount = query . executeUpdate ( ) ;
2023-12-08 10:25:07 +01:00
}
public void deleteAndSave ( UUID id ) throws MyForbiddenException , InvalidApplicationException {
2024-01-12 14:57:38 +01:00
logger . debug ( " deleting inapp notification: {} " , id ) ;
this . authService . authorizeForce ( Permission . DeleteInAppNotification ) ;
2023-12-08 10:25:07 +01:00
this . deleterFactory . deleter ( InAppNotificationDeleter . class ) . deleteAndSaveByIds ( List . of ( id ) ) ;
}
}