filter in app from current tenant

This commit is contained in:
Efstratios Giannopoulos 2024-06-07 17:56:02 +03:00
parent 6c0f66f498
commit 24651aa1b0
2 changed files with 74 additions and 44 deletions

View File

@ -6,6 +6,7 @@ import gr.cite.notification.common.enums.IsActive;
import gr.cite.notification.common.enums.NotificationInAppTracking; import gr.cite.notification.common.enums.NotificationInAppTracking;
import gr.cite.notification.common.scope.user.UserScope; import gr.cite.notification.common.scope.user.UserScope;
import gr.cite.notification.data.InAppNotificationEntity; import gr.cite.notification.data.InAppNotificationEntity;
import gr.cite.notification.data.TenantEntityManager;
import gr.cite.notification.errorcode.ErrorThesaurusProperties; import gr.cite.notification.errorcode.ErrorThesaurusProperties;
import gr.cite.notification.model.InAppNotification; import gr.cite.notification.model.InAppNotification;
import gr.cite.notification.model.Notification; import gr.cite.notification.model.Notification;
@ -50,6 +51,7 @@ public class InAppNotificationController {
private final InAppNotificationService inAppNotificationService; private final InAppNotificationService inAppNotificationService;
private final UserScope userScope; private final UserScope userScope;
private final ErrorThesaurusProperties errors; private final ErrorThesaurusProperties errors;
private final TenantEntityManager tenantEntityManager;
@Autowired @Autowired
public InAppNotificationController(BuilderFactory builderFactory, public InAppNotificationController(BuilderFactory builderFactory,
@ -57,7 +59,7 @@ public class InAppNotificationController {
NotificationService notificationService, CensorFactory censorFactory, NotificationService notificationService, CensorFactory censorFactory,
QueryFactory queryFactory, QueryFactory queryFactory,
MessageSource messageSource, MessageSource messageSource,
InAppNotificationService inAppNotificationService, UserScope userScope, ErrorThesaurusProperties errors) { InAppNotificationService inAppNotificationService, UserScope userScope, ErrorThesaurusProperties errors, TenantEntityManager tenantEntityManager) {
this.builderFactory = builderFactory; this.builderFactory = builderFactory;
this.auditService = auditService; this.auditService = auditService;
this.censorFactory = censorFactory; this.censorFactory = censorFactory;
@ -66,15 +68,18 @@ public class InAppNotificationController {
this.inAppNotificationService = inAppNotificationService; this.inAppNotificationService = inAppNotificationService;
this.userScope = userScope; this.userScope = userScope;
this.errors = errors; this.errors = errors;
this.tenantEntityManager = tenantEntityManager;
} }
@PostMapping("query") @PostMapping("query")
public QueryResult<InAppNotification> Query(@RequestBody InAppNotificationLookup lookup) throws MyApplicationException, MyForbiddenException, InvalidApplicationException { public QueryResult<InAppNotification> Query(@RequestBody InAppNotificationLookup lookup) throws MyApplicationException, MyForbiddenException, InvalidApplicationException {
logger.debug("querying {}", InAppNotification.class.getSimpleName()); logger.debug("querying {}", InAppNotification.class.getSimpleName());
UUID userId = this.userScope.getUserId(); UUID userId = this.userScope.getUserId();
this.censorFactory.censor(InAppNotificationCensor.class).censor(lookup.getProject(), userId); this.censorFactory.censor(InAppNotificationCensor.class).censor(lookup.getProject(), userId);
try {
this.tenantEntityManager.loadExplictTenantFilters();
if (userId == null) throw new MyForbiddenException(this.errors.getNonPersonPrincipal().getCode(), this.errors.getNonPersonPrincipal().getMessage()); if (userId == null) throw new MyForbiddenException(this.errors.getNonPersonPrincipal().getCode(), this.errors.getNonPersonPrincipal().getMessage());
InAppNotificationQuery query = lookup.enrich(this.queryFactory).disableTracking().userId(userId); InAppNotificationQuery query = lookup.enrich(this.queryFactory).disableTracking().userId(userId);
List<InAppNotificationEntity> data = query.collectAs(lookup.getProject()); List<InAppNotificationEntity> data = query.collectAs(lookup.getProject());
@ -84,16 +89,21 @@ public class InAppNotificationController {
this.auditService.track(AuditableAction.InApp_Notification_Query, "lookup", lookup); this.auditService.track(AuditableAction.InApp_Notification_Query, "lookup", lookup);
return new QueryResult<>(models, count); return new QueryResult<>(models, count);
} finally {
this.tenantEntityManager.reloadTenantFilters();
}
} }
@GetMapping("{id}") @GetMapping("{id}")
@Transactional @Transactional
public InAppNotification Get(@PathVariable UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException { public InAppNotification Get(@PathVariable UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException {
logger.debug(new MapLogEntry("retrieving" + InAppNotification.class.getSimpleName()).And("id", id).And("fields", fieldSet)); logger.debug(new MapLogEntry("retrieving" + InAppNotification.class.getSimpleName()).And("id", id).And("fields", fieldSet));
UUID userId = this.userScope.getUserId(); UUID userId = this.userScope.getUserId();
this.censorFactory.censor(InAppNotificationCensor.class).censor(fieldSet, userId); this.censorFactory.censor(InAppNotificationCensor.class).censor(fieldSet, userId);
try {
this.tenantEntityManager.loadExplictTenantFilters();
InAppNotificationQuery query = this.queryFactory.query(InAppNotificationQuery.class).disableTracking().authorize(AuthorizationFlags.OwnerOrPermission).userId(userId).ids(id); InAppNotificationQuery query = this.queryFactory.query(InAppNotificationQuery.class).disableTracking().authorize(AuthorizationFlags.OwnerOrPermission).userId(userId).ids(id);
InAppNotification model = this.builderFactory.builder(InAppNotificationBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(fieldSet, query.firstAs(fieldSet)); InAppNotification model = this.builderFactory.builder(InAppNotificationBuilder.class).authorize(AuthorizationFlags.OwnerOrPermission).build(fieldSet, query.firstAs(fieldSet));
if (model == null) if (model == null)
@ -106,19 +116,27 @@ public class InAppNotificationController {
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
return model; return model;
} finally {
this.tenantEntityManager.reloadTenantFilters();
}
} }
@PostMapping("{id}/read") @PostMapping("{id}/read")
@Transactional @Transactional
public Boolean persist(@PathVariable UUID id) public Boolean persist(@PathVariable UUID id) throws InvalidApplicationException {
{
logger.debug(new MapLogEntry("marking as read").And("id", id)); logger.debug(new MapLogEntry("marking as read").And("id", id));
try {
this.tenantEntityManager.loadExplictTenantFilters();
this.inAppNotificationService.markAsRead(id); this.inAppNotificationService.markAsRead(id);
this.auditService.track(AuditableAction.InApp_Notification_Read, Map.of("id", id)); this.auditService.track(AuditableAction.InApp_Notification_Read, Map.of("id", id));
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
return true; return true;
} finally {
this.tenantEntityManager.reloadTenantFilters();
}
} }
@PostMapping("read-all") @PostMapping("read-all")
@ -128,11 +146,15 @@ public class InAppNotificationController {
UUID userId = this.userScope.getUserId(); UUID userId = this.userScope.getUserId();
if (userId == null) throw new MyForbiddenException(this.errors.getNonPersonPrincipal().getCode(), this.errors.getNonPersonPrincipal().getMessage()); if (userId == null) throw new MyForbiddenException(this.errors.getNonPersonPrincipal().getCode(), this.errors.getNonPersonPrincipal().getMessage());
try {
this.tenantEntityManager.loadExplictTenantFilters();
this.inAppNotificationService.markAsReadAllUserNotification(userId); this.inAppNotificationService.markAsReadAllUserNotification(userId);
this.auditService.track(AuditableAction.InApp_Notification_Read_All, Map.of("userId", userId)); this.auditService.track(AuditableAction.InApp_Notification_Read_All, Map.of("userId", userId));
return true; return true;
} finally {
this.tenantEntityManager.reloadTenantFilters();
}
} }
@GetMapping("count-unread") @GetMapping("count-unread")
@ -144,13 +166,17 @@ public class InAppNotificationController {
if (userId == null) throw new MyForbiddenException(this.errors.getNonPersonPrincipal().getCode(), this.errors.getNonPersonPrincipal().getMessage()); if (userId == null) throw new MyForbiddenException(this.errors.getNonPersonPrincipal().getCode(), this.errors.getNonPersonPrincipal().getMessage());
this.censorFactory.censor(InAppNotificationCensor.class).censor(new BaseFieldSet(InAppNotification.Field.ID), userId); this.censorFactory.censor(InAppNotificationCensor.class).censor(new BaseFieldSet(InAppNotification.Field.ID), userId);
try {
this.tenantEntityManager.loadExplictTenantFilters();
InAppNotificationQuery query = this.queryFactory.query(InAppNotificationQuery.class).disableTracking().isActive(IsActive.Active).trackingState(NotificationInAppTracking.STORED).userId(userId); InAppNotificationQuery query = this.queryFactory.query(InAppNotificationQuery.class).disableTracking().isActive(IsActive.Active).trackingState(NotificationInAppTracking.STORED).userId(userId);
int count = Math.toIntExact(query.count()); int count = Math.toIntExact(query.count());
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
return count; return count;
} finally {
this.tenantEntityManager.reloadTenantFilters();
}
} }
@DeleteMapping("{id}") @DeleteMapping("{id}")
@ -158,10 +184,15 @@ public class InAppNotificationController {
public void Delete(@PathVariable("id") UUID id) throws MyForbiddenException, InvalidApplicationException { public void Delete(@PathVariable("id") UUID id) throws MyForbiddenException, InvalidApplicationException {
logger.debug(new MapLogEntry("deleting" + Notification.class.getSimpleName()).And("id", id)); logger.debug(new MapLogEntry("deleting" + Notification.class.getSimpleName()).And("id", id));
try {
this.tenantEntityManager.loadExplictTenantFilters();
this.inAppNotificationService.deleteAndSave(id); this.inAppNotificationService.deleteAndSave(id);
this.auditService.track(AuditableAction.InApp_Notification_Delete, "id", id); this.auditService.track(AuditableAction.InApp_Notification_Delete, "id", id);
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
} finally {
this.tenantEntityManager.reloadTenantFilters();
}
} }
} }

View File

@ -68,7 +68,6 @@ public class InAppNotificationServiceImpl implements InAppNotificationService {
item.setUpdatedAt(now); item.setUpdatedAt(now);
item.setReadTime(now); item.setReadTime(now);
this.entityManager.merge(item); this.entityManager.merge(item);
this.entityManager.persist(item);
} }
} catch (InvalidApplicationException e) { } catch (InvalidApplicationException e) {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);