Skip to content

Commit

Permalink
Fix wallet getStatictics return type and gql schema
Browse files Browse the repository at this point in the history
  • Loading branch information
DMQQ committed Sep 1, 2024
1 parent 2703334 commit 56b52bf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
8 changes: 5 additions & 3 deletions src/wallet/wallet.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
WalletEntity,
} from 'src/wallet/wallet.entity';
import { User } from 'src/utils/decorators/User';
import { GetWalletFilters, WalletStatistics } from './wallet.schemas';
import { GetWalletFilters, WalletStatisticsRange } from './wallet.schemas';

@UseGuards(AuthGuard)
@Resolver(() => WalletEntity)
Expand Down Expand Up @@ -139,14 +139,16 @@ export class WalletResolver {
return promise[1] - promise[0];
}

@Query(() => WalletStatistics)
@Query(() => WalletStatisticsRange)
async getStatistics(
@User() usrId: string,
@Args('type', { type: () => String, description: 'today, month, week' })
type: 'today' | 'month' | 'week',
) {
if (['today', 'month', 'week'].indexOf(type) === -1)
throw new BadRequestException('Invalid type');
return this.walletService.getStatistics(usrId, type);
const stats = await this.walletService.getStatistics(usrId, type);

return stats[0];
}
}
27 changes: 6 additions & 21 deletions src/wallet/wallet.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class GetWalletFilters {
}

@ObjectType()
class WalletStatisticsRange {
export class WalletStatisticsRange {
@Field(() => Float, { nullable: true })
total: number;

Expand All @@ -53,33 +53,18 @@ class WalletStatisticsRange {
@Field(() => Float, { nullable: true })
count: number;

@Field(() => String)
@Field(() => String, { nullable: true })
theMostCommonCategory: string;

@Field(() => String)
@Field(() => String, { nullable: true })
theLeastCommonCategory: string;

@Field(() => Float)
@Field(() => Float, { nullable: true })
lastBalance: number;

@Field(() => Float)
@Field(() => Float, { nullable: true })
income: number;

@Field(() => Float)
@Field(() => Float, { nullable: true })
expense: number;
}

@ObjectType()
export class WalletStatistics {
@Field(() => Float)
balance: number;

@Field(() => WalletStatisticsRange)
month: WalletStatisticsRange;

@Field(() => WalletStatisticsRange)
today: WalletStatisticsRange;

@Field(() => WalletStatisticsRange)
week: WalletStatisticsRange;
}
4 changes: 2 additions & 2 deletions src/wallet/wallet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
WalletEntity,
} from 'src/wallet/wallet.entity';
import { Repository } from 'typeorm';
import { GetWalletFilters, WalletStatistics } from './wallet.schemas';
import { GetWalletFilters, WalletStatisticsRange } from './wallet.schemas';

@Injectable()
export class WalletService {
Expand Down Expand Up @@ -260,7 +260,7 @@ export class WalletService {
getStatistics(
userId: string,
dateRange: 'today' | 'week' | 'month',
): Promise<[WalletStatistics]> {
): Promise<[WalletStatisticsRange]> {
// Determine the appropriate interval based on the dateRange
let interval: string;
switch (dateRange) {
Expand Down

0 comments on commit 56b52bf

Please sign in to comment.