All files / src/store/features/playbook/slices/tasks duplicateTask.ts

13.79% Statements 4/29
0% Branches 0/8
0% Functions 0/3
12.5% Lines 3/24

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                                                                    
import { PayloadAction } from "@reduxjs/toolkit";
import { PlaybookUIState, Task } from "../../../../../types/index.ts";
import generateUUIDWithoutHyphens from "../../../../../utils/common/generateUUIDWithoutHyphens.ts";
import { v4 as uuidv4 } from "uuid";
 
export const duplicateTask = (
  state: PlaybookUIState,
  {
    payload,
  }: PayloadAction<{
    id: string;
    keyToBeRemoved?: string;
    parentStepId?: string;
  }>,
) => {
  const { id, keyToBeRemoved, parentStepId } = payload;
  const playbook = state.currentPlaybook;
  const tasks = playbook?.ui_requirement.tasks ?? [];
  const steps = playbook?.steps ?? [];
  const task = tasks.find((e) => e.id === id);
 
  Iif (!task) return;
  const newTaskId = generateUUIDWithoutHyphens();
  const newTask: Task = JSON.parse(JSON.stringify(task));
  newTask.id = newTaskId;
  newTask.reference_id = uuidv4();
  newTask.name = uuidv4();
  newTask.ui_requirement.errors = {};
  const source = newTask.source;
  const type = newTask[source.toLowerCase()].type;
  const data = newTask[source.toLowerCase()][type.toLowerCase()];
  Iif (keyToBeRemoved) data[keyToBeRemoved] = "";
  const stepId = parentStepId ?? task.ui_requirement.stepId;
  const step = steps.find((step) => step.id === stepId);
  tasks.push(newTask);
  step?.tasks.push(newTaskId);
 
  state.currentVisibleTask = newTaskId;
};