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 3 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 @@ -12,6 +12,7 @@ import { SupplyCategoriesModule } from './supply-categories/supply-categories.mo
import { ShelterManagersModule } from './shelter-managers/shelter-managers.module';
import { ShelterSupplyModule } from './shelter-supply/shelter-supply.module';
import { PartnersModule } from './partners/partners.module';
import { DashboardModule } from './modules/dashboard/dashboard.module';

@Module({
imports: [
Expand All @@ -24,6 +25,7 @@ import { PartnersModule } from './partners/partners.module';
ShelterManagersModule,
ShelterSupplyModule,
PartnersModule,
DashboardModule,
],
controllers: [],
providers: [
Expand Down
20 changes: 20 additions & 0 deletions src/modules/dashboard/dashboard.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { DashboardController } from './dashboard.controller';
import { DashboardService } from './dashboard.service';

describe('DashboardController', () => {
let controller: DashboardController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [DashboardController],
providers: [DashboardService],
}).compile();

controller = module.get<DashboardController>(DashboardController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
20 changes: 20 additions & 0 deletions src/modules/dashboard/dashboard.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Controller, Get, HttpException, Logger, Query } from '@nestjs/common';
import { DashboardService } from './dashboard.service';
import { ServerResponse } from '@/utils/utils';

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

@Get('')
async index() {
try {
const data = await this.dashboardService.index();
return new ServerResponse(200, 'Successfully get dashboard', data);
} catch (err: any) {
this.logger.error(`Failed to get shelters: ${err}`);
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
}
}
}
11 changes: 11 additions & 0 deletions src/modules/dashboard/dashboard.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { DashboardService } from './dashboard.service';
import { DashboardController } from './dashboard.controller';
import { PrismaModule } from 'src/prisma/prisma.module';

@Module({
imports: [PrismaModule],
controllers: [DashboardController],
providers: [DashboardService],
})
export class DashboardModule {}
18 changes: 18 additions & 0 deletions src/modules/dashboard/dashboard.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { DashboardService } from './dashboard.service';

describe('DashboardService', () => {
let service: DashboardService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [DashboardService],
}).compile();

service = module.get<DashboardService>(DashboardService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
40 changes: 40 additions & 0 deletions src/modules/dashboard/dashboard.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';

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

async index() {
const allShelters = await this.prismaService.shelter.findMany({ include: { shelterSupplies: true }});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não é nada indicado buscar todos os dados do banco assim e fazer processamento em memória, o correto é fazer a consulta SQL já trazendo as informações necessárias


const allPeopleSheltered = allShelters.reduce((accumulator, current) => {
return accumulator + (current.shelteredPeople ?? 0);
}, 0);

let shelterAvailable = 0;
let shelterFull = 0;
let shelterWithoutInformation = 0;

allShelters.forEach((shelter) => {
if (shelter.shelteredPeople !== null && shelter.capacity !== null) {
if (shelter.shelteredPeople < shelter.capacity) {
shelterAvailable++;
} else if (shelter.shelteredPeople >= shelter.capacity) {
shelterFull++;
}
} else {
shelterWithoutInformation++;
}
});


return {
allShelters: allShelters.length,
allPeopleSheltered: allPeopleSheltered,
shelterAvailable: shelterAvailable,
shelterFull: shelterFull,
shelterWithoutInformation: shelterWithoutInformation,
};
}
}
1 change: 1 addition & 0 deletions src/modules/dashboard/entities/dashboard.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class Dashboard {}
rodrigooler marked this conversation as resolved.
Show resolved Hide resolved