Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@nestjs/swagger": "^6.1.2",
"@nestjs/typeorm": "^9.0.1",
"@types/bcryptjs": "^2.4.2",
"@types/geojson": "^7946.0.7",
"@types/geojson": "^7946.0.13",
"@types/kafkajs": "^1.9.0",
"@types/passport-saml": "^1.1.3",
"@types/pem": "^1.9.6",
Expand Down Expand Up @@ -92,7 +92,7 @@
"@types/cron": "^1.7.2",
"@types/crypto-js": "^4.1.1",
"@types/express": "^4.17.9",
"@types/geojson": "^7946.0.7",
"@types/geojson": "^7946.0.13",
"@types/kafkajs": "^1.9.0",
"@types/lodash": "^4.14.165",
"@types/node": "^14.14.14",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ import {
Req,
UseGuards,
} from "@nestjs/common";
import {
ApiBadRequestResponse,
ApiBearerAuth,
ApiOperation,
ApiProduces,
ApiTags,
} from "@nestjs/swagger";
import { ApiBadRequestResponse, ApiBearerAuth, ApiOperation, ApiProduces, ApiTags } from "@nestjs/swagger";

import { Read, GatewayAdmin } from "@auth/roles.decorator";
import { RolesGuard } from "@auth/roles.guard";
Expand Down Expand Up @@ -48,21 +42,15 @@ export class ChirpstackGatewayController {
@ApiOperation({ summary: "Create a new Chirpstack Gateway" })
@ApiBadRequestResponse()
@GatewayAdmin()
async create(
@Req() req: AuthenticatedRequest,
@Body() dto: CreateGatewayDto
): Promise<ChirpstackResponseStatus> {
async create(@Req() req: AuthenticatedRequest, @Body() dto: CreateGatewayDto): Promise<ChirpstackResponseStatus> {
checkIfUserHasAccessToOrganization(req, dto.organizationId, OrganizationAccessScope.GatewayWrite);
try {
const gateway = await this.chirpstackGatewayService.createNewGateway(
dto,
req.user.userId
);
const gateway = await this.chirpstackGatewayService.createNewGateway(dto, req.user.userId);
AuditLog.success(
ActionType.CREATE,
"ChirpstackGateway",
req.user.userId,
dto.gateway.id,
dto.gateway.gatewayId,
dto.gateway.name
);
return gateway;
Expand All @@ -71,7 +59,7 @@ export class ChirpstackGatewayController {
ActionType.CREATE,
"ChirpstackGateway",
req.user.userId,
dto.gateway.id,
dto.gateway.gatewayId,
dto.gateway.name
);
if (err?.response?.data?.message == "object already exists") {
Expand All @@ -94,9 +82,7 @@ export class ChirpstackGatewayController {
@ApiProduces("application/json")
@ApiOperation({ summary: "List all Chirpstack gateways" })
@Read()
async getOne(
@Param("gatewayId") gatewayId: string
): Promise<SingleGatewayResponseDto> {
async getOne(@Param("gatewayId") gatewayId: string): Promise<SingleGatewayResponseDto> {
if (gatewayId?.length != 16) {
throw new BadRequestException(ErrorCodes.WrongLength);
}
Expand All @@ -119,30 +105,14 @@ export class ChirpstackGatewayController {
@Body() dto: UpdateGatewayDto
): Promise<ChirpstackResponseStatus> {
try {
if (dto.gateway.id) {
if (dto.gateway.gatewayId) {
throw new BadRequestException(ErrorCodes.GatewayIdNotAllowedInUpdate);
}
const gateway = await this.chirpstackGatewayService.modifyGateway(
gatewayId,
dto,
req
);
AuditLog.success(
ActionType.UPDATE,
"ChirpstackGateway",
req.user.userId,
gatewayId,
dto.gateway.name
);
const gateway = await this.chirpstackGatewayService.modifyGateway(gatewayId, dto, req);
AuditLog.success(ActionType.UPDATE, "ChirpstackGateway", req.user.userId, gatewayId, dto.gateway.name);
return gateway;
} catch (err) {
AuditLog.fail(
ActionType.UPDATE,
"ChirpstackGateway",
req.user.userId,
gatewayId,
dto.gateway.name
);
AuditLog.fail(ActionType.UPDATE, "ChirpstackGateway", req.user.userId, gatewayId, dto.gateway.name);
throw err;
}
}
Expand All @@ -155,30 +125,18 @@ export class ChirpstackGatewayController {
): Promise<ChirpstackResponseStatus> {
try {
const gw = await this.chirpstackGatewayService.getOne(gatewayId);
if (gw.gateway.internalOrganizationId != null) {
if (gw.gateway.organizationId != null) {
checkIfUserHasAccessToOrganization(
req,
+gw.gateway.internalOrganizationId,
+gw.gateway.organizationId,
OrganizationAccessScope.GatewayWrite
);
}
const deleteResult = await this.chirpstackGatewayService.deleteGateway(
gatewayId
);
AuditLog.success(
ActionType.DELETE,
"ChirpstackGateway",
req.user.userId,
gatewayId
);
const deleteResult = await this.chirpstackGatewayService.deleteGateway(gatewayId);
AuditLog.success(ActionType.DELETE, "ChirpstackGateway", req.user.userId, gatewayId);
return deleteResult;
} catch (err) {
AuditLog.fail(
ActionType.DELETE,
"ChirpstackGateway",
req.user.userId,
gatewayId
);
AuditLog.fail(ActionType.DELETE, "ChirpstackGateway", req.user.userId, gatewayId);
throw err;
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/entities/dto/chirpstack/detailed-gateway-response.dto.ts

This file was deleted.

23 changes: 7 additions & 16 deletions src/entities/dto/chirpstack/gateway-contents.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,9 @@ import {
MinLength,
ValidateNested,
} from "class-validator";

import { ChirpstackBoardsDto } from "@dto/chirpstack/chirpstack-boards.dto";
import { CommonLocationDto } from "@dto/chirpstack/common-location.dto";

export class GatewayContentsDto {
@ApiProperty({
required: false,
default: [],
type: [ChirpstackBoardsDto],
})
@IsOptional()
@ValidateNested({ each: true })
@Type(() => ChirpstackBoardsDto)
boards?: ChirpstackBoardsDto[];

@ApiProperty({ required: true })
@IsOptional()
@IsString()
Expand All @@ -36,14 +24,11 @@ export class GatewayContentsDto {
@IsOptional()
discoveryEnabled: boolean;

@ApiHideProperty()
gatewayProfileID?: string;

@ApiProperty({ required: true })
@IsString()
@IsHexadecimal()
@Length(16, 16)
id: string;
gatewayId: string;

@ApiProperty({ required: false })
@ValidateNested({ each: true })
Expand Down Expand Up @@ -74,4 +59,10 @@ export class GatewayContentsDto {

@ApiHideProperty()
tags?: { [id: string]: string | number };

@ApiHideProperty()
gatewayProfileID?: string;

@ApiHideProperty()
id: string;
}
23 changes: 10 additions & 13 deletions src/entities/dto/chirpstack/gateway-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { CommonLocationDto } from "@dto/chirpstack/common-location.dto";

export class GatewayResponseDto {
id: string;
id: number;
gatewayId: string;
name: string;
description: string;
organizationID: string;
networkServerID: string;
description?: string;
rxPacketsReceived: number;
txPacketsEmitted: number;
organizationId: number;
organizationName: string;
location: CommonLocationDto;
tags: { [id: string]: string | number };
tagsString: string;

networkServerName?: string;
createdAt?: string;
updatedAt?: string;
firstSeenAt?: string;
lastSeenAt?: string;

internalOrganizationId: number;
createdAt?: Date;
updatedAt?: Date;
lastSeenAt?: Date;
updatedBy?: number;
createdBy?: number;
}
10 changes: 2 additions & 8 deletions src/entities/dto/chirpstack/single-gateway-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { DetailedGatewayResponseDto } from "@dto/chirpstack/detailed-gateway-response.dto";
import { GatewayStatsElementDto } from "@dto/chirpstack/gateway-stats.response.dto";
import { GatewayResponseDto } from "@dto/chirpstack/gateway-response.dto";

export class SingleGatewayResponseDto {
gateway: DetailedGatewayResponseDto;

createdAt?: string;
updatedAt?: string;
firstSeenAt?: string;
lastSeenAt?: string;

gateway: GatewayResponseDto;
stats: GatewayStatsElementDto[];
}
4 changes: 2 additions & 2 deletions src/entities/dto/chirpstack/update-gateway.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { ValidateNested } from "class-validator";
import { GatewayContentsDto } from "./gateway-contents.dto";
import { Type } from "class-transformer";

export class UpdateGatewayContentsDto extends OmitType(GatewayContentsDto, ["id"]) {
export class UpdateGatewayContentsDto extends OmitType(GatewayContentsDto, ["gatewayId"]) {
@ApiHideProperty()
id: string;
gatewayId: string;
}

export class UpdateGatewayDto {
Expand Down
42 changes: 42 additions & 0 deletions src/entities/gateway.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { DbBaseEntity } from "@entities/base.entity";
import { Column, Entity, ManyToOne } from "typeorm";
import { Organization } from "@entities/organization.entity";
import { Point } from "geojson";

@Entity("gateway")
export class Gateway extends DbBaseEntity {
@Column()
name: string;

@Column({ nullable: true })
description?: string;

@Column()
gatewayId: string;

@ManyToOne(_ => Organization, organization => organization.gateways, { onDelete: "CASCADE" })
organization: Organization;

@Column()
rxPacketsReceived: number;

@Column()
txPacketsEmitted: number;

@Column()
tags: string;

@Column({
type: "geometry",
nullable: true,
spatialFeatureType: "Point",
srid: 4326,
})
location?: Point;

@Column({ type: "decimal", nullable: true })
altitude?: number;

@Column({ nullable: true })
lastSeenAt?: Date;
}
4 changes: 4 additions & 0 deletions src/entities/organization.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Permission } from "@entities/permissions/permission.entity";
import { SigFoxGroup } from "./sigfox-group.entity";
import { DeviceModel } from "./device-model.entity";
import { User } from "./user.entity";
import { Gateway } from "@entities/gateway.entity";

@Entity("organization")
@Unique(["name"])
Expand Down Expand Up @@ -60,4 +61,7 @@ export class Organization extends DbBaseEntity {
nullable: true,
})
awaitingUsers?: User[];

@OneToMany(_ => Gateway, gateway => gateway.organization, { nullable: true })
gateways?: Gateway[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class CreatedDatabaseEntityForGateways1701090974275 implements MigrationInterface {
name = 'CreatedDatabaseEntityForGateways1701090974275'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "gateway" ("id" SERIAL NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "name" character varying NOT NULL, "description" character varying, "gatewayId" character varying NOT NULL, "rxPacketsReceived" integer NOT NULL, "txPacketsEmitted" integer NOT NULL, "tags" character varying NOT NULL, "location" geometry(Point,4326), "lastSeenAt" TIMESTAMP, "createdById" integer, "updatedById" integer, "organizationId" integer, CONSTRAINT "PK_22c5b7ecdd6313de143815f9991" PRIMARY KEY ("id"))`);
await queryRunner.query(`ALTER TABLE "gateway" ADD CONSTRAINT "FK_a869dbb323b91ec9fa272654ce1" FOREIGN KEY ("createdById") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "gateway" ADD CONSTRAINT "FK_9916ae412fb9b02b981d094697b" FOREIGN KEY ("updatedById") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "gateway" ADD CONSTRAINT "FK_66c3f3b7ccab40c8735e2cf07e7" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "gateway" DROP CONSTRAINT "FK_66c3f3b7ccab40c8735e2cf07e7"`);
await queryRunner.query(`ALTER TABLE "gateway" DROP CONSTRAINT "FK_9916ae412fb9b02b981d094697b"`);
await queryRunner.query(`ALTER TABLE "gateway" DROP CONSTRAINT "FK_a869dbb323b91ec9fa272654ce1"`);
await queryRunner.query(`DROP TABLE "gateway"`);
}

}
14 changes: 14 additions & 0 deletions src/migration/1701158191841-added-altitude-to-gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddedAltitudeToGateway1701158191841 implements MigrationInterface {
name = 'AddedAltitudeToGateway1701158191841'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "gateway" ADD "altitude" numeric`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "gateway" DROP COLUMN "altitude"`);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { GenericChirpstackConfigurationService } from "@services/chirpstack/gene
import { ChirpstackSetupNetworkServerService } from "@services/chirpstack/network-server.service";
import { ServiceProfileService } from "@services/chirpstack/service-profile.service";
import { NetworkServerController } from "@admin-controller/chirpstack/network-server.controller";
import { SharedModule } from "@modules/shared.module";
import { OrganizationModule } from "@modules/user-management/organization.module";

@Module({
controllers: [
Expand All @@ -20,7 +22,7 @@ import { NetworkServerController } from "@admin-controller/chirpstack/network-se
DeviceProfileController,
NetworkServerController,
],
imports: [HttpModule, ConfigModule.forRoot({ load: [configuration] })],
imports: [SharedModule, HttpModule, OrganizationModule, ConfigModule.forRoot({ load: [configuration] })],
providers: [
GenericChirpstackConfigurationService,
ChirpstackSetupNetworkServerService,
Expand Down
3 changes: 2 additions & 1 deletion src/modules/device-integrations/lorawan-gateway.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { GatewayStatusHistoryService } from "@services/chirpstack/gateway-status
import { ChirpstackSetupNetworkServerService } from "@services/chirpstack/network-server.service";
import { GatewayBootstrapperService } from "@services/chirpstack/gateway-boostrapper.service";
import { HttpModule } from "@nestjs/axios";
import { OrganizationModule } from "@modules/user-management/organization.module";

@Module({
controllers: [LoRaWANGatewayController],
imports: [SharedModule, HttpModule],
imports: [SharedModule, HttpModule, OrganizationModule],
providers: [
ChirpstackGatewayService,
ChirpstackSetupNetworkServerService,
Expand Down
Loading