Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Current log #15

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions src/logs/exceptions/log-not-running.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Exception } from '../../utils/exception';

export class LogNotRunningException extends Exception {
constructor() {
super('No log is actively running at the moment.');
}
}
12 changes: 10 additions & 2 deletions src/logs/logs.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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);
});
Expand All @@ -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);
});
});
14 changes: 14 additions & 0 deletions src/logs/logs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
15 changes: 14 additions & 1 deletion src/logs/logs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -76,11 +77,23 @@ export class LogsService {
return new LastSyncDto(lastSync);
}

async findRunning(akey: string): Promise<LogDto> {
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<LogDto[]> {
const logs = this.logModel
.find({ akey })
.select('-history')
.sort('startDate');
.sort({startDate: 'desc'});

if (type != null) {
logs.where({ type });
Expand Down
Loading