11import SirenError from './error' ;
22import {
3- verifyToken ,
43 fetchUnviewedNotificationsCount ,
54 fetchAllNotifications ,
65 markAsReadById ,
@@ -10,22 +9,18 @@ import {
109} from './api' ;
1110import {
1211 BulkUpdateType ,
13- VerificationStatus ,
1412 ErrorMap ,
1513 SirenErrorTypes ,
1614 DATA_FETCH_INTERVAL ,
17- ErrorLevelType ,
1815 EventType
1916} from './constants/generic' ;
2017import { getStartDate , sanitizeSession , emitSirenError } from './utils' ;
2118
2219import type {
23- ErrorCallbackType ,
2420 FetchNotificationsParamsType ,
2521 InitConfigType ,
2622 ActionCallbackType ,
2723 NotificationsApiResponse ,
28- VerifyTokenResponse ,
2924 NotificationDataType ,
3025 SirenErrorType ,
3126 NotificationDataResponse ,
@@ -39,19 +34,16 @@ import type {
3934export class Siren {
4035 private token : string ;
4136 private recipientId : string ;
42- private onError : ErrorCallbackType ;
4337 private notificationFetchIntervalId : number | undefined ;
4438 private unViewedCountFetchIntervalId : number | undefined ;
4539 private latestNotification : NotificationDataType | null = null ;
4640 private actionCallbacks : ActionCallbackType | undefined ;
47- private tokenValidationStatus : VerificationStatus = VerificationStatus . PENDING ;
4841 /**
4942 * Binds specific class methods to ensure the correct `this` context when called.
5043 * This is necessary for methods that are used asynchronously or passed as callbacks.
5144 * @private
5245 */
5346 private bindMethods ( ) {
54- this . verifyToken = this . verifyToken . bind ( this ) ;
5547 this . fetchUnviewedNotificationsCount = this . fetchUnviewedNotificationsCount . bind ( this ) ;
5648 this . fetchAllNotifications = this . fetchAllNotifications . bind ( this ) ;
5749 this . startRealTimeFetch = this . startRealTimeFetch . bind ( this ) ;
@@ -81,35 +73,11 @@ export class Siren {
8173
8274 this . token = session . data ?. token || '' ;
8375 this . recipientId = session . data ?. recipientId || '' ;
84- this . onError = session . data ?. onError ;
8576 this . actionCallbacks = actionCallbacks ;
8677
87- if ( session . data ) this . verifyToken ( ) ;
8878 this . bindMethods ( ) ;
8979 }
9080
91- /**
92- * Verifies the validity of the user's recipient and token.
93- *
94- * @returns {Promise<VerifyTokenResponse | null> }
95- * - `VerifyTokenResponse` object containing status as SUCCESS.
96- * - `null` if the API fails to return a valid response.
97- */
98- async verifyToken ( ) : Promise < VerifyTokenResponse | null > {
99- this . tokenValidationStatus = VerificationStatus . PENDING ;
100- const res = await verifyToken ( this . token , this . recipientId , this . onError ) ;
101-
102- if ( res )
103- this . tokenValidationStatus =
104- res && res . data ?. status === VerificationStatus . SUCCESS
105- ? VerificationStatus . SUCCESS
106- : VerificationStatus . FAILED ; // api returns null in failed scenarios
107- if ( this . actionCallbacks ?. onStatusChange )
108- this . actionCallbacks . onStatusChange ( this . tokenValidationStatus ) ;
109-
110- return res ;
111- }
112-
11381 /**
11482 * Retrieves the number of unviewed notifications count for the user.
11583 *
@@ -118,9 +86,6 @@ export class Siren {
11886 *
11987 */
12088 async fetchUnviewedNotificationsCount ( ) : Promise < UnviewedCountReturnResponse | null > {
121- const hasPermission = this . authorizeUserAction ( ) ;
122-
123- if ( ! hasPermission ) return this . getErrorForUnauthorizedAction ( ) ;
12489
12590 const response = await fetchUnviewedNotificationsCount ( this . token , this . recipientId ) ;
12691
@@ -146,9 +111,6 @@ export class Siren {
146111 async fetchAllNotifications (
147112 params : FetchNotificationsParamsType
148113 ) : Promise < NotificationsApiResponse | null > {
149- const hasPermission = this . authorizeUserAction ( ) ;
150-
151- if ( ! hasPermission ) return this . getErrorForUnauthorizedAction ( ) ;
152114 const response = await fetchAllNotifications ( this . token , this . recipientId , params ) ;
153115
154116 if ( response && response ?. data ?. length && response ?. data ?. length > 0 )
@@ -170,7 +132,6 @@ export class Siren {
170132 eventType : EventType ;
171133 params ?: FetchNotificationsParamsType ;
172134 } ) : void {
173- this . authorizeUserAction ( ) ;
174135 switch ( eventType ) {
175136 case EventType . NOTIFICATION :
176137 this . startRealTimeNotificationFetch ( params ?? { } ) ;
@@ -208,9 +169,7 @@ export class Siren {
208169 */
209170
210171 async markAsReadById ( id : string ) : Promise < NotificationDataResponse | null > {
211- const hasPermission = this . authorizeUserAction ( ) ;
212172
213- if ( ! hasPermission ) return this . getErrorForUnauthorizedAction ( ) ;
214173 let response ;
215174
216175 if ( id ) response = await markAsReadById ( this . token , this . recipientId , id ) ;
@@ -230,9 +189,6 @@ export class Siren {
230189
231190 async markAsReadByDate ( params : BulkUpdateParamsType ) : Promise < ActionResponse | null > {
232191 const { startDate, category } = params || { } ;
233- const hasPermission = this . authorizeUserAction ( ) ;
234-
235- if ( ! hasPermission ) return this . getErrorForUnauthorizedAction ( ) ;
236192 let response ;
237193
238194 if ( startDate ) {
@@ -265,9 +221,6 @@ export class Siren {
265221 */
266222
267223 async deleteById ( id : string ) : Promise < ActionResponse | null > {
268- const hasPermission = this . authorizeUserAction ( ) ;
269-
270- if ( ! hasPermission ) return this . getErrorForUnauthorizedAction ( ) ;
271224 let response ;
272225
273226 if ( id ) response = await deleteNotificationById ( this . token , this . recipientId , id ) ;
@@ -286,9 +239,7 @@ export class Siren {
286239 */
287240 async deleteByDate ( params : BulkUpdateParamsType ) : Promise < ActionResponse | null > {
288241 const { startDate, isRead, category } = params || { } ;
289- const hasPermission = this . authorizeUserAction ( ) ;
290242
291- if ( ! hasPermission ) return this . getErrorForUnauthorizedAction ( ) ;
292243 let response ;
293244
294245 if ( startDate ) {
@@ -322,9 +273,6 @@ export class Siren {
322273 *
323274 */
324275 async markAllAsViewed ( startDate : string ) : Promise < MarkAsViewedResponse | null > {
325- const hasPermission = this . authorizeUserAction ( ) ;
326-
327- if ( ! hasPermission ) return this . getErrorForUnauthorizedAction ( ) ;
328276 let response ;
329277
330278 if ( startDate )
@@ -349,7 +297,6 @@ export class Siren {
349297 params . start ?? this . latestNotification ?. createdAt ?? new Date ( ) . toISOString ( ) ;
350298
351299 this . notificationFetchIntervalId = window . setInterval ( async ( ) => {
352- if ( this . tokenValidationStatus !== VerificationStatus . SUCCESS ) return null ;
353300 const response = await fetchAllNotifications ( this . token , this . recipientId , {
354301 ...params ,
355302 ...( lastFetchedCreatedAt && {
@@ -380,7 +327,6 @@ export class Siren {
380327 */
381328 private startRealTimeUnviewedCountFetch ( ) : void {
382329 this . unViewedCountFetchIntervalId = window . setInterval ( async ( ) => {
383- if ( this . tokenValidationStatus !== VerificationStatus . SUCCESS ) return ;
384330 const response = await fetchUnviewedNotificationsCount ( this . token , this . recipientId ) ;
385331
386332 if ( this . actionCallbacks ?. onEventReceive )
@@ -404,44 +350,6 @@ export class Siren {
404350
405351 return { data : null , error } ;
406352 }
407-
408- private authorizeUserAction ( ) : boolean {
409- if ( this ?. tokenValidationStatus === VerificationStatus . FAILED ) {
410- const errorObj = new SirenError (
411- SirenErrorTypes . ERROR ,
412- ErrorMap . UNAUTHORIZED_OPERATION . message ,
413- ErrorMap . UNAUTHORIZED_OPERATION . code
414- ) ;
415-
416- if ( this . onError ) this . onError ( errorObj . getError ( ) ) ;
417-
418- return false ;
419- }
420- if ( this ?. tokenValidationStatus === VerificationStatus . PENDING ) return false ;
421-
422- return true ;
423- }
424-
425- private getErrorForUnauthorizedAction ( ) {
426- if ( this ?. tokenValidationStatus === VerificationStatus . PENDING )
427- return {
428- data : null ,
429- error : {
430- Type : ErrorLevelType . ERROR ,
431- Message : ErrorMap . AUTHENTICATION_PENDING . message ,
432- Code : ErrorMap . AUTHENTICATION_PENDING . code
433- }
434- } ;
435-
436- return {
437- data : null ,
438- error : {
439- Type : ErrorLevelType . ERROR ,
440- Message : ErrorMap . UNAUTHORIZED_OPERATION . message ,
441- Code : ErrorMap . UNAUTHORIZED_OPERATION . code
442- }
443- } ;
444- }
445353}
446354
447355if ( window ) window . Siren = Siren ;
0 commit comments