Skip to content

Commit 7d7a8d4

Browse files
changes from pr
1 parent e67e517 commit 7d7a8d4

File tree

2 files changed

+59
-62
lines changed

2 files changed

+59
-62
lines changed

src/controllers/admin-controller/application.controller.ts

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,13 @@ export class ApplicationController {
9797
@Param("id", new ParseIntPipe()) id: number
9898
): Promise<string[]> {
9999
try {
100-
return await this.getFilterInformationInOrganization(req, id, req.user.permissions.isGlobalAdmin);
100+
const allOrgs = req.user.permissions.getAllOrganizationsWithUserAdmin();
101+
102+
return await this.applicationService.getFilterInformationInOrganization(
103+
allOrgs,
104+
id,
105+
req.user.permissions.isGlobalAdmin
106+
);
101107
} catch (err) {
102108
throw new NotFoundException(ErrorCodes.IdDoesNotExists);
103109
}
@@ -113,10 +119,17 @@ export class ApplicationController {
113119
@Param("id", new ParseIntPipe()) id: number
114120
): Promise<ApplicationDashboardResponseDto> {
115121
try {
116-
const whitelist = await this.getApplicationsWhiteList(req, id, req.user.permissions.isGlobalAdmin);
122+
const allOrgs = req.user.permissions.getAllOrganizationsWithUserAdmin();
123+
117124
return {
118-
...(await this.applicationService.countApplicationsWithError(id, whitelist)),
119-
totalDevices: await this.applicationService.countAllDevices(id, whitelist),
125+
...(await this.applicationService.countApplicationsWithError(
126+
id,
127+
req.user.permissions.isGlobalAdmin ? "admin" : allOrgs
128+
)),
129+
totalDevices: await this.applicationService.countAllDevices(
130+
id,
131+
req.user.permissions.isGlobalAdmin ? "admin" : allOrgs
132+
),
120133
};
121134
} catch (err) {
122135
throw new NotFoundException(ErrorCodes.IdDoesNotExists);
@@ -165,8 +178,12 @@ export class ApplicationController {
165178
@Query() query?: ListAllIotDevicesDto
166179
): Promise<IoTDevice[]> {
167180
try {
168-
const whitelist = await this.getApplicationsWhiteList(req, organizationId, req.user.permissions.isGlobalAdmin);
169-
return await this.applicationService.getAllDevices(organizationId, query);
181+
const allOrgs = req.user.permissions.getAllOrganizationsWithUserAdmin();
182+
return await this.applicationService.getAllDevices(
183+
organizationId,
184+
query,
185+
req.user.permissions.isGlobalAdmin ? "admin" : allOrgs
186+
);
170187
} catch (err) {
171188
throw new NotFoundException(ErrorCodes.IdDoesNotExists);
172189
}
@@ -316,41 +333,4 @@ export class ApplicationController {
316333
const allowedApplications = req.user.permissions.getAllApplicationsWithAtLeastRead();
317334
return await this.applicationService.findAndCountInList(query, allowedApplications);
318335
}
319-
320-
private async getFilterInformationInOrganization(
321-
req: AuthenticatedRequest,
322-
organizationId: number,
323-
isGlobalAdmin: boolean
324-
) {
325-
if (isGlobalAdmin) {
326-
return await this.applicationService.findFilterInformation("admin", organizationId);
327-
}
328-
329-
const allFromOrg = req.user.permissions.getAllOrganizationsWithUserAdmin();
330-
331-
if (allFromOrg.some(x => x === organizationId)) {
332-
return await this.applicationService.findFilterInformation("admin", organizationId);
333-
}
334-
335-
const allowedApplications = req.user.permissions.getAllApplicationsWithAtLeastRead();
336-
return await this.applicationService.findFilterInformation(allowedApplications, organizationId);
337-
}
338-
339-
private async getApplicationsWhiteList(
340-
req: AuthenticatedRequest,
341-
organizationId: number,
342-
isGlobalAdmin: boolean
343-
): Promise<number[] | null> {
344-
if (isGlobalAdmin) {
345-
return null;
346-
}
347-
348-
const allFromOrg = req.user.permissions.getAllOrganizationsWithUserAdmin();
349-
350-
if (allFromOrg.some(x => x === organizationId)) {
351-
return null;
352-
}
353-
354-
return req.user.permissions.getAllApplicationsWithAtLeastRead();
355-
}
356336
}

src/services/device-management/application.service.ts

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)