Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task #215480: [BE] Add org_id in Camp and update using group_users member_type and status active using user_id Task #215497 [BE] Add org_id in program_beneficaires and update org_id using facilitator_id by joining program_facilitator. user_id #904

Open
wants to merge 2 commits into
base: release-2.8.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/src/cron/campEnd.cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class CampEndCron {
constructor(private hasuraService: HasuraService) {}

// Cronjob runs every day at 12am
@Cron('0 00 * * * ')
// @Cron('0 00 * * * ')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undo

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this and in other crons

async updateEndCamp() {
// Get today's date
const today = moment().format('YYYY-MM-DDTHH:mm:ss');
Expand Down
2 changes: 2 additions & 0 deletions src/src/cron/cron.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,6 +22,7 @@ import { PrepareCertificateHtmlCron } from './prepareCertificateHtml.cron';
FaFaceIndexingCron,
FaAttendanceProcessingCron,
PrepareCertificateHtmlCron,
HousekeepingCron,
CampEndCron,
Method,
],
Expand Down
2 changes: 1 addition & 1 deletion src/src/cron/faAttendanceProcessing.cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 * * * *')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undo

async markAttendanceCron() {
try {
/*----------------------- Mark attendance of from face index of users in collection -----------------------*/
Expand Down
2 changes: 1 addition & 1 deletion src/src/cron/faFaceIndexing.cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 * * * *')
manojLondhe marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undo

async indexRekognitionUsers() {
try {
/*----------------------- Create face index of users in collection -----------------------*/
Expand Down
2 changes: 1 addition & 1 deletion src/src/cron/faUserIndexing.cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 * * * *')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undo

async createCollectionUsers() {
try {
/*----------------------- Create users in collection -----------------------*/
Expand Down
84 changes: 84 additions & 0 deletions src/src/cron/housekeeping.cron.ts
Original file line number Diff line number Diff line change
@@ -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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

give it better name

// 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see if we can use transaction start end?

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;`;
manojLondhe marked this conversation as resolved.
Show resolved Hide resolved

const result1 = await this.hasuraService.executeRawSql(updateQuery);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use try catch, push to Sentry in case of failure

console.log('update org_id in groups', result1);
}
return true;
manojLondhe marked this conversation as resolved.
Show resolved Hide resolved
}

// @Cron('0/1 * * * *')
async updateLeaner() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

give this better name

// 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use try catch, push to Sentry in case of failure

const updateQuery = `UPDATE program_beneficiaries AS pb
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see if we can use transaction start, end?

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;
}
}
2 changes: 1 addition & 1 deletion src/src/cron/prepareCertificateHtml.cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class PrepareCertificateHtmlCron {
) {}

//cron issue certificate run every 5 minutes
@Cron(CronExpression.EVERY_5_MINUTES)
// @Cron(CronExpression.EVERY_5_MINUTES)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undo

async prepareCertificateHtml() {
console.log('cron job: issueCertificate started at time ' + new Date());
//fetch all test tracking data which has certificate_status null
Expand Down
13 changes: 12 additions & 1 deletion src/src/services/hasura/hasura.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand Down