-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dashboard endpoints (#131)
Co-authored-by: M4rcxs <[email protected]> Co-authored-by: Marcos Silva <[email protected]>
- Loading branch information
1 parent
25246e8
commit 38da306
Showing
10 changed files
with
272 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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(@Query() query) { | ||
try { | ||
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}`); | ||
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
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 { DefaultArgs } from '@prisma/client/runtime/library'; | ||
import { Prisma } from '@prisma/client'; | ||
|
||
@Injectable() | ||
export class DashboardService { | ||
constructor(private readonly prismaService: PrismaService) {} | ||
|
||
async index(query: any) { | ||
const { | ||
order, | ||
orderBy, | ||
page, | ||
perPage, | ||
search: searchQuery, | ||
} = SearchSchema.parse(query); | ||
const queryData = ShelterSearchPropsSchema.parse(qs.parse(searchQuery)); | ||
const { getQuery } = new ShelterSearch(this.prismaService, queryData); | ||
const where = await getQuery(); | ||
|
||
const take = perPage; | ||
const skip = perPage * (page - 1); | ||
|
||
const whereData: Prisma.ShelterFindManyArgs<DefaultArgs> = { | ||
take, | ||
skip, | ||
orderBy: { [orderBy]: order }, | ||
where, | ||
}; | ||
|
||
const allShelters = await this.prismaService.shelter.findMany({ | ||
...whereData, | ||
select: { | ||
id: true, | ||
name: true, | ||
shelteredPeople: true, | ||
actived: true, | ||
capacity: true, | ||
shelterSupplies: { | ||
select: { | ||
priority: true, | ||
supply: { | ||
select: { | ||
supplyCategory: { | ||
select: { | ||
name: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const categoriesWithPriorities = await this.prismaService.supplyCategory.findMany({ | ||
select: { | ||
id: true, | ||
name: true, | ||
supplies: { | ||
select: { | ||
shelterSupplies: { | ||
select: { | ||
priority: true, | ||
shelterId: true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
const result = categoriesWithPriorities.map(category => { | ||
const priorityCounts = { | ||
priority100: 0, | ||
priority10: 0, | ||
priority1: 0, | ||
}; | ||
|
||
const countedShelters = new Set(); | ||
|
||
category.supplies.forEach(supply => { | ||
supply.shelterSupplies.forEach(shelterSupply => { | ||
if (!countedShelters.has(shelterSupply.shelterId)) { | ||
switch (shelterSupply.priority) { | ||
case 100: | ||
priorityCounts.priority100++; | ||
break; | ||
case 10: | ||
priorityCounts.priority10++; | ||
break; | ||
case 1: | ||
priorityCounts.priority1++; | ||
break; | ||
default: | ||
break; | ||
} | ||
countedShelters.add(shelterSupply.shelterId); | ||
} | ||
}); | ||
}); | ||
|
||
return { | ||
categoryId: category.id, | ||
categoryName: category.name, | ||
...priorityCounts, | ||
}; | ||
}); | ||
|
||
const allPeopleSheltered = allShelters.reduce((accumulator, current) => { | ||
if ( | ||
current.actived && | ||
current.capacity !== null && | ||
current.capacity > 0 | ||
) { | ||
return accumulator + (current.shelteredPeople ?? 0); | ||
} else { | ||
return accumulator; | ||
} | ||
}, 0); | ||
|
||
const numSheltersAvailable = allShelters.filter(shelter => { | ||
if (shelter.actived && shelter.capacity !== null && shelter.capacity > 0) { | ||
return (shelter.shelteredPeople ?? 0) < shelter.capacity; | ||
} | ||
return false; | ||
}).length; | ||
|
||
const numSheltersFull = allShelters.reduce((count, shelter) => { | ||
if (shelter.actived && shelter.capacity !== null && shelter.capacity > 0) { | ||
if ((shelter.shelteredPeople ?? 0) >= shelter.capacity) { | ||
return count + 1; | ||
} | ||
} | ||
return count; | ||
}, 0); | ||
|
||
const shelterWithoutInformation = allShelters.reduce((count, shelter) => { | ||
if (shelter.shelteredPeople === null || shelter.shelteredPeople === undefined) { | ||
return count + 1; | ||
} | ||
return count; | ||
}, 0); | ||
|
||
|
||
return { | ||
allShelters: allShelters.length, | ||
allPeopleSheltered: allPeopleSheltered, | ||
shelterAvaliable: numSheltersAvailable, | ||
shelterFull: numSheltersFull, | ||
shelterWithoutInformation: shelterWithoutInformation, | ||
categoriesWithPriorities: result, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters