chore: dashboard report, admin route decorator and guard
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
export const ADMIN_ROUTE = "shouldAdmin";
|
||||
import { SetMetadata } from "@nestjs/common";
|
||||
|
||||
export const AdminRoute = (isAdminRoute: boolean = true) => SetMetadata(ADMIN_ROUTE, isAdminRoute);
|
||||
@@ -1,10 +1,11 @@
|
||||
import { UseGuards, applyDecorators } from "@nestjs/common";
|
||||
import { ApiBearerAuth } from "@nestjs/swagger";
|
||||
|
||||
import { AdminRouteGuard } from "../../modules/auth/guards/admin.guard";
|
||||
import { JwtAuthGuard } from "../../modules/auth/guards/auth.guard";
|
||||
import { PermissionsGuard } from "../../modules/auth/guards/permission.guard";
|
||||
// import { RoleGuard } from "../../modules/auth/guards/role.guard";
|
||||
|
||||
export function AuthGuards() {
|
||||
return applyDecorators(UseGuards(JwtAuthGuard, PermissionsGuard), ApiBearerAuth("authorization"));
|
||||
return applyDecorators(UseGuards(JwtAuthGuard, PermissionsGuard, AdminRouteGuard), ApiBearerAuth("authorization"));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { CanActivate, ExecutionContext, ForbiddenException, Injectable } from "@nestjs/common";
|
||||
import { Reflector } from "@nestjs/core";
|
||||
import { FastifyRequest } from "fastify";
|
||||
|
||||
import { ADMIN_ROUTE } from "../../../common/decorators/admin.decorator";
|
||||
import { AuthMessage } from "../../../common/enums/message.enum";
|
||||
|
||||
@Injectable()
|
||||
export class AdminRouteGuard implements CanActivate {
|
||||
constructor(private reflector: Reflector) {}
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const requiredAdmin = this.reflector.getAllAndOverride<boolean>(ADMIN_ROUTE, [context.getHandler(), context.getClass()]);
|
||||
if (!requiredAdmin) return true;
|
||||
|
||||
const req = context.switchToHttp().getRequest<FastifyRequest>();
|
||||
const user = req.user;
|
||||
|
||||
if (!user) throw new ForbiddenException(AuthMessage.UNAUTHORIZED_ACCESS);
|
||||
|
||||
if (!user.isAdmin) throw new ForbiddenException(AuthMessage.UNAUTHORIZED_ACCESS);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
import { Controller, Get } from "@nestjs/common";
|
||||
|
||||
import { DashboardService } from "./providers/dashboard.service";
|
||||
import { AdminRoute } from "../../common/decorators/admin.decorator";
|
||||
import { AuthGuards } from "../../common/decorators/auth-guard.decorator";
|
||||
|
||||
@Controller("dashboards")
|
||||
@AuthGuards()
|
||||
@AdminRoute()
|
||||
export class DashboardController {
|
||||
constructor(private readonly dashboardService: DashboardService) {}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import dayjs from "dayjs";
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
import Decimal from "decimal.js";
|
||||
import { DataSource, In } from "typeorm";
|
||||
@@ -371,12 +372,10 @@ export class SubscriptionsService {
|
||||
}
|
||||
|
||||
async getPlansSalesCountByDaysInWeek() {
|
||||
const now = new Date();
|
||||
const daysSinceStart = now.getDay(); // Remove the +1 and % 7
|
||||
const startOfWeek = new Date(now);
|
||||
startOfWeek.setDate(now.getDate() - daysSinceStart);
|
||||
const endOfWeek = new Date(startOfWeek);
|
||||
endOfWeek.setDate(startOfWeek.getDate() + 6);
|
||||
const startOfWeek = dayjs().startOf("week").toDate();
|
||||
const endOfWeek = dayjs().endOf("week").toDate();
|
||||
|
||||
console.log(startOfWeek, endOfWeek);
|
||||
|
||||
const sales = await this.userSubscriptionsRepository
|
||||
.createQueryBuilder("userSubscription")
|
||||
@@ -392,7 +391,7 @@ export class SubscriptionsService {
|
||||
const salesByDay = Array(7).fill(0);
|
||||
|
||||
sales.forEach((sale) => {
|
||||
const day = new Date(sale.date).getDay(); // Remove the +1 and % 7
|
||||
const day = (dayjs(sale.date).day() + 1) % 7;
|
||||
salesByDay[day] = parseInt(sale.count);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user