# All files / src/store clvSlice.ts

84.78% Statements 156/184
81.48% Branches 22/27
33.33% Functions 4/12
84.78% Lines 156/184

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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227

    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    3x
    3x
    2x
    3x
    1x
    1x
    3x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x

    1x
    1x
    1x
    2x
    2x
    2x
    2x

    2x
    1x

    1x
    1x
    1x

    1x

    1x
    1x
    1x
    1x
    1x
    4x
    4x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    3x
    3x
    3x
    3x
    3x
    3x
    1x
    1x
    3x
    3x
    3x
    3x
    3x

    3x
    3x
    1x
    1x
    3x
    3x
    1x
    1x
    3x
    3x

    3x

    3x
    3x
    1x
    1x
    3x
    3x
    1x
    1x
    3x
    3x

    3x

    3x
    3x
    1x
    1x
    3x
    3x
    1x
    1x
    3x
    3x

    3x

    3x
    3x
    2x
    2x
    3x
    3x
    2x
    2x
    3x
    3x

    3x

    3x
    3x

    3x
    3x

    3x
    3x

    3x
    3x
    1x

    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
     /**
     * Redux slice for CLV (Closing Line Value) analytics state management
     */
    import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
    import { clvService } from '../services/clv.service';
    import { CLVSummary, CLVBySport, CLVByBookmaker, CLVTrend, CLVReport, CLVFilters } from '../types/clv.types';
    import { RootState } from './index';

    export interface CLVState {
      summary: CLVSummary | null;
      bySport: CLVBySport[];
      byBookmaker: CLVByBookmaker[];
      trends: CLVTrend[];
      report: CLVReport | null;
      filters: CLVFilters;
      loading: boolean;
      error: string | null;
    }

    const initialState: CLVState = {
      summary: null,
      bySport: [],
      byBookmaker: [],
      trends: [],
      report: null,
      filters: {
        period: 'all-time'
      },
      loading: false,
      error: null
    };

    // Async thunks
    export const fetchCLVSummary = createAsyncThunk(
      'clv/fetchSummary',
      async (_, { rejectWithValue }) => {
        try {
          const data = await clvService.getSummary();
          return data;
        } catch (error: any) {
          return rejectWithValue(error.response?.data?.message || 'Failed to fetch CLV summary');
        }
      }
    );

    export const fetchCLVBySport = createAsyncThunk(
      'clv/fetchBySport',
      async (_, { rejectWithValue }) => {
        try {
          const data = await clvService.getBySport();
          return data;
        } catch (error: any) {
          return rejectWithValue(error.response?.data?.message || 'Failed to fetch CLV by sport');
        }
      }
    );

    export const fetchCLVByBookmaker = createAsyncThunk(
      'clv/fetchByBookmaker',
      async (_, { rejectWithValue }) => {
        try {
          const data = await clvService.getByBookmaker();
          return data;
        } catch (error: any) {
          return rejectWithValue(error.response?.data?.message || 'Failed to fetch CLV by bookmaker');
        }
      }
    );

    export const fetchCLVTrends = createAsyncThunk(
      'clv/fetchTrends',
      async (filters: CLVFilters | undefined, { rejectWithValue }) => {
        try {
          const data = await clvService.getTrends(filters);
          return data;
        } catch (error: any) {
          return rejectWithValue(error.response?.data?.message || 'Failed to fetch CLV trends');
        }
      }
    );

    export const fetchCLVReport = createAsyncThunk(
      'clv/fetchReport',
      async (filters: CLVFilters | undefined, { rejectWithValue }) => {
        try {
          const data = await clvService.getReport(filters);
          return data;
        } catch (error: any) {
          return rejectWithValue(error.response?.data?.message || 'Failed to fetch CLV report');
        }
      }
    );

    export const calculateBetCLV = createAsyncThunk(
      'clv/calculateBet',
      async (betId: string, { rejectWithValue }) => {
        try {
          const data = await clvService.calculateForBet(betId);
          return data;
        } catch (error: any) {
          return rejectWithValue(error.response?.data?.message || 'Failed to calculate bet CLV');
        }
      }
    );

    // Slice
    const clvSlice = createSlice({
      name: 'clv',
      initialState,
      reducers: {
        setFilters: (state, action: PayloadAction<CLVFilters>) => {
          state.filters = { ...state.filters, ...action.payload };
        },
        clearFilters: (state) => {
          state.filters = { period: 'all-time' };
        },
        clearError: (state) => {
          state.error = null;
        }
      },
      extraReducers: (builder) => {
        // Fetch Summary
        builder
          .addCase(fetchCLVSummary.pending, (state) => {
            state.loading = true;
            state.error = null;
          })
          .addCase(fetchCLVSummary.fulfilled, (state, action) => {
            state.loading = false;
            state.summary = action.payload;
          })
          .addCase(fetchCLVSummary.rejected, (state, action) => {
            state.loading = false;
            state.error = action.payload as string;
          });

        // Fetch By Sport
        builder
          .addCase(fetchCLVBySport.pending, (state) => {
            state.loading = true;
            state.error = null;
          })
          .addCase(fetchCLVBySport.fulfilled, (state, action) => {
            state.loading = false;
            state.bySport = action.payload;
          })
          .addCase(fetchCLVBySport.rejected, (state, action) => {
            state.loading = false;
            state.error = action.payload as string;
          });

        // Fetch By Bookmaker
        builder
          .addCase(fetchCLVByBookmaker.pending, (state) => {
            state.loading = true;
            state.error = null;
          })
          .addCase(fetchCLVByBookmaker.fulfilled, (state, action) => {
            state.loading = false;
            state.byBookmaker = action.payload;
          })
          .addCase(fetchCLVByBookmaker.rejected, (state, action) => {
            state.loading = false;
            state.error = action.payload as string;
          });

        // Fetch Trends
        builder
          .addCase(fetchCLVTrends.pending, (state) => {
            state.loading = true;
            state.error = null;
          })
          .addCase(fetchCLVTrends.fulfilled, (state, action) => {
            state.loading = false;
            state.trends = action.payload;
          })
          .addCase(fetchCLVTrends.rejected, (state, action) => {
            state.loading = false;
            state.error = action.payload as string;
          });

        // Fetch Report
        builder
          .addCase(fetchCLVReport.pending, (state) => {
            state.loading = true;
            state.error = null;
          })
          .addCase(fetchCLVReport.fulfilled, (state, action) => {
            state.loading = false;
            state.report = action.payload;
          })
          .addCase(fetchCLVReport.rejected, (state, action) => {
            state.loading = false;
            state.error = action.payload as string;
          });

        // Calculate Bet CLV
        builder
          .addCase(calculateBetCLV.pending, (state) => {
            state.loading = true;
            state.error = null;
          })
          .addCase(calculateBetCLV.fulfilled, (state) => {
            state.loading = false;
          })
          .addCase(calculateBetCLV.rejected, (state, action) => {
            state.loading = false;
            state.error = action.payload as string;
          });
      }
    });

    // Actions
    export const { setFilters, clearFilters, clearError } = clvSlice.actions;

    // Selectors
    export const selectCLVSummary = (state: RootState) => state.clv.summary;
    export const selectCLVBySport = (state: RootState) => state.clv.bySport;
    export const selectCLVByBookmaker = (state: RootState) => state.clv.byBookmaker;
    export const selectCLVTrends = (state: RootState) => state.clv.trends;
    export const selectCLVReport = (state: RootState) => state.clv.report;
    export const selectCLVFilters = (state: RootState) => state.clv.filters;
    export const selectCLVLoading = (state: RootState) => state.clv.loading;
    export const selectCLVError = (state: RootState) => state.clv.error;

    export default clvSlice.reducer;