diff --git a/src/sale/sale.constant.ts b/src/sale/sale.constant.ts index 5f7017a..646272b 100644 --- a/src/sale/sale.constant.ts +++ b/src/sale/sale.constant.ts @@ -14,8 +14,9 @@ import { id } from 'date-fns/locale'; const CONF_LOCALE = { locale: id }; const CONF_TZ = 'Asia/Jakarta'; +const CONF_TIME_FORMAT = 'HH:mm z'; const CONF_DATE_FORMAT_SHORT = 'EEEE, dd MMMM yyyy'; -const CONF_DATE_FORMAT_LONG = 'EEEE, dd MMMM yyyy - HH:mm z'; +const CONF_DATE_FORMAT_LONG = `${CONF_DATE_FORMAT_SHORT} - ${CONF_TIME_FORMAT}`; const CONF_TZ_FORMAT = 'zzzz (OOO)'; const CONF_NOW = () => new Date(); @@ -50,6 +51,9 @@ switch (process.env.TELEGRAM_SALE_DAY) { const CONF_RESET_DAY_MINUS_1_WEEK = () => subWeeks(CONF_RESET_DAY(), 1); const CONF_SALE_DAY = () => subDays(CONF_RESET_DAY(), 1); +export const TODAY_TIME = () => + formatInTimeZone(CONF_NOW(), CONF_TZ, CONF_TIME_FORMAT, CONF_LOCALE); + export const TODAY_LONG = () => formatInTimeZone(CONF_NOW(), CONF_TZ, CONF_DATE_FORMAT_LONG, CONF_LOCALE); diff --git a/src/sale/sale.update.ts b/src/sale/sale.update.ts index de9899d..d53b38b 100644 --- a/src/sale/sale.update.ts +++ b/src/sale/sale.update.ts @@ -4,16 +4,15 @@ import { SaleService } from './sale.service'; import { Logger } from '@nestjs/common'; import { Cron, CronExpression, Interval } from '@nestjs/schedule'; import { - RESET_DAY, RESET_DAY_MINUS_1_WEEK, SALE_DAY, TODAY_LONG, TODAY_SHORT, + TODAY_TIME, } from './sale.constant'; import { Context, Telegraf } from 'telegraf'; import { ConfigService } from '@nestjs/config'; import { sendMessageToGroup } from './sale.common'; -import { subWeeks } from 'date-fns'; @Update() export class SaleUpdate { @@ -33,39 +32,55 @@ export class SaleUpdate { @Cron(CronExpression.EVERY_HOUR) async saleDayScheduler() { if (TODAY_SHORT() === SALE_DAY()) { - this.logger.log('Running sale day scheduler'); + this.logger.log(`Running sale day scheduler at ${TODAY_TIME()}`); const users = await this.saleService.getUsersWithScheduledSales(); - let message = `🔥 **Today Hot Sale**\n\n`; - message += `It's **${TODAY_LONG()}**. Want to join the sale? [Chat me!](tg://user?id=${this.configService.get('TELEGRAM_SALE_BOT_TOKEN')!.split(':')[0]})\n\n`; + let messages: string[] = []; + + let index: number = 0; + messages[index] = `🔥 **Today Hot Sale**\n\n`; + messages[index] += + `It's **${TODAY_LONG()}**. Want to join the sale? [Chat me!](tg://user?id=${this.configService.get('TELEGRAM_SALE_BOT_TOKEN')!.split(':')[0]})\n\n`; users.forEach((user) => { - message += `💰 [${user.first_name}](tg://user?id=${user.id}) ${user.phone ? `(\`${user.phone}\`)` : ``}\n`; + let new_message = `💰 [${user.first_name}](tg://user?id=${user.id}) ${user.phone ? `(\`${user.phone}\`)` : ``}\n`; user.posts.forEach((post, index) => { if (index + 1 !== user.posts.length) { - message += `├ ${post.post.replace(/\n/g, ' ')}\n`; + new_message += `├ ${post.post.replace(/\n/g, ' ')}\n`; } else { - message += `└ ${post.post.replace(/\n/g, ' ')}\n`; + new_message += `└ ${post.post.replace(/\n/g, ' ')}\n`; } }); - message += `\n`; + new_message += `\n`; + + // if previous and new message length is too long + if ((messages[index] + new_message).length >= 4000) { + // move to next array + index++; + // set empty string so it can be appended later + messages[index] = ''; + } + + messages[index] += new_message; }); - sendMessageToGroup( - this.bot.telegram, - this.configService.get('TELEGRAM_GROUP_ID')!, - message, - ); + messages.forEach((message) => { + sendMessageToGroup( + this.bot.telegram, + this.configService.get('TELEGRAM_GROUP_ID')!, + message, + ); + }); } } @Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT) async resetDayScheduler() { if (TODAY_SHORT() === RESET_DAY_MINUS_1_WEEK()) { - this.logger.log('Running reset day scheduler'); - // await this.saleService.disableAllEnabledPosts(); + this.logger.log(`Running reset day scheduler at ${TODAY_TIME()}`); + await this.saleService.disableAllEnabledPosts(); } } }