Skip to content

Commit

Permalink
Merge pull request #23 from gaveshalabs/fix-timestamp
Browse files Browse the repository at this point in the history
Datapoint timestamp fixes
  • Loading branch information
Hirushan99 authored Jun 17, 2024
2 parents 6f08254 + ba5d9a6 commit d4660cf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
25 changes: 12 additions & 13 deletions src/modules/weather-data/weather-data.controller.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import {
BadRequestException,
Body,
Controller,
Delete,
ForbiddenException,
Get,
Post,
Body,
Headers,
Param,
Delete,
ParseUUIDPipe,
Post,
Query,
Req,
UseGuards,
BadRequestException,
UsePipes,
ValidationPipe,
Query,
Headers,
Req,
ForbiddenException,
} from '@nestjs/common';
import { WeatherDataService } from './weather-data.service';
import { ApiTags } from '@nestjs/swagger';
import { ValidateGaveshaClientGuard } from '../common/guards/gavesha-client.guard';
import { ValidateGaveshaUserGuard } from '../common/guards/gavesha-user.guard';
import { CreateBulkWeatherDataDto } from './dto/create-bulk-weather-data.dto';
import { WeatherStationsService } from '../weather-stations/weather-stations.service';
import { BulkCreateWeatherDataResponseDto } from './dto/bulk-create-weather-data-response.dto';
import { GetWeatherDatumDto } from './dto/get-weather-datum.dto';
import { CreateBulkWeatherDataDto } from './dto/create-bulk-weather-data.dto';
import { CreateWeatherComBulkWeatherDataDto } from './dto/create-weathercom-bulk-weather-data.dto';
import { WeatherStationsService } from '../weather-stations/weather-stations.service';
import { GetWeatherDatumDto } from './dto/get-weather-datum.dto';
import { WeatherDataService } from './weather-data.service';

@Controller('weather-data')
@ApiTags('weather-data')
Expand Down
47 changes: 35 additions & 12 deletions src/modules/weather-data/weather-data.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { InjectConnection, InjectModel } from '@nestjs/mongoose';
import * as moment from 'moment';
import { Connection, Model } from 'mongoose';
import { PointsService } from '../points/points.service';
import { BulkCreateWeatherDataResponseDto } from './dto/bulk-create-weather-data-response.dto';
import { CreateBulkWeatherDataDto } from './dto/create-bulk-weather-data.dto';
import { GetWeatherDatumDto } from './dto/get-weather-datum.dto';
import { WeatherDataPoint } from './entities/weather-datapoint.entity';
import {
WeatherDatum,
WeatherDatumDocument,
} from './entities/weather-datum.entity';
import { CreateBulkWeatherDataDto } from './dto/create-bulk-weather-data.dto';
import { BulkCreateWeatherDataResponseDto } from './dto/bulk-create-weather-data-response.dto';
import { WeatherDataMetadata } from './schema/weatherdata-metadata.schema';
import { WeatherDataPoint } from './entities/weather-datapoint.entity';
import * as moment from 'moment';

@Injectable()
export class WeatherDataService {
Expand Down Expand Up @@ -45,11 +45,15 @@ export class WeatherDataService {

for (let i = 0; i < data.length; i++) {
const element = data[i];
if (!element.timestamp) {
if (!element.timestamp_iso) {
throw new BadRequestException('Invalid data');
}
element.timestamp = new Date(element.timestamp_iso).getTime();
// if (!element.timestamp) {
// if (!element.timestamp_iso) {
// throw new BadRequestException('Invalid data');
// }
// element.timestamp = new Date(element.timestamp_iso).getTime();
// }

if(weather_station_id === 'dbfb6590-93c1-455b-aaf2-668560a73e4b'){
element.timestamp = new Date().getTime();
}
}

Expand Down Expand Up @@ -161,14 +165,33 @@ export class WeatherDataService {
// Return the _id, timestamp, created_at fields.
const responseData = [...insertedData, ...existingWeatherData];

return responseData.map((datum) => {
// return responseData.map((datum) => {
// return {
// _id: datum._id,
// timestamp: datum.timestamp,
// timestamp_iso: moment(datum.timestamp).toISOString(true),
// created_at: datum.createdAt,
// } as BulkCreateWeatherDataResponseDto;
// }) as BulkCreateWeatherDataResponseDto[];

const finalResponse = responseData.map((datum) => {
return {
_id: datum._id,
timestamp: datum.timestamp,
timestamp_iso: moment(datum.timestamp).toISOString(true),
created_at: datum.createdAt,
} as BulkCreateWeatherDataResponseDto;
}) as BulkCreateWeatherDataResponseDto[];
});

const returnObject = {
_id:null,
timestamp_iso: moment().toISOString(true),
} as BulkCreateWeatherDataResponseDto;

finalResponse.push(returnObject);

return finalResponse;

}

// TODO: Add types.
Expand Down

0 comments on commit d4660cf

Please sign in to comment.