Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add dashboard endpoints #131

Merged
merged 19 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: cron e query no dashboard
  • Loading branch information
LeonardoSantosBR committed May 16, 2024
commit 16406e90e24dafa327bcdfe256dffae620d3c3bc
48 changes: 48 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/platform-fastify": "^10.3.8",
"@nestjs/schedule": "^4.0.2",
"@nestjs/swagger": "^7.3.1",
"@prisma/client": "^5.13.0",
"bcrypt": "^5.1.1",
Expand Down
6 changes: 4 additions & 2 deletions src/modules/dashboard/dashboard.controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Controller, Get, HttpException, Logger, Query } from '@nestjs/common';
import { DashboardService } from './dashboard.service';
import { ServerResponse } from '@/utils/utils';
import { ApiTags } from '@nestjs/swagger';

@ApiTags('Dashboard')
@Controller('dashboard')
export class DashboardController {
private logger = new Logger();
constructor(private readonly dashboardService: DashboardService) {}

@Get('')
async index() {
async index(@Query() query) {
rodrigooler marked this conversation as resolved.
Show resolved Hide resolved
try {
const data = await this.dashboardService.index();
const data = await this.dashboardService.index(query);
return new ServerResponse(200, 'Successfully get dashboard', data);
} catch (err: any) {
this.logger.error(`Failed to get shelters: ${err}`);
Expand Down
35 changes: 32 additions & 3 deletions src/modules/dashboard/dashboard.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
import * as qs from 'qs';
import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { ShelterSearchPropsSchema } from 'src/shelter/types/search.types';
import { SearchSchema } from 'src/types';
import { ShelterSearch } from 'src/shelter/ShelterSearch';
import { Cron, CronExpression } from '@nestjs/schedule';

@Injectable()
export class DashboardService {
constructor(private readonly prismaService: PrismaService) {}

async index() {
const allShelters = await this.prismaService.shelter.findMany({ include: { shelterSupplies: true }});
@Cron(CronExpression.EVERY_6_HOURS)
rodrigooler marked this conversation as resolved.
Show resolved Hide resolved
async index(query: any) {
rodrigooler marked this conversation as resolved.
Show resolved Hide resolved
const {
order,
orderBy,
page,
perPage,
search: searchQuery,
} = SearchSchema.parse(query);

const queryData = ShelterSearchPropsSchema.parse(qs.parse(searchQuery));
const { query: where } = new ShelterSearch(this.prismaService, queryData);

const take = perPage;
const skip = perPage * (page - 1);

const whereData = {
take,
skip,
orderBy: { [orderBy]: order },
where,
};

const allShelters = await this.prismaService.shelter.findMany({
...whereData,
include: { shelterSupplies: true },
});

const allPeopleSheltered = allShelters.reduce((accumulator, current) => {
return accumulator + (current.shelteredPeople ?? 0);
Expand All @@ -28,7 +58,6 @@ export class DashboardService {
}
});


return {
allShelters: allShelters.length,
allPeopleSheltered: allPeopleSheltered,
Expand Down
2 changes: 2 additions & 0 deletions src/partners/partners.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Controller, Get, HttpException, Logger } from '@nestjs/common';
import { PartnersService } from './partners.service';
import { ServerResponse } from '../utils';
import { ApiTags } from '@nestjs/swagger';

@ApiTags('Parceiros')
@Controller('partners')
export class PartnersController {
private logger = new Logger(PartnersController.name);
Expand Down