All files / client/src/views/admin adminThunks.ts

70% Statements 56/80
46.15% Branches 12/26
84.61% Functions 22/26
70% Lines 56/80

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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267                                                                520x     6x 6x   5x       5x           520x 607x 607x   606x 15x     591x           591x             591x                     591x                     591x 519x     591x       520x     6x 6x   6x       6x       520x       4x 4x         4x       4x                 520x     6x 6x                 6x       6x       520x     2x 2x   2x       2x       520x     2x 2x   2x       2x       520x 9x 9x   9x       9x       520x           520x     3x 3x   3x       3x           520x                           520x                                       520x                          
import { isDeepEqual } from "remeda";
import {
  EventSignupStrategy,
  LoginProvider,
} from "shared/config/eventConfigTypes";
import { EmailNotificationTrigger } from "shared/types/emailNotification";
import { SignupQuestion } from "shared/types/models/settings";
import { postAssignment } from "client/services/assignmentServices";
import { postHidden } from "client/services/hiddenServices";
import {
  deleteSignupQuestion,
  getSettings,
  postSettings,
  postSignupQuestion,
} from "client/services/settingsServices";
import { getSignupMessages } from "client/services/userServices";
import { AppThunk } from "client/types/reduxTypes";
import { getSentryTest, postEmailTest } from "client/views/admin/adminService";
import {
  submitAssignmentResponseMessageAsync,
  submitGetSettingsAsync,
  submitGetSignupMessagesAsync,
  submitSetAdminMessageAsync,
  submitSetEmailNotificationTriggersAsync,
  submitSetLoginProviderAsync,
  submitSetSignupStrategyAsync,
  submitToggleAppOpenAsync,
  submitUpdateHiddenAsync,
  updateServerAppBuildTime,
  updateSignupQuestions,
} from "client/views/admin/adminSlice";
 
export const submitUpdateHidden = (
  hiddenProgramItemIds: readonly string[],
): AppThunk<Promise<string | undefined>> => {
  return async (dispatch): Promise<string | undefined> => {
    const updateHiddenResponse = await postHidden(hiddenProgramItemIds);
 
    Iif (updateHiddenResponse.status === "error") {
      return updateHiddenResponse.message;
    }
 
    dispatch(
      submitUpdateHiddenAsync(updateHiddenResponse.hiddenProgramItemIds),
    );
  };
};
 
export const submitGetSettings = (): AppThunk<Promise<boolean>> => {
  return async (dispatch, useState): Promise<boolean> => {
    const settingsResponse = await getSettings();
 
    if (settingsResponse.status === "error") {
      return false;
    }
 
    const state = useState();
 
    // Tracked outside the settings comparison below: the build time changes on
    // every deploy and shouldn't count as a settings change. The reducer
    // owns the acceptance logic and returns the same state when nothing
    // changes, so the dispatch is unconditional
    dispatch(
      updateServerAppBuildTime({
        buildTime: settingsResponse.appBuildTime,
        receivedAt: performance.now(),
      }),
    );
 
    const currentSettings = {
      hiddenProgramItemIds: state.admin.hiddenProgramItemIds,
      appOpen: state.admin.appOpen,
      adminMessageFi: state.admin.adminMessageFi,
      adminMessageEn: state.admin.adminMessageEn,
      signupQuestions: state.admin.signupQuestions,
      signupStrategy: state.admin.signupStrategy,
      loginProvider: state.admin.loginProvider,
      emailNotificationTrigger: state.admin.emailNotificationTrigger,
    };
 
    const updatedSettings = {
      hiddenProgramItemIds: settingsResponse.hiddenProgramItemIds,
      appOpen: settingsResponse.appOpen,
      adminMessageFi: settingsResponse.adminMessageFi,
      adminMessageEn: settingsResponse.adminMessageEn,
      signupQuestions: settingsResponse.signupQuestions,
      signupStrategy: settingsResponse.signupStrategy,
      loginProvider: settingsResponse.loginProvider,
      emailNotificationTrigger: settingsResponse.emailNotificationTrigger,
    };
 
    if (!isDeepEqual(currentSettings, updatedSettings)) {
      dispatch(submitGetSettingsAsync(updatedSettings));
    }
 
    return true;
  };
};
 
