message.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 findByFieldsAndUpdateMany = async (companionId, oldId, userId, body) => {
  65. const messages = await Message.find({
  66. companionId,
  67. oldId,
  68. owner: userId,
  69. }).updateMany({ ...body });
  70. return messages;
  71. };
  72. const findByFields = async (companionId, idTime, userId) => {
  73. const message = await Message.findOne({
  74. companionId,
  75. idTime,
  76. owner: userId,
  77. });
  78. return message;
  79. };
  80. const removeAll = async (companionId, userId) => {
  81. const removedAllMessages = await Message.deleteMany({
  82. companionId,
  83. owner: userId,
  84. });
  85. return removedAllMessages;
  86. };
  87. const updateCompanionsMessages = async (companionId, obj) => {
  88. const messages = await Message.updateMany({ companionId }, { ...obj });
  89. return messages;
  90. };
  91. const updateOwnerMessages = async (options, obj) => {
  92. const messages = await Message.updateMany({ ...options }, { ...obj });
  93. return messages;
  94. };
  95. const updateMessage = async (id, userId, body) => {
  96. return await Message.findByIdAndUpdate(
  97. { _id: id, owner: userId },
  98. { ...body },
  99. { new: true }
  100. );
  101. };
  102. module.exports = {
  103. getList,
  104. getByField,
  105. getById,
  106. add,
  107. remove,
  108. removeByFields,
  109. findByFieldsAndUpdate,
  110. findByFieldsAndUpdateMany,
  111. findByFields,
  112. removeAll,
  113. updateCompanionsMessages,
  114. updateOwnerMessages,
  115. updateMessage,
  116. };