add inapp notification listing like filter and read all sql query

This commit is contained in:
amentis 2024-01-10 15:12:26 +02:00
parent c7ea2ce2db
commit 29bea9f246
7 changed files with 55 additions and 41 deletions

View File

@ -31,7 +31,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.management.InvalidApplicationException;
@ -112,7 +111,7 @@ public class InAppNotificationController {
@PostMapping("{id}/read")
@Transactional
public ResponseEntity<String> persist(@PathVariable UUID id)
public Boolean persist(@PathVariable UUID id)
{
logger.debug(new MapLogEntry("marking as read").And("id", id));
@ -120,12 +119,12 @@ public class InAppNotificationController {
this.auditService.track(AuditableAction.InApp_Notification_Read, Map.of("id", id));
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
return ResponseEntity.ok("ok");
return true;
}
@PostMapping("read-all")
@Transactional
public ResponseEntity<String> MarkAsReadAllUserInAppNotification() throws InvalidApplicationException {
public Boolean MarkAsReadAllUserInAppNotification() throws InvalidApplicationException, NoSuchFieldException {
logger.debug(new MapLogEntry("marking as read all"));
UUID userId = this.userScope.getUserId();
@ -134,12 +133,12 @@ public class InAppNotificationController {
this.inAppNotificationService.markAsReadAllUserNotification(userId);
this.auditService.track(AuditableAction.InApp_Notification_Read_All, Map.of("userId", userId));
return ResponseEntity.ok("ok");
return true;
}
@GetMapping("count-unread")
@Transactional
public ResponseEntity<Integer> CountUnread() throws InvalidApplicationException {
public Integer CountUnread() throws InvalidApplicationException {
logger.debug("count-unread");
UUID userId = this.userScope.getUserId();
@ -152,7 +151,7 @@ public class InAppNotificationController {
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
return ResponseEntity.ok(count);
return count;
}
@DeleteMapping("{id}")

View File

@ -8,12 +8,12 @@ import gr.cite.tools.exception.MyForbiddenException;
import org.hibernate.Session;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope;
import javax.management.InvalidApplicationException;
import javax.persistence.EntityManager;
import javax.persistence.FlushModeType;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.UUID;
@Service
@ -32,14 +32,16 @@ public class TenantScopedEntityManager {
public int getBulkSize() {
Session session = this.entityManager.unwrap(Session.class);
if (session.getJdbcBatchSize() != null) return session.getJdbcBatchSize();
return 0;
return session.getJdbcBatchSize();
}
public void setBulkSize(int size) {
Session session = this.entityManager.unwrap(Session.class);
session.setJdbcBatchSize(size);
}
public Query createQuery(String query){
return this.entityManager.createQuery(query);
}
public void persist(Object entity) {
this.entityManager.persist(entity);
}

View File

@ -1,13 +1,9 @@
package gr.cite.notification.query;
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.notification.authorization.AuthorizationFlags;
import gr.cite.notification.authorization.Permission;
import gr.cite.notification.common.enums.*;
import gr.cite.notification.common.scope.user.UserScope;
import gr.cite.notification.data.InAppNotificationEntity;
import gr.cite.notification.data.InAppNotificationEntity;
import gr.cite.notification.model.InAppNotification;
import gr.cite.notification.data.LanguageEntity;
import gr.cite.notification.model.InAppNotification;
import gr.cite.tools.data.query.FieldResolver;
import gr.cite.tools.data.query.QueryBase;
@ -25,6 +21,7 @@ import java.util.*;
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class InAppNotificationQuery extends QueryBase<InAppNotificationEntity> {
private String like;
private Collection<UUID> ids;
private List<UUID> excludeIds;
private List<UUID> userId;
@ -37,6 +34,11 @@ public class InAppNotificationQuery extends QueryBase<InAppNotificationEntity> {
private Instant to;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
public InAppNotificationQuery like(String value) {
this.like = value;
return this;
}
public InAppNotificationQuery ids(UUID value) {
this.ids = List.of(value);
return this;
@ -164,6 +166,10 @@ public class InAppNotificationQuery extends QueryBase<InAppNotificationEntity> {
predicates.add(queryContext.Root.get(InAppNotificationEntity.Field._id).in(ids));
}
if (this.like != null && !this.like.isEmpty()) {
predicates.add(queryContext.CriteriaBuilder.like(queryContext.Root.get(InAppNotificationEntity.Field._subject), this.like));
}
if (this.excludeIds != null) {
predicates.add(queryContext.CriteriaBuilder.not(queryContext.Root.get(InAppNotificationEntity.Field._id).in(ids)));
}

View File

@ -13,6 +13,8 @@ import java.util.List;
import java.util.UUID;
public class InAppNotificationLookup extends Lookup {
private String like;
private Collection<UUID> ids;
private List<UUID> excludeIds;
private List<UUID> userId;
@ -24,6 +26,14 @@ public class InAppNotificationLookup extends Lookup {
private Instant from;
private Instant to;
public String getLike() {
return like;
}
public void setLike(String like) {
this.like = like;
}
public Collection<UUID> getIds() {
return ids;
}
@ -106,6 +116,7 @@ public class InAppNotificationLookup extends Lookup {
public InAppNotificationQuery enrich(QueryFactory queryFactory) {
InAppNotificationQuery query = queryFactory.query(InAppNotificationQuery.class);
if (this.like != null) query.like(this.like);
if (this.isActive != null) query.isActive(this.isActive);
if (this.ids != null) query.ids(this.ids);
if (this.type != null) query.type(this.type);

View File

@ -9,5 +9,5 @@ public interface InAppNotificationService {
void markAsRead(List<UUID> ids);
void markAsRead(UUID id);
void deleteAndSave(UUID id) throws InvalidApplicationException;
void markAsReadAllUserNotification(UUID userId);
void markAsReadAllUserNotification(UUID userId) throws NoSuchFieldException;
}

View File

@ -18,6 +18,8 @@ import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope;
import javax.management.InvalidApplicationException;
import javax.persistence.Column;
import javax.persistence.Query;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
@ -66,31 +68,25 @@ public class InAppNotificationServiceImpl implements InAppNotificationService{
}
}
public void markAsReadAllUserNotification(UUID userId)
{
try {
logger.debug(new DataLogEntry("marking as read all user in-app notifications", userId));
public void markAsReadAllUserNotification(UUID userId) throws NoSuchFieldException {
if (userId == null || userId.equals(new UUID(0L, 0L))) return;
String entity = InAppNotificationEntity.class.getSimpleName();
String trackingStateProperty = InAppNotificationEntity.class.getDeclaredField(InAppNotificationEntity.Field._trackingState).getAnnotation(Column.class).name().replace("\"", "");
String updateProperty = InAppNotificationEntity.class.getDeclaredField(InAppNotificationEntity.Field._updatedAt).getAnnotation(Column.class).name().replace("\"", "");
String readTimeProperty = InAppNotificationEntity.class.getDeclaredField(InAppNotificationEntity.Field._readTime).getAnnotation(Column.class).name().replace("\"", "");
String userProperty = InAppNotificationEntity.class.getDeclaredField(InAppNotificationEntity.Field._userId).getAnnotation(Column.class).name().replace("\"", "");
List<InAppNotificationEntity> inAppNotificationEntities = this.queryFactory.query(InAppNotificationQuery.class)
.userId(userId)
.trackingState(NotificationInAppTracking.STORED)
.collect();
Instant now = Instant.now();
int bulkSize = this.entityManager.getBulkSize();
this.entityManager.setBulkSize(inAppNotificationEntities.size());
for (InAppNotificationEntity entity : inAppNotificationEntities) {
entity.setReadTime(now);
entity.setUpdatedAt(now);
entity.setTrackingState(NotificationInAppTracking.DELIVERED);
this.entityManager.merge(entity);
this.entityManager.persist(entity);
}
this.entityManager.setBulkSize(bulkSize);
} catch (InvalidApplicationException e) {
logger.error(e.getMessage(), e);
}
String sqlQuery = "UPDATE " + entity + " SET "+ trackingStateProperty +" = :trackingState, "+ updateProperty + "= :updatedAt, " + readTimeProperty + " = :readTime " +
"WHERE " + trackingStateProperty + " = :trackingStateCondition";//TODO AND \"'user\" = :userId";
Query query = this.entityManager.createQuery(sqlQuery)
.setParameter("trackingState", NotificationInAppTracking.DELIVERED.getValue())
.setParameter("updatedAt", Instant.now())
.setParameter("readTime", Instant.now())
//.setParameter("userId", userId)
.setParameter("trackingStateCondition", NotificationInAppTracking.STORED.getValue());
int updateCount = query.executeUpdate();
}

View File

@ -121,8 +121,8 @@ export class InAppNotificationEditorComponent extends BaseComponent implements O
}
onCallbackSuccess(data?: any): void {
this.uiNotificationService.snackBarNotification(this.language.instant('COMMONS.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
this.router.navigate(['/mine-notifications']);
// this.uiNotificationService.snackBarNotification(this.language.instant('COMMONS.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
// this.router.navigate(['/mine-notifications']);
}
onCallbackError(errorResponse: HttpErrorResponse) {