export const submitToggleAppOpen = (
  appOpen: boolean,
): AppThunk<Promise<string | undefined>> => {
  return async (dispatch): Promise<string | undefined> => {
    const postSettingsResponse = await postSettings({ appOpen });
 
    Iif (postSettingsResponse.status === "error") {
      return postSettingsResponse.message;
    }
 
    dispatch(submitToggleAppOpenAsync(postSettingsResponse.settings.appOpen));
  };
};
 
export const submitSetAdminMessage = (
  adminMessageFi: string,
  adminMessageEn: string,
): AppThunk<Promise<string | undefined>> => {
  return async (dispatch): Promise<string | undefined> => {
    const postSettingsResponse = await postSettings({
      adminMessageFi,
      adminMessageEn,
    });
 
    Iif (postSettingsResponse.status === "error") {
      return postSettingsResponse.message;
    }
 
    dispatch(
      submitSetAdminMessageAsync({
        adminMessageFi: postSettingsResponse.settings.adminMessageFi,
        adminMessageEn: postSettingsResponse.settings.adminMessageEn,
      }),
    );
  };
};
 
export const submitAddSignupQuestion = (
  signupQuestion: SignupQuestion,
): AppThunk<Promise<string | undefined>> => {
  return async (dispatch): Promise<string | undefined> => {
    const response = await postSignupQuestion({
      programItemId: signupQuestion.programItemId,
      questionFi: signupQuestion.questionFi,
      questionEn: signupQuestion.questionEn,
      private: signupQuestion.private,
      type: signupQuestion.type,
      selectOptions: signupQuestion.selectOptions,
    });
 
    Iif (response.status === "error") {
      return response.message;
    }
 
    dispatch(updateSignupQuestions(response.signupQuestions));
  };
};
 
export const submitDeleteSignupQuestion = (
  programItemId: string,
): AppThunk<Promise<string | undefined>> => {
  return async (dispatch): Promise<string | undefined> => {
    const response = await deleteSignupQuestion(programItemId);
 
    Iif (response.status === "error") {
      return response.message;
    }
 
    dispatch(updateSignupQuestions(response.signupQuestions));
  };
};
 
export const submitSetSignupStrategy = (
  signupStrategy: EventSignupStrategy,
): AppThunk => {
  return async (dispatch): Promise<void> => {
    const response = await postSettings({ signupStrategy });
 
    Iif (response.status === "error") {
      return;
    }
 
    dispatch(submitSetSignupStrategyAsync(response.settings.signupStrategy));
  };
};
 
export const submitGetSignupMessages = (): AppThunk => {
  return async (dispatch): Promise<void> => {
    const response = await getSignupMessages();
 
    Iif (response.status === "error") {
      return;
    }
 
    dispatch(submitGetSignupMessagesAsync(response.signupMessages));
  };
};
 
export const submitGetSentryTest = (): AppThunk => {
  return async (_dispatch): Promise<void> => {
    await getSentryTest();
  };
};
 
export const submitAssignment = (
  assignmentTime: string,
): AppThunk<Promise<string | undefined>> => {
  return async (dispatch): Promise<string | undefined> => {
    const assignResponse = await postAssignment(assignmentTime);
 
    Iif (assignResponse.status === "error") {
      return assignResponse.message;
    }
 
    dispatch(
      submitAssignmentResponseMessageAsync(assignResponse.resultMessage),
    );
  };
};
 
export const submitSetLoginProvider = (
  loginProvider: LoginProvider,
): AppThunk => {
  return async (dispatch): Promise<void> => {
    const response = await postSettings({ loginProvider });
 
    if (response.status === "error") {
      return;
    }
 
    dispatch(submitSetLoginProviderAsync(response.settings.loginProvider));
  };
};
 
export const submitSetEmailNotificationTriggers = (
  emailNotificationTrigger: readonly EmailNotificationTrigger[],
): AppThunk => {
  return async (dispatch): Promise<void> => {
    const response = await postSettings({
      emailNotificationTrigger: [...emailNotificationTrigger],
    });
 
    if (response.status === "error") {
      return;
    }
 
    dispatch(
      submitSetEmailNotificationTriggersAsync(
        response.settings.emailNotificationTrigger,
      ),
    );
  };
};
 
export const submitEmailTest = (
  email: string,
  notificationType: EmailNotificationTrigger,
  programId: string,
): AppThunk<Promise<string | undefined>> => {
  return async (): Promise<string | undefined> => {
    const response = await postEmailTest(email, notificationType, programId);
 
    if (response.status === "error") {
      return response.message;
    }
  };
};