diff --git a/src/logs/exceptions/log-not-running.exception.ts b/src/logs/exceptions/log-not-running.exception.ts new file mode 100644 index 0000000..bcca7da --- /dev/null +++ b/src/logs/exceptions/log-not-running.exception.ts @@ -0,0 +1,7 @@ +import { Exception } from '../../utils/exception'; + +export class LogNotRunningException extends Exception { + constructor() { + super('No log is actively running at the moment.'); + } +} diff --git a/src/logs/logs.controller.spec.ts b/src/logs/logs.controller.spec.ts index 868a0de..760d7e3 100644 --- a/src/logs/logs.controller.spec.ts +++ b/src/logs/logs.controller.spec.ts @@ -284,8 +284,8 @@ describe('LogsController', () => { const response = await controller.findAll(testAccount.akey); expect(response).toHaveLength(2); - expect(response.at(1)).toHaveProperty('type', TYPE.CHARGE); - chargeLogId = response[1].id; + expect(response.at(0)).toHaveProperty('type', TYPE.CHARGE); + chargeLogId = response[0].id; }); it('should mark previous log as finished', async () => { @@ -301,6 +301,7 @@ describe('LogsController', () => { expect(response).toBeInstanceOf(LogDto); expect(response).toHaveProperty('type', TYPE.CHARGE); + expect(response).toHaveProperty('status', STATUS.RUNNING); expect(response).toHaveProperty('startSOC', 75); expect(response).toHaveProperty('averageKW', 10); }); @@ -317,4 +318,11 @@ describe('LogsController', () => { expect(response).toBeInstanceOf(LogDto); expect(response).toHaveProperty('averageKW', 6); }); + + it('should find current running log', async () => { + const response = await controller.findRunning(testAccount.akey); + + expect(response).toBeInstanceOf(LogDto); + expect(response).toHaveProperty('id', chargeLogId); + }); }); diff --git a/src/logs/logs.controller.ts b/src/logs/logs.controller.ts index ca1bf1a..50b7505 100644 --- a/src/logs/logs.controller.ts +++ b/src/logs/logs.controller.ts @@ -23,6 +23,7 @@ import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { LogNotExistsException } from './exceptions/log-not-exists.exception'; import { LogMissingSyncDataException } from './exceptions/log-missing-sync-data.exception'; import { TYPE } from './entities/type.entity'; +import { LogNotRunningException } from './exceptions/log-not-running.exception'; @Controller('logs') @UseGuards(AuthGuard) @@ -42,6 +43,19 @@ export class LogsController { return this.logsService.lastSync(akey); } + @Get(':akey/running') + async findRunning(@Param('akey') akey: string) { + try { + return await this.logsService.findRunning(akey); + } catch (error) { + if (error instanceof LogNotRunningException) { + throw new NotFoundException(error.message); + } + + throw new InternalServerErrorException(); + } + } + @Get(':akey/:id') @OwnsLog() async findOne(@Param('akey') akey: string, @Param('id') id: string) { diff --git a/src/logs/logs.service.ts b/src/logs/logs.service.ts index a89c023..4a999ee 100644 --- a/src/logs/logs.service.ts +++ b/src/logs/logs.service.ts @@ -18,6 +18,7 @@ import { LastSync } from './schemas/last-sync.schema'; import { Log } from './schemas/log.schema'; import { Sync } from './schemas/sync.schema'; import { TYPE } from './entities/type.entity'; +import { LogNotRunningException } from './exceptions/log-not-running.exception'; @Injectable() export class LogsService { @@ -76,11 +77,23 @@ export class LogsService { return new LastSyncDto(lastSync); } + async findRunning(akey: string): Promise { + const log = await this.logModel + .findOne({ akey, status: STATUS.RUNNING }) + .select('-history'); + + if (!log) { + throw new LogNotRunningException(); + } + + return Promise.resolve(new LogDto(log)); + } + async findAll(akey: string, type?: TYPE): Promise { const logs = this.logModel .find({ akey }) .select('-history') - .sort('startDate'); + .sort({startDate: 'desc'}); if (type != null) { logs.where({ type });