Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan-g committed Sep 11, 2023
1 parent 5a45544 commit 928b54f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
17 changes: 16 additions & 1 deletion src/features/need/need.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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);
}
}
}
}
18 changes: 13 additions & 5 deletions src/features/need/need.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -737,12 +738,12 @@ export class NeedService {
return await queryBuilder.getMany();
}

async getDeleteCandidates(): Promise<any> {
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',
Expand Down Expand Up @@ -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<Need[]> {
Expand Down
10 changes: 7 additions & 3 deletions src/features/schedule/schedule.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/features/ticket/ticket.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
1 change: 1 addition & 0 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...');
Expand Down

0 comments on commit 928b54f

Please sign in to comment.