diff --git a/src/features/campaign/campaign.controller.ts b/src/features/campaign/campaign.controller.ts index d3c324690..6edde10a1 100644 --- a/src/features/campaign/campaign.controller.ts +++ b/src/features/campaign/campaign.controller.ts @@ -1,4 +1,13 @@ -import { Controller, Get, Req, ForbiddenException, Post, Body, Res, Param } from '@nestjs/common'; +import { + Controller, + Get, + Req, + ForbiddenException, + Post, + Body, + Res, + Param, +} from '@nestjs/common'; import { ApiHeader, ApiSecurity, ApiTags } from '@nestjs/swagger'; import { FlaskUserTypesEnum } from 'src/types/interfaces/interface'; import { isAuthenticated } from 'src/utils/auth'; @@ -15,7 +24,7 @@ import { ServerError } from 'src/filters/server-exception.filter'; }) @Controller('campaign') export class CampaignController { - constructor(private readonly campaignService: CampaignService) { } + constructor(private readonly campaignService: CampaignService) {} @Get('/all') getAvailableContributions(@Req() req: Request) { @@ -35,21 +44,16 @@ export class CampaignController { } @Get(':code') - async redirect( - @Res() res, - @Param('code') code: string, - ) { + async redirect(@Res() res, @Param('code') code: string) { const url = await this.campaignService.redirect(code); if (!url) { - throw new ServerError('Could not find the url', 500) + throw new ServerError('Could not find the url', 500); } - return res.redirect(url.longUrl) + return res.redirect(url.longUrl); } @Post('shorten') - shortenUrl( - @Body() longUrl: ShortenURLDto, - ) { + shortenUrl(@Body() longUrl: ShortenURLDto) { return this.campaignService.shortenUrl(longUrl); } } diff --git a/src/features/campaign/campaign.service.ts b/src/features/campaign/campaign.service.ts index 17bfcddd9..13e20cf82 100644 --- a/src/features/campaign/campaign.service.ts +++ b/src/features/campaign/campaign.service.ts @@ -11,10 +11,8 @@ import { ServerError } from 'src/filters/server-exception.filter'; import { ChildrenService } from '../children/children.service'; import { NeedService } from '../need/need.service'; import { - daysDifference, fetchCampaginCode as fetchCampaignCode, isUnpayable, - persianDay, persianMonthStringFarsi, prepareUrl, removeDuplicates, @@ -53,7 +51,7 @@ export class CampaignService { private familyService: FamilyService, private mailerService: MailerService, private childrenService: ChildrenService, - ) { } + ) {} private readonly logger = new Logger(CampaignService.name); smsApi = new MelipayamakApi(process.env.SMS_USER, process.env.SMS_PASSWORD); smsRest = this.smsApi.sms(); @@ -235,12 +233,7 @@ export class CampaignService { const emailReceivers = []; const smsReceivers = []; const flaskUsers = await this.userService.getFlaskUsers(); - const shuffledUsers = shuffleArray( - flaskUsers.filter( - (u) => u.userName === 'ehsan' || u.userName === 'mamad', - ), - ); - + const shuffledUsers = shuffleArray(flaskUsers); // 1- loop shuffled users let alreadyReceivedEmailCount = 0; @@ -264,7 +257,7 @@ export class CampaignService { const alreadyReceivedEmail = emailCampaign.receivers.find( (r) => r.flaskUserId === flaskUser.id, ); - if (!alreadyReceivedEmail) { + if (alreadyReceivedEmail) { alreadyReceivedEmailCount++; continue; } @@ -274,7 +267,7 @@ export class CampaignService { const alreadyReceivedSms = smsCampaign.receivers.find( (r) => r.flaskUserId === flaskUser.id, ); - if (!alreadyReceivedSms) { + if (alreadyReceivedSms) { alreadyReceivedSmsCount++; continue; } @@ -305,8 +298,9 @@ export class CampaignService { longUrl: `https://dapp.saydao.org/main/search?utm_source=monthly_campaign&utm_medium=${CampaignTypeEnum.SMS}&utm_campaign=${CampaignNameEnum.MONTHLY_CAMPAIGNS}&utm_id=${campaignSmsCode}`, }); - const text = `سلام ${flaskUser.firstName ? flaskUser.firstName : flaskUser.userName - }، شما در حال حاضر سرپرستی هیچ کودکی را ندارید، برای گسترش خانواده مجازی‌تان: ${shortNeedUrl} `; + const text = `سلام ${ + flaskUser.firstName ? flaskUser.firstName : flaskUser.userName + }، شما در حال حاضر سرپرستی هیچ کودکی را ندارید، برای گسترش خانواده مجازی‌تان: ${shortNeedUrl} `; await this.smsRest.send(to, from, text); } skippedUsersNoChildren++; @@ -317,8 +311,9 @@ export class CampaignService { // 5- loop shuffled children for await (const child of shuffleArray(userChildren)) { if (counter <= 3) { - const childUnpaidNeeds = - (await this.needService.getFlaskChildUnpaidNeeds(child.id)).filter(n => !isUnpayable(n)); + const childUnpaidNeeds = ( + await this.needService.getFlaskChildUnpaidNeeds(child.id) + ).filter((n) => !isUnpayable(n)); if (!childUnpaidNeeds || !childUnpaidNeeds[0]) { // we separately email social workers continue; @@ -387,9 +382,11 @@ export class CampaignService { longUrl: `https://dapp.saydao.org/child/${eligibleChildren[0].id}/needs/${eligibleChildren[0].unPaidNeeds[0].id}?utm_source=monthly_campaign&utm_medium=${CampaignTypeEnum.SMS}&utm_campaign=${CampaignNameEnum.MONTHLY_CAMPAIGNS}&utm_id=${campaignSmsCode}`, }); - const text = `سلام ${flaskUser.firstName ? flaskUser.firstName : flaskUser.userName - }،\n از آخرین نیازهای کودک شما، ${eligibleChildren[0].sayName - }: ${shortNeedUrl} لغو۱۱`; + const text = `سلام ${ + flaskUser.firstName ? flaskUser.firstName : flaskUser.userName + }،\n از آخرین نیازهای کودک شما، ${ + eligibleChildren[0].sayName + }: ${shortNeedUrl} لغو۱۱`; await this.smsRest.send(to, from, text); diff --git a/src/features/schedule/schedule.service.ts b/src/features/schedule/schedule.service.ts index 85b4bc843..de43a0c56 100644 --- a/src/features/schedule/schedule.service.ts +++ b/src/features/schedule/schedule.service.ts @@ -13,7 +13,7 @@ export class ScheduleService { private campaignService: CampaignService, private familyService: FamilyService, private analyticService: AnalyticService, - ) { } + ) {} private readonly logger = new Logger(ScheduleService.name); async completePays() { @@ -97,7 +97,9 @@ export class ScheduleService { timeZone: 'Asia/Tehran', }) async handleMonthlyCron() { - this.logger.debug('Active Families (One and Three months report) Called every Month'); + this.logger.debug( + 'Active Families (One and Three months report) Called every Month', + ); // how many amoos? ammes? this.rolesCount(); // active families @@ -124,21 +126,22 @@ export class ScheduleService { } } - // @Cron('53 13 * * Thu', { - // name: 'MonthlyCampaigns try At 13:00 on Wednesday.', // we try every week and only send to those who did not receive (because their child have no needs, etc.) - // timeZone: 'Asia/Tehran', - // }) - @Timeout(15000) + @Cron('40 14 * * Thu', { + name: 'MonthlyCampaigns try At 13:00 on Wednesday.', // we try every week and only send to those who did not receive (because their child have no needs, etc.) + timeZone: 'Asia/Tehran', + }) async handleSummaryMailCron() { - const farsiDay = persianDay(new Date()) + const farsiDay = persianDay(new Date()); if (farsiDay > 20) { - this.logger.warn(`We are near the end of this month let's skip one more week`); - return + this.logger.warn( + `We are near the end of this month let's skip one more week`, + ); + return; } - // if (process.env.NODE_ENV === 'production') { + if (process.env.NODE_ENV === 'production') { this.logger.debug('Sending user Campaigns at 02:00 PM, only on Sunday'); await this.campaignService.sendUserMonthlyCampaigns(); - // } + } } @Cron('30 8 * * Mon', {