From 928b54ff1b47f1a823561a49188ecc7600485e5d Mon Sep 17 00:00:00 2001 From: Gbuntu Date: Mon, 11 Sep 2023 10:20:29 +0330 Subject: [PATCH] minor changes --- src/features/need/need.controller.ts | 17 ++++++++++++++++- src/features/need/need.service.ts | 18 +++++++++++++----- src/features/schedule/schedule.service.ts | 10 +++++++--- src/features/ticket/ticket.controller.ts | 2 +- src/utils/auth.ts | 1 + 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/features/need/need.controller.ts b/src/features/need/need.controller.ts index c8c7e21bd..2cdb57296 100644 --- a/src/features/need/need.controller.ts +++ b/src/features/need/need.controller.ts @@ -15,7 +15,7 @@ import { SUPER_ADMIN_ID, } from 'src/types/interfaces/interface'; import config from 'src/config'; -import { convertFlaskToSayRoles } from 'src/utils/helpers'; +import { convertFlaskToSayRoles, daysDifference } from 'src/utils/helpers'; @ApiTags('Needs') @ApiSecurity('flask-access-token') @@ -150,4 +150,19 @@ export class NeedController { return await this.needService.getDuplicateNeeds(flaskChildId, flaskNeedId); } + + @Get('delete/old') + @ApiOperation({ description: 'Get duplicates need for confirming' }) + async deleteOldNeeds() { + // delete old confirmed needs + const deleteCandidates = await this.needService.getDeleteCandidates(); + for await (const need of deleteCandidates[0]) { + const daysDiff = daysDifference(need.confirmDate, new Date()); + if (daysDiff > 90) { + const accessToken = + config().dataCache.fetchPanelAuthentication(25).token; + await this.needService.deleteOneNeed(need.id, accessToken); + } + } + } } diff --git a/src/features/need/need.service.ts b/src/features/need/need.service.ts index 7f0af8902..f04c86d5b 100644 --- a/src/features/need/need.service.ts +++ b/src/features/need/need.service.ts @@ -5,6 +5,7 @@ import { Need } from '../../entities/flaskEntities/need.entity'; import { Brackets, IsNull, Not, Repository, UpdateResult } from 'typeorm'; import { Configuration, + NeedAPIApi, PreneedAPIApi, PreneedSummary, PublicAPIApi, @@ -737,12 +738,12 @@ export class NeedService { return await queryBuilder.getMany(); } - async getDeleteCandidates(): Promise { + async getDeleteCandidates(): Promise<[Need[], number]> { const date = new Date(); date.setMonth(date.getMonth() - 3); // three months ago console.log(date); - const queryBuilder = this.flaskNeedRepository + return this.flaskNeedRepository .createQueryBuilder('need') .leftJoinAndMapOne( 'need.child', @@ -771,10 +772,17 @@ export class NeedService { ]) .cache(60000) .limit(500) - .orderBy('need.created', 'ASC'); - const accurateCount = await queryBuilder.getManyAndCount(); + .orderBy('need.created', 'ASC') + .getManyAndCount(); + } - return accurateCount; + async deleteOneNeed(flaskNeedId: number, accessToken: string) { + const needApi = new NeedAPIApi(); + const deleted = needApi.apiV2NeedDeleteNeedIdneedIdPatch( + accessToken, + flaskNeedId, + ); + return deleted; } async getMidjourneyNeeds(): Promise { diff --git a/src/features/schedule/schedule.service.ts b/src/features/schedule/schedule.service.ts index 81040c07c..271a223bd 100644 --- a/src/features/schedule/schedule.service.ts +++ b/src/features/schedule/schedule.service.ts @@ -4,11 +4,13 @@ import { VirtualFamilyRole } from 'src/types/interfaces/interface'; import config from 'src/config'; import { FamilyService } from '../family/family.service'; import { AnalyticService } from '../analytic/analytic.service'; +import { NeedService } from '../need/need.service'; @Injectable() export class ScheduleService { constructor( private familyService: FamilyService, + private needService: NeedService, private analyticService: AnalyticService, ) {} private readonly logger = new Logger(ScheduleService.name); @@ -80,11 +82,11 @@ export class ScheduleService { }); } - @Timeout(2000) - @Cron(CronExpression.EVERY_10_MINUTES) + @Timeout(15000) + // @Cron(CronExpression.EVERY_10_MINUTES) async handleCronOnce() { this.logger.debug( - 'Called only once after 10 minutes of the server initiation', + 'Called only once after 15 seconds of the server initiation', ); this.completePays(); this.rolesCount(); @@ -104,6 +106,8 @@ export class ScheduleService { @Cron(CronExpression.EVERY_1ST_DAY_OF_MONTH_AT_MIDNIGHT) async handleMonthlyCron() { this.logger.debug('Called every Month'); + + // active families let actives = config().dataCache.fetchActiveFamilies(); if (!actives) { actives = await this.analyticService.getChildrenFamilyAnalytic(); diff --git a/src/features/ticket/ticket.controller.ts b/src/features/ticket/ticket.controller.ts index a3409f3ec..ec5a571aa 100644 --- a/src/features/ticket/ticket.controller.ts +++ b/src/features/ticket/ticket.controller.ts @@ -79,7 +79,7 @@ export class TicketController { } } - @Get('ticket/:id/:userId') + @Get('ticket/:id') async getTicketById(@Req() req: Request, @Param('id') id: string) { const panelFlaskUserId = req.headers['panelFlaskUserId']; const panelFlaskTypeId = req.headers['panelFlaskTypeId']; diff --git a/src/utils/auth.ts b/src/utils/auth.ts index bc0bad387..ae438c622 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -71,6 +71,7 @@ export async function checkFlaskCacheAuthentication(req, logger: Logger) { // for panel else if ( String(req.headers.origin) === 'http://localhost:3000' || + !req.headers.origin || String(req.headers.origin) === 'https://panel.saydao.org' ) { logger.log('fetching panel cache token...');