diff --git a/src/database/migrations/1619179734907-users.ts b/src/database/migrations/1619179734907-users.ts index c38eec4..8c73373 100644 --- a/src/database/migrations/1619179734907-users.ts +++ b/src/database/migrations/1619179734907-users.ts @@ -38,6 +38,9 @@ VALUES(uid(),now(),now(),'Admin','portal@admin.portal','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')", ); diff --git a/src/modules/job/services/job.service.ts b/src/modules/job/services/job.service.ts index 57547f9..df78f7f 100644 --- a/src/modules/job/services/job.service.ts +++ b/src/modules/job/services/job.service.ts @@ -23,7 +23,7 @@ export class JobService extends BaseService { 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}>`, diff --git a/src/modules/user/controllers/auth.controller.ts b/src/modules/user/controllers/auth.controller.ts index 36cd2bd..9efe17b 100644 --- a/src/modules/user/controllers/auth.controller.ts +++ b/src/modules/user/controllers/auth.controller.ts @@ -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 diff --git a/src/modules/user/services/auth.service.ts b/src/modules/user/services/auth.service.ts index 911e21e..d385653 100644 --- a/src/modules/user/services/auth.service.ts +++ b/src/modules/user/services/auth.service.ts @@ -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 { @@ -80,20 +80,47 @@ export class AuthService { relations: getRelations(fields, metaData), }); } - async getMertics(): Promise { - 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 { + 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 }, + }; + } } }