Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function preloadMessagesForMonth(loadToDate = '2025.01.01') {
- const toTime = new Date(loadToDate).getTime();
- let currentTime = new Date().getTime();
- let blocks = [];
- const companyId = B.leftmenu.conversations.info.getActiveCompanyId();
- const conversation_key = B.conversation.info.getActiveConversationKey();
- const paymentByMonth = {};
- while (currentTime > toTime) {
- const { message_list, previous_block_id_list } =
- await B.messages.loader.actions.getConversationMessagesFromBatching(companyId, {
- conversation_key,
- block_id_list: blocks,
- });
- blocks = previous_block_id_list;
- if (!message_list.length) {
- continue;
- }
- currentTime = DATE.getDayStart(message_list[0].created_at) * 1000;
- const messagesWithPayment = message_list.filter((message) => {
- const isText = message.type === 'text';
- const isContainPayment = message.text.includes('оплата');
- return isText && isContainPayment;
- });
- messagesWithPayment.forEach((message) => {
- const month = new Intl.DateTimeFormat('ru-RU', {
- year: 'numeric',
- month: 'numeric'
- }).format(
- new Date(DATE.getDayStart(message.created_at) * 1000),
- );
- let currentPayment = paymentByMonth[month] ?? 0;
- let newPayment = message.text.match(/-?\s*\*?[\d\s.]{3,}\*?\s*(₽|Р|руб)?\b/ig);
- if (!newPayment) {
- console.log('НЕ смогли распарсит цены ', message.text);
- return;
- }
- const findFirstPayment = Array.from(newPayment).map((number) => {
- return number.replace(/\D/g, '');
- }).find((number) => number.length >= 4);
- if (!findFirstPayment) {
- console.log('НЕ смогли распарсит цены ', message.text);
- return;
- }
- newPayment = Number(findFirstPayment);
- paymentByMonth[month] = currentPayment + newPayment;
- });
- }
- console.table(paymentByMonth);
- const sum = Object.keys(paymentByMonth).reduce((acc, key) => acc + paymentByMonth[key], 0)
- console.log("Всего за этот промежуток: ~", sum);
- console.log("В среднем за месяц: ~", Math.ceil(sum / Object.keys(paymentByMonth).length));
- }
- preloadMessagesForMonth();
Advertisement
Add Comment
Please, Sign In to add comment