2023-12-08 10:25:07 +01:00
package gr.cite.notification.service.inappnotification ;
import gr.cite.commons.web.authz.service.AuthorizationService ;
2024-04-15 17:36:55 +02:00
import gr.cite.notification.authorization.OwnedResource ;
2023-12-08 10:25:07 +01:00
import gr.cite.notification.authorization.Permission ;
import gr.cite.notification.common.enums.NotificationInAppTracking ;
2024-03-29 13:32:47 +01:00
import gr.cite.notification.common.scope.user.UserScope ;
2023-12-08 10:25:07 +01:00
import gr.cite.notification.data.InAppNotificationEntity ;
2024-04-04 15:39:40 +02:00
import gr.cite.notification.data.TenantEntityManager ;
2023-12-08 10:25:07 +01:00
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 ;
2024-01-24 13:24:58 +01:00
import jakarta.persistence.Query ;
2023-12-08 10:25:07 +01:00
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 ;
import java.time.Instant ;
import java.util.List ;
import java.util.UUID ;
2024-01-24 13:24:58 +01:00
2023-12-08 10:25:07 +01:00
@Service
@RequestScope
2024-01-24 13:24:58 +01:00
public class InAppNotificationServiceImpl implements InAppNotificationService {
2023-12-08 10:25:07 +01:00
private static final LoggerService logger = new LoggerService ( LoggerFactory . getLogger ( InAppNotificationServiceImpl . class ) ) ;
private final QueryFactory queryFactory ;
2024-01-24 13:24:58 +01:00
2024-04-04 15:39:40 +02:00
private final TenantEntityManager entityManager ;
2024-01-24 13:24:58 +01:00
2023-12-08 10:25:07 +01:00
private final AuthorizationService authService ;
2024-01-24 13:24:58 +01:00
2023-12-08 10:25:07 +01:00
private final DeleterFactory deleterFactory ;
2024-03-29 13:32:47 +01:00
private final UserScope userScope ;
2023-12-08 10:25:07 +01:00
@Autowired
2024-04-04 15:39:40 +02:00
public InAppNotificationServiceImpl ( QueryFactory queryFactory , TenantEntityManager entityManager , AuthorizationService authService , DeleterFactory deleterFactory , UserScope userScope ) {
2023-12-08 10:25:07 +01:00
this . queryFactory = queryFactory ;
this . entityManager = entityManager ;
this . authService = authService ;
this . deleterFactory = deleterFactory ;
2024-03-29 13:32:47 +01:00
this . userScope = userScope ;
2023-12-08 10:25:07 +01:00
}
2024-01-24 13:24:58 +01:00
public void markAsRead ( UUID id ) {
2023-12-08 10:25:07 +01:00
this . markAsRead ( List . of ( id ) ) ;
}
2024-01-24 13:24:58 +01:00
public void markAsRead ( List < UUID > ids ) {
2023-12-08 10:25:07 +01:00
try {
logger . debug ( new DataLogEntry ( " marking as read in-app notifications " , ids ) ) ;
2024-03-29 13:32:47 +01:00
UUID userId = this . userScope . getUserId ( ) ;
2023-12-08 10:25:07 +01:00
List < InAppNotificationEntity > items = this . queryFactory . query ( InAppNotificationQuery . class )
. ids ( ids )
2024-03-29 13:32:47 +01:00
. userId ( userId )
2023-12-08 10:25:07 +01:00
. 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-24 13:24:58 +01:00
String sqlQuery = " UPDATE " + entity + " as e SET " + InAppNotificationEntity . _trackingState + " = :trackingState, " + InAppNotificationEntity . _updatedAt + " = :updatedAt, " + InAppNotificationEntity . _readTime + " = :readTime " +
" WHERE e. " + InAppNotificationEntity . _trackingState + " = :trackingStateCondition AND e. " + InAppNotificationEntity . _userId + " = :userId " ;
2024-01-10 14:12:26 +01:00
2024-04-04 15:39:40 +02:00
Query query = this . entityManager . getEntityManager ( ) . 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-24 13:24:58 +01:00
logger . debug ( " deleting in-app notification: {} " , id ) ;
2024-04-15 17:36:55 +02:00
this . authService . authorizeAtLeastOneForce ( this . userScope . getUserId ( ) ! = null ? List . of ( new OwnedResource ( this . userScope . getUserId ( ) ) ) : null , Permission . DeleteInAppNotification ) ;
2023-12-08 10:25:07 +01:00
this . deleterFactory . deleter ( InAppNotificationDeleter . class ) . deleteAndSaveByIds ( List . of ( id ) ) ;
}
}