diff --git a/src/src/cron/campEnd.cron.ts b/src/src/cron/campEnd.cron.ts index e38d5b606..a7eea6a0d 100644 --- a/src/src/cron/campEnd.cron.ts +++ b/src/src/cron/campEnd.cron.ts @@ -8,7 +8,7 @@ export class CampEndCron { constructor(private hasuraService: HasuraService) {} // Cronjob runs every day at 12am - @Cron('0 00 * * * ') + // @Cron('0 00 * * * ') async updateEndCamp() { // Get today's date const today = moment().format('YYYY-MM-DDTHH:mm:ss'); diff --git a/src/src/cron/cron.module.ts b/src/src/cron/cron.module.ts index c7375cf12..5c307e7a2 100644 --- a/src/src/cron/cron.module.ts +++ b/src/src/cron/cron.module.ts @@ -9,6 +9,7 @@ import { FaAttendanceProcessingCron } from './faAttendanceProcessing.cron'; import { FaFaceIndexingCron } from './faFaceIndexing.cron'; import { FaUserIndexingCron } from './faUserIndexing.cron'; import { PrepareCertificateHtmlCron } from './prepareCertificateHtml.cron'; +import { HousekeepingCron } from './housekeeping.cron'; @Module({ imports: [ AwsRekognitionModule, @@ -21,6 +22,7 @@ import { PrepareCertificateHtmlCron } from './prepareCertificateHtml.cron'; FaFaceIndexingCron, FaAttendanceProcessingCron, PrepareCertificateHtmlCron, + HousekeepingCron, CampEndCron, Method, ], diff --git a/src/src/cron/faAttendanceProcessing.cron.ts b/src/src/cron/faAttendanceProcessing.cron.ts index 28cc78605..cbf41674b 100644 --- a/src/src/cron/faAttendanceProcessing.cron.ts +++ b/src/src/cron/faAttendanceProcessing.cron.ts @@ -19,7 +19,7 @@ export class FaAttendanceProcessingCron { } //3rd cron runs for each hour's 25th minute eg: 10:25am, 11::25am - @Cron('25 * * * *') + // @Cron('25 * * * *') async markAttendanceCron() { try { /*----------------------- Mark attendance of from face index of users in collection -----------------------*/ diff --git a/src/src/cron/faFaceIndexing.cron.ts b/src/src/cron/faFaceIndexing.cron.ts index 34ab16da0..930fb587b 100644 --- a/src/src/cron/faFaceIndexing.cron.ts +++ b/src/src/cron/faFaceIndexing.cron.ts @@ -19,7 +19,7 @@ export class FaFaceIndexingCron { } //2nd cron runs for each hour's 15th minute eg: 10:15am, 11::15am - @Cron('15 * * * *') + // @Cron('15 * * * *') async indexRekognitionUsers() { try { /*----------------------- Create face index of users in collection -----------------------*/ diff --git a/src/src/cron/faUserIndexing.cron.ts b/src/src/cron/faUserIndexing.cron.ts index d21a82b31..421dd847f 100644 --- a/src/src/cron/faUserIndexing.cron.ts +++ b/src/src/cron/faUserIndexing.cron.ts @@ -19,7 +19,7 @@ export class FaUserIndexingCron { } //first cron runs for each hour's 5th minute eg: 10:05am, 11::05am - @Cron('05 * * * *') + // @Cron('05 * * * *') async createCollectionUsers() { try { /*----------------------- Create users in collection -----------------------*/ diff --git a/src/src/cron/housekeeping.cron.ts b/src/src/cron/housekeeping.cron.ts new file mode 100644 index 000000000..513c3f2d7 --- /dev/null +++ b/src/src/cron/housekeeping.cron.ts @@ -0,0 +1,84 @@ +import { Injectable } from '@nestjs/common'; +import { Cron } from '@nestjs/schedule'; +import { HasuraService } from '../services/hasura/hasura.service'; + +@Injectable() +export class HousekeepingCron { + constructor(private hasuraService: HasuraService) {} + + // Cronjob runs every day at 12am + // @Cron('0/1 * * * *') + async updateCamp() { + // Get today's date + const column_create_or_exists = await this.addColumn({ + table_name: 'groups', + column_name: 'org_id', + }); + console.log(column_create_or_exists); + if (column_create_or_exists?.exist) { + const updateQuery = `UPDATE groups AS g + SET org_id = pf.parent_ip + FROM ( + SELECT g.id, g.program_id, g.academic_year_id, gu.user_id, gu.member_type, gu.status + FROM groups AS g + JOIN group_users AS gu ON g.id = gu.group_id + WHERE gu.member_type = 'owner' AND gu.status = 'active' + ) AS sub + JOIN program_faciltators AS pf ON sub.user_id = pf.user_id + AND sub.program_id = pf.program_id + AND sub.academic_year_id = pf.academic_year_id + WHERE g.id = sub.id;`; + + const result1 = await this.hasuraService.executeRawSql(updateQuery); + console.log('update org_id in groups', result1); + } + return true; + } + + // @Cron('0/1 * * * *') + async updateLeaner() { + // Get today's date + const column_create_or_exists = await this.addColumn({ + table_name: 'program_beneficiaries', + column_name: 'org_id', + }); + if (column_create_or_exists?.exist) { + const updateQuery = `UPDATE program_beneficiaries AS pb + SET org_id = pf.parent_ip + FROM program_faciltators AS pf + WHERE pb.facilitator_id = pf.user_id + AND pb.program_id = pf.program_id + AND pb.academic_year_id = pf.academic_year_id;`; + + const result1 = await this.hasuraService.executeRawSql(updateQuery); + console.log('update org_id in program_beneficiaries', result1); + } + return true; + } + + async addColumn({ table_name, column_name }) { + const r = await this.checkColumn({ + table_name, + column_name, + }); + if (r === 0) { + const sql = `ALTER TABLE ${table_name} ADD COLUMN ${column_name} TEXT`; + const sqlResult = await this.hasuraService.executeRawSql(sql); + console.log(sql); + return sqlResult; + } + return { exist: true }; + } + + async checkColumn({ table_name, column_name }) { + const sql = `SELECT EXISTS ( + SELECT 1 + FROM information_schema.columns + WHERE table_name = '${table_name}' + AND column_name = '${column_name}' + ) AS column_exists`; + const sqlResult = await this.hasuraService.executeRawSql(sql); + const datar = sqlResult?.result?.filter((e) => e.includes('t')).length; + return datar; + } +} diff --git a/src/src/cron/prepareCertificateHtml.cron.ts b/src/src/cron/prepareCertificateHtml.cron.ts index 249d20f31..b53e42adf 100644 --- a/src/src/cron/prepareCertificateHtml.cron.ts +++ b/src/src/cron/prepareCertificateHtml.cron.ts @@ -23,7 +23,7 @@ export class PrepareCertificateHtmlCron { ) {} //cron issue certificate run every 5 minutes - @Cron(CronExpression.EVERY_5_MINUTES) + // @Cron(CronExpression.EVERY_5_MINUTES) async prepareCertificateHtml() { console.log('cron job: issueCertificate started at time ' + new Date()); //fetch all test tracking data which has certificate_status null diff --git a/src/src/services/hasura/hasura.service.ts b/src/src/services/hasura/hasura.service.ts index 6b23ccbf9..b7de75619 100644 --- a/src/src/services/hasura/hasura.service.ts +++ b/src/src/services/hasura/hasura.service.ts @@ -113,7 +113,18 @@ export class HasuraService { }) .pipe(map((res) => res.data)), ); - } catch (e) {} + } catch (e) { + return this.handleResponseException(e); + } + } + + public handleResponseException(obj: any) { + const { response, message } = obj; + return { + status: response?.status ? response?.status : 404, + error: response?.data?.message ? response?.data?.message : message, + ...(response ? response?.data : response), + }; } public getFormattedData(arr, excludeFieldsIndex?) {