Skip to content

Commit

Permalink
Add totalAmountUDS resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Nov 23, 2023
1 parent d638aa4 commit cfba153
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 18 deletions.
34 changes: 32 additions & 2 deletions src/domain-services/flows/flow-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import { UsageYearService } from '../usage-years/usage-year-service';
import {
type FlowObjectFilters,
type SearchFlowsArgs,
type SearchFlowsArgsNonPaginated,
type SearchFlowsFilters,
} from './graphql/args';
import {
type FlowPaged,
type FlowParkedParentSource,
type FlowSearchResult,
type FlowSearchTotalAmountResult,
} from './graphql/types';
import { type FlowEntity } from './model';
import { type FlowSearchStrategy } from './strategy/flow-search-strategy';
Expand Down Expand Up @@ -79,10 +81,10 @@ export class FlowSearchService {
// Obtain flows and its count based on the strategy selected
const { flows, count } = await strategy.search(
conditions,
models,
orderBy,
limitComputed,
cursorCondition,
models
cursorCondition
);

// Remove the extra item used to check hasNextPage
Expand Down Expand Up @@ -448,4 +450,32 @@ export class FlowSearchService {
cursor: flow.id.valueOf(),
};
}

async searchTotalAmount(
models: Database,
args: SearchFlowsArgsNonPaginated
): Promise<FlowSearchTotalAmountResult> {
const { flowFilters, flowObjectFilters } = args;

const { strategy, conditions } = this.determineStrategy(
flowFilters,
flowObjectFilters
);

const { flows, count } = await strategy.search(conditions, models);

const flowsAmountUSD: Array<string | number> = flows.map(
(flow) => flow.amountUSD
);

const totalAmount = flowsAmountUSD.reduce((a, b) => +a + +b, 0);

return {
totalAmountUSD: totalAmount.toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}),
flowsCount: count,
};
}
}
4 changes: 2 additions & 2 deletions src/domain-services/flows/flow-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export class FlowService {
async getFlows(
models: Database,
conditions: any,
orderBy: any,
limit: number
orderBy?: any,
limit?: number
) {
return await models.flow.find({
orderBy,
Expand Down
15 changes: 15 additions & 0 deletions src/domain-services/flows/graphql/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ export class SearchFlowsArgs extends PaginationArgs<FlowSortField> {
@Field({ nullable: true })
includeChildrenOfParkedFlows: boolean;
}

@ArgsType()
export class SearchFlowsArgsNonPaginated {
@Field(() => SearchFlowsFilters, { nullable: true })
flowFilters: SearchFlowsFilters;

@Field(() => [FlowObjectFilters], { nullable: true })
flowObjectFilters: FlowObjectFilters[];

@Field(() => [FlowCategory], { nullable: true })
categoryFilters: FlowCategory[];

@Field({ nullable: true })
includeChildrenOfParkedFlows: boolean;
}
17 changes: 15 additions & 2 deletions src/domain-services/flows/graphql/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { Args, Ctx, Query, Resolver } from 'type-graphql';
import { Service } from 'typedi';
import Context from '../../Context';
import { FlowSearchService } from '../flow-search-service';
import { SearchFlowsArgs } from './args';
import { FlowPaged, FlowSearchResult } from './types';
import { SearchFlowsArgs, SearchFlowsArgsNonPaginated } from './args';
import {
FlowPaged,
FlowSearchResult,
FlowSearchTotalAmountResult,
} from './types';

@Service()
@Resolver(FlowPaged)
Expand All @@ -18,4 +22,13 @@ export default class FlowResolver {
): Promise<FlowSearchResult> {
return await this.flowSearchService.search(context.models, args);
}

@Query(() => FlowSearchTotalAmountResult)
async searchFlowsTotalAmountUSD(
@Ctx() context: Context,
@Args(() => SearchFlowsArgsNonPaginated, { validate: false })
args: SearchFlowsArgsNonPaginated
): Promise<FlowSearchTotalAmountResult> {
return await this.flowSearchService.searchTotalAmount(context.models, args);
}
}
9 changes: 9 additions & 0 deletions src/domain-services/flows/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ export class FlowSearchResult extends PageInfo<FlowSortField> {
flows: FlowPaged[];
}

@ObjectType()
export class FlowSearchTotalAmountResult {
@Field(() => String, { nullable: false })
totalAmountUSD: string;

@Field(() => Number, { nullable: false })
flowsCount: number;
}

export type FlowSortField =
| 'id'
| 'versionID'
Expand Down
8 changes: 4 additions & 4 deletions src/domain-services/flows/strategy/flow-search-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export interface FlowSearchStrategyResponse {
export interface FlowSearchStrategy {
search(
flowConditions: Map<string, any>,
orderBy: any,
limit: number,
cursorCondition: any,
models: Database
models: Database,
orderBy?: any,
limit?: number,
cursorCondition?: any
): Promise<FlowSearchStrategyResponse>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy {

async search(
flowConditions: Map<string, any>,
orderBy: any,
limit: number,
cursorCondition: any,
models: Database
models: Database,
orderBy?: any,
limit?: number,
cursorCondition?: any
): Promise<FlowSearchStrategyResponse> {
// Obtain flowObjects conditions
const flowObjectsConditions: Map<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export class OnlyFlowFiltersStrategy implements FlowSearchStrategy {

async search(
flowConditions: any,
orderBy: any,
limit: number,
cursorCondition: any,
models: Database
models: Database,
orderBy?: any,
limit?: number,
cursorCondition?: any
): Promise<FlowSearchStrategyResponse> {
// Build conditions object
const conditions: any = { ...cursorCondition, ...flowConditions };
Expand Down

0 comments on commit cfba153

Please sign in to comment.