message.js 2.7 KB

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