Skip to content

Commit

Permalink
Merge pull request #172 from nab-in/issues
Browse files Browse the repository at this point in the history
update metrics for date ranges
  • Loading branch information
BaharaJr authored Aug 23, 2021
2 parents 0f5468f + b1a09c2 commit c9366e9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/database/migrations/1619179734907-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ VALUES(uid(),now(),now(),'Admin','[email protected]','admin','Portal','${hash}
await queryRunner.query(
'ALTER TABLE APPLIEDJOB ADD COLUMN LOCATION CHARACTER VARYING(255)',
);

await queryRunner.query('ALTER TABLE APPLIEDJOB ADD COLUMN CREATED DATE');

await queryRunner.query(
"INSERT INTO USERROLE(uid,name) values(UID(), 'SUPER USER')",
);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/job/services/job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class JobService extends BaseService<Job> {
return sessionUser;
}
async apply({ job, user }): Promise<{ message: string }> {
const query = `INSERT INTO APPLIEDJOB(USERId, JOBID) VALUES(${user.id}, ${job.id})`;
const query = `INSERT INTO APPLIEDJOB(CREATED, USERId, JOBID) VALUES(NOW(),${user.id}, ${job.id})`;
await this.userrepository.manager.query(query);
return {
message: `You have successfully applied to with name <${job.name}>`,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/user/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class AuthController {
(role) => role.name === 'SUPER USER' || role.name === 'ADMIN',
);
if (admin.length > 0) {
const metrics = await this.authService.getMertics();
const metrics = await this.authService.getMertics(query);
return res.status(HttpStatus.OK).send(metrics);
} else {
return res
Expand Down
59 changes: 43 additions & 16 deletions src/modules/user/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { JwtService } from '@nestjs/jwt';
import { InjectRepository } from '@nestjs/typeorm';
import { Job } from '../../job/entities/job.entity';
import { In, Repository } from 'typeorm';
import { Between, In, Repository } from 'typeorm';
import { User } from '../entities/user.entity';
import jwt_decode from 'jwt-decode';
import {
Expand Down Expand Up @@ -80,20 +80,47 @@ export class AuthService {
relations: getRelations(fields, metaData),
});
}
async getMertics(): Promise<any> {
const users = await this.userrepository.count();
const companies = await this.companyrepository.count();
const applications = Number(
(
await this.jobrepository.manager.query(
'SELECT COUNT(*) FROM APPLIEDJOB',
)
)[0]['count'],
);
const jobs = await this.jobrepository.count();
return {
message: 'Job Portal Admin Metrics',
metrics: { users, companies, applications, jobs },
};
async getMertics(query: { startDate: Date; endDate: Date }): Promise<any> {
if (Object.keys(query).length === 0) {
const users = await this.userrepository.count();
const companies = await this.companyrepository.count();
const applications = Number(
(
await this.jobrepository.manager.query(
'SELECT COUNT(*) FROM APPLIEDJOB',
)
)[0]['count'],
);
const jobs = await this.jobrepository.count();
return {
message: 'Job Portal Admin Metrics',
metrics: { users, companies, applications, jobs },
};
} else {
const date = new Date(query.startDate);
date.setDate(date.getDate() - 1);
const startdate = date.toISOString().split('T')[0];
const dates = new Date(query.endDate);
dates.setDate(dates.getDate() + 1);
const enddate = dates.toISOString().split('T')[0];
const users = await this.userrepository.count({
where: { created: Between(startdate, enddate) },
});
const companies = await this.companyrepository.count({
where: { created: Between(startdate, enddate) },
});
const applications = Number(
(
await this.jobrepository.manager.query(
`SELECT COUNT(*) FROM APPLIEDJOB WHERE CREATED >= '${startdate}' AND CREATED <='${enddate}'`,
)
)[0]['count'],
);
const jobs = await this.jobrepository.count();
return {
message: `Job Portal Admin Metrics from ${query.startDate} to ${query.endDate}`,
metrics: { users, companies, applications, jobs },
};
}
}
}

0 comments on commit c9366e9

Please sign in to comment.