All files / src/app/extensions/copilot-embedded/store/copilot-embedded-chat copilot-embedded-chat.effects.ts

100% Statements 16/16
100% Branches 0/0
100% Functions 8/8
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 365x 5x 5x   5x         5x     5x 3x           3x   3x   2x   2x 6x 6x 5x              
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { filter, map } from 'rxjs/operators';
 
import { resetUserData } from 'ish-core/store/customer/user';
 
/**
 * Suffix of the storage keys of the persisted chat; `dataRetentionMeta` stores it as `{baseHref}-copilotEmbedded-_chat`.
 */
const COPILOT_EMBEDDED_CHAT_STORAGE_KEY_SUFFIX = '-copilotEmbedded-_chat';
 
@Injectable()
export class CopilotEmbeddedChatEffects {
  constructor(private actions$: Actions) {}
 
  /**
   * Removes the persisted chats under every base href on logout, so the next user of the browser cannot see them.
   * The reducer only clears the chat of the current base href, but the logout applies to the whole site.
   */
  removePersistedChatsOnLogout$ = createEffect(
    () =>
      this.actions$.pipe(
        ofType(resetUserData),
        filter(() => !SSR),
        map(() =>
          [localStorage, sessionStorage].forEach(storage =>
            Array.from({ length: storage.length }, (_, index) => storage.key(index))
              .filter(key => key?.endsWith(COPILOT_EMBEDDED_CHAT_STORAGE_KEY_SUFFIX))
              .forEach(key => storage.removeItem(key))
          )
        )
      ),
    { dispatch: false }
  );
}