1+ import { LoRaWANDevice } from '@entities/lorawan-device.entity' ;
12import { Inject , Injectable , forwardRef , ConflictException } from "@nestjs/common" ;
23import { InjectRepository } from "@nestjs/typeorm" ;
34import { DeleteResult , getManager , In , QueryBuilder , Repository } from "typeorm" ;
4-
55import { CreateApplicationDto } from "@dto/create-application.dto" ;
66import { ListAllApplicationsResponseDto } from "@dto/list-all-applications-response.dto" ;
77import { ListAllEntitiesDto } from "@dto/list-all-entities.dto" ;
@@ -14,7 +14,6 @@ import { IoTDeviceType } from "@enum/device-type.enum";
1414import { LoRaWANDeviceWithChirpstackDataDto } from "@dto/lorawan-device-with-chirpstack-data.dto" ;
1515import { CreateLoRaWANSettingsDto } from "@dto/create-lorawan-settings.dto" ;
1616import { PermissionService } from "@services/user-management/permission.service" ;
17- import { ListAllPaginated } from "@dto/list-all-paginated.dto" ;
1817import { ErrorCodes } from "@enum/error-codes.enum" ;
1918import { IoTDevice } from "@entities/iot-device.entity" ;
2019import { ListAllIoTDevicesResponseDto } from "@dto/list-all-iot-devices-response.dto" ;
@@ -25,11 +24,11 @@ export class ApplicationService {
2524 @InjectRepository ( Application )
2625 private applicationRepository : Repository < Application > ,
2726 @Inject ( forwardRef ( ( ) => OrganizationService ) )
28- private organizationService : OrganizationService ,
27+ private organizationService : OrganizationService ,
2928 private chirpstackDeviceService : ChirpstackDeviceService ,
3029 @Inject ( forwardRef ( ( ) => PermissionService ) )
3130 private permissionService : PermissionService
32- ) { }
31+ ) { }
3332
3433 async findAndCountInList (
3534 query ?: ListAllEntitiesDto ,
@@ -63,9 +62,9 @@ export class ApplicationService {
6362 where :
6463 organizationIds . length > 0
6564 ? [
66- { id : In ( allowedApplications ) } ,
67- { belongsTo : In ( organizationIds ) } ,
68- ]
65+ { id : In ( allowedApplications ) } ,
66+ { belongsTo : In ( organizationIds ) } ,
67+ ]
6968 : { id : In ( allowedApplications ) } ,
7069 take : query . limit ,
7170 skip : query . offset ,
@@ -237,12 +236,12 @@ export class ApplicationService {
237236 }
238237
239238 async delete ( id : number ) : Promise < DeleteResult > {
240- // Don't allow delete if this application contains any sigfox devices.
241239 const application = await this . applicationRepository . findOne ( {
242240 where : { id : id } ,
243241 relations : [ "iotDevices" ] ,
244242 } ) ;
245243
244+ // Don't allow delete if this application contains any sigfox devices.
246245 if (
247246 application . iotDevices . some ( iotDevice => {
248247 return iotDevice . type == IoTDeviceType . SigFox ;
@@ -251,6 +250,15 @@ export class ApplicationService {
251250 throw new ConflictException ( ErrorCodes . DeleteNotAllowedHasSigfoxDevice ) ;
252251 }
253252
253+ // Delete all LoRaWAN devices in ChirpStack
254+ const loRaWANDevices = application . iotDevices
255+ . filter ( device => device . type === IoTDeviceType . LoRaWAN ) ;
256+
257+ for ( let device of loRaWANDevices ) {
258+ const lwDevice = device as LoRaWANDevice ;
259+ await this . chirpstackDeviceService . deleteDevice ( lwDevice . deviceEUI ) ;
260+ }
261+
254262 return this . applicationRepository . delete ( id ) ;
255263 }
256264
0 commit comments