Notifications.js

import PagingQuery from './models/PagingQuery.js';
import PagingResult from './models/PagingResult.js';
import Notification from './models/notifications/Notification.js';
import NotificationContent from './models/notifications/NotificationContent.js';
import NotificationsQuery from './models/notifications/NotificationsQuery.js';
import SendNotificationTarget from './models/notifications/SendNotificationTarget.js';
import { uploadAttachment } from './utils.js';
import SDK from './sdk';

import './thrift/iris_types';
import './thrift/common_types';
import './thrift/actions_types';

/**
 * @class Notifications
 * @hideconstructor
 */
export default class Notifications {
    /**
     * Returns notifications based on the provided query.
     *
     * @memberof Notifications
     * @param {PagingQuery<NotificationsQuery>} pagingQuery - Query to filter notifications.
     * @return {Promise<PagingResult<Notification[]>>} Promise with list of notifications and cursor for next query.
     */
    static get({ query, ...pagination }) {
        const request = new GetNotificationsRequest({
            types: query.types,
            statuses: query.statuses,
            actions: query.actions,
            pagination: new Pagination(pagination)
        });

        return SDK.request('getNotifications', request)
            .then((res) => {
                return new PagingResult(
                    res.notifications.map(Notification.create),
                    res.nextCursor
                );
            });
    }

    /**
     * Returns number of notifications based on the provided query.
     *
     * @memberof Notifications
     * @param {PagingQuery<NotificationsQuery>} pagingQuery - Query to filter notifications.
     * @return {Promise<number>} - Promise with number of notifications based on the query.
     */
    static getCount({ query, ...pagination }) {
        const request = new GetNotificationsRequest({
            types: query.types,
            statuses: query.statuses,
            actions: query.actions,
            pagination: new Pagination(pagination)
        });

        return SDK.request('getNotifications', request)
            .then((res) => {
                return res.totalNumber;
            });
    }

    /**
     * Sends the provided notification content to the recipients.
     *
     * @memberof Notifications
     * @param {NotificationContent} content - Content of push notification.
     * @param {SendNotificationTarget} target - List of recipients.
     * @return {Promise<void>} - Promise.
     */
    static send(content, target) {
        const media = content.mediaAttachment || {};
        const customization = content.customization || {};
        const userIds = target.userIdList && target.userIdList.ids
            ? target.userIdList.ids.concat(target.placeholders)
            : target.placeholders;

        return uploadAttachment(media, 'NOTIFICATION_MEDIA')
            .then(()  => {
                const request = new SendNotificationRequest({
                    userIds,
                    ...content,
                    templateData: content.templatePlaceholders,
                    media: {
                        image: media.imageUrl,
                        video: media.videoUrl,
                        ...customization
                    }
                });

                return SDK.request('sendNotification', request);
            });
    }

    /**
     * Updates notification(s) status.
     *
     * @memberof Notifications
     * @param {string} status - Status defined in {@link NotificationStatus}.
     * @param {string[]} notificationIds - IDs of Notifications to be updated.
     * @return {Promise<boolean>} Promise with true if successful.
     */
    static setStatus(status, notificationIds) {
        const request = new THNotificationsSetStatusParams({
            ids: notificationIds,
            status
        });

        return SDK.request('setNotificationsStatus', request);
    }
}