Skip to content

Commit

Permalink
Merge pull request #14 from EVNotify/feature/log-type
Browse files Browse the repository at this point in the history
✨ introduce log type, adapt code and log creation
  • Loading branch information
GPlay97 authored Aug 18, 2024
2 parents c7143f8 + c76105c commit 7f9345d
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 23 deletions.
10 changes: 6 additions & 4 deletions src/logs/dto/log.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import {
} from 'class-validator';
import { STATUS } from '../entities/status.entity';
import { Log } from '../schemas/log.schema';
import { TYPE } from '../entities/type.entity';

export class LogDto {
constructor(log?: Log) {
this.id = log?._id.toString();
this.updatedAt = log?.updatedAt;
this.title = log?.title;
this.status = log?.status;
this.type = log?.type;
this.startDate = log?.startDate;
this.endDate = log?.endDate;
this.isCharge = log?.isCharge;
this.startSOC = log?.startSOC;
this.endSOC = log?.endSOC;
this.startODO = log?.startODO;
Expand Down Expand Up @@ -50,6 +51,10 @@ export class LogDto {
@IsNotEmpty()
status: STATUS = STATUS.RUNNING;

@IsEnum(TYPE)
@IsNotEmpty()
type: TYPE = TYPE.UNKNOWN;

@IsDate()
@IsNotEmpty()
startDate: Date;
Expand All @@ -58,9 +63,6 @@ export class LogDto {
@IsOptional()
endDate: Date;

@IsBoolean()
isCharge = false;

@IsNumber()
@Min(0)
@Max(100)
Expand Down
7 changes: 4 additions & 3 deletions src/logs/dto/update-log.dto.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { IsString, IsOptional, IsBoolean } from 'class-validator';
import { IsString, IsOptional, IsBoolean, IsEnum } from 'class-validator';
import { TYPE } from '../entities/type.entity';

export class UpdateLogDto {
@IsString()
@IsOptional()
title: string;

@IsBoolean()
@IsEnum(TYPE)
@IsOptional()
isCharge = false;
type: TYPE = TYPE.UNKNOWN;
}
6 changes: 3 additions & 3 deletions src/logs/entities/status.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export enum STATUS {
RUNNING = 'RUNNING',
FINISHED = 'FINISHED',
ABORTED = 'ABORTED',
RUNNING = 'running',
FINISHED = 'finished',
ABORTED = 'aborted',
}
5 changes: 5 additions & 0 deletions src/logs/entities/type.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum TYPE {
CHARGE = 'charge',
DRIVE = 'drive',
UNKNOWN = 'unknown',
}
14 changes: 11 additions & 3 deletions src/logs/handler/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { STATUS } from '../entities/status.entity';
import { Log } from '../schemas/log.schema';
import { Sync } from '../schemas/sync.schema';
import { TYPE } from '../entities/type.entity';

@Injectable()
export class MetadataHandler {
Expand Down Expand Up @@ -37,7 +38,7 @@ export class MetadataHandler {
}

const mapping = {
isCharge: 'charging',
type: 'charging',
startODO: 'odo',
startCEC: 'cec',
startCED: 'ced',
Expand All @@ -46,9 +47,16 @@ export class MetadataHandler {
for (const logField in mapping) {
if (Object.prototype.hasOwnProperty.call(mapping, logField)) {
const syncField = mapping[logField];
const syncValue = sync[syncField];

if (log[logField] == null && sync[syncField] != null) {
log[logField] = sync[syncField];
if (syncField === 'charging' && log[logField] === TYPE.UNKNOWN && syncValue != null) {
log[logField] = syncValue ? TYPE.CHARGE : TYPE.DRIVE;
} else if (log[logField] == null && syncValue != null) {
log[logField] = syncValue;
}

if ((syncField === 'charging' ? log[logField] == TYPE.UNKNOWN : log[logField] == null) && syncValue != null) {
log[logField] = syncValue;
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/logs/logs.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { EventEmitterModule } from '@nestjs/event-emitter';
import { UpdateLogDto } from './dto/update-log.dto';
import { STATUS } from './entities/status.entity';
import { LastSyncDto } from './dto/last-sync.dto';
import { TYPE } from './entities/type.entity';

describe('LogsController', () => {
let accountService: AccountService;
Expand Down Expand Up @@ -123,7 +124,7 @@ describe('LogsController', () => {
});

it('should be able to filter out log in list', async () => {
const response = await controller.findAll(testAccount.akey, false);
const response = await controller.findAll(testAccount.akey, TYPE.DRIVE);

expect(response).toHaveLength(0);
});
Expand Down Expand Up @@ -221,6 +222,7 @@ describe('LogsController', () => {
const response = await controller.findAll(testAccount.akey);

expect(response).toHaveLength(1);
expect(response.at(0)).toHaveProperty('type', TYPE.UNKNOWN);

logId = response[0].id;
});
Expand Down Expand Up @@ -256,6 +258,20 @@ describe('LogsController', () => {
);
});

it('should update log type instead of creating new log if unknown', async () => {
const dto = new SyncDto();

dto.charging = false;

await controller.syncData(testAccount.akey, dto);

const response = await controller.findAll(testAccount.akey);

expect(response).toHaveLength(1);
expect(response.at(0)).toHaveProperty('id', logId);
expect(response.at(0)).toHaveProperty('type', TYPE.DRIVE);
});

it('should create a new log once charging started', async () => {
const dto = new SyncDto();

Expand All @@ -268,6 +284,7 @@ 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;
});

Expand All @@ -283,7 +300,7 @@ describe('LogsController', () => {
const response = await controller.findOne(testAccount.akey, chargeLogId);

expect(response).toBeInstanceOf(LogDto);
expect(response).toHaveProperty('isCharge', true);
expect(response).toHaveProperty('type', TYPE.CHARGE);
expect(response).toHaveProperty('startSOC', 75);
expect(response).toHaveProperty('averageKW', 10);
});
Expand Down
5 changes: 3 additions & 2 deletions src/logs/logs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { SyncDto } from './dto/sync.dto';
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';

@Controller('logs')
@UseGuards(AuthGuard)
Expand All @@ -32,8 +33,8 @@ export class LogsController {
constructor(private readonly logsService: LogsService) {}

@Get(':akey')
async findAll(@Param('akey') akey: string, @Query('isCharge') isCharge?: boolean) {
return this.logsService.findAll(akey, isCharge);
async findAll(@Param('akey') akey: string, @Query('type') type?: TYPE) {
return this.logsService.findAll(akey, type);
}

@Get(':akey/last-sync')
Expand Down
12 changes: 8 additions & 4 deletions src/logs/logs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { LogNotExistsException } from './exceptions/log-not-exists.exception';
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';

@Injectable()
export class LogsService {
Expand Down Expand Up @@ -54,7 +55,10 @@ export class LogsService {
if (!log) {
return await this.logModel.create({ akey });
} else {
if (syncDto.charging != null && syncDto.charging !== log.isCharge) {
if (syncDto.charging != null && log.type == TYPE.UNKNOWN) {
log.type = syncDto.charging ? TYPE.CHARGE : TYPE.DRIVE;
await log.save();
} else if (syncDto.charging && log.type != TYPE.CHARGE || syncDto.charging == false && log.type != TYPE.DRIVE) {
log.status = STATUS.FINISHED;
await log.save();
this.eventEmitter.emit(LOG_FINISHED_EVENT, log);
Expand All @@ -72,14 +76,14 @@ export class LogsService {
return new LastSyncDto(lastSync);
}

async findAll(akey: string, isCharge?: boolean): Promise<LogDto[]> {
async findAll(akey: string, type?: TYPE): Promise<LogDto[]> {
const logs = this.logModel
.find({ akey })
.select('-history')
.sort('startDate');

if (isCharge != null) {
logs.where({ isCharge });
if (type != null) {
logs.where({ type });
}

return Promise.resolve((await logs).map((log) => new LogDto(log)));
Expand Down
9 changes: 7 additions & 2 deletions src/logs/schemas/log.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Document, Types } from 'mongoose';
import { AKEY_LENGTH } from '../../account/dto/account.dto';
import { STATUS } from '../entities/status.entity';
import { Sync } from './sync.schema';
import { TYPE } from '../entities/type.entity';

export type LogDocument = Log & Document;

Expand Down Expand Up @@ -34,8 +35,12 @@ export class Log {
})
status: STATUS;

@Prop()
isCharge: boolean;
@Prop({
required: true,
default: TYPE.UNKNOWN,
enum: TYPE,
})
type: TYPE;

@Prop()
startDate: Date;
Expand Down

0 comments on commit 7f9345d

Please sign in to comment.