All files / src/controllers workSpace.controller.js

100% Statements 37/37
100% Branches 8/8
100% Functions 3/3
100% Lines 37/37

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 1004x 4x   4x 2x 2x   2x   2x 1x     1x   1x       1x           4x 2x 2x 2x   2x 1x     1x   1x 1x   1x   1x           4x 3x 3x 3x   3x 3x 1x     2x 2x 1x     1x                 1x                 1x               2x           4x          
const WorkSpace = require('../models/workSpace.model');
const User = require('../models/user.model');
 
const listWorkSpace = async (req, res) => {
  try {
    const { uid } = req
 
    const user = await User.findById(uid);
 
    if (!user) {
      throw new Error('User not found')
    }
 
    const workSpace = await WorkSpace.find();
 
    res
      .status(200)
      .json({ ok: true, message: 'Work Space found', data: workSpace });
  } catch (err) {
    res
      .status(404)
      .json({ ok: false, message: 'Work Space not found', data: err.message });
  }
};
 
const createWorkSpace = async (req, res) => {
  try {
    const { uid } = req
    const user = await User.findById(uid);
 
    if (!user) {
      throw new Error('User not found')
    }
 
    const workspace = await WorkSpace.create({ ...req.body, users: user._id });
 
    user.workSpaceId.push(workspace);
    await user.save({ validateBeforeSave: false });
 
    res.status(200).json({ ok: true, msg: 'WorkSpace created', workspace });
  } catch (err) {
    res
      .status(500)
      .json({ ok: false, msg: 'Error, workSpace could not be create', msgError: err.message });
  }
};
 
const addUser = async (req, res) => {
  try {
    const { uid } = req
    const { workspaceId } = req.body;
 
    const workspace = await WorkSpace.findById(workspaceId);
    if (!workspace) {
      throw new Error('Invalid workspace');
    }
 
    const user = await User.findById(uid);
    if (!user) {
      throw new Error('Invalid user');
    }
 
    const updateWorkSpace = await WorkSpace.findByIdAndUpdate(
      workspaceId,
      {
        ...req.body,
        users: workspace.users.concat(user._id),
      },
      { new: true }
    );
 
    await User.findByIdAndUpdate(
      user._id,
      {
        ...req.body,
        workSpaceId: user.workSpaceId.concat(workspaceId),
      },
      { new: true }
    );
 
    res.status(200).json({
      ok: true,
      message: 'WorkSpace updated',
      workspaceId: workspaceId,
      userId: user._id,
      data: updateWorkSpace,
    });
  } catch (err) {
    res
      .status(404)
      .json({ ok: false, message: 'WorkSpace could not be updated', data: err.message });
  }
};
 
module.exports = {
  listWorkSpace,
  createWorkSpace,
  addUser,
};