From db553e4fd7ebe41ffa4ba36f51d219c2d6e3395d Mon Sep 17 00:00:00 2001 From: G Date: Sun, 14 Jul 2024 17:27:24 +0330 Subject: [PATCH] monthly campaign --- src/features/campaign/campaign.module.ts | 8 +++ src/features/campaign/campaign.service.ts | 79 ++++++++++++++++++++--- src/features/schedule/schedule.service.ts | 25 ++++--- 3 files changed, 94 insertions(+), 18 deletions(-) diff --git a/src/features/campaign/campaign.module.ts b/src/features/campaign/campaign.module.ts index 5c0340790..7eb64e586 100644 --- a/src/features/campaign/campaign.module.ts +++ b/src/features/campaign/campaign.module.ts @@ -48,6 +48,10 @@ import { ProviderEntity } from 'src/entities/provider.entity'; import { LocationEntity } from 'src/entities/location.entity'; import { Cities } from 'src/entities/flaskEntities/cities.entity'; import { Countries } from 'src/entities/flaskEntities/countries.entity'; +import { TicketService } from '../ticket/ticket.service'; +import { TicketViewEntity } from 'src/entities/ticketView.entity'; +import { TicketEntity } from 'src/entities/ticket.entity'; +import { TicketContentEntity } from 'src/entities/ticketContent.entity'; @Global() // 👈 global module @Module({ @@ -116,6 +120,9 @@ import { Countries } from 'src/entities/flaskEntities/countries.entity'; ProviderJoinNeedEntity, ProviderEntity, LocationEntity, + TicketViewEntity, + TicketEntity, + TicketContentEntity, ]), ], providers: [ @@ -133,6 +140,7 @@ import { Countries } from 'src/entities/flaskEntities/countries.entity'; StatusService, ProviderService, LocationService, + TicketService, ], controllers: [CampaignController], exports: [CampaignService], diff --git a/src/features/campaign/campaign.service.ts b/src/features/campaign/campaign.service.ts index 5079884dd..f55d27a7a 100644 --- a/src/features/campaign/campaign.service.ts +++ b/src/features/campaign/campaign.service.ts @@ -19,13 +19,17 @@ import { shuffleArray, sleep, convertFlaskToSayPanelRoles, + daysDifference, } from 'src/utils/helpers'; import { + AnnouncementEnum, CampaignNameEnum, CampaignTypeEnum, ChildExistence, NeedTypeEnum, PanelContributors, + ProductStatusEnum, + ServiceStatusEnum, } from 'src/types/interfaces/interface'; import { FamilyService } from '../family/family.service'; import { CampaignEntity } from 'src/entities/campaign.entity'; @@ -47,6 +51,7 @@ import { } from 'nestjs-paginate'; import { CreateSendNewsLetterDto } from 'src/types/dtos/CreateSendNewsLetter.dto'; import { SyncService } from '../sync/sync.service'; +import { TicketService } from '../ticket/ticket.service'; @Injectable() export class CampaignService { @@ -61,6 +66,7 @@ export class CampaignService { private mailerService: MailerService, private childrenService: ChildrenService, private syncService: SyncService, + private ticketService: TicketService, ) {} private readonly logger = new Logger(CampaignService.name); smsApi = new MelipayamakApi(process.env.SMS_USER, process.env.SMS_PASSWORD); @@ -244,6 +250,61 @@ export class CampaignService { }); } + async sendSwAnnounceReminder() { + function findSwById( + objects: { swId: number; counter: number }[], + id: number, + ): { swId: number; counter: number } { + return objects.find((object) => object.swId === id); + } + try { + const list: [{ swId: number; counter: number }] = [ + { swId: 0, counter: 0 }, + ]; + const needs = await this.needService.getArrivalUpdateCandidates(); + console.log(`Number of needs: ${needs[1]}`); + for await (const need of needs[0]) { + if ( + (need.type === NeedTypeEnum.PRODUCT && + need.status === ProductStatusEnum.PURCHASED_PRODUCT) || + (need.type === NeedTypeEnum.SERVICE && + need.status === ServiceStatusEnum.MONEY_TO_NGO) + ) { + const ticket = await this.ticketService.getTicketByFlaskNeedId( + need.id, + ); + + if ( + // (daysDifference(need.purchase_date, new Date()) > 1 && !ticket) || + !ticket || + !ticket.ticketHistories || + (ticket.ticketHistories && + !ticket.ticketHistories.find( + (h) => h.announcement == AnnouncementEnum.ARRIVED_AT_NGO, + )) + ) { + console.log(`Adding need: ${need.id} Sw Id: ${need.created_by_id}`); + + let foundObject: { counter: any; swId: number }; + if (!list || list.length < 1) { + foundObject = null; + } else { + foundObject = findSwById(list, need.created_by_id); + } + if (foundObject) { + foundObject.counter++; + } else { + list.push({ swId: need.created_by_id, counter: 1 }); + } + } + } + } + console.log(list); + } catch (e) { + console.log(e); + } + } + async sendSwChildNoNeedReminder() { const list = await this.childrenWithNoNeed(); const swIds = removeDuplicates(list.map((e) => e.swId)); @@ -313,7 +374,7 @@ export class CampaignService { const title = `نیازهای ${persianStringMonth} ماه کودکان شما`; const flaskUsers = await this.userService.getFlaskUsers(); - let shuffledUsers = shuffleArray(flaskUsers); + const shuffledUsers = shuffleArray(flaskUsers); let alreadyReceivedEmailCount = 0; let alreadyReceivedSmsCount = 0; @@ -323,14 +384,14 @@ export class CampaignService { let emailReceiversTotal = 0; let smsReceiversTotal = 0; - const testUsers = [ - await this.userService.getFlaskUser(12687), - await this.userService.getFlaskUser(115), - ]; - shuffledUsers = testUsers; - if (shuffledUsers.length > 2) { - return; - } + // const testUsers = [ + // await this.userService.getFlaskUser(12687), + // await this.userService.getFlaskUser(115), + // ]; + // shuffledUsers = testUsers; + // if (shuffledUsers.length > 2) { + // return; + // } // 1- loop shuffled users for await (const flaskUser of shuffledUsers) { diff --git a/src/features/schedule/schedule.service.ts b/src/features/schedule/schedule.service.ts index 1dee92e33..c5a295c17 100644 --- a/src/features/schedule/schedule.service.ts +++ b/src/features/schedule/schedule.service.ts @@ -16,7 +16,7 @@ export class ScheduleService { ) {} private readonly logger = new Logger(ScheduleService.name); - async completePays() { + async completePays() { const father = await this.familyService.getFamilyRoleCompletePay( VirtualFamilyRole.FATHER, 0, @@ -128,18 +128,18 @@ export class ScheduleService { } // ERROR [Scheduler] ServerError: Can't send mail - all recipients were rejected: 550 No such user here - @Cron('37 18 * * Sun', { - name: 'MonthlyCampaigns try At 17:25 on Sunday.', // we try every week and only send to those who did not receive (because their child have no needs, etc.) + @Cron('37 17 * * Sun', { + name: 'MonthlyCampaigns try At 17:37 on Sunday.', // we try every week and only send to those who did not receive (because their child have no needs, etc.) timeZone: 'Asia/Tehran', }) async handleMonthlyCampaignsCron() { 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; - } + // if (farsiDay > 20) { + // this.logger.warn( + // `We are near the end of this month let's skip one more week`, + // ); + // return; + // } // ############## BE CAREFUL ################# if (process.env.NODE_ENV === 'production') { this.logger.debug( @@ -160,6 +160,13 @@ export class ScheduleService { } } + @Timeout(5000) + async handleAnnounceArrivalCron() { + if (process.env.NODE_ENV === 'production') { + // this.logger.debug('Sending Reminder to Social workers to announce arrivals'); + // await this.campaignService.sendSwAnnounceReminder(); + } + } // @Cron('30 8 * * Sat', { // name: 'Confirm Needs At 08:30 on Saturday.', // timeZone: 'Asia/Tehran',