All files / src/controllers channel.controller.js

98.38% Statements 61/62
100% Branches 14/14
100% Functions 6/6
98.38% Lines 61/62

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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 1794x 4x 4x     4x 1x 1x   1x                       4x 2x 2x 2x 2x 1x     1x   1x               4x 3x 3x 3x 3x 1x   2x 2x 1x   1x           1x 1x 1x 1x 1x       2x                       4x 3x 3x   3x 3x 1x     2x 2x 1x     1x                 1x                 1x               2x                       4x 2x 2x   2x       2x 1x     1x       1x               4x 2x 2x 2x 2x 1x     1x       1x           4x                
const Channel = require('../models/channel.model');
const User = require('../models/user.model');
const WorkSpace = require('../models/workSpace.model');
 
// module.exports = {
const list = async (req, res) => {
  try {
    const channels = await Channel.find();
 
    res
      .status(200)
      .json({ ok: true, message: 'Channels found', data: channels });
  } catch (err) {
    res
      .status(404)
      .json({ ok: false, message: 'Channels not found', data: err });
  }
};
 
// GET ID - READ id
 
const show = async (req, res) => {
  try {
    const { channelId } = req.params;
    const channel = await Channel.findById(channelId);
    if (!channel) {
      throw new Error('Invalid channel');
    }
 
    res.status(200).json({ ok: true, message: 'Channel found', data: channel });
  } catch (err) {
    res
      .status(404)
      .json({ ok: false, message: 'Channel not found', data: err.message });
  }
};
 
// CREATE - POST
 
const create = async (req, res) => {
  try {
    const { userId, workSpaceId } = req.body;
    const user = await User.findById(userId);
    if (!user) {
      throw new Error('Invalid user');
    }
    const workSpace = await WorkSpace.findById(workSpaceId);
    if (!workSpace) {
      throw new Error('Invalid workspace');
    }
    const channel = await Channel.create({
      ...req.body,
      users: userId,
      workSpaceId,
    });
 
    workSpace.channels.push(channel);
    await workSpace.save({ validateBeforeSave: false });
    user.channels.push(channel);
    await user.save({ validateBeforeSave: false });
    res
      .status(200)
      .json({ ok: true, message: 'Channel created', data: channel });
  } catch (err) {
    res
      .status(404)
      .json({
        ok: false,
        message: 'channel could not be create',
        MsgError: err.message,
      });
  }
};
 
// PUT - EDIT - UPDATE - USER-CHANNEL
 
const update = async (req, res) => {
  try {
    const { channelId, userId } = req.body;
 
    const channel = await Channel.findById(channelId);
    if (!channel) {
      throw new Error('Invalid Channel');
    }
 
    const user = await User.findById(userId);
    if (!user) {
      throw new Error('Invalid user');
    }
 
    const newChannel = await Channel.findByIdAndUpdate(
      channelId,
      {
        ...req.body,
        users: channel.users.concat(userId),
      },
      { new: true }
    );
 
    await User.findByIdAndUpdate(
      userId,
      {
        ...req.body,
        channels: user.channels.concat(channelId),
      },
      { new: true }
    );
 
    res.status(200).json({
      ok: true,
      message: 'Channel updated',
      channelId: channelId,
      userId: userId,
      data: newChannel,
    });
  } catch (err) {
    res
      .status(404)
      .json({
        ok: false,
        message: 'Channel could not be updated',
        data: err.message,
      });
  }
};
 
// PUT - EDIT - UPDATE
 
const updateChannel = async (req, res) => {
  try {
    const { channelId } = req.params;
 
    const channel = await Channel.findByIdAndUpdate(channelId, req.body, {
      new: true,
    });
 
    if (!channel) {
      throw new Error('Invalid Channel');
    }
 
    res
      .status(200)
      .json({ ok: true, message: 'Channel updated', data: channel });
  } catch (err) {
    res
      .status(404)
      .json({ ok: false, message: 'Channel could not be updated', data: err.message });
  }
};
 
// DELETE DESTROY
 
const destroy = async (req, res) => {
  try {
    const { channelId } = req.params;
    const channel = await Channel.findByIdAndDelete(channelId);
    if (!channel) {
      throw new Error('Invalid channel');
    }
 
    res
      .status(200)
      .json({ ok: true, message: 'Channel deleted', data: channel });
  } catch (err) {
    res
      .status(404)
      .json({ ok: false, message: 'Channel could not be deleted', data: err.message });
  }
};
 
module.exports = {
  list,
  show,
  create,
  update,
  destroy,
  updateChannel,
};