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,14 +51,15 @@ 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,
AuditService auditService, AuditService auditService,
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,59 +68,75 @@ 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);
if (userId == null) throw new MyForbiddenException(this.errors.getNonPersonPrincipal().getCode(), this.errors.getNonPersonPrincipal().getMessage()); try {
InAppNotificationQuery query = lookup.enrich(this.queryFactory).disableTracking().userId(userId); this.tenantEntityManager.loadExplictTenantFilters();
List<InAppNotificationEntity> data = query.collectAs(lookup.getProject());
List<InAppNotification> models = this.builderFactory.builder(InAppNotificationBuilder.class).build(lookup.getProject(), data);
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
this.auditService.track(AuditableAction.InApp_Notification_Query, "lookup", lookup); if (userId == null) throw new MyForbiddenException(this.errors.getNonPersonPrincipal().getCode(), this.errors.getNonPersonPrincipal().getMessage());
InAppNotificationQuery query = lookup.enrich(this.queryFactory).disableTracking().userId(userId);
List<InAppNotificationEntity> data = query.collectAs(lookup.getProject());
List<InAppNotification> models = this.builderFactory.builder(InAppNotificationBuilder.class).build(lookup.getProject(), data);
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
return new QueryResult<>(models, count); this.auditService.track(AuditableAction.InApp_Notification_Query, "lookup", lookup);
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)
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Notification.class.getSimpleName()}, LocaleContextHolder.getLocale())); throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Notification.class.getSimpleName()}, LocaleContextHolder.getLocale()));
this.auditService.track(AuditableAction.InApp_Notification_Lookup, Map.ofEntries( this.auditService.track(AuditableAction.InApp_Notification_Lookup, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id), new AbstractMap.SimpleEntry<String, Object>("id", id),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet) new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
)); ));
//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));
this.inAppNotificationService.markAsRead(id); try {
this.tenantEntityManager.loadExplictTenantFilters();
this.auditService.track(AuditableAction.InApp_Notification_Read, Map.of("id", id));
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); this.inAppNotificationService.markAsRead(id);
return true;
this.auditService.track(AuditableAction.InApp_Notification_Read, Map.of("id", id));
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
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));
return true;
this.auditService.track(AuditableAction.InApp_Notification_Read_All, Map.of("userId", userId)); } finally {
return true; 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);
int count = Math.toIntExact(query.count());
InAppNotificationQuery query = this.queryFactory.query(InAppNotificationQuery.class).disableTracking().isActive(IsActive.Active).trackingState(NotificationInAppTracking.STORED).userId(userId); //this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action);
int count = Math.toIntExact(query.count());
//this.auditService.trackIdentity(AuditableAction.IdentityTracking_Action); return count;
} finally {
return count; 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));
this.inAppNotificationService.deleteAndSave(id); try {
this.tenantEntityManager.loadExplictTenantFilters();
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);