@magicbell/core
Version:
Official MagicBell API wrapper
153 lines • 4.82 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const dompurify_1 = tslib_1.__importDefault(require("dompurify"));
const isNil_js_1 = tslib_1.__importDefault(require("lodash/isNil.js"));
const date_js_1 = require("../../lib/date.js");
const camelize_js_1 = tslib_1.__importDefault(require("../../lib/decorators/camelize.js"));
const unwrap_js_1 = tslib_1.__importDefault(require("../../lib/decorators/unwrap.js"));
const wrap_js_1 = tslib_1.__importDefault(require("../../lib/decorators/wrap.js"));
const NotificationFactory_js_1 = tslib_1.__importDefault(require("./NotificationFactory.js"));
const NotificationRepository_js_1 = tslib_1.__importDefault(require("./NotificationRepository.js"));
/**
* A notification.
*
* @example
* const notification = new Notification({ id })
* notification.fetch()
*/
class Notification {
id = null;
title;
content;
category;
actionUrl;
customAttributes;
readAt;
seenAt;
sentAt;
deletedAt = null;
repo;
constructor(attrs = {}) {
this.set(attrs);
this.repo = new NotificationRepository_js_1.default();
}
static async create(notificationContent) {
const repo = new NotificationRepository_js_1.default();
try {
const data = await repo.create(notificationContent);
return NotificationFactory_js_1.default.create(data);
}
catch (error) {
if (error.response?.status === 422)
throw error.response.data;
else
throw error;
}
}
get seenAtDate() {
return (0, date_js_1.secondsToDate)(this.seenAt);
}
get sentAtDate() {
return (0, date_js_1.secondsToDate)(this.sentAt);
}
get readAtDate() {
return (0, date_js_1.secondsToDate)(this.readAt);
}
get isRead() {
return !!this.readAt;
}
set isRead(isRead) {
this.readAt = isRead ? (0, date_js_1.toUnix)() : null;
}
get isSeen() {
return !!this.seenAt;
}
set isSeen(isSeen) {
this.seenAt = isSeen ? (0, date_js_1.toUnix)() : null;
}
get sanitizedContent() {
if (!(0, isNil_js_1.default)(this.content))
return dompurify_1.default.sanitize(this.content);
return this.content;
}
/**
* Fetch the notification from the API server.
*/
async fetch() {
if (this.id) {
const json = await this.repo.get(this.id);
this.set(json);
return json;
}
else {
throw Error('The notification does not exist yet, save it first');
}
}
/**
* Delete a notification from the API server.
*/
async delete() {
if (this.id) {
this.deletedAt = Date.now();
return this.repo.delete(this.id);
}
else {
throw Error('The notification does not exist yet, save it first');
}
}
/**
* Mark a notification as read. Sets the `readAt` attribute to the current unix
* timestamp. It also marks the notification as seen.
*/
markAsRead() {
if (this.id) {
this.isRead = true;
this.isSeen = true;
return this.repo.markAsRead(this.id);
}
else {
const error = Error('The notification does not exist yet, save it first');
return Promise.reject(error);
}
}
/**
* Mark a notification as unread. Sets the `readAt` attribute to null.
*/
markAsUnread() {
if (this.id) {
this.isRead = false;
return this.repo.markAsUnread(this.id);
}
else {
const error = Error('The notification does not exist yet, save it first');
return Promise.reject(error);
}
}
set(json = {}) {
const jsonWithCustomAttributes = this.transformCustomAttributes(json);
Object.assign(this, jsonWithCustomAttributes);
}
transformCustomAttributes(json) {
const { customAttributes, ...otherAttrs } = json;
if (typeof customAttributes === 'string') {
try {
const parsedCustomAttributes = JSON.parse(customAttributes);
return { customAttributes: parsedCustomAttributes, ...otherAttrs };
}
catch (e) {
// intentionally left blank
}
}
return json;
}
}
exports.default = Notification;
tslib_1.__decorate([
(0, unwrap_js_1.default)('notification'),
(0, camelize_js_1.default)()
], Notification.prototype, "set", null);
tslib_1.__decorate([
(0, wrap_js_1.default)('notification')
], Notification, "create", null);
//# sourceMappingURL=Notification.js.map