All files / src/store/tenant operations.ts

97.62% Statements 41/42
83.33% Branches 5/6
100% Functions 17/17
97.22% Lines 35/36

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                                                                                    4x 4x 4x 4x 4x 4x 4x 4x 4x               4x 4x       6x 1x 1x       6x 3x 3x 9x 2x 1x 1x         3x       6x 1x 1x 1x         6x 2x 2x 1x 1x   1x           6x 1x 1x      
/*
 * Copyright 2019 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import Tenant from 'domain/Tenant';
import { ThunkCommandFactory, AppState } from '../types';
import * as actions from './actions';
import {
  ChangeTenantAction, RefreshTenantListAction, RefreshSupportedTimezoneListAction,
  AddTenantAction, RemoveTenantAction,
} from './types';
import { ThunkDispatch } from 'redux-thunk';
import { Action } from 'redux';
import { rosterOperations } from 'store/roster';
import { skillOperations } from 'store/skill';
import { spotOperations } from 'store/spot';
import { contractOperations } from 'store/contract';
import { employeeOperations } from 'store/employee';
import { shiftTemplateOperations } from 'store/rotation';
import * as rosterActions from 'store/roster/actions';
import * as skillActions from 'store/skill/actions';
import * as spotActions from 'store/spot/actions';
import * as contractActions from 'store/contract/actions';
import * as employeeActions from 'store/employee/actions';
import * as shiftTemplateActions from 'store/rotation/actions';
import { alert } from 'store/alert';
import RosterState from 'domain/RosterState';
import { AddAlertAction } from 'store/alert/types';
 
function refreshData(dispatch: ThunkDispatch<any, any, Action<any>>, state: () => AppState): Promise<any> {
  dispatch(rosterActions.setShiftRosterIsLoading(true));
  dispatch(rosterActions.setAvailabilityRosterIsLoading(true));
  dispatch(rosterActions.setRosterStateIsLoading(true));
  dispatch(skillActions.setIsSkillListLoading(true));
  dispatch(spotActions.setIsSpotListLoading(true));
  dispatch(contractActions.setIsContractListLoading(true));
  dispatch(employeeActions.setIsEmployeeListLoading(true));
  dispatch(shiftTemplateActions.setIsShiftTemplateListLoading(true));
  return Promise.all([
    dispatch(skillOperations.refreshSkillList()),
    dispatch(rosterOperations.getRosterState()),
    dispatch(spotOperations.refreshSpotList()),
    dispatch(contractOperations.refreshContractList()),
    dispatch(employeeOperations.refreshEmployeeList()),
    dispatch(shiftTemplateOperations.refreshShiftTemplateList()),
  ]).then(() => {
    dispatch(rosterOperations.getInitialShiftRoster());
    dispatch(rosterOperations.getInitialAvailabilityRoster());
  });
}
 
export const changeTenant: ThunkCommandFactory<number, ChangeTenantAction> = tenantId => (dispatch, state, client) => {
  dispatch(actions.changeTenant(tenantId));
  return refreshData(dispatch, state);
};
 
export const refreshTenantList:
ThunkCommandFactory<void, RefreshTenantListAction> = () => (dispatch, state, client) => (
  client.get<Tenant[]>('/tenant/').then((tenantList) => {
    const { currentTenantId } = state().tenantData;
    if (tenantList.filter(tenant => tenant.id === currentTenantId).length !== 0) {
      dispatch(actions.refreshTenantList({ tenantList, currentTenantId }));
    } else Eif (tenantList.length > 0) {
      dispatch(actions.refreshTenantList({ tenantList, currentTenantId: tenantList[0].id as number }));
    } else {
      // TODO: this case occurs iff there are no tenants; need a special screen for that
      dispatch(actions.refreshTenantList({ tenantList, currentTenantId: 0 }));
    }
    refreshData(dispatch, state);
  }));
 
export const addTenant:
ThunkCommandFactory<RosterState, AddTenantAction | AddAlertAction> = rs => (dispatch, state, client) => (
  client.post<Tenant>('/tenant/add', rs).then((tenant) => {
    dispatch(alert.showSuccessMessage("addTenant", { name: tenant.name }))
    dispatch(actions.addTenant(tenant));
  })
);
 
export const removeTenant:
ThunkCommandFactory<Tenant, RemoveTenantAction | AddAlertAction> = tenant => (dispatch, state, client) => (
  client.post<boolean>(`/tenant/remove/${tenant.id}`, {}).then((isSuccess) => {
    if (isSuccess) {
      dispatch(alert.showSuccessMessage("removeTenant", { name: tenant.name }))
      dispatch(actions.removeTenant(tenant));
    } else {
      dispatch(alert.showErrorMessage("removeTenantError", { name: tenant.name }))
    }
  })
);
 
export const refreshSupportedTimezones:
ThunkCommandFactory<void, RefreshSupportedTimezoneListAction> = () => (dispatch, state, client) => (
  client.get<string[]>('/tenant/supported/timezones').then((supportedTimezones) => {
    dispatch(actions.refreshSupportedTimezones(supportedTimezones));
  })
);