Merge remote-tracking branch 'klimov/feature/notification-system' into feature/notification-system
This commit is contained in:
commit
7e4d92e8eb
@ -65,7 +65,8 @@ CREATE TABLE IF NOT EXISTS notification_request (
|
|||||||
created_time BIGINT NOT NULL,
|
created_time BIGINT NOT NULL,
|
||||||
tenant_id UUID NULL CONSTRAINT fk_notification_request_tenant_id REFERENCES tenant(id) ON DELETE CASCADE,
|
tenant_id UUID NULL CONSTRAINT fk_notification_request_tenant_id REFERENCES tenant(id) ON DELETE CASCADE,
|
||||||
targets VARCHAR(10000) NOT NULL,
|
targets VARCHAR(10000) NOT NULL,
|
||||||
template_id UUID NOT NULL,
|
template_id UUID,
|
||||||
|
template VARCHAR(10000),
|
||||||
info VARCHAR(1000),
|
info VARCHAR(1000),
|
||||||
additional_config VARCHAR(1000),
|
additional_config VARCHAR(1000),
|
||||||
originator_entity_id UUID,
|
originator_entity_id UUID,
|
||||||
|
|||||||
@ -189,17 +189,25 @@ public class NotificationController extends BaseController {
|
|||||||
|
|
||||||
@PostMapping("/notification/request/preview")
|
@PostMapping("/notification/request/preview")
|
||||||
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
|
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
|
||||||
public NotificationRequestPreview getNotificationRequestPreview(@RequestBody @Valid NotificationRequest notificationRequest,
|
public NotificationRequestPreview getNotificationRequestPreview(@RequestBody @Valid NotificationRequest request,
|
||||||
@AuthenticationPrincipal SecurityUser user) {
|
@AuthenticationPrincipal SecurityUser user) {
|
||||||
NotificationRequestPreview preview = new NotificationRequestPreview();
|
NotificationRequestPreview preview = new NotificationRequestPreview();
|
||||||
|
|
||||||
notificationRequest.setOriginatorEntityId(user.getId());
|
request.setOriginatorEntityId(user.getId());
|
||||||
NotificationTemplate notificationTemplate = notificationTemplateService.findNotificationTemplateById(user.getTenantId(), notificationRequest.getTemplateId());
|
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()
|
NotificationProcessingContext mockProcessingCtx = NotificationProcessingContext.builder()
|
||||||
.tenantId(user.getTenantId())
|
.tenantId(user.getTenantId())
|
||||||
.request(notificationRequest)
|
.request(request)
|
||||||
.settings(null)
|
.settings(null)
|
||||||
.template(notificationTemplate)
|
.template(template)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Map<NotificationDeliveryMethod, DeliveryMethodNotificationTemplate> processedTemplates = mockProcessingCtx.getDeliveryMethods().stream()
|
Map<NotificationDeliveryMethod, DeliveryMethodNotificationTemplate> processedTemplates = mockProcessingCtx.getDeliveryMethods().stream()
|
||||||
@ -215,7 +223,7 @@ public class NotificationController extends BaseController {
|
|||||||
preview.setProcessedTemplates(processedTemplates);
|
preview.setProcessedTemplates(processedTemplates);
|
||||||
|
|
||||||
Map<String, Integer> recipientsCountByTarget = new HashMap<>();
|
Map<String, Integer> recipientsCountByTarget = new HashMap<>();
|
||||||
notificationRequest.getTargets().forEach(targetId -> {
|
request.getTargets().forEach(targetId -> {
|
||||||
NotificationTarget notificationTarget = notificationTargetService.findNotificationTargetById(user.getTenantId(), new NotificationTargetId(targetId));
|
NotificationTarget notificationTarget = notificationTargetService.findNotificationTargetById(user.getTenantId(), new NotificationTargetId(targetId));
|
||||||
if (notificationTarget == null) {
|
if (notificationTarget == null) {
|
||||||
throw new IllegalArgumentException("Notification target with id " + targetId + " not found");
|
throw new IllegalArgumentException("Notification target with id " + targetId + " not found");
|
||||||
|
|||||||
@ -99,7 +99,14 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple
|
|||||||
@Override
|
@Override
|
||||||
public NotificationRequest processNotificationRequest(TenantId tenantId, NotificationRequest notificationRequest) {
|
public NotificationRequest processNotificationRequest(TenantId tenantId, NotificationRequest notificationRequest) {
|
||||||
NotificationSettings settings = notificationSettingsService.findNotificationSettings(tenantId);
|
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()
|
List<NotificationTarget> targets = notificationRequest.getTargets().stream()
|
||||||
.map(NotificationTargetId::new)
|
.map(NotificationTargetId::new)
|
||||||
.map(targetId -> notificationTargetService.findNotificationTargetById(tenantId, targetId))
|
.map(targetId -> notificationTargetService.findNotificationTargetById(tenantId, targetId))
|
||||||
|
|||||||
@ -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.TenantId;
|
||||||
import org.thingsboard.server.common.data.id.UserId;
|
import org.thingsboard.server.common.data.id.UserId;
|
||||||
import org.thingsboard.server.common.data.notification.info.NotificationInfo;
|
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.Valid;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
@ -48,9 +49,10 @@ public class NotificationRequest extends BaseData<NotificationRequestId> impleme
|
|||||||
@NotNull
|
@NotNull
|
||||||
private List<UUID> targets;
|
private List<UUID> targets;
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private NotificationTemplateId templateId;
|
private NotificationTemplateId templateId;
|
||||||
@Valid
|
@Valid
|
||||||
|
private NotificationTemplate template;
|
||||||
|
@Valid
|
||||||
private NotificationInfo info;
|
private NotificationInfo info;
|
||||||
@NotNull
|
@NotNull
|
||||||
@Valid
|
@Valid
|
||||||
@ -68,6 +70,7 @@ public class NotificationRequest extends BaseData<NotificationRequestId> impleme
|
|||||||
this.tenantId = other.tenantId;
|
this.tenantId = other.tenantId;
|
||||||
this.targets = other.targets;
|
this.targets = other.targets;
|
||||||
this.templateId = other.templateId;
|
this.templateId = other.templateId;
|
||||||
|
this.template = other.template;
|
||||||
this.info = other.info;
|
this.info = other.info;
|
||||||
this.additionalConfig = other.additionalConfig;
|
this.additionalConfig = other.additionalConfig;
|
||||||
this.originatorEntityId = other.originatorEntityId;
|
this.originatorEntityId = other.originatorEntityId;
|
||||||
|
|||||||
@ -34,7 +34,6 @@ public class NotificationTemplate extends BaseData<NotificationTemplateId> imple
|
|||||||
|
|
||||||
private TenantId tenantId;
|
private TenantId tenantId;
|
||||||
@NoXss
|
@NoXss
|
||||||
@NotNull
|
|
||||||
private String name;
|
private String name;
|
||||||
@NoXss
|
@NoXss
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@ -682,6 +682,7 @@ public class ModelConstants {
|
|||||||
public static final String NOTIFICATION_REQUEST_TABLE_NAME = "notification_request";
|
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_TARGETS_PROPERTY = "targets";
|
||||||
public static final String NOTIFICATION_REQUEST_TEMPLATE_ID_PROPERTY = "template_id";
|
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_INFO_PROPERTY = "info";
|
||||||
public static final String NOTIFICATION_REQUEST_ORIGINATOR_ENTITY_ID_PROPERTY = "originator_entity_id";
|
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";
|
public static final String NOTIFICATION_REQUEST_ORIGINATOR_ENTITY_TYPE_PROPERTY = "originator_entity_type";
|
||||||
|
|||||||
@ -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.NotificationRequestConfig;
|
||||||
import org.thingsboard.server.common.data.notification.NotificationRequestStats;
|
import org.thingsboard.server.common.data.notification.NotificationRequestStats;
|
||||||
import org.thingsboard.server.common.data.notification.NotificationRequestStatus;
|
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.BaseSqlEntity;
|
||||||
import org.thingsboard.server.dao.model.ModelConstants;
|
import org.thingsboard.server.dao.model.ModelConstants;
|
||||||
import org.thingsboard.server.dao.util.mapping.JsonStringType;
|
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)
|
@Column(name = ModelConstants.NOTIFICATION_REQUEST_TARGETS_PROPERTY, nullable = false)
|
||||||
private String targets;
|
private String targets;
|
||||||
|
|
||||||
@Column(name = ModelConstants.NOTIFICATION_REQUEST_TEMPLATE_ID_PROPERTY, nullable = false)
|
@Column(name = ModelConstants.NOTIFICATION_REQUEST_TEMPLATE_ID_PROPERTY)
|
||||||
private UUID templateId;
|
private UUID templateId;
|
||||||
|
|
||||||
|
@Type(type = "json")
|
||||||
|
@Column(name = ModelConstants.NOTIFICATION_REQUEST_TEMPLATE_PROPERTY)
|
||||||
|
private JsonNode template;
|
||||||
|
|
||||||
@Type(type = "json")
|
@Type(type = "json")
|
||||||
@Column(name = ModelConstants.NOTIFICATION_REQUEST_INFO_PROPERTY)
|
@Column(name = ModelConstants.NOTIFICATION_REQUEST_INFO_PROPERTY)
|
||||||
private JsonNode info;
|
private JsonNode info;
|
||||||
@ -92,6 +97,7 @@ public class NotificationRequestEntity extends BaseSqlEntity<NotificationRequest
|
|||||||
setTenantId(getTenantUuid(notificationRequest.getTenantId()));
|
setTenantId(getTenantUuid(notificationRequest.getTenantId()));
|
||||||
setTargets(listToString(notificationRequest.getTargets()));
|
setTargets(listToString(notificationRequest.getTargets()));
|
||||||
setTemplateId(getUuid(notificationRequest.getTemplateId()));
|
setTemplateId(getUuid(notificationRequest.getTemplateId()));
|
||||||
|
setTemplate(toJson(notificationRequest.getTemplate()));
|
||||||
setInfo(toJson(notificationRequest.getInfo()));
|
setInfo(toJson(notificationRequest.getInfo()));
|
||||||
setAdditionalConfig(toJson(notificationRequest.getAdditionalConfig()));
|
setAdditionalConfig(toJson(notificationRequest.getAdditionalConfig()));
|
||||||
if (notificationRequest.getOriginatorEntityId() != null) {
|
if (notificationRequest.getOriginatorEntityId() != null) {
|
||||||
@ -109,6 +115,7 @@ public class NotificationRequestEntity extends BaseSqlEntity<NotificationRequest
|
|||||||
this.tenantId = other.tenantId;
|
this.tenantId = other.tenantId;
|
||||||
this.targets = other.targets;
|
this.targets = other.targets;
|
||||||
this.templateId = other.templateId;
|
this.templateId = other.templateId;
|
||||||
|
this.template = other.template;
|
||||||
this.info = other.info;
|
this.info = other.info;
|
||||||
this.additionalConfig = other.additionalConfig;
|
this.additionalConfig = other.additionalConfig;
|
||||||
this.originatorEntityId = other.originatorEntityId;
|
this.originatorEntityId = other.originatorEntityId;
|
||||||
@ -126,6 +133,7 @@ public class NotificationRequestEntity extends BaseSqlEntity<NotificationRequest
|
|||||||
notificationRequest.setTenantId(getTenantId(tenantId));
|
notificationRequest.setTenantId(getTenantId(tenantId));
|
||||||
notificationRequest.setTargets(listFromString(targets, UUID::fromString));
|
notificationRequest.setTargets(listFromString(targets, UUID::fromString));
|
||||||
notificationRequest.setTemplateId(getEntityId(templateId, NotificationTemplateId::new));
|
notificationRequest.setTemplateId(getEntityId(templateId, NotificationTemplateId::new));
|
||||||
|
notificationRequest.setTemplate(fromJson(template, NotificationTemplate.class));
|
||||||
notificationRequest.setInfo(fromJson(info, NotificationInfo.class));
|
notificationRequest.setInfo(fromJson(info, NotificationInfo.class));
|
||||||
notificationRequest.setAdditionalConfig(fromJson(additionalConfig, NotificationRequestConfig.class));
|
notificationRequest.setAdditionalConfig(fromJson(additionalConfig, NotificationRequestConfig.class));
|
||||||
if (originatorEntityId != null) {
|
if (originatorEntityId != null) {
|
||||||
|
|||||||
@ -48,9 +48,14 @@ public class NotificationRequestInfoEntity extends NotificationRequestEntity {
|
|||||||
public NotificationRequestInfo toData() {
|
public NotificationRequestInfo toData() {
|
||||||
NotificationRequest request = super.toData();
|
NotificationRequest request = super.toData();
|
||||||
List<NotificationDeliveryMethod> deliveryMethods;
|
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) {
|
if (templateConfig != null) {
|
||||||
deliveryMethods = fromJson(templateConfig, NotificationTemplateConfig.class)
|
deliveryMethods = templateConfig.getDeliveryMethodsTemplates().entrySet().stream()
|
||||||
.getDeliveryMethodsTemplates().entrySet().stream()
|
|
||||||
.filter(entry -> entry.getValue().isEnabled())
|
.filter(entry -> entry.getValue().isEnabled())
|
||||||
.map(Map.Entry::getKey).collect(Collectors.toList());
|
.map(Map.Entry::getKey).collect(Collectors.toList());
|
||||||
} else if (request.getStats() != null) {
|
} else if (request.getStats() != null) {
|
||||||
|
|||||||
@ -829,7 +829,8 @@ CREATE TABLE IF NOT EXISTS notification_request (
|
|||||||
created_time BIGINT NOT NULL,
|
created_time BIGINT NOT NULL,
|
||||||
tenant_id UUID NULL CONSTRAINT fk_notification_request_tenant_id REFERENCES tenant(id) ON DELETE CASCADE,
|
tenant_id UUID NULL CONSTRAINT fk_notification_request_tenant_id REFERENCES tenant(id) ON DELETE CASCADE,
|
||||||
targets VARCHAR(10000) NOT NULL,
|
targets VARCHAR(10000) NOT NULL,
|
||||||
template_id UUID NOT NULL,
|
template_id UUID,
|
||||||
|
template VARCHAR(10000),
|
||||||
info VARCHAR(1000),
|
info VARCHAR(1000),
|
||||||
additional_config VARCHAR(1000),
|
additional_config VARCHAR(1000),
|
||||||
originator_entity_id UUID,
|
originator_entity_id UUID,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user