<!--  this was the code before the merging this branch to board duplicatio across the workspace
  duplicateBoards = async (board_id, payload, user) => {
    try {
      const [board_exist, role_data] = await Promise.all([
        Board.findById(board_id).where("is_deleted").equals(false).lean(),
        Role_Master.find({
          name: { $in: ["agency", "team_agency"] },
        }),
      ]);

      if (!board_exist)
        return throwError(returnMessage("board", "boardNotFound"), 404);

      //this will use to provide the options like to duplicate the members, attachements and tasks
      const {
        board_ids,
        workspace_ids,
        include_tasks,
        include_task_attachments,
        include_task_assignees,
        include_board_assignees,
        include_sub_tasks,
        include_sub_tasks_attachments,
        include_sub_tasks_assignees,
      } = payload;

      const [boards_count, status_data, configuration, sections] =
        await Promise.all([
          Board.countDocuments({
            project_name: { $regex: board_exist?.project_name, $options: "i" },
            workspace_id: user?.workspace,
            is_deleted: false,
          }),
          Board_Status_Master.findOne({
            name: "active",
          }).lean(),
          Configuration.findOne({}).lean(),
          Section.find({
            workspace_id: user?.workspace,
            board_id: board_exist?._id,
            is_deleted: false,
          }).lean(),
        ]);

      const update_board = {
        project_name:
          lowercaseFirstLetter(board_exist?.project_name?.trim()) +
          " " +
          (boards_count + 1),
        description: board_exist?.description,
        workspace_id: user?.workspace,
        created_by: user?._id,
        board_color: getRandomHexColor(boardColor),
        agency_id: user?.workspace_detail?.created_by,
        board_status: status_data?._id,
      };

      if (include_board_assignees) update_board.members = board_exist?.members;

      if (board_exist?.board_image && board_exist?.board_image !== "") {
        const board_image = duplicateFiles([
          { preview: board_exist?.board_image },
        ]);

        if (board_image?.length > 0) {
          update_board.board_image = board_image?.[0]?.preview;
        }
      }

      // Save Board
      const new_board = (await Board.create(update_board)).toJSON();

      let user_data = [];
      if (include_board_assignees) {
        user_data = await BoardOrder.find(
          {
            board_id: board_id,
            workspace_id: user.workspace,
            is_deleted: false,
          },
          { user_id: 1, order: 1 } // Fetch only user_id field
        ).lean();
      } else {
        const role_id = role_data?.map((role) => role?._id?.toString());
        let filter_new_member = [];
        user?.workspace_detail?.members?.forEach((member) => {
          if (
            role_id?.includes(member?.role?.toString()) &&
            member?.status === "confirmed"
          ) {
            filter_new_member?.push(member?.user_id);
          }
        });

        user_data = await BoardOrder.find({
          user_id: { $in: filter_new_member },
          board_id: board_id,
        }).lean();
      }

      const user_ids = [];
      user_data = user_data?.map((user) => {
        user_ids.push(user?.user_id.toString());
        return {
          user_id: user?.user_id,
          workspace_id: new_board.workspace_id,
          agency_id: new_board.agency_id,
          board_id: new_board?._id,
          order: user?.order + 1,
          is_pinned: include_board_assignees ? user?.is_pinned : false,
        };
      });

      const data = await BoardOrder.insertMany(user_data);
      await this.updateBoardOrdersAfterChange(user.workspace, user_ids, [
        new_board?._id?.toString(),
      ]);

      // ------------- Notifications -------------
      notificationService.addNotification(
        {
          module_name: "board",
          action_name: "created",
          workspace_id: user?.workspace,
          members: new_board?.members?.filter((item) => item !== user?._id),
          project_name: lowercaseFirstLetter(new_board?.project_name),
          created_by_name:
            capitalizeFirstLetter(user?.first_name) +
            " " +
            capitalizeFirstLetter(user?.last_name),
        },
        new_board?._id
      );

      const member_send_mail = new_board?.members?.filter(
        (item) => item !== user?._id
      );

      const link = `${process.env.REACT_APP_URL}/${workspaceNameReplace(
        user?.workspace_detail?.name
      )}/tasks/board/${new_board?._id}`;
      const board_template = boardTemplate({
        project_name: new_board?.project_name,
        description: new_board?.description,
        added_by:
          capitalizeFirstLetter(user?.first_name) +
          " " +
          capitalizeFirstLetter(user?.last_name),
        REACT_APP_URL: process.env.REACT_APP_URL,
        SERVER_URL: process.env.SERVER_URL,
        link: link,
        instagram: configuration?.urls?.instagram,
        facebook: configuration?.urls?.facebook,
        privacy_policy: configuration?.urls?.privacy_policy,
        year: new Date().getFullYear(),
      });

      member_send_mail.map(async (member) => {
        const member_data = await Authentication.findById(member?.member_id)
          .select("email")
          .lean();
        sendEmail({
          email: member_data?.email,
          subject: returnMessage("emailTemplate", "memberAddedBoard"),
          message: board_template,
        });
      });
      // ------------- Notifications -------------

      this.boardDataGenerator(
        new_board?._id,
        user,
        user?.workspace_detail,
        "new_board"
      );

      // duplicate the sections
      sections.map(async (section) => {
        const section_obj = { ...section };
        delete section_obj?._id;
        section_obj.board_id = new_board?._id;
        section.workspace_id = new_board?.workspace_id;

        const is_section_already_exits = await Section.findOne({
          board_id: new_board?._id,
          workspace_id: new_board?.workspace_id,
          is_deleted: false,
          _id: { $in: workspace_ids },
        }).lean(),
        Board_Status_Master.findOne({
          name: "active",
        }).lean(),
        Configuration.findOne({}).lean(),
      ]);

      workspaces = workspaces?.reduce((acc, workspace) => {
        if (!acc[workspace?._id?.toString()])
          acc[workspace?._id?.toString()] = workspace?.members
            ?.filter((member) => member?.status === "confirmed")
            ?.map((member) => member?.user_id?.toString());

        return acc;
      }, {});

      board_ids?.map(async (board_id) => {
        const board_exist = await Board.findById(board_id)
          .where("is_deleted")
          .equals(false)
          .lean();

        if (!board_exist)
          return throwError(returnMessage("board", "boardNotFound"), 404);

        workspace_ids?.map(async (workspace) => {
          const [boards_count, sections] = await Promise.all([
            Board.countDocuments({
              project_name: {
                $regex: board_exist?.project_name,
                $options: "i",
              },
              workspace_id: workspace,
              is_deleted: false,
            }),
            Section.find({
              workspace_id: workspace,
              board_id: board_exist?._id,
              is_deleted: false,
            }).lean(),
          ]);

          const update_board = {
            project_name:
              lowercaseFirstLetter(board_exist?.project_name?.trim()) +
              " " +
              (boards_count + 1),
            description: board_exist?.description,
            workspace_id: workspace,
            created_by: user?._id,
            board_color: getRandomHexColor(boardColor),
            agency_id: user?.workspace_detail?.created_by,
            board_status: status_data?._id,
          };

          if (include_board_assignees) {
            const workspace_members = workspaces[workspace]; // if the member is not exist in the another workspace
            update_board.members = board_exist?.members?.filter((member) =>
              workspace_members?.includes(member?.member_id?.toString())
            );
          }

          if (board_exist?.board_image && board_exist?.board_image !== "") {
            const board_image = duplicateFiles([
              { preview: board_exist?.board_image },
            ]);

            if (board_image?.length > 0) {
              update_board.board_image = board_image?.[0]?.preview;
            }
          }

          // Save Board
          const new_board = (await Board.create(update_board)).toJSON();

          // ------------- Notifications -------------
          notificationService.addNotification(
            {
              module_name: "board",
              action_name: "created",
              workspace_id: workspace,
              members: new_board?.members?.filter((item) => item !== user?._id),
              project_name: lowercaseFirstLetter(new_board?.project_name),
              created_by_name:
                capitalizeFirstLetter(user?.first_name) +
                " " +
                capitalizeFirstLetter(user?.last_name),
            },
            new_board?._id
          );

          const member_send_mail = new_board?.members?.filter(
            (item) => item !== user?._id
          );

          const link = `${process.env.REACT_APP_URL}/${workspaceNameReplace(
            user?.workspace_detail?.name
          )}/tasks/board/${new_board?._id}`;
          const board_template = boardTemplate({
            project_name: new_board?.project_name,
            description: new_board?.description,
            added_by:
              capitalizeFirstLetter(user?.first_name) +
              " " +
              capitalizeFirstLetter(user?.last_name),
            REACT_APP_URL: process.env.REACT_APP_URL,
            SERVER_URL: process.env.SERVER_URL,
            link: link,
            instagram: configuration?.urls?.instagram,
            facebook: configuration?.urls?.facebook,
            privacy_policy: configuration?.urls?.privacy_policy,
            year: new Date().getFullYear(),
          });

          member_send_mail.map(async (member) => {
            const member_data = await Authentication.findById(member?.member_id)
              .select("email")
              .lean();
            sendEmail({
              email: member_data?.email,
              subject: returnMessage("emailTemplate", "memberAddedBoard"),
              message: board_template,
            });
          });
          // ------------- Notifications -------------

          this.boardDataGenerator(
            new_board?._id,
            user,
            user?.workspace_detail,
            "new_board"
          );

          // duplicate the sections
          sections.map(async (section) => {
            const section_obj = { ...section };
            delete section_obj?._id;
            section_obj.board_id = new_board?._id;
            section.workspace_id = new_board?.workspace_id;

            const is_section_already_exits = await Section.findOne({
              board_id: new_board?._id,
              workspace_id: new_board?.workspace_id,
              is_deleted: false,
              section_name: section?.section_name,
              sort_order: section?.sort_order,
            });

            if (!section?.is_deletable) {
              Section.create(section_obj);
            } else {
              let new_section = is_section_already_exits;
              if (!is_section_already_exits) {
                new_section = (await Section.create(section_obj)).toJSON();
              }

              if (include_tasks) {
                const query = {
                  is_deleted: false,
                  activity_status: section?._id,
                  workspace_id: workspace,
                  board_id: board_exist?._id,
                };

                if (!include_sub_tasks) {
                  query.parent_task = { $exists: false };
                }

                const tasks = await Task.find(query).lean();

                // tasks.map(async (task) => {
                for (const task of tasks) {
                  if (include_sub_tasks && task?.parent_task) {
                    // Sub task

                    // Check the parent task exit in new board
                    let parentTaskExitInNewBoard = await Task.findOne({
                      is_deleted: false,
                      workspace_id: workspace,
                      board_id: new_board?._id,
                      duplicated_from: task?.parent_task,
                    });

                    // Parent task not exist in new board
                    if (!parentTaskExitInNewBoard) {
                      // Find Section in old board
                      const oldBoardSectionData = await Task.findOne({
                        is_deleted: false,
                        workspace_id: workspace,
                        board_id: board_exist?._id,
                        _id: task?.parent_task,
                      }).populate("activity_status");

                      // Check the section in exist in new board
                      let newBoardSectionData = await Section.findOne({
                        board_id: new_board?._id,
                        workspace_id: new_board?.workspace_id,
                        is_deleted: false,
                        section_name:
                          oldBoardSectionData?.activity_status?.section_name,
                        sort_order:
                          oldBoardSectionData?.activity_status?.sort_order,
                      }).lean();

                      // Create section if not exist
                      if (!newBoardSectionData) {
                        newBoardSectionData = (
                          await Section.create({
                            ...oldBoardSectionData?.activity_status,
                            board_id: new_board?._id,
                            workspace_id: new_board?.workspace_id,
                          })
                        ).toJSON();
                      }

                      // Create Parent task in new board
                      const parent_task_data = await Task.findOne({
                        is_deleted: false,
                        workspace_id: workspace,
                        board_id: board_exist?._id,
                        _id: task?.parent_task,
                      }).lean();

                      const parent_task_data_obj = { ...parent_task_data };
                      delete parent_task_data_obj?._id;
                      parent_task_data_obj.status = newBoardSectionData?._id;
                      parent_task_data_obj.workspace_id = workspace;
                      parent_task_data_obj.board_id = new_board?._id;
                      parent_task_data_obj.assign_by = user?._id;
                      parent_task_data_obj.agency_id =
                        user?.workspace_detail?.created_by;
                      parent_task_data_obj.status_history = [
                        {
                          status: newBoardSectionData?._id,
                          updated_by: user?._id,
                        },
                      ];
                      parent_task_data_obj.custom_fields =
                        parent_task_data_obj?.custom_fields
                          ? JSON.stringify(parent_task_data_obj?.custom_fields)
                          : [];
                      parent_task_data_obj.tags = parent_task_data_obj.tags
                        ? parent_task_data_obj.tags
                        : [];

                      parent_task_data_obj.duplicated_from =
                        parent_task_data?._id;
                      delete parent_task_data_obj?._id;
                      delete parent_task_data_obj?.comments;
                      if (!include_task_attachments)
                        delete parent_task_data_obj?.attachments;
                      if (!include_task_assignees) {
                        delete parent_task_data_obj?.assign_to;
                      } else {
                        parent_task_data_obj.assign_to =
                          parent_task_data_obj?.assign_to?.filter((assigned) =>
                            workspaces[workspace]?.includes(
                              assigned?.toString()
                            )
                          );
                      }

                      const createTask = await taskService.createTask(
                        parent_task_data_obj,
                        user,
                        null,
                        true
                      );

                      // Create Sub task
                      const sub_task_data_obj = { ...task };
                      delete sub_task_data_obj?._id;
                      delete sub_task_data_obj?.comments;

                      sub_task_data_obj.status = new_section?._id;
                      sub_task_data_obj.workspace_id = workspace;
                      sub_task_data_obj.board_id = new_board?._id;
                      sub_task_data_obj.assign_by = user?._id;
                      sub_task_data_obj.agency_id =
                        user?.workspace_detail?.created_by;
                      sub_task_data_obj.status_history = [
                        { status: new_section?._id, updated_by: user?._id },
                      ];
                      sub_task_data_obj.custom_fields =
                        sub_task_data_obj?.custom_fields
                          ? JSON.stringify(sub_task_data_obj?.custom_fields)
                          : [];
                      sub_task_data_obj.tags = sub_task_data_obj.tags
                        ? sub_task_data_obj.tags
                        : [];
                      sub_task_data_obj.parent_task = createTask?._id;
                      sub_task_data_obj.duplicated_from = task?._id;
                      if (!include_sub_tasks_attachments)
                        delete sub_task_data_obj?.attachments;
                      if (!include_sub_tasks_assignees)
                        delete sub_task_data_obj?.assign_to;
                      else {
                        sub_task_data_obj.assign_to =
                          sub_task_data_obj?.assign_to?.filter((assigned) =>
                            workspaces[workspace]?.includes(
                              assigned?.toString()
                            )
                          );
                      }

                      await taskService.createTask(
                        sub_task_data_obj,
                        user,
                        null,
                        true
                      );
                    } else {
                      // Create Sub Task if parent task already exist in new board
                      const task_obj = { ...task };
                      delete task_obj?._id;
                      delete task_obj?.comments;

                      task_obj.status = new_section?._id;
                      task_obj.workspace_id = workspace;
                      task_obj.board_id = new_board?._id;
                      task_obj.assign_by = user?._id;
                      task_obj.agency_id = user?.workspace_detail?.created_by;
                      task_obj.status_history = [
                        { status: new_section?._id, updated_by: user?._id },
                      ];
                      task_obj.custom_fields = task_obj?.custom_fields
                        ? JSON.stringify(task_obj?.custom_fields)
                        : [];
                      task_obj.tags = task_obj.tags ? task_obj.tags : [];
                      task_obj.parent_task = parentTaskExitInNewBoard?._id;
                      task_obj.duplicated_from = task?._id;
                      if (!include_sub_tasks_attachments)
                        delete task_obj?.attachments;
                      if (!include_sub_tasks_assignees)
                        delete task_obj?.assign_to;
                      else {
                        task_obj.assign_to = task_obj?.assign_to?.filter(
                          (assigned) =>
                            workspaces[workspace]?.includes(
                              assigned?.toString()
                            )
                        );
                      }

                      await taskService.createTask(task_obj, user, null, true);
                    }
                  } else {
                    let parentTaskExitInNewBoard = await Task.findOne({
                      is_deleted: false,
                      workspace_id: workspace,
                      board_id: new_board?._id,
                      duplicated_from: task?._id,
                    });

                    if (!parentTaskExitInNewBoard) {
                      // Parent task

                      const task_obj = { ...task };
                      delete task_obj?._id;
                      task_obj.status = new_section?._id;
                      task_obj.workspace_id = workspace;
                      task_obj.board_id = new_board?._id;
                      task_obj.assign_by = user?._id;
                      task_obj.agency_id = user?.workspace_detail?.created_by;
                      task_obj.status_history = [
                        { status: new_section?._id, updated_by: user?._id },
                      ];
                      task_obj.custom_fields = JSON.stringify(
                        task_obj?.custom_fields
                      );
                      task_obj.tags = task_obj.tags ? task_obj.tags : [];
                      delete task_obj?.comments;

                      task_obj.duplicated_from = task?._id;

                      if (!include_task_attachments)
                        delete task_obj?.attachments;

                      if (!include_task_assignees) delete task_obj?.assign_to;
                      else {
                        task_obj.assign_to = task_obj?.assign_to?.filter(
                          (assigned) =>
                            workspaces[workspace]?.includes(
                              assigned?.toString()
                            )
                        );
                      }

                      await taskService.createTask(task_obj, user, null, true);
                    }
                  }
                }
                // });
              }
            }
          });
        });
      });
    } catch (error) {
      logger.error(`Error while duplicating the boards: ${error}`);
      return throwError(error?.message, error?.statusCode);
    }
  };

  this is the code where the boards will duplicate accross the workspace without having the boards order
   duplicateBoards = async (payload, user) => {
    try {
      //this will use to provide the options like to duplicate the members, attachements and tasks
      const {
        board_ids,
        workspace_ids,
        include_tasks,
        include_task_attachments,
        include_task_assignees,
        include_board_assignees,
        include_sub_tasks,
        include_sub_tasks_attachments,
        include_sub_tasks_assignees,
      } = payload;

      let [workspaces, status_data, configuration] = await Promise.all([
        Workspace.find({
          is_deleted: false,
          _id: { $in: workspace_ids },
        }).lean(),
        Board_Status_Master.findOne({
          name: "active",
        }).lean(),
        Configuration.findOne({}).lean(),
      ]);

      workspaces = workspaces?.reduce((acc, workspace) => {
        if (!acc[workspace?._id?.toString()])
          acc[workspace?._id?.toString()] = workspace?.members
            ?.filter((member) => member?.status === "confirmed")
            ?.map((member) => member?.user_id?.toString());

        return acc;
      }, {});

      board_ids?.map(async (board_id) => {
        const board_exist = await Board.findById(board_id)
          .where("is_deleted")
          .equals(false)
          .lean();

        if (!board_exist)
          return throwError(returnMessage("board", "boardNotFound"), 404);

        workspace_ids?.map(async (workspace) => {
          const [boards_count, sections] = await Promise.all([
            Board.countDocuments({
              project_name: {
                $regex: board_exist?.project_name,
                $options: "i",
              },
              workspace_id: workspace,
              is_deleted: false,
            }),
            Section.find({
              workspace_id: workspace,
              board_id: board_exist?._id,
              is_deleted: false,
            }).lean(),
          ]);

          const update_board = {
            project_name:
              lowercaseFirstLetter(board_exist?.project_name?.trim()) +
              " " +
              (boards_count + 1),
            description: board_exist?.description,
            workspace_id: workspace,
            created_by: user?._id,
            board_color: getRandomHexColor(boardColor),
            agency_id: user?.workspace_detail?.created_by,
            board_status: status_data?._id,
          };

          if (include_board_assignees) {
            const workspace_members = workspaces[workspace]; // if the member is not exist in the another workspace
            update_board.members = board_exist?.members?.filter((member) =>
              workspace_members?.includes(member?.member_id?.toString())
            );
          }

          if (board_exist?.board_image && board_exist?.board_image !== "") {
            const board_image = duplicateFiles([
              { preview: board_exist?.board_image },
            ]);

            if (board_image?.length > 0) {
              update_board.board_image = board_image?.[0]?.preview;
            }
          }

          // Save Board
          const new_board = (await Board.create(update_board)).toJSON();

          // ------------- Notifications -------------
          notificationService.addNotification(
            {
              module_name: "board",
              action_name: "created",
              workspace_id: workspace,
              members: new_board?.members?.filter((item) => item !== user?._id),
              project_name: lowercaseFirstLetter(new_board?.project_name),
              created_by_name:
                capitalizeFirstLetter(user?.first_name) +
                " " +
                capitalizeFirstLetter(user?.last_name),
            },
            new_board?._id
          );

          const member_send_mail = new_board?.members?.filter(
            (item) => item !== user?._id
          );

          const link = `${process.env.REACT_APP_URL}/${workspaceNameReplace(
            user?.workspace_detail?.name
          )}/tasks/board/${new_board?._id}`;
          const board_template = boardTemplate({
            project_name: new_board?.project_name,
            description: new_board?.description,
            added_by:
              capitalizeFirstLetter(user?.first_name) +
              " " +
              capitalizeFirstLetter(user?.last_name),
            REACT_APP_URL: process.env.REACT_APP_URL,
            SERVER_URL: process.env.SERVER_URL,
            link: link,
            instagram: configuration?.urls?.instagram,
            facebook: configuration?.urls?.facebook,
            privacy_policy: configuration?.urls?.privacy_policy,
            year: new Date().getFullYear(),
          });

          member_send_mail.map(async (member) => {
            const member_data = await Authentication.findById(member?.member_id)
              .select("email")
              .lean();
            sendEmail({
              email: member_data?.email,
              subject: returnMessage("emailTemplate", "memberAddedBoard"),
              message: board_template,
            });
          });
          // ------------- Notifications -------------

          this.boardDataGenerator(
            new_board?._id,
            user,
            user?.workspace_detail,
            "new_board"
          );

          // duplicate the sections
          sections.map(async (section) => {
            const section_obj = { ...section };
            delete section_obj?._id;
            section_obj.board_id = new_board?._id;
            section.workspace_id = new_board?.workspace_id;

            const is_section_already_exits = await Section.findOne({
              board_id: new_board?._id,
              workspace_id: new_board?.workspace_id,
              is_deleted: false,
              section_name: section?.section_name,
              sort_order: section?.sort_order,
            });

            if (!section?.is_deletable) {
              Section.create(section_obj);
            } else {
              let new_section = is_section_already_exits;
              if (!is_section_already_exits) {
                new_section = (await Section.create(section_obj)).toJSON();
              }

              if (include_tasks) {
                const query = {
                  is_deleted: false,
                  activity_status: section?._id,
                  workspace_id: workspace,
                  board_id: board_exist?._id,
                };

                if (!include_sub_tasks) {
                  query.parent_task = { $exists: false };
                }

                const tasks = await Task.find(query).lean();

                // tasks.map(async (task) => {
                for (const task of tasks) {
                  if (include_sub_tasks && task?.parent_task) {
                    // Sub task

                    // Check the parent task exit in new board
                    let parentTaskExitInNewBoard = await Task.findOne({
                      is_deleted: false,
                      workspace_id: workspace,
                      board_id: new_board?._id,
                      duplicated_from: task?.parent_task,
                    });

                    // Parent task not exist in new board
                    if (!parentTaskExitInNewBoard) {
                      // Find Section in old board
                      const oldBoardSectionData = await Task.findOne({
                        is_deleted: false,
                        workspace_id: workspace,
                        board_id: board_exist?._id,
                        _id: task?.parent_task,
                      }).populate("activity_status");

                      // Check the section in exist in new board
                      let newBoardSectionData = await Section.findOne({
                        board_id: new_board?._id,
                        workspace_id: new_board?.workspace_id,
                        is_deleted: false,
                        section_name:
                          oldBoardSectionData?.activity_status?.section_name,
                        sort_order:
                          oldBoardSectionData?.activity_status?.sort_order,
                      }).lean();

                      // Create section if not exist
                      if (!newBoardSectionData) {
                        newBoardSectionData = (
                          await Section.create({
                            ...oldBoardSectionData?.activity_status,
                            board_id: new_board?._id,
                            workspace_id: new_board?.workspace_id,
                          })
                        ).toJSON();
                      }

                      // Create Parent task in new board
                      const parent_task_data = await Task.findOne({
                        is_deleted: false,
                        workspace_id: workspace,
                        board_id: board_exist?._id,
                        _id: task?.parent_task,
                      }).lean();

                      const parent_task_data_obj = { ...parent_task_data };
                      delete parent_task_data_obj?._id;
                      parent_task_data_obj.status = newBoardSectionData?._id;
                      parent_task_data_obj.workspace_id = workspace;
                      parent_task_data_obj.board_id = new_board?._id;
                      parent_task_data_obj.assign_by = user?._id;
                      parent_task_data_obj.agency_id =
                        user?.workspace_detail?.created_by;
                      parent_task_data_obj.status_history = [
                        {
                          status: newBoardSectionData?._id,
                          updated_by: user?._id,
                        },
                      ];
                      parent_task_data_obj.custom_fields =
                        parent_task_data_obj?.custom_fields
                          ? JSON.stringify(parent_task_data_obj?.custom_fields)
                          : [];
                      parent_task_data_obj.tags = parent_task_data_obj.tags
                        ? parent_task_data_obj.tags
                        : [];

                      parent_task_data_obj.duplicated_from =
                        parent_task_data?._id;
                      delete parent_task_data_obj?._id;
                      delete parent_task_data_obj?.comments;
                      if (!include_task_attachments)
                        delete parent_task_data_obj?.attachments;
                      if (!include_task_assignees) {
                        delete parent_task_data_obj?.assign_to;
                      } else {
                        parent_task_data_obj.assign_to =
                          parent_task_data_obj?.assign_to?.filter((assigned) =>
                            workspaces[workspace]?.includes(
                              assigned?.toString()
                            )
                          );
                      }

                      const createTask = await taskService.createTask(
                        parent_task_data_obj,
                        user,
                        null,
                        true
                      );

                      // Create Sub task
                      const sub_task_data_obj = { ...task };
                      delete sub_task_data_obj?._id;
                      delete sub_task_data_obj?.comments;

                      sub_task_data_obj.status = new_section?._id;
                      sub_task_data_obj.workspace_id = workspace;
                      sub_task_data_obj.board_id = new_board?._id;
                      sub_task_data_obj.assign_by = user?._id;
                      sub_task_data_obj.agency_id =
                        user?.workspace_detail?.created_by;
                      sub_task_data_obj.status_history = [
                        { status: new_section?._id, updated_by: user?._id },
                      ];
                      sub_task_data_obj.custom_fields =
                        sub_task_data_obj?.custom_fields
                          ? JSON.stringify(sub_task_data_obj?.custom_fields)
                          : [];
                      sub_task_data_obj.tags = sub_task_data_obj.tags
                        ? sub_task_data_obj.tags
                        : [];
                      sub_task_data_obj.parent_task = createTask?._id;
                      sub_task_data_obj.duplicated_from = task?._id;
                      if (!include_sub_tasks_attachments)
                        delete sub_task_data_obj?.attachments;
                      if (!include_sub_tasks_assignees)
                        delete sub_task_data_obj?.assign_to;
                      else {
                        sub_task_data_obj.assign_to =
                          sub_task_data_obj?.assign_to?.filter((assigned) =>
                            workspaces[workspace]?.includes(
                              assigned?.toString()
                            )
                          );
                      }

                      await taskService.createTask(
                        sub_task_data_obj,
                        user,
                        null,
                        true
                      );
                    } else {
                      // Create Sub Task if parent task already exist in new board
                      const task_obj = { ...task };
                      delete task_obj?._id;
                      delete task_obj?.comments;

                      task_obj.status = new_section?._id;
                      task_obj.workspace_id = workspace;
                      task_obj.board_id = new_board?._id;
                      task_obj.assign_by = user?._id;
                      task_obj.agency_id = user?.workspace_detail?.created_by;
                      task_obj.status_history = [
                        { status: new_section?._id, updated_by: user?._id },
                      ];
                      task_obj.custom_fields = task_obj?.custom_fields
                        ? JSON.stringify(task_obj?.custom_fields)
                        : [];
                      task_obj.tags = task_obj.tags ? task_obj.tags : [];
                      task_obj.parent_task = parentTaskExitInNewBoard?._id;
                      task_obj.duplicated_from = task?._id;
                      if (!include_sub_tasks_attachments)
                        delete task_obj?.attachments;
                      if (!include_sub_tasks_assignees)
                        delete task_obj?.assign_to;
                      else {
                        task_obj.assign_to = task_obj?.assign_to?.filter(
                          (assigned) =>
                            workspaces[workspace]?.includes(
                              assigned?.toString()
                            )
                        );
                      }

                      await taskService.createTask(task_obj, user, null, true);
                    }
                  } else {
                    let parentTaskExitInNewBoard = await Task.findOne({
                      is_deleted: false,
                      workspace_id: workspace,
                      board_id: new_board?._id,
                      duplicated_from: task?._id,
                    });

                    if (!parentTaskExitInNewBoard) {
                      // Parent task

                      const task_obj = { ...task };
                      delete task_obj?._id;
                      task_obj.status = new_section?._id;
                      task_obj.workspace_id = workspace;
                      task_obj.board_id = new_board?._id;
                      task_obj.assign_by = user?._id;
                      task_obj.agency_id = user?.workspace_detail?.created_by;
                      task_obj.status_history = [
                        { status: new_section?._id, updated_by: user?._id },
                      ];
                      task_obj.custom_fields = JSON.stringify(
                        task_obj?.custom_fields
                      );
                      task_obj.tags = task_obj.tags ? task_obj.tags : [];
                      delete task_obj?.comments;

                      task_obj.duplicated_from = task?._id;

                      if (!include_task_attachments)
                        delete task_obj?.attachments;

                      if (!include_task_assignees) delete task_obj?.assign_to;
                      else {
                        task_obj.assign_to = task_obj?.assign_to?.filter(
                          (assigned) =>
                            workspaces[workspace]?.includes(
                              assigned?.toString()
                            )
                        );
                      }

                      await taskService.createTask(task_obj, user, null, true);
                    }
                  }
                }
                // });
              }
            }
          });
        });
      });
    } catch (error) {
      logger.error(`Error while duplicating the boards: ${error}`);
      return throwError(error?.message, error?.statusCode);
    }
  };
 -->
