Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Context } from '../../../utils';
  2. import { ACTION_LOGGER } from './../../logger';
  3. import { pubsub } from '..';
  4. import { COMMENT_ADDED, COMMENT_REMOVED, COMMENT_UPDATED } from './../Subscription/comment';
  5. import { NotificationTypeEnum } from '../../../entity/Notification';
  6. import { NOTIFICATION_ADDED } from '../Subscription/notification';
  7.  
  8. export default {
  9.   /**
  10.    * Adds new comment.
  11.    *
  12.    * @param _ Parent.
  13.    * @param params The parameters.
  14.    * @param context The context.
  15.    */
  16.   async addComment(_, { action_id, content, attachments }, context: Context) {
  17.     try {
  18.       ACTION_LOGGER.log({
  19.         level: 'info',
  20.         message: `Add new comment ${action_id}, user=${context.loggedUser.email}`,
  21.       });
  22.       const user = await context.user.findOne(context.userLogin);
  23.       const action = await context.action.findOne(action_id, { relations: ['creator'] });
  24.       if (!action) {
  25.         throw new Error('ACTION_NOT_FOUND');
  26.       }
  27.  
  28.       const addedComment = await context.comment.addComment(user, action, content);
  29.  
  30.       if (attachments) {
  31.         await Promise.all(
  32.           attachments.map((attachment: any) =>
  33.             context.attachment.addAttachment(
  34.               context.loggedUser,
  35.               addedComment,
  36.               attachment.size,
  37.               attachment.type,
  38.               attachment.path,
  39.               attachment.name,
  40.             ),
  41.           ),
  42.         );
  43.       }
  44.  
  45.       pubsub.publish(COMMENT_ADDED, { commentAdded: addedComment });
  46.  
  47.  
  48.       action.assignments.map(async assignment => {
  49.         const notification = await context.notification.addNotification({
  50.           from: context.loggedUser,
  51.           to: assignment.to,
  52.           type: NotificationTypeEnum.CREATE_COMMENT,
  53.           content: action.id.toString(),
  54.           message: `Comment updated on action: "${action.description}"`,
  55.         });
  56.         pubsub.publish(NOTIFICATION_ADDED, { notificationAdded: notification });
  57.       });
  58.  
  59.  
  60.       // add a new Notification when a new Comment is added
  61.       if (action.creator.id !== context.loggedUser.id) {
  62.         const notification = await context.notification.addNotification({
  63.           from: context.loggedUser,
  64.           to: action.creator,
  65.           type: NotificationTypeEnum.CREATE_COMMENT,
  66.           content: action.id.toString(),
  67.           message: `Comment added on action: "${action.description}"`,
  68.         });
  69.         pubsub.publish(NOTIFICATION_ADDED, { notificationAdded: notification });
  70.       }
  71.       return addedComment;
  72.     } catch (error) {
  73.       ACTION_LOGGER.log({
  74.         level: 'error',
  75.         message: `Add comment error= ${error.message}, action_id=${action_id}, user=${
  76.           context.loggedUser.email
  77.           }`,
  78.       });
  79.       return error;
  80.     }
  81.   },
  82.  
  83.   /**
  84.    * Removes a comment.
  85.    *
  86.    * @param _ Parent.
  87.    * @param params The parameters.
  88.    * @param context The context.
  89.    */
  90.   async removeComment(_, { id }, context: Context) {
  91.     try {
  92.       ACTION_LOGGER.log({ level: 'info', message: `Remove comment id=${id}` });
  93.       const comment = await context.comment.findOne(id, {
  94.         relations: ['action', 'action.creator', 'user'],
  95.       });
  96.       if (!comment) {
  97.         throw new Error('COMMENT_NOT_FOUND');
  98.       }
  99.  
  100.       comment.removed = 1;
  101.       await context.comment.save(comment);
  102.  
  103.       pubsub.publish(COMMENT_REMOVED, { commentRemoved: comment });
  104.  
  105.       comment.action.assignments.map(async assignment => {
  106.         const notification = await context.notification.addNotification({
  107.           from: context.loggedUser,
  108.           to: assignment.to,
  109.           type: NotificationTypeEnum.REMOVE_COMMENT,
  110.           content: comment.action.id.toString(),
  111.           message: `Comment updated on action: "${comment.action.description}"`,
  112.         });
  113.         pubsub.publish(NOTIFICATION_ADDED, { notificationAdded: notification });
  114.       });
  115.  
  116.       // add a new Notification when a new Comment is removed
  117.       if (comment.action.creator.id !== context.loggedUser.id) {
  118.         const notification = await context.notification.addNotification({
  119.           from: context.loggedUser,
  120.           to: comment.action.creator,
  121.           type: NotificationTypeEnum.REMOVE_COMMENT,
  122.           content: comment.action.id.toString(),
  123.           message: `Comment removed on action: "${comment.action.description}"`,
  124.         });
  125.         pubsub.publish(NOTIFICATION_ADDED, { notificationAdded: notification });
  126.       }
  127.  
  128.       return comment;
  129.     } catch (error) {
  130.       ACTION_LOGGER.log({
  131.         level: 'error',
  132.         message: `Remove comment error=${error.message}, id=${id}`,
  133.       });
  134.       return error;
  135.     }
  136.   },
  137.  
  138.   /**
  139.    * Update a comment.
  140.    *
  141.    * @param _ Parent.
  142.    * @param params The parameters.
  143.    * @param context The context.
  144.    */
  145.   async updateComment(_, { id, content }, context: Context) {
  146.     try {
  147.       ACTION_LOGGER.info({ level: 'info', message: `Update comment id=${id}` });
  148.       const commentUpdated = await context.comment.editComment(context.userLogin, id, content);
  149.       const comment = await context.comment.findOne(id, {
  150.         relations: ['user', 'action', 'action.creator'],
  151.       });
  152.       pubsub.publish(COMMENT_UPDATED, { commentUpdated });
  153.  
  154.       comment.action.assignments.map(async assignment => {
  155.         const notification = await context.notification.addNotification({
  156.           from: context.loggedUser,
  157.           to: assignment.to,
  158.           type: NotificationTypeEnum.UPDATE_COMMENT,
  159.           content: comment.action.id.toString(),
  160.           message: `Comment updated on action: "${comment.action.description}"`,
  161.         });
  162.         pubsub.publish(NOTIFICATION_ADDED, { notificationAdded: notification });
  163.       });
  164.  
  165.       // add a new Notification when a Comment is updated
  166.       if (comment.action.creator.id !== context.loggedUser.id) {
  167.         const notification = await context.notification.addNotification({
  168.           from: context.loggedUser,
  169.           to: comment.action.creator,
  170.           type: NotificationTypeEnum.UPDATE_COMMENT,
  171.           content: comment.action.id.toString(),
  172.           message: `Comment updated on action: "${comment.action.description}"`,
  173.         });
  174.         pubsub.publish(NOTIFICATION_ADDED, { notificationAdded: notification });
  175.       }
  176.  
  177.       return commentUpdated;
  178.     } catch (error) {
  179.       ACTION_LOGGER.log({
  180.         level: 'error',
  181.         message: `Update comment error=${error.message}, id=${id}`,
  182.       });
  183.       return error;
  184.     }
  185.   },
  186.  
  187.   /**
  188.    * Update a comment to add user that seen it.
  189.    *
  190.    * @param _ Parent.
  191.    * @param params The parameters.
  192.    * @param context The context.
  193.    */
  194.   async updateAllCommentToSeen(_, { actionId }, context: Context) {
  195.     try {
  196.       ACTION_LOGGER.info({
  197.         level: 'info',
  198.         message: `Update action's comment as seen id=${actionId}`,
  199.      });
  200.      const actionUpdated = await context.comment.updateAllCommentToSeen(
  201.        context.userLogin,
  202.        actionId,
  203.        context,
  204.      );
  205.      return actionUpdated;
  206.    } catch (error) {
  207.      ACTION_LOGGER.log({
  208.        level: 'error',
  209.        message: `Update comment error=${error.message}, id=${actionId}`,
  210.      });
  211.      return error;
  212.    }
  213.  },
  214. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement