Skip to content

Commit

Permalink
monthly campaign
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan-g committed Jul 14, 2024
1 parent 40dfb9c commit db553e4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 18 deletions.
8 changes: 8 additions & 0 deletions src/features/campaign/campaign.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -116,6 +120,9 @@ import { Countries } from 'src/entities/flaskEntities/countries.entity';
ProviderJoinNeedEntity,
ProviderEntity,
LocationEntity,
TicketViewEntity,
TicketEntity,
TicketContentEntity,
]),
],
providers: [
Expand All @@ -133,6 +140,7 @@ import { Countries } from 'src/entities/flaskEntities/countries.entity';
StatusService,
ProviderService,
LocationService,
TicketService,
],
controllers: [CampaignController],
exports: [CampaignService],
Expand Down
79 changes: 70 additions & 9 deletions src/features/campaign/campaign.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
25 changes: 16 additions & 9 deletions src/features/schedule/schedule.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -128,18 +128,18 @@ export class ScheduleService {
}

// ERROR [Scheduler] ServerError: Can't send mail - all recipients were rejected: 550 <[email protected]> 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(
Expand All @@ -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',
Expand Down

0 comments on commit db553e4

Please sign in to comment.