saloev

Сколько в месяц зарабатываем

Apr 30th, 2025 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. async function preloadMessagesForMonth(loadToDate = '2025.01.01') {
  2. const toTime = new Date(loadToDate).getTime();
  3. let currentTime = new Date().getTime();
  4. let blocks = [];
  5. const companyId = B.leftmenu.conversations.info.getActiveCompanyId();
  6. const conversation_key = B.conversation.info.getActiveConversationKey();
  7. const paymentByMonth = {};
  8.  
  9. while (currentTime > toTime) {
  10. const { message_list, previous_block_id_list } =
  11. await B.messages.loader.actions.getConversationMessagesFromBatching(companyId, {
  12. conversation_key,
  13. block_id_list: blocks,
  14. });
  15. blocks = previous_block_id_list;
  16.  
  17. if (!message_list.length) {
  18. continue;
  19. }
  20.  
  21. currentTime = DATE.getDayStart(message_list[0].created_at) * 1000;
  22. const messagesWithPayment = message_list.filter((message) => {
  23. const isText = message.type === 'text';
  24. const isContainPayment = message.text.includes('оплата');
  25. return isText && isContainPayment;
  26. });
  27.  
  28. messagesWithPayment.forEach((message) => {
  29. const month = new Intl.DateTimeFormat('ru-RU', {
  30. year: 'numeric',
  31. month: 'numeric'
  32. }).format(
  33. new Date(DATE.getDayStart(message.created_at) * 1000),
  34. );
  35. let currentPayment = paymentByMonth[month] ?? 0;
  36. let newPayment = message.text.match(/-?\s*\*?[\d\s.]{3,}\*?\s*(₽|Р|руб)?\b/ig);
  37. if (!newPayment) {
  38. console.log('НЕ смогли распарсит цены ', message.text);
  39. return;
  40. }
  41. const findFirstPayment = Array.from(newPayment).map((number) => {
  42. return number.replace(/\D/g, '');
  43. }).find((number) => number.length >= 4);
  44. if (!findFirstPayment) {
  45. console.log('НЕ смогли распарсит цены ', message.text);
  46. return;
  47. }
  48. newPayment = Number(findFirstPayment);
  49. paymentByMonth[month] = currentPayment + newPayment;
  50. });
  51. }
  52. console.table(paymentByMonth);
  53. const sum = Object.keys(paymentByMonth).reduce((acc, key) => acc + paymentByMonth[key], 0)
  54. console.log("Всего за этот промежуток: ~", sum);
  55. console.log("В среднем за месяц: ~", Math.ceil(sum / Object.keys(paymentByMonth).length));
  56. }
  57.  
  58. preloadMessagesForMonth();
Advertisement
Add Comment
Please, Sign In to add comment