<commit_before>import moment from 'moment'
import numeral from 'numeral'
import { Action } from 'redux'
import { ofType } from 'redux-observable'
import { applicationConnected, applicationDisconnected } from 'rt-actions'
import { CurrencyPair, CurrencyPairMap, Trade, Trades, TradeStatus } from 'rt-types'
import { filter, ignoreElements, map, skipWhile, switchMapTo, takeUntil, tap } from 'rxjs/operators'

import { ApplicationEpic } from 'StoreTypes'
import { BLOTTER_ACTION_TYPES, BlotterActions } from '../actions'

import { interval, EMPTY } from 'rxjs'

type NewTradesAction = ReturnType<typeof BlotterActions.createNewTradesAction>

const formatTradeNotification = (trade: Trade, currencyPair: CurrencyPair) => ({
  symbol: trade.symbol,
  spotRate: trade.spotRate,
  notional: numeral(trade.notional).format('0,000,000[.]00'),
  direction: trade.direction,
  tradeId: trade.tradeId.toString(),
  tradeDate: moment(trade.tradeDate).format(),
  status: trade.status,
  dealtCurrency: trade.dealtCurrency,
  termsCurrency: currencyPair.terms,
  valueDate: moment.utc(trade.valueDate).format('DD MMM'),
  traderName: trade.traderName,
})

function parseBlotterData(blotterData: Trades, currencyPairs: CurrencyPairMap) {
  if (Object.keys(currencyPairs).length === 0 || Object.keys(blotterData).length === 0) {
    return []
  }
  return Object.keys(blotterData).map(x =>
    formatTradeNotification(blotterData[x], currencyPairs[blotterData[x].symbol]),
  )
}

export const publishBlotterToExcelEpic: ApplicationEpic = (action$, state$, { platform }) => {
  if (!platform.hasFeature('excel')) {
    return EMPTY
  }

  return action$.pipe(
    applicationConnected,
    switchMapTo(
      interval(7500).pipe(
        takeUntil(action$.pipe(applicationDisconnected)),
        tap(() => {
          if (platform.excel.isOpen()) {
            const parsedData = parseBlotterData(
              state$.value.blotterService.trades,
              state$.value.currencyPairs,
            )
            platform.excel.publishBlotter(parsedData)
          }
        }),
        ignoreElements(),
      ),
    ),
  )
}
export const connectBlotterToNotifications: ApplicationEpic = (action$, state$, { platform }) =>
  action$.pipe(
    ofType<Action, NewTradesAction>(BLOTTER_ACTION_TYPES.BLOTTER_SERVICE_NEW_TRADES),
    filter(() => state$.value.layout.displayBlotter),
    filter(action => action.payload.trades.length > 0),
    map(action => action.payload.trades[0]),
    skipWhile(trade => !state$.value.currencyPairs[trade.symbol]),
    filter(trade => trade.status === TradeStatus.Done || trade.status === TradeStatus.Rejected),
    map(trade => formatTradeNotification(trade, state$.value.currencyPairs[trade.symbol])),
    tap(tradeNotification => platform.notification.notify({ tradeNotification })),
    ignoreElements(),
  )

export const requestBrowserNotificationPermission: ApplicationEpic = (
  action$,
  state$,
  { platform },<commit_msg>ARTP-640 Do not send notification at blotter initialisation<commit_after>import moment from 'moment'
import numeral from 'numeral'
import { Action } from 'redux'
import { ofType } from 'redux-observable'
import { applicationConnected, applicationDisconnected } from 'rt-actions'
import { CurrencyPair, CurrencyPairMap, Trade, Trades, TradeStatus } from 'rt-types'
import { filter, ignoreElements, map, skipWhile, switchMapTo, takeUntil, tap } from 'rxjs/operators'

import { ApplicationEpic } from 'StoreTypes'
import { BLOTTER_ACTION_TYPES, BlotterActions } from '../actions'

import { interval, EMPTY } from 'rxjs'

type NewTradesAction = ReturnType<typeof BlotterActions.createNewTradesAction>

const formatTradeNotification = (trade: Trade, currencyPair: CurrencyPair) => ({
  symbol: trade.symbol,
  spotRate: trade.spotRate,
  notional: numeral(trade.notional).format('0,000,000[.]00'),
  direction: trade.direction,
  tradeId: trade.tradeId.toString(),
  tradeDate: moment(trade.tradeDate).format(),
  status: trade.status,
  dealtCurrency: trade.dealtCurrency,
  termsCurrency: currencyPair.terms,
  valueDate: moment.utc(trade.valueDate).format('DD MMM'),
  traderName: trade.traderName,
})

function parseBlotterData(blotterData: Trades, currencyPairs: CurrencyPairMap) {
  if (Object.keys(currencyPairs).length === 0 || Object.keys(blotterData).length === 0) {
    return []
  }
  return Object.keys(blotterData).map(x =>
    formatTradeNotification(blotterData[x], currencyPairs[blotterData[x].symbol]),
  )
}

export const publishBlotterToExcelEpic: ApplicationEpic = (action$, state$, { platform }) => {
  if (!platform.hasFeature('excel')) {
    return EMPTY
  }

  return action$.pipe(
    applicationConnected,
    switchMapTo(
      interval(7500).pipe(
        takeUntil(action$.pipe(applicationDisconnected)),
        tap(() => {
          if (platform.excel.isOpen()) {
            const parsedData = parseBlotterData(
              state$.value.blotterService.trades,
              state$.value.currencyPairs,
            )
            platform.excel.publishBlotter(parsedData)
          }
        }),
        ignoreElements(),
      ),
    ),
  )
}
export const connectBlotterToNotifications: ApplicationEpic = (action$, state$, { platform }) =>
  action$.pipe(
    ofType<Action, NewTradesAction>(BLOTTER_ACTION_TYPES.BLOTTER_SERVICE_NEW_TRADES),
    filter(() => state$.value.layout.displayBlotter),
    filter(action => !action.payload.isStateOfTheWorld && action.payload.trades.length > 0),
    map(action => action.payload.trades[0]),
    skipWhile(trade => !state$.value.currencyPairs[trade.symbol]),
    filter(trade => trade.status === TradeStatus.Done || trade.status === TradeStatus.Rejected),
    map(trade => formatTradeNotification(trade, state$.value.currencyPairs[trade.symbol])),
    tap(tradeNotification => platform.notification.notify({ tradeNotification })),
    ignoreElements(),
  )

export const requestBrowserNotificationPermission: ApplicationEpic = (
  action$,
  state$,
  { platform },
{'fraction_non_alphanumeric': 0.1292369343796961, 'fraction_numerical': 0.006177992987143096, 'mean_word_length': 4.420814479638009, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4665308', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1809, 'n_words': 340}