Merge remote-tracking branch 'klimov/feature/notification-system' into feature/notification-system

This commit is contained in:
Vladyslav_Prykhodko 2023-02-20 10:13:43 +02:00
commit 7e4d92e8eb
9 changed files with 47 additions and 14 deletions

View File

@ -65,7 +65,8 @@ CREATE TABLE IF NOT EXISTS notification_request (
created_time BIGINT NOT NULL,
tenant_id UUID NULL CONSTRAINT fk_notification_request_tenant_id REFERENCES tenant(id) ON DELETE CASCADE,
targets VARCHAR(10000) NOT NULL,
template_id UUID NOT NULL,
template_id UUID,
template VARCHAR(10000),
info VARCHAR(1000),
additional_config VARCHAR(1000),
originator_entity_id UUID,

View File

@ -189,17 +189,25 @@ public class NotificationController extends BaseController {
@PostMapping("/notification/request/preview")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
public NotificationRequestPreview getNotificationRequestPreview(@RequestBody @Valid NotificationRequest notificationRequest,
public NotificationRequestPreview getNotificationRequestPreview(@RequestBody @Valid NotificationRequest request,
@AuthenticationPrincipal SecurityUser user) {
NotificationRequestPreview preview = new NotificationRequestPreview();
notificationRequest.setOriginatorEntityId(user.getId());
NotificationTemplate notificationTemplate = notificationTemplateService.findNotificationTemplateById(user.getTenantId(), notificationRequest.getTemplateId());
request.setOriginatorEntityId(user.getId());
NotificationTemplate template;
if (request.getTemplateId() != null) {
template = notificationTemplateService.findNotificationTemplateById(user.getTenantId(), request.getTemplateId());
} else {
template = request.getTemplate();
}
if (template == null) {
throw new IllegalArgumentException("Template is missing");
}
NotificationProcessingContext mockProcessingCtx = NotificationProcessingContext.builder()
.tenantId(user.getTenantId())
.request(notificationRequest)
.request(request)
.settings(null)
.template(notificationTemplate)
.template(template)
.build();
Map<NotificationDeliveryMethod, DeliveryMethodNotificationTemplate> processedTemplates = mockProcessingCtx.getDeliveryMethods().stream()
@ -215,7 +223,7 @@ public class NotificationController extends BaseController {
preview.setProcessedTemplates(processedTemplates);
Map<String, Integer> recipientsCountByTarget = new HashMap<>();
notificationRequest.getTargets().forEach(targetId -> {
request.getTargets().forEach(targetId -> {
NotificationTarget notificationTarget = notificationTargetService.findNotificationTargetById(user.getTenantId(), new NotificationTargetId(targetId));
if (notificationTarget == null) {
throw new IllegalArgumentException("Notification target with id " + targetId + " not found");

View File

@ -99,7 +99,14 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple
@Override
public NotificationRequest processNotificationRequest(TenantId tenantId, NotificationRequest notificationRequest) {
NotificationSettings settings = notificationSettingsService.findNotificationSettings(tenantId);
NotificationTemplate notificationTemplate = notificationTemplateService.findNotificationTemplateById(tenantId, notificationRequest.getTemplateId());
NotificationTemplate notificationTemplate;
if (notificationRequest.getTemplateId() != null) {
notificationTemplate = notificationTemplateService.findNotificationTemplateById(tenantId, notificationRequest.getTemplateId());
} else {
notificationTemplate = notificationRequest.getTemplate();
}
if (notificationTemplate == null) throw new IllegalArgumentException("Template is missing");
List<NotificationTarget> targets = notificationRequest.getTargets().stream()
.map(NotificationTargetId::new)
.map(targetId -> notificationTargetService.findNotificationTargetById(tenantId, targetId))

View File

@ -31,6 +31,7 @@ import org.thingsboard.server.common.data.id.NotificationTemplateId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.notification.info.NotificationInfo;
import org.thingsboard.server.common.data.notification.template.NotificationTemplate;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
@ -48,9 +49,10 @@ public class NotificationRequest extends BaseData<NotificationRequestId> impleme
@NotNull
private List<UUID> targets;
@NotNull
private NotificationTemplateId templateId;
@Valid
private NotificationTemplate template;
@Valid
private NotificationInfo info;
@NotNull
@Valid
@ -68,6 +70,7 @@ public class NotificationRequest extends BaseData<NotificationRequestId> impleme
this.tenantId = other.tenantId;
this.targets = other.targets;
this.templateId = other.templateId;
this.template = other.template;
this.info = other.info;
this.additionalConfig = other.additionalConfig;
this.originatorEntityId = other.originatorEntityId;

View File

@ -34,7 +34,6 @@ public class NotificationTemplate extends BaseData<NotificationTemplateId> imple
private TenantId tenantId;
@NoXss
@NotNull
private String name;
@NoXss
@NotNull

View File

@ -682,6 +682,7 @@ public class ModelConstants {
public static final String NOTIFICATION_REQUEST_TABLE_NAME = "notification_request";
public static final String NOTIFICATION_REQUEST_TARGETS_PROPERTY = "targets";
public static final String NOTIFICATION_REQUEST_TEMPLATE_ID_PROPERTY = "template_id";
public static final String NOTIFICATION_REQUEST_TEMPLATE_PROPERTY = "template";
public static final String NOTIFICATION_REQUEST_INFO_PROPERTY = "info";
public static final String NOTIFICATION_REQUEST_ORIGINATOR_ENTITY_ID_PROPERTY = "originator_entity_id";
public static final String NOTIFICATION_REQUEST_ORIGINATOR_ENTITY_TYPE_PROPERTY = "originator_entity_type";

View File

@ -31,6 +31,7 @@ import org.thingsboard.server.common.data.notification.NotificationRequest;
import org.thingsboard.server.common.data.notification.NotificationRequestConfig;
import org.thingsboard.server.common.data.notification.NotificationRequestStats;
import org.thingsboard.server.common.data.notification.NotificationRequestStatus;
import org.thingsboard.server.common.data.notification.template.NotificationTemplate;
import org.thingsboard.server.dao.model.BaseSqlEntity;
import org.thingsboard.server.dao.model.ModelConstants;
import org.thingsboard.server.dao.util.mapping.JsonStringType;
@ -55,9 +56,13 @@ public class NotificationRequestEntity extends BaseSqlEntity<NotificationRequest
@Column(name = ModelConstants.NOTIFICATION_REQUEST_TARGETS_PROPERTY, nullable = false)
private String targets;
@Column(name = ModelConstants.NOTIFICATION_REQUEST_TEMPLATE_ID_PROPERTY, nullable = false)
@Column(name = ModelConstants.NOTIFICATION_REQUEST_TEMPLATE_ID_PROPERTY)
private UUID templateId;
@Type(type = "json")
@Column(name = ModelConstants.NOTIFICATION_REQUEST_TEMPLATE_PROPERTY)
private JsonNode template;
@Type(type = "json")
@Column(name = ModelConstants.NOTIFICATION_REQUEST_INFO_PROPERTY)
private JsonNode info;
@ -92,6 +97,7 @@ public class NotificationRequestEntity extends BaseSqlEntity<NotificationRequest
setTenantId(getTenantUuid(notificationRequest.getTenantId()));
setTargets(listToString(notificationRequest.getTargets()));
setTemplateId(getUuid(notificationRequest.getTemplateId()));
setTemplate(toJson(notificationRequest.getTemplate()));
setInfo(toJson(notificationRequest.getInfo()));
setAdditionalConfig(toJson(notificationRequest.getAdditionalConfig()));
if (notificationRequest.getOriginatorEntityId() != null) {
@ -109,6 +115,7 @@ public class NotificationRequestEntity extends BaseSqlEntity<NotificationRequest
this.tenantId = other.tenantId;
this.targets = other.targets;
this.templateId = other.templateId;
this.template = other.template;
this.info = other.info;
this.additionalConfig = other.additionalConfig;
this.originatorEntityId = other.originatorEntityId;
@ -126,6 +133,7 @@ public class NotificationRequestEntity extends BaseSqlEntity<NotificationRequest
notificationRequest.setTenantId(getTenantId(tenantId));
notificationRequest.setTargets(listFromString(targets, UUID::fromString));
notificationRequest.setTemplateId(getEntityId(templateId, NotificationTemplateId::new));
notificationRequest.setTemplate(fromJson(template, NotificationTemplate.class));
notificationRequest.setInfo(fromJson(info, NotificationInfo.class));
notificationRequest.setAdditionalConfig(fromJson(additionalConfig, NotificationRequestConfig.class));
if (originatorEntityId != null) {

View File

@ -48,9 +48,14 @@ public class NotificationRequestInfoEntity extends NotificationRequestEntity {
public NotificationRequestInfo toData() {
NotificationRequest request = super.toData();
List<NotificationDeliveryMethod> deliveryMethods;
NotificationTemplateConfig templateConfig = fromJson(this.templateConfig, NotificationTemplateConfig.class);
String templateName = this.templateName;
if (templateConfig == null && request.getTemplate() != null) {
templateConfig = request.getTemplate().getConfiguration();
}
if (templateConfig != null) {
deliveryMethods = fromJson(templateConfig, NotificationTemplateConfig.class)
.getDeliveryMethodsTemplates().entrySet().stream()
deliveryMethods = templateConfig.getDeliveryMethodsTemplates().entrySet().stream()
.filter(entry -> entry.getValue().isEnabled())
.map(Map.Entry::getKey).collect(Collectors.toList());
} else if (request.getStats() != null) {

View File

@ -829,7 +829,8 @@ CREATE TABLE IF NOT EXISTS notification_request (
created_time BIGINT NOT NULL,
tenant_id UUID NULL CONSTRAINT fk_notification_request_tenant_id REFERENCES tenant(id) ON DELETE CASCADE,
targets VARCHAR(10000) NOT NULL,
template_id UUID NOT NULL,
template_id UUID,
template VARCHAR(10000),
info VARCHAR(1000),
additional_config VARCHAR(1000),
originator_entity_id UUID,