Skip to content

Commit

Permalink
feat(origin-device-registry-irec-form-api): added validate device own…
Browse files Browse the repository at this point in the history
…ership hander
  • Loading branch information
kosecki123 committed Mar 3, 2021
1 parent 6d8a06d commit e5f123e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { DeviceController } from './device.controller';
import { Device } from './device.entity';
import { DeviceService, SM_READS_ADAPTER } from './device.service';
import { ValidateDeviceOwnershipQueryHandler } from './handlers/validate-device-ownership.handler';

@Module({})
export class DeviceModule {
Expand All @@ -19,7 +20,8 @@ export class DeviceModule {
provide: SM_READS_ADAPTER,
useValue: smartMeterReadingsAdapter
},
DeviceService
DeviceService,
ValidateDeviceOwnershipQueryHandler
],
controllers: [DeviceController],
exports: [DeviceService]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ValidateDeviceOwnershipQuery } from '@energyweb/origin-backend-core';
import { ConfigService } from '@nestjs/config';
import { IQueryHandler, QueryHandler } from '@nestjs/cqrs';
import { DeviceService } from '../device.service';

@QueryHandler(ValidateDeviceOwnershipQuery)
export class ValidateDeviceOwnershipQueryHandler
implements IQueryHandler<ValidateDeviceOwnershipQuery> {
private issuerTypeId: string;

constructor(private readonly deviceService: DeviceService, configService: ConfigService) {
this.issuerTypeId = configService.get<string>('ISSUER_ID');
}

public async execute({
ownerId,
externalRegistryId
}: ValidateDeviceOwnershipQuery): Promise<boolean> {
const device = await this.deviceService.findByExternalId({
id: externalRegistryId,
type: this.issuerTypeId
});

return device?.organizationId === parseInt(ownerId, 10);
}
}

0 comments on commit e5f123e

Please sign in to comment.