Skip to content

Commit

Permalink
campaign sms url check
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan-g committed Nov 23, 2023
1 parent cf90b38 commit c318c18
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
26 changes: 15 additions & 11 deletions src/features/campaign/campaign.controller.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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) {
Expand All @@ -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);
}
}
33 changes: 15 additions & 18 deletions src/features/campaign/campaign.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -264,7 +257,7 @@ export class CampaignService {
const alreadyReceivedEmail = emailCampaign.receivers.find(
(r) => r.flaskUserId === flaskUser.id,
);
if (!alreadyReceivedEmail) {
if (alreadyReceivedEmail) {
alreadyReceivedEmailCount++;
continue;
}
Expand All @@ -274,7 +267,7 @@ export class CampaignService {
const alreadyReceivedSms = smsCampaign.receivers.find(
(r) => r.flaskUserId === flaskUser.id,
);
if (!alreadyReceivedSms) {
if (alreadyReceivedSms) {
alreadyReceivedSmsCount++;
continue;
}
Expand Down Expand Up @@ -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++;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
27 changes: 15 additions & 12 deletions src/features/schedule/schedule.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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', {
Expand Down

0 comments on commit c318c18

Please sign in to comment.