Fixing missing parts on notification service migration

This commit is contained in:
Thomas Georgios Giannos 2024-01-17 17:36:22 +02:00
parent 70c94c5bdc
commit d0df9c7e79
4 changed files with 177 additions and 13 deletions

View File

@ -4,6 +4,8 @@ import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
import gr.cite.notification.audit.AuditableAction;
import gr.cite.notification.common.JsonHandlingService;
import gr.cite.notification.common.enums.NotificationNotifyState;
import gr.cite.notification.common.enums.NotificationTrackingProcess;
import gr.cite.notification.common.enums.NotificationTrackingState;
import gr.cite.notification.common.scope.fake.FakeRequestScope;
import gr.cite.notification.common.scope.tenant.TenantScope;
import gr.cite.notification.data.TenantEntity;
@ -32,6 +34,7 @@ import org.springframework.context.annotation.Scope;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.AbstractMap;
import java.util.Map;
@ -66,12 +69,19 @@ public class NotifyIntegrationEventHandlerImpl implements NotifyIntegrationEvent
}
NotificationPersist model = new NotificationPersist();
model.setType(event.getNotificationType());
model.setUserId(event.getUserId());
model.setContactHint(event.getContactHint());
model.setContactTypeHint(event.getContactTypeHint());
model.setNotifiedWith(event.getContactTypeHint());
model.setData(event.getData());
model.setNotifyState(NotificationNotifyState.PENDING);
model.setType(event.getNotificationType());
model.setNotifiedWith(event.getContactTypeHint());
model.setRetryCount(0);
model.setTrackingState(NotificationTrackingState.UNDEFINED);
model.setTrackingProcess(NotificationTrackingProcess.PENDING);
model.setTrackingData(null);
model.setProvenanceRef(event.getProvenanceRef());
model.setNotifiedAt(Instant.now());
EntityManager entityManager = null;
EntityTransaction transaction = null;

View File

