All files / api index.js

0% Statements 0/64
0% Branches 0/22
0% Functions 0/17
0% Lines 0/41
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                                                                                                                                                                                                                                                                                                                                                                 
import db from './db';
import {
  postComment as postSlackComment,
  editComment as editSlackComment
} from './slack';
 
// Return all the comments for the particular component and story
// NOTE: Version is ignored for now, all comments are returned
export const getComments = (
  component,
  story /* , version = 'version_not_set' */
) =>
  db
    .find({
      selector: {
        $and: [
          { componentId: component },
          { stateId: story }
          // { version },
        ]
      },
      sort: [
        {
          _id: 'desc'
        }
      ]
    })
    .then(data => ({
      success: true,
      ...data
    }))
    .catch(() => ({
      success: false,
      msg: 'No comments available.'
    }));
 
export const postComment = ({
  timestampId,
  userName,
  userEmail,
  userComment,
  component,
  story,
  version,
  eventName
}) => {
  const record = {
    _id: timestampId,
    userName,
    userEmail,
    componentId: component,
    comment: userComment,
    timestamp: timestampId,
    stateId: story,
    version,
    edited: false,
    lastUpdated: timestampId,
    eventName
  };
 
  postSlackComment({
    userName,
    userEmail,
    comment: userComment,
    componentName: component,
    componentUrl: window.location.href
  });
 
  return db
    .put(record)
    .then(data => {
      if (data.ok) {
        return {
          success: data.ok,
          msg: 'Your comment was added successfully.',
          ...record
        };
      }
 
      return Promise.reject(new Error('Request for data was unsuccessful.'));
    })
    .catch(error => ({
      success: false,
      msg: `There was a problem posting your comment. Error: ${error.message}`
    }));
};
 
export const updateComment = ({
  commentId,
  component,
  userCommentText,
  userEmail,
  userName
}) => {
  const timestampId = `${new Date().getTime()}`;
  const userComment = userCommentText && userCommentText.trim();
 
  editSlackComment({
    userName,
    userEmail,
    comment: userComment,
    componentName: component,
    componentUrl: window.location.href
  });
 
  return db
    .find({
      selector: {
        _id: commentId
      }
    })
    .then(data => {
      const record = data.docs[0];
 
      if (userComment === '' || userComment === null) {
        return {
          success: false,
          msg: 'Cannot update with empty comment'
        };
      }
      record.comment = userComment;
      record.edited = true;
      record.lastUpdated = timestampId;
 
      return db
        .put(record)
        .then(() => ({
          success: true,
          msg: 'Your comment was edited successfully.'
        }))
        .catch(() => ({
          success: false,
          msg: 'There was an error editing your comment.'
        }));
    });
};
 
export const deleteComment = commentId =>
  db
    .find({
      selector: {
        _id: commentId
      }
    })
    .then(data => {
      if (data.docs && data.docs.length) {
        const record = data.docs[0];
        record._deleted = true;
        return db
          .put(record)
          .then(result => {
            if (result.ok) {
              return {
                success: true,
                msg: 'Your comment was removed successfully.'
              };
            }
            return {
              success: 0,
              msg: 'There was a problem deleting your comment.'
            };
          })
          .catch(error => ({
            success: 0,
            msg: `There was a problem deleting your comment. Error: ${error.message}`
          }));
      }
      return {
        success: 0,
        msg: 'There was a problem deleting your comment. Not found.'
      };
    })
    .catch(error => ({
      success: 0,
      msg: `There was a problem deleting your comment. Not found. Error: ${error.message}`
    }));