Browse Source

fix(core): Add missing SellerEvent and correctly update relations

Relates to #2216
Michael Bromley 2 years ago
parent
commit
4f421d375f

+ 28 - 0
packages/core/src/event-bus/events/seller-event.ts

@@ -0,0 +1,28 @@
+import { CreateSellerInput, UpdateSellerInput } from '@vendure/common/lib/generated-types';
+import { ID } from '@vendure/common/lib/shared-types';
+
+import { RequestContext } from '../../api/common/request-context';
+import { Role } from '../../entity';
+import { Seller } from '../../entity/seller/seller.entity';
+import { VendureEntityEvent } from '../vendure-entity-event';
+
+type SellerInputTypes = CreateSellerInput | UpdateSellerInput | ID;
+
+/**
+ * @description
+ * This event is fired whenever one {@link Seller} is added, updated or deleted.
+ *
+ * @docsCategory events
+ * @docsPage Event Types
+ * @since 2.0.1
+ */
+export class SellerEvent extends VendureEntityEvent<Seller, SellerInputTypes> {
+    constructor(
+        ctx: RequestContext,
+        entity: Seller,
+        type: 'created' | 'updated' | 'deleted',
+        input?: SellerInputTypes,
+    ) {
+        super(entity, type, ctx, input);
+    }
+}

+ 1 - 0
packages/core/src/event-bus/index.ts

@@ -49,6 +49,7 @@ export * from './events/refund-state-transition-event';
 export * from './events/role-change-event';
 export * from './events/role-event';
 export * from './events/search-event';
+export * from './events/seller-event';
 export * from './events/shipping-method-event';
 export * from './events/stock-movement-event';
 export * from './events/tax-category-event';

+ 28 - 3
packages/core/src/service/services/seller.service.ts

@@ -8,9 +8,12 @@ import {
 import { ID, PaginatedList } from '@vendure/common/lib/shared-types';
 
 import { RequestContext } from '../../api/common/request-context';
+import { assertFound } from '../../common/index';
 import { ListQueryOptions } from '../../common/types/common-types';
 import { TransactionalConnection } from '../../connection/transactional-connection';
 import { Seller } from '../../entity/seller/seller.entity';
+import { EventBus, SellerEvent } from '../../event-bus/index';
+import { CustomFieldRelationService } from '../helpers/custom-field-relation/custom-field-relation.service';
 import { ListQueryBuilder } from '../helpers/list-query-builder/list-query-builder';
 
 /**
@@ -21,7 +24,12 @@ import { ListQueryBuilder } from '../helpers/list-query-builder/list-query-build
  */
 @Injectable()
 export class SellerService {
-    constructor(private connection: TransactionalConnection, private listQueryBuilder: ListQueryBuilder) {}
+    constructor(
+        private connection: TransactionalConnection,
+        private listQueryBuilder: ListQueryBuilder,
+        private eventBus: EventBus,
+        private customFieldRelationService: CustomFieldRelationService,
+    ) {}
 
     async initSellers() {
         await this.ensureDefaultSellerExists();
@@ -44,8 +52,16 @@ export class SellerService {
             .then(result => result ?? undefined);
     }
 
-    create(ctx: RequestContext, input: CreateSellerInput) {
-        return this.connection.getRepository(ctx, Seller).save(new Seller(input));
+    async create(ctx: RequestContext, input: CreateSellerInput) {
+        const seller = await this.connection.getRepository(ctx, Seller).save(new Seller(input));
+        const sellerWithRelations = await this.customFieldRelationService.updateRelations(
+            ctx,
+            Seller,
+            input,
+            seller,
+        );
+        this.eventBus.publish(new SellerEvent(ctx, sellerWithRelations, 'created', input));
+        return assertFound(this.findOne(ctx, seller.id));
     }
 
     async update(ctx: RequestContext, input: UpdateSellerInput) {
@@ -54,12 +70,21 @@ export class SellerService {
             seller.name = input.name;
             await this.connection.getRepository(ctx, Seller).save(seller);
         }
+        const sellerWithRelations = await this.customFieldRelationService.updateRelations(
+            ctx,
+            Seller,
+            input,
+            seller,
+        );
+        this.eventBus.publish(new SellerEvent(ctx, sellerWithRelations, 'updated', input));
         return seller;
     }
 
     async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
         const seller = await this.connection.getEntityOrThrow(ctx, Seller, id);
         await this.connection.getRepository(ctx, Seller).remove(seller);
+        const deletedSeller = new Seller(seller);
+        this.eventBus.publish(new SellerEvent(ctx, deletedSeller, 'deleted', id));
         return {
             result: DeletionResult.DELETED,
         };