Skip to content

Commit

Permalink
Merge pull request #3 from Giveth/fix/is_number_test
Browse files Browse the repository at this point in the history
Fixed bug in parsing date in number format
  • Loading branch information
aminlatifi authored Aug 21, 2023
2 parents 7b61ed3 + e016d14 commit ed8cb69
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/modules/fetch-state/data-fetch-state.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from '@nestjs/common';
import { isNumber } from '@nestjs/common/utils/shared.utils';
import { InjectRepository } from '@nestjs/typeorm';
import { SingleFetchConfig } from 'src/modules/data-fetcher/load-blockchain-config.service';
import { DataFetchState } from 'src/modules/fetch-state/data-fetch-state.entity';
import { isNumber } from 'src/utils';
import { Repository } from 'typeorm';

@Injectable()
Expand Down Expand Up @@ -102,7 +102,7 @@ export class DataFetchStateService {
// Single network
if (isNumber(networks)) {
query = query.andWhere('state.network = :network', {
network: networks,
network: +networks,
});
}
// Multiple networks
Expand Down
18 changes: 10 additions & 8 deletions src/modules/token-balance/token-balance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ValidatorConstraintInterface,
} from 'class-validator';
import { TokenBalanceService } from 'src/modules/token-balance/token-balance.service';
import { isNumber } from 'src/utils';

class EthereumAddress implements ValidatorConstraintInterface {
validate(value: string) {
Expand Down Expand Up @@ -87,15 +88,16 @@ class QueryParamsUpdatedAfterDate {
network?: number;

@IsDate()
@Type(() => Date)
@Transform(({ value }) => {
console.log('value', value);
return new Date(value);
})
@Transform(({ value }) => new Date(isNumber(value) ? +value : value))
@ApiProperty({
type: 'string',
description:
'Date in acceptable by NodeJS Date constructor (e.g. ISO, Timestamp milliseconds, ...)',
oneOf: [
{ type: 'number', description: 'Date in timestamp milliseconds' },
{
type: 'string',
description:
'Date in acceptable by NodeJS Date constructor (e.g. ISO, ...)',
},
],
})
date: Date;

Expand Down
6 changes: 3 additions & 3 deletions src/modules/token-balance/token-balance.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable } from '@nestjs/common';
import { isNumber } from '@nestjs/common/utils/shared.utils';
import { InjectRepository } from '@nestjs/typeorm';
import { SubgraphBalanceChangeEntity } from 'src/modules/subgraph/graphql-client-adapter.service';
import { TokenBalanceUpdate } from 'src/modules/token-balance/token-balance-update.entity';
import { TokenBalance } from 'src/modules/token-balance/token-balance.entity';
import { isNumber } from 'src/utils';
import { Repository } from 'typeorm';

@Injectable()
Expand Down Expand Up @@ -107,7 +107,7 @@ export class TokenBalanceService {
// Single network
if (isNumber(networks)) {
query = query.andWhere('tokenBalance.network = :network', {
network: networks,
network: +networks,
});
}
// Multiple networks
Expand Down Expand Up @@ -172,7 +172,7 @@ export class TokenBalanceService {

if (isNumber(networks)) {
query = query.andWhere('tokenBalance.network = :network', {
network: networks,
network: +networks,
});
}
// Multiple networks
Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const isNumber = (val: any): val is number => {
if (typeof val === 'number') return true;

if (typeof val === 'string') {
return !isNaN(+val);
}

return false;
};

0 comments on commit ed8cb69

Please sign in to comment.