@@ -38,11 +38,13 @@ import { ListAllApplicationsDto } from "@dto/list-all-applications.dto";
3838import { ListAllEntitiesDto } from "@dto/list-all-entities.dto" ;
3939import { ListAllIoTDevicesResponseDto } from "@dto/list-all-iot-devices-response.dto" ;
4040import { IoTDevicesListToMapResponseDto } from "@dto/list-all-iot-devices-to-map-response.dto" ;
41+ import { ListAllIotDevicesDto } from "@dto/list-all-iot-devices.dto" ;
4142import { UpdateApplicationOrganizationDto } from "@dto/update-application-organization.dto" ;
4243import { UpdateApplicationDto } from "@dto/update-application.dto" ;
4344import { Application } from "@entities/application.entity" ;
4445import { ActionType } from "@entities/audit-log-entry" ;
4546import { AuthenticatedRequest } from "@entities/dto/internal/authenticated-request" ;
47+ import { IoTDevice } from "@entities/iot-device.entity" ;
4648import { ErrorCodes } from "@enum/error-codes.enum" ;
4749import {
4850 ApplicationAccessScope ,
@@ -111,7 +113,11 @@ export class ApplicationController {
111113 @Param ( "id" , new ParseIntPipe ( ) ) id : number
112114 ) : Promise < ApplicationDashboardResponseDto > {
113115 try {
114- return await this . getApplicationsWithError ( req , id , req . user . permissions . isGlobalAdmin ) ;
116+ const whitelist = await this . getApplicationsWhiteList ( req , id , req . user . permissions . isGlobalAdmin ) ;
117+ return {
118+ ...( await this . applicationService . countApplicationsWithError ( id , whitelist ) ) ,
119+ totalDevices : await this . applicationService . countAllDevices ( id , whitelist ) ,
120+ } ;
115121 } catch ( err ) {
116122 throw new NotFoundException ( ErrorCodes . IdDoesNotExists ) ;
117123 }
@@ -149,6 +155,23 @@ export class ApplicationController {
149155 }
150156 }
151157
158+ @Read ( )
159+ @Get ( ":id/iot-devices-org" )
160+ @ApiOperation ( { summary : "Find the IoTDevice of an organization" } )
161+ @ApiNotFoundResponse ( )
162+ async findIoTDevicesForOrganization (
163+ @Req ( ) req : AuthenticatedRequest ,
164+ @Param ( "id" , new ParseIntPipe ( ) ) organizationId : number ,
165+ @Query ( ) query ?: ListAllIotDevicesDto
166+ ) : Promise < IoTDevice [ ] > {
167+ try {
168+ const whitelist = await this . getApplicationsWhiteList ( req , organizationId , req . user . permissions . isGlobalAdmin ) ;
169+ return await this . applicationService . getAllDevices ( organizationId , query ) ;
170+ } catch ( err ) {
171+ throw new NotFoundException ( ErrorCodes . IdDoesNotExists ) ;
172+ }
173+ }
174+
152175 @Read ( )
153176 @Get ( ":id/iot-devices-map" )
154177 @ApiOperation ( { summary : "Find the IoTDevices of an Application" } )
@@ -313,27 +336,21 @@ export class ApplicationController {
313336 return await this . applicationService . findFilterInformation ( allowedApplications , organizationId ) ;
314337 }
315338
316- private async getApplicationsWithError ( req : AuthenticatedRequest , organizationId : number , isGlobalAdmin : boolean ) {
339+ private async getApplicationsWhiteList (
340+ req : AuthenticatedRequest ,
341+ organizationId : number ,
342+ isGlobalAdmin : boolean
343+ ) : Promise < number [ ] | null > {
317344 if ( isGlobalAdmin ) {
318- return {
319- ...( await this . applicationService . countApplicationsWithError ( organizationId ) ) ,
320- totalDevices : await this . applicationService . countAllDevices ( organizationId ) ,
321- } ;
345+ return null ;
322346 }
323347
324348 const allFromOrg = req . user . permissions . getAllOrganizationsWithUserAdmin ( ) ;
325349
326350 if ( allFromOrg . some ( x => x === organizationId ) ) {
327- return {
328- ...( await this . applicationService . countApplicationsWithError ( organizationId ) ) ,
329- totalDevices : await this . applicationService . countAllDevices ( organizationId ) ,
330- } ;
351+ return null ;
331352 }
332353
333- const allowedApplications = req . user . permissions . getAllApplicationsWithAtLeastRead ( ) ;
334- return {
335- ...( await this . applicationService . countApplicationsWithError ( organizationId , allowedApplications ) ) ,
336- totalDevices : await this . applicationService . countAllDevices ( organizationId , allowedApplications ) ,
337- } ;
354+ return req . user . permissions . getAllApplicationsWithAtLeastRead ( ) ;
338355 }
339356}
0 commit comments