Skip to content

Commit 1e3d16c

Browse files
Fix application dashboard fetch
1 parent b320ac5 commit 1e3d16c

File tree

1 file changed

+89
-86
lines changed

1 file changed

+89
-86
lines changed

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

Lines changed: 89 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,20 @@ export class ApplicationService {
7070
queryBuilder.where("app.id IN (:...whitelist)", { whitelist });
7171
}
7272

73-
queryBuilder.andWhere(
74-
new Brackets(qb => {
75-
qb.where("dataTargets.id IS NULL").orWhere("latestMessage.sentTime < NOW() - INTERVAL '24 HOURS'");
76-
})
77-
);
78-
7973
try {
80-
const [result, total] = await queryBuilder.getManyAndCount();
74+
const totalApplications = await queryBuilder.getCount();
75+
76+
queryBuilder.andWhere(
77+
new Brackets(qb => {
78+
qb.where("dataTargets.id IS NULL").orWhere("latestMessage.sentTime < NOW() - INTERVAL '24 HOURS'");
79+
})
80+
);
81+
82+
const errorApplications = await queryBuilder.getCount();
8183

8284
return {
83-
withError: result.length,
84-
total: total,
85+
withError: errorApplications,
86+
total: totalApplications,
8587
};
8688
} catch (error) {
8789
throw new Error("Database query failed");
@@ -169,6 +171,7 @@ export class ApplicationService {
169171
.leftJoinAndSelect("app.belongsTo", "organization")
170172
.leftJoinAndSelect("device.latestReceivedMessage", "latestMessage")
171173
.leftJoinAndSelect("app.dataTargets", "dataTargets")
174+
.leftJoinAndSelect("app.controlledProperties", "controlledProperties")
172175
.andWhere("app.belongsToId = :organizationId", { organizationId: query.organizationId });
173176

174177
if (whitelist && whitelist.length > 0) {
@@ -279,29 +282,6 @@ export class ApplicationService {
279282
};
280283
}
281284

282-
// Some sorting fields can't be done in the database
283-
private externalSortResult(query: ListAllEntitiesDto, result: Application[]) {
284-
// Since openDataDkEnabled is not a database attribute sorting has to be done manually after reading
285-
if (query.orderOn === "openDataDkEnabled") {
286-
result.sort(
287-
(a, b) =>
288-
(query.sort.toLowerCase() === "asc" ? -1 : 1) *
289-
(Number(!!a.dataTargets.find(t => t.type === DataTargetType.OpenDataDK)) -
290-
Number(!!b.dataTargets.find(t => t.type === DataTargetType.OpenDataDK)))
291-
);
292-
}
293-
if (query.orderOn === "devices") {
294-
result.sort(
295-
(a, b) => (query.sort.toLowerCase() === "asc" ? 1 : -1) * (a.iotDevices.length - b.iotDevices.length)
296-
);
297-
}
298-
if (query.orderOn === "dataTargets") {
299-
result.sort(
300-
(a, b) => (query.sort.toLowerCase() === "asc" ? 1 : -1) * (a.dataTargets.length - b.dataTargets.length)
301-
);
302-
}
303-
}
304-
305285
async getApplicationsOnPermissionId(
306286
permissionId: number,
307287
query: ListAllApplicationsDto
@@ -531,48 +511,6 @@ export class ApplicationService {
531511
return false;
532512
}
533513

534-
private async mapApplicationDtoToApplication(
535-
applicationDto: CreateApplicationDto | UpdateApplicationDto,
536-
application: Application,
537-
userId: number
538-
): Promise<Application> {
539-
application.name = applicationDto.name;
540-
application.description = applicationDto.description;
541-
application.belongsTo = await this.organizationService.findById(applicationDto.organizationId);
542-
application.status = applicationDto.status;
543-
// Setting a date to 'undefined' will set it to today in the database
544-
application.startDate = applicationDto.startDate ?? null;
545-
application.endDate = applicationDto.endDate ?? null;
546-
application.category = applicationDto.category;
547-
application.owner = applicationDto.owner;
548-
application.contactPerson = applicationDto.contactPerson;
549-
application.contactEmail = applicationDto.contactEmail;
550-
application.contactPhone = applicationDto.contactPhone;
551-
application.personalData = applicationDto.personalData;
552-
application.hardware = applicationDto.hardware;
553-
application.permissions = await this.permissionService.findManyByIds(applicationDto.permissionIds);
554-
555-
// Set metadata dependencies
556-
application.controlledProperties = applicationDto.controlledProperties
557-
? this.buildControlledPropertyDeviceType(
558-
ControlledPropertyTypes,
559-
applicationDto.controlledProperties,
560-
userId,
561-
ControlledProperty
562-
)
563-
: undefined;
564-
application.deviceTypes = applicationDto.deviceTypes
565-
? this.buildControlledPropertyDeviceType(
566-
ApplicationDeviceTypes,
567-
applicationDto.deviceTypes,
568-
userId,
569-
ApplicationDeviceType
570-
)
571-
: undefined;
572-
573-
return application;
574-
}
575-
576514
buildControlledPropertyDeviceType<
577515
T extends Record<string, string>,
578516
Entity extends ControlledProperty | ApplicationDeviceType
@@ -649,6 +587,83 @@ export class ApplicationService {
649587
return deviceList;
650588
}
651589

590+
public async getFilterInformationInOrganization(
591+
allowedOrganizations: number[],
592+
organizationId: number,
593+
isGlobalAdmin: boolean
594+
) {
595+
if (isGlobalAdmin || allowedOrganizations.some(x => x === organizationId)) {
596+
return await this.findOwnerFilterInformation("admin", organizationId);
597+
}
598+
599+
return await this.findOwnerFilterInformation(allowedOrganizations, organizationId);
600+
}
601+
602+
// Some sorting fields can't be done in the database
603+
private externalSortResult(query: ListAllEntitiesDto, result: Application[]) {
604+
// Since openDataDkEnabled is not a database attribute sorting has to be done manually after reading
605+
if (query.orderOn === "openDataDkEnabled") {
606+
result.sort(
607+
(a, b) =>
608+
(query.sort.toLowerCase() === "asc" ? -1 : 1) *
609+
(Number(!!a.dataTargets.find(t => t.type === DataTargetType.OpenDataDK)) -
610+
Number(!!b.dataTargets.find(t => t.type === DataTargetType.OpenDataDK)))
611+
);
612+
}
613+
if (query.orderOn === "devices") {
614+
result.sort(
615+
(a, b) => (query.sort.toLowerCase() === "asc" ? 1 : -1) * (a.iotDevices.length - b.iotDevices.length)
616+
);
617+
}
618+
if (query.orderOn === "dataTargets") {
619+
result.sort(
620+
(a, b) => (query.sort.toLowerCase() === "asc" ? 1 : -1) * (a.dataTargets.length - b.dataTargets.length)
621+
);
622+
}
623+
}
624+
625+
private async mapApplicationDtoToApplication(
626+
applicationDto: CreateApplicationDto | UpdateApplicationDto,
627+
application: Application,
628+
userId: number
629+
): Promise<Application> {
630+
application.name = applicationDto.name;
631+
application.description = applicationDto.description;
632+
application.belongsTo = await this.organizationService.findById(applicationDto.organizationId);
633+
application.status = applicationDto.status;
634+
// Setting a date to 'undefined' will set it to today in the database
635+
application.startDate = applicationDto.startDate ?? null;
636+
application.endDate = applicationDto.endDate ?? null;
637+
application.category = applicationDto.category;
638+
application.owner = applicationDto.owner;
639+
application.contactPerson = applicationDto.contactPerson;
640+
application.contactEmail = applicationDto.contactEmail;
641+
application.contactPhone = applicationDto.contactPhone;
642+
application.personalData = applicationDto.personalData;
643+
application.hardware = applicationDto.hardware;
644+
application.permissions = await this.permissionService.findManyByIds(applicationDto.permissionIds);
645+
646+
// Set metadata dependencies
647+
application.controlledProperties = applicationDto.controlledProperties
648+
? this.buildControlledPropertyDeviceType(
649+
ControlledPropertyTypes,
650+
applicationDto.controlledProperties,
651+
userId,
652+
ControlledProperty
653+
)
654+
: undefined;
655+
application.deviceTypes = applicationDto.deviceTypes
656+
? this.buildControlledPropertyDeviceType(
657+
ApplicationDeviceTypes,
658+
applicationDto.deviceTypes,
659+
userId,
660+
ApplicationDeviceType
661+
)
662+
: undefined;
663+
664+
return application;
665+
}
666+
652667
private getSortingForIoTDevices(query: ListAllEntitiesDto) {
653668
let orderBy = `iot_device.id`;
654669
if (
@@ -702,16 +717,4 @@ export class ApplicationService {
702717

703718
return sorting;
704719
}
705-
706-
public async getFilterInformationInOrganization(
707-
allowedOrganizations: number[],
708-
organizationId: number,
709-
isGlobalAdmin: boolean
710-
) {
711-
if (isGlobalAdmin || allowedOrganizations.some(x => x === organizationId)) {
712-
return await this.findOwnerFilterInformation("admin", organizationId);
713-
}
714-
715-
return await this.findOwnerFilterInformation(allowedOrganizations, organizationId);
716-
}
717720
}

0 commit comments

Comments
 (0)