@haus-tech/badge-plugin 4.0.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,18 @@
1
- import { AssetService, CollectionService, EntityHydrator, ErrorResult, ListQueryBuilder, ListQueryOptions, PaginatedList, Product, RequestContext, TransactionalConnection } from '@vendure/core';
1
+ import { OnApplicationBootstrap } from '@nestjs/common';
2
+ import { AssetService, ChannelService, CollectionService, EntityHydrator, ErrorResult, ListQueryBuilder, ListQueryOptions, PaginatedList, Product, RequestContext, TransactionalConnection } from '@vendure/core';
2
3
  import { Badge } from '../entity/badge.entity';
3
4
  import { CreateBadgeInput, DeletionResponse, UpdateBadgeInput } from '../gql/generated';
4
5
  import { SearchResult } from '@vendure/common/lib/generated-shop-types';
5
- export declare class BadgeService {
6
+ import { AssignBadgesToChannelInput } from '../types';
7
+ export declare class BadgeService implements OnApplicationBootstrap {
6
8
  private listQueryBuilder;
7
9
  private connection;
8
10
  private assetService;
9
11
  private entityHydratorService;
10
12
  private collectionService;
11
- constructor(listQueryBuilder: ListQueryBuilder, connection: TransactionalConnection, assetService: AssetService, entityHydratorService: EntityHydrator, collectionService: CollectionService);
13
+ private channelService;
14
+ constructor(listQueryBuilder: ListQueryBuilder, connection: TransactionalConnection, assetService: AssetService, entityHydratorService: EntityHydrator, collectionService: CollectionService, channelService: ChannelService);
15
+ onApplicationBootstrap(): Promise<void>;
12
16
  findOne(ctx: RequestContext, id: number): Promise<Badge | null>;
13
17
  findAll(ctx: RequestContext, options?: ListQueryOptions<Badge>): Promise<PaginatedList<Badge>>;
14
18
  create(ctx: RequestContext, input: CreateBadgeInput): Promise<Badge>;
@@ -18,4 +22,5 @@ export declare class BadgeService {
18
22
  findByCollectionIds(ctx: RequestContext, collectionIds: string[]): Promise<Badge[]>;
19
23
  findBadgesForProduct(ctx: RequestContext, product: Product): Promise<Badge[]>;
20
24
  findBadgesForSearchResult(ctx: RequestContext, searchResult: SearchResult): Promise<Badge[]>;
25
+ assignToChannel(ctx: RequestContext, input: AssignBadgesToChannelInput): Promise<Badge[]>;
21
26
  }
@@ -21,12 +21,28 @@ let BadgeService = class BadgeService {
21
21
  assetService;
22
22
  entityHydratorService;
23
23
  collectionService;
24
- constructor(listQueryBuilder, connection, assetService, entityHydratorService, collectionService) {
24
+ channelService;
25
+ constructor(listQueryBuilder, connection, assetService, entityHydratorService, collectionService, channelService) {
25
26
  this.listQueryBuilder = listQueryBuilder;
26
27
  this.connection = connection;
27
28
  this.assetService = assetService;
28
29
  this.entityHydratorService = entityHydratorService;
29
30
  this.collectionService = collectionService;
31
+ this.channelService = channelService;
32
+ }
33
+ async onApplicationBootstrap() {
34
+ // Assign all badges which are not assigned to any channel to the default channel
35
+ const defaultChannel = await this.channelService.getDefaultChannel();
36
+ const badgesWithoutChannels = await this.connection.rawConnection
37
+ .getRepository(badge_entity_1.Badge)
38
+ .createQueryBuilder('badge')
39
+ .leftJoinAndSelect('badge.channels', 'channel')
40
+ .where('channel.id IS NULL')
41
+ .getMany();
42
+ for (const badge of badgesWithoutChannels) {
43
+ badge.channels = [defaultChannel];
44
+ await this.connection.rawConnection.getRepository(badge_entity_1.Badge).save(badge);
45
+ }
30
46
  }
31
47
  async findOne(ctx, id) {
32
48
  return this.connection.getRepository(ctx, badge_entity_1.Badge).findOne({ where: { id } });
@@ -107,6 +123,13 @@ let BadgeService = class BadgeService {
107
123
  const collectionIds = collections.map((c) => c.id);
108
124
  return this.findByCollectionIds(ctx, collectionIds);
109
125
  }
126
+ async assignToChannel(ctx, input) {
127
+ const badges = await this.connection.findByIdsInChannel(ctx, badge_entity_1.Badge, input.badgeIds, ctx.channelId, {});
128
+ await Promise.all(badges.map(async (badge) => {
129
+ await this.channelService.assignToChannels(ctx, badge_entity_1.Badge, badge.id, [input.channelId]);
130
+ }));
131
+ return this.connection.findByIdsInChannel(ctx, badge_entity_1.Badge, badges.map((b) => b.id), ctx.channelId, {});
132
+ }
110
133
  };
111
134
  BadgeService = __decorate([
112
135
  (0, common_1.Injectable)(),
@@ -114,6 +137,7 @@ BadgeService = __decorate([
114
137
  core_1.TransactionalConnection,
115
138
  core_1.AssetService,
116
139
  core_1.EntityHydrator,
117
- core_1.CollectionService])
140
+ core_1.CollectionService,
141
+ core_1.ChannelService])
118
142
  ], BadgeService);
119
143
  exports.BadgeService = BadgeService;
@@ -0,0 +1,5 @@
1
+ import { Scalars } from '@vendure/core';
2
+ export type AssignBadgesToChannelInput = {
3
+ badgeIds: Array<Scalars['ID']>;
4
+ channelId: Scalars['ID'];
5
+ };
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haus-tech/badge-plugin",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "Adds a badge to product images",
5
5
  "author": "Haus Tech",
6
6
  "repository": "https://github.com/WeAreHausTech/haus-tech-vendure-plugins",
@@ -16,7 +16,7 @@
16
16
  ],
17
17
  "scripts": {
18
18
  "start": "yarn ts-node test/dev-server.ts",
19
- "build": "rimraf dist && tsc && copyfiles -u 1 'src/ui/**/*' dist/",
19
+ "build": "yarn run -T rimraf dist && yarn run -T tsc && yarn run -T copyfiles -u 1 'src/ui/**/*' dist/",
20
20
  "test": "jest --preset=\"ts-jest\"",
21
21
  "prepublishOnly": "yarn && yarn build"
22
22
  },