|
1 | 1 | import { CreateApplicationDto } from "@dto/create-application.dto"; |
2 | | -import { ListAllApplicationsResponseDto } from "@dto/list-all-applications-response.dto"; |
| 2 | +import { |
| 3 | + ListAllApplicationsResponseDto, |
| 4 | + ListAllApplicationsWithStatusResponseDto, |
| 5 | +} from "@dto/list-all-applications-response.dto"; |
3 | 6 | import { ListAllApplicationsDto } from "@dto/list-all-applications.dto"; |
4 | 7 | import { ListAllEntitiesDto } from "@dto/list-all-entities.dto"; |
5 | 8 | import { ListAllIoTDevicesResponseDto } from "@dto/list-all-iot-devices-response.dto"; |
@@ -50,25 +53,101 @@ export class ApplicationService { |
50 | 53 | ) {} |
51 | 54 |
|
52 | 55 | async findAndCountInList( |
53 | | - query?: ListAllEntitiesDto, |
54 | | - whitelist?: number[], |
55 | | - allFromOrgs?: number[] |
56 | | - ): Promise<ListAllApplicationsResponseDto> { |
| 56 | + query?: ListAllApplicationsDto, |
| 57 | + whitelist?: number[] |
| 58 | + ): Promise<ListAllApplicationsWithStatusResponseDto> { |
57 | 59 | const sorting = this.getSortingForApplications(query); |
58 | | - const orgCondition = |
59 | | - allFromOrgs != null ? { id: In(whitelist), belongsTo: In(allFromOrgs) } : { id: In(whitelist) }; |
60 | | - const [result, total] = await this.applicationRepository.findAndCount({ |
61 | | - where: orgCondition, |
62 | | - take: query.limit, |
63 | | - skip: query.offset, |
64 | | - relations: ["iotDevices", "dataTargets", "controlledProperties", "deviceTypes", nameof<Application>("belongsTo")], |
65 | | - order: sorting, |
66 | | - }); |
67 | 60 |
|
68 | | - return { |
69 | | - data: result, |
70 | | - count: total, |
71 | | - }; |
| 61 | + const queryBuilder = this.applicationRepository |
| 62 | + .createQueryBuilder("app") |
| 63 | + .leftJoinAndSelect("app.iotDevices", "device") |
| 64 | + .leftJoinAndSelect("app.dataTargets", "dataTarget") |
| 65 | + .leftJoinAndSelect("app.belongsTo", "organization") |
| 66 | + .leftJoinAndSelect("device.latestReceivedMessage", "latestMessage"); |
| 67 | + |
| 68 | + if (whitelist && whitelist.length > 0) { |
| 69 | + queryBuilder.where("app.id IN (:...whitelist)", { whitelist }); |
| 70 | + } |
| 71 | + |
| 72 | + if (query.organizationId) { |
| 73 | + queryBuilder.andWhere("app.belongsToId = :organizationId", { organizationId: query.organizationId }); |
| 74 | + } |
| 75 | + |
| 76 | + if (query.status) { |
| 77 | + console.log("status : " + query.status); |
| 78 | + queryBuilder.andWhere("app.status = :status", { status: query.status }); |
| 79 | + } |
| 80 | + |
| 81 | + if (query.owner) { |
| 82 | + queryBuilder.andWhere("app.owner = :owner", { owner: query.owner }); |
| 83 | + } |
| 84 | + |
| 85 | + if (query.statusCheck) { |
| 86 | + queryBuilder.andWhere( |
| 87 | + ` |
| 88 | + app.id IN ( |
| 89 | + SELECT |
| 90 | + app.id, |
| 91 | + CASE |
| 92 | + WHEN COUNT(DISTINCT dataTarget.id) = 0 THEN 'alert' |
| 93 | + WHEN latestMessage.sentTime < NOW() - INTERVAL '24 HOURS' THEN 'alert' |
| 94 | + ELSE 'stable' |
| 95 | + END AS statusCheck |
| 96 | + FROM application app |
| 97 | + LEFT JOIN data_target dataTarget ON dataTarget.applicationId = app.id |
| 98 | + LEFT JOIN device ON device.applicationId = app.id |
| 99 | + LEFT JOIN latestMessage ON latestMessage.deviceId = device.id |
| 100 | + WHERE app.belongsToId = :organizationId |
| 101 | + GROUP BY app.id |
| 102 | + HAVING |
| 103 | + ( |
| 104 | + COUNT(DISTINCT dataTarget.id) = 0 |
| 105 | + OR latestMessage.sentTime < NOW() - INTERVAL '24 HOURS' |
| 106 | + ) |
| 107 | + AND ( |
| 108 | + :statusCheck = 'alert' |
| 109 | + OR COUNT(DISTINCT dataTarget.id) > 0 |
| 110 | + ) |
| 111 | + ) |
| 112 | + `, |
| 113 | + { statusCheck: query.statusCheck, organizationId: query.organizationId } |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + queryBuilder.orderBy(sorting); |
| 118 | + queryBuilder.take(query.limit).skip(query.offset); |
| 119 | + |
| 120 | + try { |
| 121 | + const [result, total] = await queryBuilder.getManyAndCount(); |
| 122 | + const mappedResult = result.map(app => { |
| 123 | + const latestMessage = app.iotDevices |
| 124 | + ?.map(device => device.latestReceivedMessage) |
| 125 | + .find(msg => msg !== undefined); |
| 126 | + const hasDataTargets = app.dataTargets && app.dataTargets.length > 0; |
| 127 | + const isLatestMessageOld = latestMessage |
| 128 | + ? new Date(latestMessage.sentTime) < new Date(Date.now() - 24 * 60 * 60 * 1000) |
| 129 | + : false; |
| 130 | + |
| 131 | + let statusCheck: "stable" | "alert" = "stable"; |
| 132 | + |
| 133 | + if (!hasDataTargets || isLatestMessageOld) { |
| 134 | + statusCheck = "alert"; |
| 135 | + } |
| 136 | + |
| 137 | + return { |
| 138 | + ...app, |
| 139 | + statusCheck, |
| 140 | + }; |
| 141 | + }); |
| 142 | + |
| 143 | + return { |
| 144 | + data: mappedResult, |
| 145 | + count: total, |
| 146 | + }; |
| 147 | + } catch (error) { |
| 148 | + console.error("Error executing query:", error); |
| 149 | + throw new Error("Database query failed"); |
| 150 | + } |
72 | 151 | } |
73 | 152 |
|
74 | 153 | async findAndCountApplicationInWhitelistOrOrganization( |
@@ -512,26 +591,34 @@ export class ApplicationService { |
512 | 591 | } |
513 | 592 | return orderBy; |
514 | 593 | } |
| 594 | + private getSortingForApplications(query: ListAllEntitiesDto): Record<string, "ASC" | "DESC"> { |
| 595 | + const sorting: Record<string, "ASC" | "DESC"> = {}; |
| 596 | + |
| 597 | + if (query.orderOn) { |
| 598 | + const validFields = new Set([ |
| 599 | + "id", |
| 600 | + "name", |
| 601 | + "updatedAt", |
| 602 | + "startDate", |
| 603 | + "endDate", |
| 604 | + "owner", |
| 605 | + "contactPerson", |
| 606 | + "personalData", |
| 607 | + ]); |
| 608 | + |
| 609 | + const sortOrder = query.sort.toUpperCase() === "DESC" ? "DESC" : "ASC"; |
| 610 | + |
| 611 | + if (query.orderOn === "status") { |
| 612 | + sorting["status"] = sortOrder; |
| 613 | + } else if (validFields.has(query.orderOn)) { |
| 614 | + sorting[`app.${query.orderOn}`] = sortOrder; |
| 615 | + } |
| 616 | + } |
515 | 617 |
|
516 | | - private getSortingForApplications(query: ListAllEntitiesDto): Record<string, string | number> { |
517 | | - const sorting: Record<string, string | number> = {}; |
518 | | - if ( |
519 | | - // TODO: Make this nicer |
520 | | - query.orderOn != null && |
521 | | - (query.orderOn === "id" || |
522 | | - query.orderOn === "name" || |
523 | | - query.orderOn === "updatedAt" || |
524 | | - query.orderOn === "status" || |
525 | | - query.orderOn === "startDate" || |
526 | | - query.orderOn === "endDate" || |
527 | | - query.orderOn === "owner" || |
528 | | - query.orderOn === "contactPerson" || |
529 | | - query.orderOn === "personalData") |
530 | | - ) { |
531 | | - sorting[query.orderOn] = query.sort.toLocaleUpperCase(); |
532 | | - } else { |
533 | | - sorting["id"] = "ASC"; |
| 618 | + if (Object.keys(sorting).length === 0) { |
| 619 | + sorting["app.id"] = "ASC"; |
534 | 620 | } |
| 621 | + |
535 | 622 | return sorting; |
536 | 623 | } |
537 | 624 | } |
0 commit comments