Skip to content

Commit

Permalink
feat(core): ChannelService.findAll() returns PaginatedList
Browse files Browse the repository at this point in the history
BREAKING CHANGE: ChannelService.findAll() will now returns a PaginatedList<Channel> instead of a Channel[]
  • Loading branch information
alexisvigoureux authored Nov 24, 2022
1 parent 88fb438 commit 53fa2a0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/api/resolvers/admin/channel.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export class ChannelResolver {

@Query()
@Allow(Permission.ReadSettings, Permission.ReadChannel)
channels(@Ctx() ctx: RequestContext): Promise<Channel[]> {
return this.channelService.findAll(ctx);
async channels(@Ctx() ctx: RequestContext): Promise<Channel[]> {
const { items } = await this.channelService.findAll(ctx);
return items
}

@Query()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class GlobalSettingsResolver {
const { availableLanguages } = args.input;
if (availableLanguages) {
const channels = await this.channelService.findAll(ctx);
const unavailableDefaults = channels.filter(
const unavailableDefaults = channels.items.filter(
c => !availableLanguages.includes(c.defaultLanguageCode),
);
if (unavailableDefaults.length) {
Expand Down
35 changes: 27 additions & 8 deletions packages/core/src/service/services/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
UpdateChannelResult,
} from '@vendure/common/lib/generated-types';
import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
import { ID, Type } from '@vendure/common/lib/shared-types';
import { ID, PaginatedList, Type } from '@vendure/common/lib/shared-types';
import { unique } from '@vendure/common/lib/unique';

import { RequestContext } from '../../api/common/request-context';
import { ErrorResultUnion, isGraphQlErrorResult } from '../../common/error/error-result';
import { ChannelNotFoundError, EntityNotFoundError, InternalServerError } from '../../common/error/errors';
import { LanguageNotAvailableError } from '../../common/error/generated-graphql-admin-errors';
import { createSelfRefreshingCache, SelfRefreshingCache } from '../../common/self-refreshing-cache';
import { ChannelAware } from '../../common/types/common-types';
import { ChannelAware, ListQueryOptions } from '../../common/types/common-types';
import { assertFound, idsAreEqual } from '../../common/utils';
import { ConfigService } from '../../config/config.service';
import { TransactionalConnection } from '../../connection/transactional-connection';
Expand All @@ -32,8 +32,9 @@ import { ChangeChannelEvent } from '../../event-bus/events/change-channel-event'
import { ChannelEvent } from '../../event-bus/events/channel-event';
import { CustomFieldRelationService } from '../helpers/custom-field-relation/custom-field-relation.service';
import { patchEntity } from '../helpers/utils/patch-entity';

import { GlobalSettingsService } from './global-settings.service';
import { RelationPaths } from '../../api';
import { ListQueryBuilder } from '../helpers/list-query-builder/list-query-builder';

/**
* @description
Expand All @@ -51,6 +52,7 @@ export class ChannelService {
private globalSettingsService: GlobalSettingsService,
private customFieldRelationService: CustomFieldRelationService,
private eventBus: EventBus,
private listQueryBuilder: ListQueryBuilder,
) {}

/**
Expand All @@ -73,7 +75,13 @@ export class ChannelService {
return createSelfRefreshingCache({
name: 'ChannelService.allChannels',
ttl: this.configService.entityOptions.channelCacheTtl,
refresh: { fn: ctx => this.findAll(ctx), defaultArgs: [RequestContext.empty()] },
refresh: {
fn: async (ctx) => {
const { items } = await this.findAll(ctx);
return items
},
defaultArgs: [RequestContext.empty()]
},
});
}

Expand Down Expand Up @@ -185,10 +193,21 @@ export class ChannelService {
return defaultChannel;
}

findAll(ctx: RequestContext): Promise<Channel[]> {
return this.connection
.getRepository(ctx, Channel)
.find({ relations: ['defaultShippingZone', 'defaultTaxZone'] });
findAll(
ctx: RequestContext,
options?: ListQueryOptions<Channel>,
relations?: RelationPaths<Channel>
): Promise<PaginatedList<Channel>> {
return this.listQueryBuilder
.build(Channel, options, {
relations: relations ?? ['defaultShippingZone', 'defaultTaxZone'],
ctx,
})
.getManyAndCount()
.then(([items, totalItems]) => ({
items,
totalItems,
}));
}

findOne(ctx: RequestContext, id: ID): Promise<Channel | undefined> {
Expand Down

0 comments on commit 53fa2a0

Please sign in to comment.