All files / ee/app/assets/javascripts/oncall_schedules/utils cache_updates.js

58.67% Statements 44/75
32% Branches 8/25
60.87% Functions 14/23
58.11% Lines 43/74

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                    7x   7x                                                       7x 2x 2x       2x         2x 2x 2x       2x             7x 2x 2x       2x         2x 2x   2x         2x             7x                                                                     7x           2x 2x       2x         2x 2x 2x     2x 8x     2x     2x             7x         7x   7x           7x 2x     2x       7x 2x     2x       7x               7x 2x     2x      
import produce from 'immer';
import createFlash from '~/flash';
 
import {
  DELETE_SCHEDULE_ERROR,
  UPDATE_SCHEDULE_ERROR,
  UPDATE_ROTATION_ERROR,
  DELETE_ROTATION_ERROR,
} from './error_messages';
 
const ROTATION_CONNECTION_TYPE = 'IncidentManagementOncallRotationConnection';
 
const addScheduleToStore = (store, query, { oncallScheduleCreate }, variables) => {
  const schedule = oncallScheduleCreate?.oncallSchedule;
  if (!schedule) {
    return;
  }
 
  const sourceData = store.readQuery({
    query,
    variables,
  });
 
  const data = produce(sourceData, (draftData) => {
    draftData.project.incidentManagementOncallSchedules.nodes = [
      ...draftData.project.incidentManagementOncallSchedules.nodes,
      {
        ...schedule,
        rotations: { nodes: [], __typename: ROTATION_CONNECTION_TYPE },
      },
    ];
  });
 
  store.writeQuery({
    query,
    variables,
    data,
  });
};
 
const deleteScheduleFromStore = (store, query, { oncallScheduleDestroy }, variables) => {
  const schedule = oncallScheduleDestroy?.oncallSchedule;
  Iif (!schedule) {
    return;
  }
 
  const sourceData = store.readQuery({
    query,
    variables,
  });
 
  const data = produce(sourceData, (draftData) => {
    draftData.project.incidentManagementOncallSchedules.nodes = draftData.project.incidentManagementOncallSchedules.nodes.filter(
      ({ iid }) => iid !== schedule.iid,
    );
  });
 
  store.writeQuery({
    query,
    variables,
    data,
  });
};
 
const updateScheduleFromStore = (store, query, { oncallScheduleUpdate }, variables) => {
  const schedule = oncallScheduleUpdate?.oncallSchedule;
  Iif (!schedule) {
    return;
  }
 
  const sourceData = store.readQuery({
    query,
    variables,
  });
 
  const data = produce(sourceData, (draftData) => {
    draftData.project.incidentManagementOncallSchedules.nodes = draftData.project.incidentManagementOncallSchedules.nodes.map(
      (scheduleToUpdate) => {
        return scheduleToUpdate.iid === schedule.iid ? schedule : scheduleToUpdate;
      },
    );
  });
 
  store.writeQuery({
    query,
    variables,
    data,
  });
};
 
const updateRotationFromStore = (
  store,
  query,
  { oncallRotationUpdate, scheduleIid },
  variables,
) => {
  const rotation = oncallRotationUpdate?.oncallRotation;
  if (!rotation) {
    return;
  }
 
  const sourceData = store.readQuery({
    query,
    variables,
  });
 
  const data = produce(sourceData, (draftData) => {
    const scheduleToUpdate = draftData.project.incidentManagementOncallSchedules.nodes.find(
      ({ iid }) => iid === scheduleIid,
    );
 
    const updatedRotations = scheduleToUpdate.rotations.nodes.map((rotationToUpdate) => {
      return rotationToUpdate.id === rotation.id ? rotation : rotationToUpdate;
    });
 
    scheduleToUpdate.rotations.nodes = updatedRotations;
  });
 
  store.writeQuery({
    query,
    variables,
    data,
  });
};
 
const deleteRotationFromStore = (
  store,
  query,
  { oncallRotationDestroy, scheduleIid },
  variables,
) => {
  const rotation = oncallRotationDestroy?.oncallRotation;
  Iif (!rotation) {
    return;
  }
 
  const sourceData = store.readQuery({
    query,
    variables,
  });
 
  const data = produce(sourceData, (draftData) => {
    const scheduleToUpdate = draftData.project.incidentManagementOncallSchedules.nodes.find(
      ({ iid }) => iid === scheduleIid,
    );
 
    const updatedRotations = scheduleToUpdate.rotations.nodes.filter(
      ({ id }) => id !== rotation.id,
    );
 
    scheduleToUpdate.rotations.nodes = updatedRotations;
  });
 
  store.writeQuery({
    query,
    variables,
    data,
  });
};
 
const onError = (data, message) => {
  createFlash({ message });
  throw new Error(data.errors);
};
 
export const hasErrors = ({ errors = [] }) => errors?.length;
 
export const updateStoreOnScheduleCreate = (store, query, data, variables) => {
  if (!hasErrors(data)) {
    addScheduleToStore(store, query, data, variables);
  }
};
 
export const updateStoreAfterScheduleDelete = (store, query, data, variables) => {
  Iif (hasErrors(data)) {
    onError(data, DELETE_SCHEDULE_ERROR);
  } else {
    deleteScheduleFromStore(store, query, data, variables);
  }
};
 
export const updateStoreAfterScheduleEdit = (store, query, data, variables) => {
  Iif (hasErrors(data)) {
    onError(data, UPDATE_SCHEDULE_ERROR);
  } else {
    updateScheduleFromStore(store, query, data, variables);
  }
};
 
export const updateStoreAfterRotationEdit = (store, query, data, variables) => {
  if (hasErrors(data)) {
    onError(data, UPDATE_ROTATION_ERROR);
  } else {
    updateRotationFromStore(store, query, data, variables);
  }
};
 
export const updateStoreAfterRotationDelete = (store, query, data, variables) => {
  Iif (hasErrors(data)) {
    onError(data, DELETE_ROTATION_ERROR);
  } else {
    deleteRotationFromStore(store, query, data, variables);
  }
};