@ -2,6 +2,8 @@ package gr.cite.notification.model.persist;
import gr.cite.notification.common.enums.NotificationContactType;
import gr.cite.notification.common.enums.NotificationNotifyState;
import gr.cite.notification.common.enums.NotificationTrackingProcess;
import gr.cite.notification.common.enums.NotificationTrackingState;
import gr.cite.notification.common.validation.ValidId;
import jakarta.validation.constraints.NotNull;
@ -45,6 +47,14 @@ public class NotificationPersist {
private Integer retryCount;
private NotificationTrackingState trackingState;
private NotificationTrackingProcess trackingProcess;
private String trackingData;
private String provenanceRef;
private String hash;
public UUID getId() {
@ -71,11 +81,11 @@ public class NotificationPersist {
this.type = type;
}
public gr.cite.notification.common.enums.NotificationContactType getContactTypeHint() {
public NotificationContactType getContactTypeHint() {
return contactTypeHint;
}
public void setContactTypeHint(gr.cite.notification.common.enums.NotificationContactType contactTypeHint) {
public void setContactTypeHint(NotificationContactType contactTypeHint) {
this.contactTypeHint = contactTypeHint;
}
@ -103,11 +113,11 @@ public class NotificationPersist {
this.notifyState = notifyState;
}
public gr.cite.notification.common.enums.NotificationContactType getNotifiedWith() {
public NotificationContactType getNotifiedWith() {
return notifiedWith;
}
public void setNotifiedWith(gr.cite.notification.common.enums.NotificationContactType notifiedWith) {
public void setNotifiedWith(NotificationContactType notifiedWith) {
this.notifiedWith = notifiedWith;
}
@ -127,6 +137,38 @@ public class NotificationPersist {
this.retryCount = retryCount;
}
public NotificationTrackingState getTrackingState() {
return trackingState;
}
public void setTrackingState(NotificationTrackingState trackingState) {
this.trackingState = trackingState;
}
public NotificationTrackingProcess getTrackingProcess() {
return trackingProcess;
}
public void setTrackingProcess(NotificationTrackingProcess trackingProcess) {
this.trackingProcess = trackingProcess;
}
public String getTrackingData() {
return trackingData;
}
public void setTrackingData(String trackingData) {
this.trackingData = trackingData;
}
public String getProvenanceRef() {
return provenanceRef;
}
public void setProvenanceRef(String provenanceRef) {
this.provenanceRef = provenanceRef;
}
public String getHash() {
return hash;
}
@ -134,5 +176,4 @@ public class NotificationPersist {
public void setHash(String hash) {
this.hash = hash;
}
}

View File

@ -2,25 +2,36 @@ package gr.cite.notification.service.tenant;
import com.fasterxml.jackson.core.JsonProcessingException;
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.IsActive;
import gr.cite.notification.convention.ConventionService;
import gr.cite.notification.data.TenantEntity;
import gr.cite.notification.model.Tenant;
import gr.cite.notification.model.builder.TenantBuilder;
import gr.cite.notification.model.deleter.TenantDeleter;
import gr.cite.notification.model.deleter.UserDeleter;
import gr.cite.notification.model.persist.TenantTouchedIntegrationEventPersist;
import gr.cite.notification.service.user.UserServiceImpl;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.deleter.DeleterFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.exception.MyForbiddenException;
import gr.cite.tools.exception.MyNotFoundException;
import gr.cite.tools.exception.MyValidationException;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import jakarta.persistence.EntityManager;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.management.InvalidApplicationException;
import java.time.Instant;
import java.util.EnumSet;
import java.util.List;
import java.util.UUID;
@Service
public class TenantServiceImpl implements TenantService {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantServiceImpl.class));
@ -29,14 +40,57 @@ public class TenantServiceImpl implements TenantService {
private final DeleterFactory deleterFactory;
public TenantServiceImpl(AuthorizationService authorizationService, DeleterFactory deleterFactory) {
private final ConventionService conventionService;
private final EntityManager entityManager;
private final BuilderFactory builderFactory;
public TenantServiceImpl(AuthorizationService authorizationService,
DeleterFactory deleterFactory,
ConventionService conventionService,
EntityManager entityManager,
BuilderFactory builderFactory) {
this.authorizationService = authorizationService;
this.deleterFactory = deleterFactory;
this.conventionService = conventionService;
this.entityManager = entityManager;
this.builderFactory = builderFactory;
}
@Override
public Tenant persist(TenantTouchedIntegrationEventPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JsonProcessingException {
return null;
logger.debug(new MapLogEntry("persisting tenant").And("model", model).And("fields", fields));
this.authorizationService.authorizeForce(Permission.EditTenant);
Boolean isValid = this.conventionService.isValidGuid(model.getId());
TenantEntity data;
if (isValid) {
data = this.entityManager.find(TenantEntity.class, model.getId());
if (data == null) {
data = new TenantEntity();
data.setId(model.getId());
data.setCode(model.getCode());
data.setIsActive(IsActive.Active);
data.setCreatedAt(Instant.now());
this.entityManager.persist(data);
} else {
data.setCode(model.getCode());
data.setUpdatedAt(Instant.now());
data.setIsActive(IsActive.Active);
this.entityManager.merge(data);
}
} else {
throw new MyApplicationException("Not valid tenant id");
}
this.entityManager.flush();
return this.builderFactory.builder(TenantBuilder.class).authorize(EnumSet.of(AuthorizationFlags.None)).build(BaseFieldSet.build(fields, Tenant._id), data);
}
@Override

View File

@ -2,21 +2,36 @@ package gr.cite.notification.service.user;
import com.fasterxml.jackson.core.JsonProcessingException;
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.notification.authorization.AuthorizationFlags;
import gr.cite.notification.authorization.OwnedResource;
import gr.cite.notification.authorization.Permission;
import gr.cite.notification.common.JsonHandlingService;
import gr.cite.notification.common.enums.IsActive;
import gr.cite.notification.convention.ConventionService;
import gr.cite.notification.data.UserEntity;
import gr.cite.notification.model.User;
import gr.cite.notification.model.builder.UserBuilder;
import gr.cite.notification.model.deleter.UserDeleter;
import gr.cite.notification.model.persist.UserTouchedIntegrationEventPersist;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.deleter.DeleterFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.exception.MyForbiddenException;
import gr.cite.tools.exception.MyNotFoundException;
import gr.cite.tools.exception.MyValidationException;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import jakarta.persistence.EntityManager;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import javax.management.InvalidApplicationException;
import java.time.Instant;
import java.util.EnumSet;
import java.util.List;
import java.util.UUID;
@ -29,14 +44,58 @@ public class UserServiceImpl implements UserService {
private final DeleterFactory deleterFactory;
public UserServiceImpl(AuthorizationService authorizationService, DeleterFactory deleterFactory) {
private final ConventionService conventionService;
private final EntityManager entityManager;
private final BuilderFactory builderFactory;
public UserServiceImpl(AuthorizationService authorizationService,
DeleterFactory deleterFactory,
ConventionService conventionService,
EntityManager entityManager,
BuilderFactory builderFactory) {
this.authorizationService = authorizationService;
this.deleterFactory = deleterFactory;
this.conventionService = conventionService;
this.entityManager = entityManager;
this.builderFactory = builderFactory;
}
@Override
public User persist(UserTouchedIntegrationEventPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JsonProcessingException {
return null;
logger.debug(new MapLogEntry("persisting user").And("model", model).And("fields", fields));
this.authorizationService.authorizeAtLeastOneForce(model.getId() != null ? List.of(new OwnedResource(model.getId())) : null, Permission.EditUser);
Boolean isValid = this.conventionService.isValidGuid(model.getId());
UserEntity data;
if (isValid) {
data = this.entityManager.find(UserEntity.class, model.getId());
if (data == null) {
data = new UserEntity();
data.setId(model.getId());
data.setName(model.getFirstName() + " " + model.getLastName());
data.setCreatedAt(Instant.now());
data.setUpdatedAt(Instant.now());
data.setIsActive(IsActive.Active);
this.entityManager.persist(data);
} else {
data.setName(model.getFirstName() + " " + model.getLastName());
data.setUpdatedAt(Instant.now());
data.setIsActive(IsActive.Active);
this.entityManager.merge(data);
}
} else {
throw new MyApplicationException("Not valid user id");
}
this.entityManager.flush();
return this.builderFactory.builder(UserBuilder.class).authorize(EnumSet.of(AuthorizationFlags.None)).build(BaseFieldSet.build(fields, User._id), data);
}
@Override