All files / src/store/features/playbook/slices/steps duplicateStep.ts

20% Statements 5/25
0% Branches 0/7
0% Functions 0/6
18.18% Lines 4/22

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    1x 1x 1x   1x                                                                  
import { PayloadAction } from "@reduxjs/toolkit";
import { PlaybookUIState, Step, Task } from "../../../../../types/index.ts";
import generateUUIDWithoutHyphens from "../../../../../utils/common/generateUUIDWithoutHyphens.ts";
import { v4 as uuidv4 } from "uuid";
import { playbookSlice } from "../../playbookSlice.ts";
 
export const duplicateStep = (
  state: PlaybookUIState,
  { payload }: PayloadAction<{ id: string }>,
) => {
  const { id } = payload;
  const playbook = state.currentPlaybook;
  const tasks = playbook?.ui_requirement.tasks ?? [];
  const step = playbook?.steps.find((e) => e.id === id);
  Iif (!step) return;
  const stepTasks = step.tasks
    .map((task) =>
      tasks.find((t) =>
        typeof task === "string" ? t.id === task : t.id === task.id,
      ),
    )
    .filter((task) => task !== undefined) as Task[];
 
  const newStepId = generateUUIDWithoutHyphens();
  const newStep: Step = JSON.parse(JSON.stringify(step));
  newStep.id = newStepId;
  newStep.reference_id = uuidv4();
  newStep.tasks = [];
  state.currentPlaybook?.steps.push(newStep);
 
  stepTasks.forEach((task) => {
    playbookSlice.caseReducers.duplicateTask(state, {
      payload: { id: task.id ?? "", parentStepId: newStepId },
      type: "",
    });
  });
 
  state.currentVisibleStep = newStep.id;
};