Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { PaymentModule } from './payments/payment.module';
import { OrderModule } from './order/order.module';
import { NotificationModule } from './notification/notification.module';
import { RecommendationModule } from './recommendation/recommendation.module';
import { ReportsModule } from './reports/reports.module';

@Module({
imports: [
Expand Down Expand Up @@ -61,6 +62,7 @@ import { RecommendationModule } from './recommendation/recommendation.module';
OrderModule,
NotificationModule,
RecommendationModule,
ReportsModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
1 change: 1 addition & 0 deletions src/order/order.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ import { InventoryModule } from 'src/inventory/inventory.module';
PromoService,
CouponService,
],
exports: [OrderService],
})
export class OrderModule {}
96 changes: 96 additions & 0 deletions src/order/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,100 @@ export class OrderService {
}
return await this.orderDeliveryRepository.save(updateDelivery);
}
async countOrdersCompleted(
status: OrderStatus,
startDate: Date,
endDate: Date,
branchId?: string,
): Promise<number> {
const startStr = startDate.toISOString().slice(0, 10);
const endStr = endDate.toISOString().slice(0, 10);
const qb = this.orderRepository
.createQueryBuilder('order')
.where(`DATE(order.created_at) BETWEEN :start AND :end`, {
start: startStr,
end: endStr,
})
.andWhere('order.status = :status', { status })
.leftJoin('order.branch', 'branch');
if (branchId) {
qb.andWhere('branch.id = :branchId', { branchId });
}
return qb.getCount();
}
async countOpenOrders(
startDate: Date,
endDate: Date,
branchId?: string,
): Promise<number> {
const startStr = startDate.toISOString().slice(0, 10);
const endStr = endDate.toISOString().slice(0, 10);
const qb = this.orderRepository
.createQueryBuilder('order')
.where(`DATE(order.created_at) BETWEEN :start AND :end`, {
start: startStr,
end: endStr,
})
.andWhere('order.status NOT IN (:...closed)', {
closed: [OrderStatus.COMPLETED, OrderStatus.CANCELED],
})
.leftJoin('order.branch', 'branch');

if (branchId) {
qb.andWhere('branch.id = :branchId', { branchId });
}
return qb.getCount();
}
async sumTotalSales(
startDate: Date,
endDate: Date,
branchId?: string,
): Promise<number> {
const startStr = startDate.toISOString().slice(0, 10);
const endStr = endDate.toISOString().slice(0, 10);
const qb = this.orderRepository
.createQueryBuilder('order')
.select('SUM(order.total_price)', 'sum')
.where(`DATE(order.created_at) BETWEEN :start AND :end`, {
start: startStr,
end: endStr,
})
.andWhere('order.status = :status', { status: OrderStatus.COMPLETED })
.leftJoin('order.branch', 'branch');

if (branchId) {
qb.andWhere('branch.id = :branchId', { branchId });
}
const raw = await qb.getRawOne<{ sum: string }>();
return Number(raw?.sum ?? '0');
}
async countOrdersByStatus(
startDate: Date,
endDate: Date,
branchId?: string,
): Promise<Record<OrderStatus, number>> {
const startStr = startDate.toISOString().slice(0, 10);
const endStr = endDate.toISOString().slice(0, 10);
const qb = this.orderRepository
.createQueryBuilder('order')
.select('order.status', 'status')
.addSelect('COUNT(*)', 'count')
.where(`DATE(order.created_at) BETWEEN :start AND :end`, {
start: startStr,
end: endStr,
})
.leftJoin('order.branch', 'branch');
if (branchId) {
qb.andWhere('branch.id = :branchId', { branchId });
}
qb.groupBy('order.status');
const rows = await qb.getRawMany<{ status: OrderStatus; count: string }>();
return rows.reduce(
(acc, { status, count }) => {
acc[status] = Number(count);
return acc;
},
{} as Record<OrderStatus, number>,
);
}
}
69 changes: 69 additions & 0 deletions src/reports/reports.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {
Controller,
Get,
Query,
BadRequestException,
UseGuards,
} from '@nestjs/common';
import { OrderService } from 'src/order/order.service';
import { UserService } from 'src/user/user.service';
import { OrderStatus } from 'src/order/entities/order.entity';
import { Roles } from 'src/auth/roles.decorador';
import { AuthGuard } from 'src/auth/auth.guard';
import { RolesGuard } from 'src/auth/roles.guard';
import { UserRole } from 'src/user/entities/user.entity';

@UseGuards(AuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@Controller('report')
export class ReportsController {
constructor(
private readonly orderService: OrderService,
private readonly userService: UserService,
) {}

@Get('dashboard')
async getDashboard(
@Query('startDate') start: string,
@Query('endDate') end: string,
@Query('branchId') branchId?: string,
) {
if (!start || !end) {
throw new BadRequestException('startDate y endDate son requeridos');
}
const startDate = new Date(start);
const endDate = new Date(end);
const [openOrders, completedOrders, totalSales, totalNewUsers] =
await Promise.all([
this.orderService.countOpenOrders(startDate, endDate, branchId),
this.orderService.countOrdersCompleted(
OrderStatus.COMPLETED,
startDate,
endDate,
branchId,
),
this.orderService.sumTotalSales(startDate, endDate, branchId),
this.userService.countNewUsers(startDate, endDate),
]);

return {
openOrders,
completedOrders,
totalSales,
totalNewUsers,
};
}
@Get('order')
async getOrdersByStatus(
@Query('startDate') start: string,
@Query('endDate') end: string,
@Query('branchId') branchId?: string,
) {
if (!start || !end) {
throw new BadRequestException('startDate y endDate son requeridos');
}
const startDate = new Date(start);
const endDate = new Date(end);
return this.orderService.countOrdersByStatus(startDate, endDate, branchId);
}
}
11 changes: 11 additions & 0 deletions src/reports/reports.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { ReportsController } from './reports.controller';
import { OrderModule } from 'src/order/order.module';
import { UserModule } from 'src/user/user.module';
import { AuthModule } from 'src/auth/auth.module';

@Module({
imports: [OrderModule, UserModule, AuthModule],
controllers: [ReportsController],
})
export class ReportsModule {}
13 changes: 13 additions & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,17 @@ export class UserService {
const updatedMoto = this.UserMotoRepository.merge(userMoto, updateMotoDto);
return await this.UserMotoRepository.save(updatedMoto);
}

async countNewUsers(startDate: Date, endDate: Date): Promise<number> {
const startStr = startDate.toISOString().slice(0, 10);
const endStr = endDate.toISOString().slice(0, 10);
const qb = this.userRepository
.createQueryBuilder('user')
.where(`DATE(user.created_at) BETWEEN :start AND :end`, {
start: startStr,
end: endStr,
});

return qb.getCount();
}
}
Loading