|
|
@@ -7,7 +7,7 @@ import {
|
|
|
InternalServerErrorException,
|
|
|
UnauthorizedException
|
|
|
} from '@nestjs/common'
|
|
|
-import { In, Repository, UpdateResult } from 'typeorm'
|
|
|
+import { In, Repository, UpdateResult, MoreThanOrEqual, LessThanOrEqual, And } from 'typeorm'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
import { Users } from './entities/users.entity'
|
|
|
import { IUsers } from './interfaces/users.interface'
|
|
|
@@ -23,6 +23,7 @@ import { paginate, Pagination } from 'nestjs-typeorm-paginate'
|
|
|
import { Role } from '../model/role.enum'
|
|
|
import { PageRequest } from '../common/dto/page-request'
|
|
|
import { th } from 'date-fns/locale'
|
|
|
+import { startOfDay, endOfDay, addDays, format } from 'date-fns'
|
|
|
|
|
|
@Injectable()
|
|
|
export class UsersService {
|
|
|
@@ -232,4 +233,32 @@ export class UsersService {
|
|
|
user.iat = iat || Math.floor(new Date().getTime() / 1000) - 1
|
|
|
return await this.userRepository.save(user)
|
|
|
}
|
|
|
+
|
|
|
+ public async getCount(apiUserId: number, roles?: Role, date?: string) {
|
|
|
+ return await this.userRepository.count({
|
|
|
+ where: {
|
|
|
+ apiUserId: apiUserId,
|
|
|
+ roles: roles,
|
|
|
+ createdAt: date
|
|
|
+ ? And(MoreThanOrEqual(startOfDay(new Date(date))), LessThanOrEqual(endOfDay(new Date(date))))
|
|
|
+ : undefined
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ public async getWeekCount(apiUserId: number) {
|
|
|
+ const date1 = new Date()
|
|
|
+ let dayInfo = {}
|
|
|
+ for (let i = 0; i < 7; i++) {
|
|
|
+ let date2 = format(addDays(date1, 0 - i), 'yyyy-MM-dd')
|
|
|
+ let api = await this.getCount(apiUserId, Role.Api, date2)
|
|
|
+ let user = await this.getCount(apiUserId, Role.User, date2)
|
|
|
+ dayInfo[format(addDays(date1, 0 - i), 'MM-dd')] = {
|
|
|
+ api: api,
|
|
|
+ user: user
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return dayInfo
|
|
|
+ }
|
|
|
}
|