message.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const Message = require('./schemas/message');
  2. const getList = async (
  3. options,
  4. { sortBy, sortByDesc, filter, limit = '500', page = '1', sub }
  5. ) => {
  6. if (sub) options.subscription = { $all: [sub] };
  7. const results = await Message.paginate(options, {
  8. limit,
  9. page,
  10. sort: {
  11. ...(sortBy ? { [`${sortBy}`]: 1 } : {}),
  12. ...(sortByDesc ? { [`${sortByDesc}`]: -1 } : {}),
  13. },
  14. select: filter ? filter.split('|').join(' ') : '',
  15. populate: {
  16. path: 'owner',
  17. select: '_id',
  18. },
  19. });
  20. const { docs: messages, totalDocs: total } = results;
  21. return { total: total.toString(), limit, page, messages };
  22. };
  23. const getById = async (id, userId) => {
  24. const foundMessage = await Message.findById({
  25. _id: id,
  26. owner: userId,
  27. });
  28. return foundMessage;
  29. };
  30. const getByField = async (number, userId) => {
  31. const message = await Message.findOne({
  32. number,
  33. owner: userId,
  34. });
  35. return message;
  36. };
  37. const add = async (obj) => {
  38. const message = await Message.create(obj);
  39. return message;
  40. };
  41. const remove = async (id, userId) => {
  42. const removedMessage = await Message.findByIdAndRemove({
  43. _id: id,
  44. owner: userId,
  45. });
  46. return removedMessage;
  47. };
  48. const removeByFields = async (companionId, idTime, userId) => {
  49. const removedMessage = await Message.findOne({
  50. companionId,
  51. idTime,
  52. owner: userId,
  53. }).deleteOne();
  54. return removedMessage;
  55. };
  56. const findByFieldsAndUpdate = async (companionId, idTime, userId, body) => {
  57. const message = await Message.findOne({
  58. companionId,
  59. idTime,
  60. owner: userId,
  61. }).updateOne({ ...body });
  62. return message;
  63. };
  64. const removeAll = async (companionId, userId) => {
  65. const removedAllMessages = await Message.deleteMany({
  66. companionId,
  67. owner: userId,
  68. });
  69. return removedAllMessages;
  70. };
  71. const updateCompanionsMessages = async (companionId, obj) => {
  72. const messages = await Message.updateMany({ companionId }, { ...obj });
  73. return messages;
  74. };
  75. const updateOwnerMessages = async (options, obj) => {
  76. const messages = await Message.updateMany({ ...options }, { ...obj });
  77. return messages;
  78. };
  79. const updateMessage = async (id, userId, body) => {
  80. return await Message.findByIdAndUpdate(
  81. { _id: id, owner: userId },
  82. { ...body },
  83. { new: true }
  84. );
  85. };
  86. module.exports = {
  87. getList,
  88. getByField,
  89. getById,
  90. add,
  91. remove,
  92. removeByFields,
  93. findByFieldsAndUpdate,
  94. removeAll,
  95. updateCompanionsMessages,
  96. updateOwnerMessages,
  97. updateMessage,
  98. };