Untitled

 avatar
user_7626021
plain_text
5 months ago
1.0 kB
1
Indexable
const addRefToViewAllGroups2 = async (userId, projectBinId) => {
  const viewAllGroup = await UserProjectGroup.findOne({ user: userId });


  if (!viewAllGroup || !viewAllGroup.viewAllGroups) return;

  const viewAllGroups = Array.from(viewAllGroup.viewAllGroups).map(([key, value]) => ({
    [key]: value,
  }));

  console.log("viewAllGroups before",viewAllGroups)

  const id = `Ungroup_${projectBinId}`
  const newObj = { [id]: [] };
  viewAllGroups.unshift(newObj);

  console.log("viewAllGroups after",viewAllGroups)
  const resultObject = viewAllGroups.reduce((acc, obj) => {
    const key = Object.keys(obj)[0];
    acc[key] = obj[key];
    return acc;
  }, {});
  
  console.log("resultObject",resultObject)
  try {
    await UserProjectGroup.updateOne(
      { user: userId },
      { viewAllGroups: resultObject }
    );
  } catch (error) {
    console.log(error)
  }

  await addRefToViewAllGroups(userId, projectBinId, id)
  

  res.json({ viewAllGroup });
};
Leave a Comment