@sitecore/sc-contenthub-webclient-sdk
Version:
Sitecore Content Hub WebClient SDK.
122 lines • 5.98 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { TaJson } from "ta-json";
import { NOTIFICATIONS } from "../constants";
import Guard from "../guard";
import { NotificationMapper } from "../mappers/notification-mapper";
import { DistributeNotificationResource } from "../models/notifications/distribute-notification-resource";
import { TypeGuards } from "../type-guards";
import { ResponseHandler } from "./response-handler";
export class NotificationsClient {
constructor(client) {
Guard.notNullOrUndefined(client);
this._client = client;
}
//! Not necessary for now, can be added later (lots of boilerplate)
// // public async getMailTemplateAsync(name: string, loadConfiguration: Nullable<IEntityLoadConfiguration> = null)
sendConfirmationEmailAsync(users) {
return __awaiter(this, void 0, void 0, function* () {
let userIds = [];
if (TypeGuards.isStringArray(users)) {
userIds = (yield this._client.users.getUserIdsAsync(users));
}
else {
userIds = users;
}
Guard.validIds(userIds);
for (const userId of userIds) {
const link = yield this._client.linkHelper.sendConfirmationMailToLinkAsync(userId);
const response = yield this._client.raw.postAsync(link.href, TaJson.serialize(userId));
ResponseHandler.handleErrors(response);
}
});
}
sendEmailNotificationAsync(mailRequest) {
return __awaiter(this, void 0, void 0, function* () {
Guard.notNullOrUndefined(mailRequest);
mailRequest.validate();
if ("recipients" in mailRequest) {
if (TypeGuards.isStringArray(mailRequest.recipients)) {
// Usernames
const mailUsernames = mailRequest;
const ids = yield this._client.users.getUserIdsAsync(mailUsernames.recipients);
if (ids !== null) {
yield this.sendPrivateEmailNotificationAsync(mailUsernames, ids);
}
}
else {
// Ids
const mailIds = mailRequest;
yield this.sendPrivateEmailNotificationAsync(mailIds, mailIds.recipients);
}
}
else {
// Broadcast
yield this.sendPrivateEmailNotificationAsync(mailRequest, null);
}
});
}
sendRealTimeNotificationAsync(realtimeRequest) {
return __awaiter(this, void 0, void 0, function* () {
Guard.notNull(realtimeRequest);
realtimeRequest.validate();
if ("recipients" in realtimeRequest) {
if (TypeGuards.isStringArray(realtimeRequest.recipients)) {
// Usernames
const realtimeUsers = realtimeRequest;
const ids = yield this._client.users.getUserIdsAsync(realtimeUsers.recipients);
if (ids !== null) {
yield this.sendPrivateRealTimeNotificationAsync(realtimeRequest, ids);
}
}
else {
// Ids
const realtimeIds = realtimeRequest;
yield this.sendPrivateRealTimeNotificationAsync(realtimeIds, realtimeIds.recipients);
}
}
else {
// Broadcast
yield this.sendPrivateRealTimeNotificationAsync(realtimeRequest, null);
}
});
}
//#region Private functions
sendPrivateEmailNotificationAsync(mailRequest, recipients) {
return __awaiter(this, void 0, void 0, function* () {
return this.sendNotificationAsync(mailRequest.mailTemplateName, recipients, "EmailNotification", mailRequest.variables);
});
}
sendPrivateRealTimeNotificationAsync(realtimeRequest, recipients) {
return __awaiter(this, void 0, void 0, function* () {
const parameters = NotificationMapper.mapParameters(realtimeRequest);
return this.sendNotificationAsync(NOTIFICATIONS.REALTIME.raw, recipients, "RealTimeNotification", parameters);
});
}
sendNotificationAsync(notificationName, recipients, channel, parameters) {
return __awaiter(this, void 0, void 0, function* () {
const link = yield this._client.linkHelper.sendNotificationToLinkAsync(notificationName);
// Get user links.
let userLinks = null;
const linkPromises = recipients === null || recipients === void 0 ? void 0 : recipients.map(id => this._client.linkHelper.entityToLinkAsync(id));
if (linkPromises != null) {
userLinks = yield Promise.all(linkPromises);
}
// Create the resource.
const resource = new DistributeNotificationResource();
resource.recipients = userLinks;
resource.channels = [channel];
resource.parameters = parameters;
const response = yield this._client.raw.postAsync(link.href, TaJson.serialize(resource));
ResponseHandler.handleErrors(response);
});
}
}
//# sourceMappingURL=notifications-client.js.map