@@ -56,7 +56,7 @@ export class ApplicationService {
5656
5757 async countApplicationsWithError (
5858 organizationId : number ,
59- whitelist ?: number [ ]
59+ whitelist ?: number [ ] | "admin"
6060 ) : Promise < ApplicationsWithErrorsResponseDto > {
6161 const queryBuilder = this . applicationRepository
6262 . createQueryBuilder ( "app" )
@@ -66,7 +66,7 @@ export class ApplicationService {
6666 . leftJoin ( "app.dataTargets" , "dataTargets" )
6767 . andWhere ( "app.belongsToId = :organizationId" , { organizationId : organizationId } ) ;
6868
69- if ( whitelist && whitelist . length > 0 ) {
69+ if ( whitelist !== "admin" && whitelist . length > 0 ) {
7070 queryBuilder . where ( "app.id IN (:...whitelist)" , { whitelist } ) ;
7171 }
7272
@@ -87,14 +87,15 @@ export class ApplicationService {
8787 throw new Error ( "Database query failed" ) ;
8888 }
8989 }
90- async countAllDevices ( organizationId : number , whitelist ?: number [ ] ) : Promise < number > {
90+
91+ async countAllDevices ( organizationId : number , whitelist ?: number [ ] | "admin" ) : Promise < number > {
9192 const queryBuilder = this . applicationRepository
9293 . createQueryBuilder ( "app" )
9394 . leftJoinAndSelect ( "app.iotDevices" , "device" )
9495 . leftJoin ( "app.dataTargets" , "dataTargets" )
9596 . where ( "app.belongsToId = :organizationId" , { organizationId } ) ;
9697
97- if ( whitelist && whitelist . length > 0 ) {
98+ if ( whitelist != "admin" && whitelist . length > 0 ) {
9899 queryBuilder . andWhere ( "app.id IN (:...whitelist)" , { whitelist } ) ;
99100 }
100101
@@ -110,7 +111,7 @@ export class ApplicationService {
110111 async getAllDevices (
111112 organizationId : number ,
112113 query ?: ListAllIotDevicesDto ,
113- whitelist ?: number [ ] | null
114+ whitelist ?: number [ ] | "admin"
114115 ) : Promise < IoTDevice [ ] > {
115116 const queryBuilder = this . iotDeviceRepository
116117 . createQueryBuilder ( "device" )
@@ -120,7 +121,7 @@ export class ApplicationService {
120121 . addSelect ( [ "latestMessage.id" , "latestMessage.sentTime" ] )
121122 . where ( "app.belongsToId = :organizationId" , { organizationId } ) ;
122123
123- if ( whitelist && whitelist . length > 0 ) {
124+ if ( whitelist !== "admin" && whitelist . length > 0 ) {
124125 queryBuilder . andWhere ( "app.id IN (:...whitelist)" , { whitelist } ) ;
125126 }
126127
@@ -149,8 +150,7 @@ export class ApplicationService {
149150 }
150151
151152 try {
152- const test = await queryBuilder . getMany ( ) ;
153- return test ;
153+ return await queryBuilder . getMany ( ) ;
154154 } catch ( error ) {
155155 console . error ( "Database query failed:" , error ) ;
156156 throw new Error ( "Database query failed" ) ;
@@ -168,16 +168,13 @@ export class ApplicationService {
168168 . leftJoinAndSelect ( "app.iotDevices" , "device" )
169169 . leftJoinAndSelect ( "app.belongsTo" , "organization" )
170170 . leftJoinAndSelect ( "device.latestReceivedMessage" , "latestMessage" )
171- . leftJoinAndSelect ( "app.dataTargets" , "dataTargets" ) ;
171+ . leftJoinAndSelect ( "app.dataTargets" , "dataTargets" )
172+ . andWhere ( "app.belongsToId = :organizationId" , { organizationId : query . organizationId } ) ;
172173
173174 if ( whitelist && whitelist . length > 0 ) {
174175 queryBuilder . where ( "app.id IN (:...whitelist)" , { whitelist } ) ;
175176 }
176177
177- if ( query . organizationId ) {
178- queryBuilder . andWhere ( "app.belongsToId = :organizationId" , { organizationId : query . organizationId } ) ;
179- }
180-
181178 if ( query . status ) {
182179 queryBuilder . andWhere ( "app.status = :status" , { status : query . status } ) ;
183180 }
@@ -338,7 +335,7 @@ export class ApplicationService {
338335 return await this . applicationRepository . findOneByOrFail ( { id } ) ;
339336 }
340337
341- async findFilterInformation ( applicationIds : number [ ] | "admin" , organizationId : number ) {
338+ async findOwnerFilterInformation ( applicationIds : number [ ] | "admin" , organizationId : number ) {
342339 const query = this . applicationRepository
343340 . createQueryBuilder ( "application" )
344341 . leftJoinAndSelect ( "application.belongsTo" , "organization" )
@@ -676,14 +673,22 @@ export class ApplicationService {
676673 }
677674 return orderBy ;
678675 }
676+
679677 private getSortingForApplications ( query : ListAllEntitiesDto ) : Record < string , "ASC" | "DESC" > {
680678 const sorting : Record < string , "ASC" | "DESC" > = { } ;
681679
682- if ( query . orderOn === "statusCheck" ) {
683- return sorting ;
684- }
685-
686- if ( query . orderOn ) {
680+ if (
681+ query . orderOn != null &&
682+ ( query . orderOn === "id" ||
683+ query . orderOn === "name" ||
684+ query . orderOn === "updatedAt" ||
685+ query . orderOn === "status" ||
686+ query . orderOn === "startDate" ||
687+ query . orderOn === "endDate" ||
688+ query . orderOn === "owner" ||
689+ query . orderOn === "contactPerson" ||
690+ query . orderOn === "personalData" )
691+ ) {
687692 const sortOrder = query . sort . toUpperCase ( ) === "DESC" ? "DESC" : "ASC" ;
688693
689694 sorting [ `app.${ query . orderOn } ` ] = sortOrder ;
@@ -695,4 +700,16 @@ export class ApplicationService {
695700
696701 return sorting ;
697702 }
703+
704+ public async getFilterInformationInOrganization (
705+ allowedOrganizations : number [ ] ,
706+ organizationId : number ,
707+ isGlobalAdmin : boolean
708+ ) {
709+ if ( isGlobalAdmin || allowedOrganizations . some ( x => x === organizationId ) ) {
710+ return await this . findOwnerFilterInformation ( "admin" , organizationId ) ;
711+ }
712+
713+ return await this . findOwnerFilterInformation ( allowedOrganizations , organizationId ) ;
714+ }
698715}
0 commit comments