gaf-ng-notifications
Version:
359 lines (351 loc) • 20.8 kB
JavaScript
import { Component, Inject, Injectable, Input, NgModule, bundle } from 'ng-metadata/core';
import { element, module as module$1 } from 'angular';
var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (undefined && undefined.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (undefined && undefined.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var NotificationsService = /** @class */ (function () {
function NotificationsService($http, $location, moment, $translate, $log, $rootScope) {
this.$http = $http;
this.$location = $location;
this.moment = moment;
this.$translate = $translate;
this.$log = $log;
this.$rootScope = $rootScope;
this.notificationsApiUrl = '/framework/notifications';
this.notificationsLookup = {
twitterThrottled: {
filter: function (notification, company, user) {
return !user
|| !user.notifications
|| !user.notifications[company._id]
|| !user.notifications[company._id].twitterThrottle
|| !user.notifications[company._id].twitterThrottle.toastMessage;
},
map: function (notification, company, user) {
notification.html = "<twitter-throttled-notification notification-id=\"" + notification.id + "\" company-id=\"" + company._id + "\" user-id=\"" + user._id + "\"></twitter-throttled-notification>";
return notification;
}
}
};
}
NotificationsService.prototype.init = function (config) {
var _this = this;
if (!config.user ||
!config.user._id ||
!config.company ||
!config.company._id) {
this.$log.error('Not all notifications configuration present. Will not retrieve');
return;
}
// It's possible we're dealing with an app that isn't under the
// app.meltwater.com umbrella, in which case they can supply the
// proper URL themselves in the configuration object.
if (config.fairhairWebUrl && config.fairhairWebUrl.length) {
// This is broken
this.notificationsApiUrl = config.fairhairWebUrl.replace('/', '') + this.notificationsApiUrl;
}
this.get(this.notificationsApiUrl, config.company, config.user)
.then(function (notifications) { return _this.handleNotifications(notifications, config); });
};
NotificationsService.prototype.get = function (notificationsApiUrl, company, user) {
var _this = this;
return this.getServersideNotifications(notificationsApiUrl, company, user)
.then(function (serverSideNotifications) { return serverSideNotifications !== undefined ? _this.getClientsideNotifications(company, user).concat(serverSideNotifications) : _this.getClientsideNotifications(company, user); });
};
NotificationsService.prototype.getServersideNotifications = function (notificationsApiUrl, company, user) {
var _this = this;
return this.$http.get(notificationsApiUrl)
.then(function (response) { return response.data.notifications
.filter(function (notification) { return !_this.notificationsLookup[notification.id] || _this.notificationsLookup[notification.id].filter(notification, company, user); })
.map(function (notification) { return _this.notificationsLookup[notification.id] ? _this.notificationsLookup[notification.id].map(notification, company, user) : notification; }); });
};
NotificationsService.prototype.getClientsideNotifications = function (company, user) {
var notifications = [];
var companyMismatchNotification = this.getCompanyMismatchNotification(company);
if (companyMismatchNotification != undefined) {
notifications.push(companyMismatchNotification);
}
var timezoneMismatchNotification = this.getTimezoneMismatchNotification(user);
if (timezoneMismatchNotification) {
notifications.push(timezoneMismatchNotification);
}
return notifications;
};
NotificationsService.prototype.getCompanyMismatchNotification = function (company) {
var requestedCompanyId = this.$location.search().companyId;
var requestedOpportunityId = this.$location.search().opportunityId;
if ((requestedCompanyId && requestedCompanyId !== company._id)
|| (requestedOpportunityId !== undefined && company.opportunityId !== undefined && requestedOpportunityId !== company.opportunityId)) {
return {
id: 'companyMismatch',
translationKey: 'The user you logged in with does not have access to the resource you requested.',
type: 'error'
};
}
return undefined;
};
NotificationsService.prototype.getTimezoneMismatchNotification = function (user) {
var currentUserTime = this.moment.tz(user.timezone);
var currentBrowserTime = this.moment();
var showTimezoneNotification;
var isBrowserTimeUserTimeMismatch = currentBrowserTime.hour() != currentUserTime.hour() || currentBrowserTime.minute() != currentUserTime.minute();
if (isBrowserTimeUserTimeMismatch) {
var shouldUserGetTimezoneNotification = typeof user.nextTimezoneNotificationCheck !== 'undefined' && user.timezoneNotificationStatus.toLowerCase() === 'no';
if (shouldUserGetTimezoneNotification) {
showTimezoneNotification = this.moment(this.moment().toISOString()).isAfter(user.nextTimezoneNotificationCheck, 'day');
}
if (user.timezoneNotificationStatus === undefined || user.timezoneNotificationStatus.toLowerCase() === 'yes' || showTimezoneNotification) {
return {
id: 'timezoneMismatch',
html: "<timezone-mismatch-notification notification-id=\"timezoneMismatch\" user-id=\"" + user._id + "\" on-action-select=\"$ctrl.handleOnActionSelect()\"></timezone-mismatch-notification>",
type: 'warning'
};
}
}
return undefined;
};
NotificationsService.prototype.handleNotifications = function (notifications, config) {
var _this = this;
if (config && config.hasLegacyAlertArea) {
this.legacyAlertController = element(document.getElementsByClassName('alertArea')[0]).scope();
// Populates the legacy AlertCrtl area with notifications
if (this.legacyAlertController !== undefined && this.legacyAlertController.hasOwnProperty('alerts')) {
notifications.forEach(function (notification) {
_this.legacyAlertController.alerts.push(_this.convertNotificationToLegacyAlert(notification));
});
}
}
};
NotificationsService.prototype.convertNotificationToLegacyAlert = function (notification) {
return {
className: notification.id,
hideOnPageChange: false,
iconName: undefined,
text: notification.translationKey ? this.$translate.instant(notification.translationKey) : notification.html,
timeout: 0,
type: notification.type,
uniqueId: notification.id
};
};
NotificationsService.prototype.handleNotificationUserAction = function (notificationId, userId, settingsPayload, requiresUserConfirmation) {
var _this = this;
this.$rootScope.$broadcast('userUpdateFromNotificationRequired', {
userId: userId,
requiresUserConfirmation: requiresUserConfirmation,
settings: settingsPayload
});
if (this.legacyAlertController && this.legacyAlertController.alerts && this.legacyAlertController.alerts.length) {
this.legacyAlertController.alerts.forEach(function (alert, index) {
if (alert.uniqueId === notificationId) {
_this.legacyAlertController.alerts.splice(index, 1);
}
});
}
};
NotificationsService = __decorate([
Injectable(),
__param(0, Inject('$http')),
__param(1, Inject('$location')),
__param(2, Inject('moment')),
__param(3, Inject('$translate')),
__param(4, Inject('$log')),
__param(5, Inject('$rootScope')),
__metadata("design:paramtypes", [Function, Object, Object, Object, Object, Object])
], NotificationsService);
return NotificationsService;
}());
var __decorate$1 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata$1 = (undefined && undefined.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param$1 = (undefined && undefined.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var supportedTimezones = [
{ name: '(-12:00) Eniwetok, Kwajalein', tz: 'Pacific/Kwajalein', utcTime: '-12:00' },
{ name: '(-11:00) Midway Island, Samoa', tz: 'Pacific/Samoa', utcTime: '-11:00' },
{ name: '(-10:00) Hawaii', tz: 'Pacific/Honolulu', utcTime: '-10:00' },
{ name: '(-9:00) Alaska', tz: 'America/Anchorage', utcTime: '-09:00' },
{ name: '(-8:00) Pacific Time (US, Canada)', tz: 'America/Los_Angeles', utcTime: '-08:00' },
{ name: '(-7:00) Mountain Time (US, Canada)', tz: 'America/Denver', utcTime: '-07:00' },
{ name: '(-6:00) Central Time (US, Canada), Mexico City', tz: 'America/Menominee', utcTime: '-06:00' },
{ name: '(-5:00) Eastern Time (US, Canada), Bogota, Lima', tz: 'America/New_York', utcTime: '-05:00' },
{ name: '(-4:00) Atlantic Time (Canada), Caracas, La Paz', tz: 'America/Moncton', utcTime: '-04:00' },
{ name: '(-3:30) Newfoundland', tz: 'America/St_Johns', utcTime: '-03:30' },
{ name: '(-3:00) Brazil, Buenos Aires, Georgetown', tz: 'America/Argentina/Buenos_Aires', utcTime: '-03:00' },
{ name: '(-2:00) Mid-Atlantic', tz: 'America/Noronha', utcTime: '-02:00' },
{ name: '(-1:00) Azores, Cape Verde Islands', tz: 'Atlantic/Azores', utcTime: '-01:00' },
{ name: '(0:00) Western Europe Time, London, Lisbon, Casablanca', tz: 'Europe/London', utcTime: '+00:00' },
{ name: '(+1:00) Brussels, Copenhagen, Madrid, Paris', tz: 'Europe/Paris', utcTime: '+01:00' },
{ name: '(+2:00) Kaliningrad, South Africa', tz: 'Europe/Kaliningrad', utcTime: '+02:00' },
{ name: '(+3:00) Baghdad, Riyadh, Moscow, St. Petersburg', tz: 'Asia/Baghdad', utcTime: '+03:00' },
{ name: '(+3:30) Tehran', tz: 'Asia/Tehran', utcTime: '+03:30' },
{ name: '(+4:00) Abu Dhabi, Muscat, Baku, Tbilisi', tz: 'Asia/Muscat', utcTime: '+04:00' },
{ name: '(+4:30) Kabul', tz: 'Asia/Kabul', utcTime: '+04:30' },
{ name: '(+5:00) Ekaterinburg, Islamabad, Karachi, Tashkent', tz: 'Asia/Karachi', utcTime: '+05:00' },
{ name: '(+5:30) Bombay, Calcutta, Madras, New Delhi', tz: 'Asia/Kolkata', utcTime: '+05:30' },
{ name: '(+5:45) Kathmandu', tz: 'Asia/Kathmandu', utcTime: '+05:45' },
{ name: '(+6:00) Almaty, Dhaka, Colombo', tz: 'Asia/Colombo', utcTime: '+06:00' },
{ name: '(+7:00) Bangkok, Hanoi, Jakarta', tz: 'Asia/Bangkok', utcTime: '+07:00' },
{ name: '(+8:00) Beijing, Perth, Singapore, Hong Kong', tz: 'Asia/Singapore', utcTime: '+08:00' },
{ name: '(+9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk', tz: 'Asia/Tokyo', utcTime: '+09:00' },
{ name: '(+9:30) Adelaide, Darwin', tz: 'Australia/Adelaide', utcTime: '+09:30' },
{ name: '(+10:00) Eastern Australia, Guam, Vladivostok', tz: 'Pacific/Guam', utcTime: '+10:00' },
{ name: '(+10:00) Australia, Brisbane, Queensland', tz: 'Australia/Queensland', utcTime: '+10:00' },
{ name: '(+11:00) Magadan, Solomon Islands, New Caledonia', tz: 'Asia/Magadan', utcTime: '+11:00' },
{ name: '(+11:00) Australia, Melbourne, Victoria', tz: 'Australia/Melbourne', utcTime: '+11:00' },
{ name: '(+11:00) Australia, Sydney', tz: 'Australia/Sydney', utcTime: '+11:00' },
{ name: '(+12:00) Auckland, Wellington, Fiji, Kamchatka', tz: 'Pacific/Auckland', utcTime: '+12:00' }
];
var TimezoneMismatchComponent = /** @class */ (function () {
function TimezoneMismatchComponent($translate, moment, NotificationsService$$1) {
this.$translate = $translate;
this.moment = moment;
this.NotificationsService = NotificationsService$$1;
}
TimezoneMismatchComponent.prototype.updateUserTimezone = function (value) {
var settingsToUpdate = {};
switch (value) {
case 'N':
settingsToUpdate.timezoneNotificationStatus = 'no';
settingsToUpdate.nextTimezoneNotificationCheck = this.moment().add({ days: 7 }).toDate().toISOString();
break;
case 'D':
settingsToUpdate.timezoneNotificationStatus = 'never';
break;
default:
settingsToUpdate.timezoneNotificationStatus = 'yes';
settingsToUpdate.timezone = this.getTimezoneByBrowserAttribute('tz');
break;
}
this.NotificationsService.handleNotificationUserAction(this.notificationId, this.userId, settingsToUpdate, true);
};
TimezoneMismatchComponent.prototype.getTimezoneByBrowserAttribute = function (attribute) {
var browserTime = this.moment();
var dstAdjustment = browserTime.isDST() ? 1 : 0;
var returnStr = 'Unknown';
browserTime.utcOffset(browserTime.utcOffset() - 60 * dstAdjustment);
for (var i = 0; i < supportedTimezones.length; i++) {
if (supportedTimezones[i].utcTime === browserTime.format('Z')) {
if (supportedTimezones[i][attribute] !== undefined) {
returnStr = supportedTimezones[i][attribute];
}
break;
}
}
return returnStr;
};
__decorate$1([
Input('@'),
__metadata$1("design:type", String)
], TimezoneMismatchComponent.prototype, "notificationId", void 0);
__decorate$1([
Input('@'),
__metadata$1("design:type", String)
], TimezoneMismatchComponent.prototype, "userId", void 0);
TimezoneMismatchComponent = __decorate$1([
Component({
selector: 'timezone-mismatch-notification',
template: "<span> <span translate>time-zone-smarter-alert__header-text</span> {{ $ctrl.getTimezoneByBrowserAttribute('name') }}? <span class=\"timezone-mismatch__actions\"> <a href=\"#\" ng-click=\"$ctrl.updateUserTimezone(\'Y\')\"> <span translate>time-zone-smarter-alert__action-label__yes</span> </a> <a href=\"#\" ng-click=\"$ctrl.updateUserTimezone(\'N\')\"> <span translate>time-zone-smarter-alert__action-label__no</span> </a> <a href=\"#\" ng-click=\"$ctrl.updateUserTimezone(\'D\')\"> <span translate>time-zone-smarter-alert__action-label__dont-ask-again</span> </a> <a href=\"/settings/personal\"> <span translate>Settings</span> </a> </span> </span>",
}),
__param$1(0, Inject('$translate')),
__param$1(1, Inject('moment')),
__param$1(2, Inject('mwNotificationsService')),
__metadata$1("design:paramtypes", [Object, Object, NotificationsService])
], TimezoneMismatchComponent);
return TimezoneMismatchComponent;
}());
var __decorate$2 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata$2 = (undefined && undefined.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param$2 = (undefined && undefined.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var TwitterThrottledComponent = /** @class */ (function () {
function TwitterThrottledComponent($translate, NotificationsService$$1) {
this.$translate = $translate;
this.NotificationsService = NotificationsService$$1;
}
TwitterThrottledComponent.prototype.dontShowAgainHandler = function () {
var settingsToUpdate = {
notifications: {}
};
settingsToUpdate.notifications[this.companyId] = {
twitterThrottle: {
toastMessage: true
}
};
this.NotificationsService.handleNotificationUserAction(this.notificationId, this.userId, settingsToUpdate);
};
__decorate$2([
Input('@'),
__metadata$2("design:type", Object)
], TwitterThrottledComponent.prototype, "notificationId", void 0);
__decorate$2([
Input('@'),
__metadata$2("design:type", Object)
], TwitterThrottledComponent.prototype, "companyId", void 0);
__decorate$2([
Input('@'),
__metadata$2("design:type", Object)
], TwitterThrottledComponent.prototype, "userId", void 0);
TwitterThrottledComponent = __decorate$2([
Component({
selector: 'twitter-throttled-notification',
template: "<span> <span translate>TWITTER_THROTTLED_WARNING_MESSAGE</span> <strong> <a href ng-click=\"$ctrl.dontShowAgainHandler()\"> <span>Don\'t show again</span> </a> </strong> </span>",
}),
__param$2(0, Inject('$translate')),
__param$2(1, Inject('mwNotificationsService')),
__metadata$2("design:paramtypes", [Object, NotificationsService])
], TwitterThrottledComponent);
return TwitterThrottledComponent;
}());
var __decorate$3 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var NotificationsModule = /** @class */ (function () {
function NotificationsModule() {
}
NotificationsModule = __decorate$3([
NgModule({
imports: [
'MW.Framework.CoreUserService',
'MW.Framework.DialogService',
'pascalprecht.translate'
],
declarations: [
TimezoneMismatchComponent,
TwitterThrottledComponent
],
providers: [{ provide: 'mwNotificationsService', useClass: NotificationsService }]
})
], NotificationsModule);
return NotificationsModule;
}());
var index = module$1('MW.Framework.NotificationsModule', [
bundle(NotificationsModule).name
]);
export default index;