import {authenticate, STRATEGY} from '@bleco/authentication'; import {inject} from '@loopback/core'; import {Count, CountSchema, Filter, repository, Where} from '@loopback/repository'; import { get, getFilterSchemaFor, getModelSchemaRef, getWhereSchemaFor, param, ResponseObject, RestBindings, } from '@loopback/rest'; import {CONTENT_TYPE, OPERATION_SECURITY_SPEC, STATUS_CODE} from '@loopx/core'; import {authorise} from 'loopback4-acl'; import moment from 'moment'; import {AuthenticationAuthActions} from '../auth.actions'; import {AuthenticationAuthSubjects} from '../auth.subjects'; import {ActiveUsersRange} from '../enums'; import {LoginActivity} from '../models'; import {LoginActivityRepository} from '../repositories'; import {ActiveUsersGroupData} from '../types'; const baseUrl = '/login-activity'; export class LoginActivityController { constructor( @repository(LoginActivityRepository) private readonly loginActivityRepo: LoginActivityRepository, @inject(RestBindings.Http.RESPONSE) private response: ResponseObject, ) {} @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorise(AuthenticationAuthActions.read, AuthenticationAuthSubjects.LoginActivity) @get(`${baseUrl}/count`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'LoginActivity model count', content: {[CONTENT_TYPE.JSON]: {schema: CountSchema}}, }, }, }) async count( @param.query.object('where', getWhereSchemaFor(LoginActivity)) where?: Where, ): Promise { return this.loginActivityRepo.count(where); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorise(AuthenticationAuthActions.read, AuthenticationAuthSubjects.LoginActivity) @get(baseUrl, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'Array of LoginActivity model instances', content: { [CONTENT_TYPE.JSON]: { schema: { type: 'array', items: getModelSchemaRef(LoginActivity, {includeRelations: true}), }, }, }, }, }, }) async find( @param.query.object('filter', getFilterSchemaFor(LoginActivity)) filter?: Filter, ): Promise { return this.loginActivityRepo.find(filter); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorise(AuthenticationAuthActions.read, AuthenticationAuthSubjects.LoginActivity) @get(`${baseUrl}/{id}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'LoginActivity model instance', content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRef(LoginActivity, {includeRelations: true}), }, }, }, }, }) async findById( @param.path.string('id') id: string, @param.query.object('filter', getFilterSchemaFor(LoginActivity)) filter?: Filter, ): Promise { return this.loginActivityRepo.findById(id, filter); } @authenticate(STRATEGY.BEARER, { passReqToCallback: true, }) @authorise(AuthenticationAuthActions.read, AuthenticationAuthSubjects.LoginActivity) @get(`active-users/{range}`, { security: OPERATION_SECURITY_SPEC, responses: { [STATUS_CODE.OK]: { description: 'LoginActivity model instance', content: { [CONTENT_TYPE.JSON]: { schema: getModelSchemaRef(Object, {includeRelations: true}), }, }, }, }, }) async getActiveUsers( @param.path.string('range') range: ActiveUsersRange, @param.query.dateTime('startDate') startDate: Date, @param.query.dateTime('endDate') endDate: Date, ): Promise { const activeUsersForTime = await this.loginActivityRepo.find({ where: { loginTime: {between: [startDate, endDate]}, }, }); //all the BL here const groupByDate: ActiveUsersGroupData = {}; activeUsersForTime.forEach(item => { let date = ''; const loginTime = moment(item.loginTime); if (range === ActiveUsersRange.DAILY) { date = loginTime.format('YYYY-MM-DD'); } else if (range === ActiveUsersRange.MONTHLY) { date = loginTime.format('MM-YYYY'); } else { //intentional } if (!groupByDate[date]) { groupByDate[date] = {}; } const type = item.loginType; if (!groupByDate[date][type]) { groupByDate[date][type] = []; } const isDuplicate = groupByDate[date][type].some(existItem => existItem.actor === item.actor); if (!isDuplicate) { groupByDate[date][type].push(item); } }); return groupByDate; } }