-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
packages/api/src/modules/configuration/configuration.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Controller, Get, Param, UnauthorizedException } from '@nestjs/common'; | ||
|
||
import { ActionConfig } from '../../interfaces/actions.types'; | ||
import { ConfigurationConfigService } from './config/configuration-config.service'; | ||
import { ConfigurationService } from './configuration.service'; | ||
|
||
@Controller('configuration') | ||
export class ConfigurationController { | ||
constructor( | ||
private readonly configService: ConfigurationService, | ||
private configurationConfigService: ConfigurationConfigService, | ||
) {} | ||
|
||
@Get('getDeviceActionConfig/:id') | ||
getDeviceActionConfig(@Param('id') id: string): Promise<ActionConfig> { | ||
if (id !== this.configurationConfigService.getDeviceTicket()) { | ||
throw new UnauthorizedException('Wrong device ticket'); | ||
} | ||
return this.configService.getDeviceActionConfig(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/api/src/modules/configuration/configuration.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { RepositoryModule } from '../repository/repository.module'; | ||
import { ConfigurationConfigModule } from './config/configuration-config.module'; | ||
import { ConfigurationController } from './configuration.controller'; | ||
import { ConfigurationService } from './configuration.service'; | ||
|
||
@Module({ | ||
imports: [RepositoryModule, ConfigurationConfigModule], | ||
controllers: [ConfigurationController], | ||
providers: [ConfigurationService], | ||
}) | ||
export class ConfigurationModule {} |
23 changes: 23 additions & 0 deletions
23
packages/api/src/modules/configuration/configuration.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { ActionConfig } from '../../interfaces/actions.types'; | ||
import { ConfigRepository } from '../repository/config.repository'; | ||
|
||
const INITIAL_ACTION_CONFIG: ActionConfig = { | ||
pinDefinition: {}, | ||
onInit: [], | ||
onToggle: [], | ||
}; | ||
|
||
@Injectable() | ||
export class ConfigurationService { | ||
constructor(private configRepository: ConfigRepository) {} | ||
|
||
async getDeviceActionConfig(): Promise<ActionConfig> { | ||
const config = await this.configRepository.getRaspberryPiActionConfig(); | ||
if (config) { | ||
return JSON.parse(config.value); | ||
} | ||
return INITIAL_ACTION_CONFIG; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/api/src/modules/database/migrations/1627835745237-AddConfigEntity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddConfigEntity1627835745237 implements MigrationInterface { | ||
name = 'AddConfigEntity1627835745237'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE TABLE "config" ("id" SERIAL NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "value" character varying NOT NULL, CONSTRAINT "PK_d0ee79a681413d50b0a4f98cf7b" PRIMARY KEY ("id"))`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE "config"`); | ||
} | ||
} |