Skip to content

Commit

Permalink
feat(core): Implemented AssetEvent with new entity event base class (v…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin committed Nov 15, 2021
1 parent 469c6ac commit b16e9f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
28 changes: 22 additions & 6 deletions packages/core/src/event-bus/events/asset-event.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
import { RequestContext } from '../../api/common/request-context';
import { CreateAssetInput, DeleteAssetInput, UpdateAssetInput } from '@vendure/common/lib/generated-types';

import { RequestContext } from '../../api';
import { Asset } from '../../entity';
import { VendureEvent } from '../vendure-event';
import { VendureEntityEvent } from '../vendure-entity-event';

type AssetInputTypes = CreateAssetInput | UpdateAssetInput | DeleteAssetInput;

/**
* @description
* This event is fired whenever aa {@link Asset} is added, updated
* or deleted.
* or deleted. The `input` property is only defined for `'created'` & `'updated'` event types.
*
* @docsCategory events
* @docsPage Event Types
* @since 1.4
*/
export class AssetEvent extends VendureEvent {
export class AssetEvent extends VendureEntityEvent<Asset, AssetInputTypes> {
constructor(
public ctx: RequestContext,
public asset: Asset,
public entity: Asset,
public type: 'created' | 'updated' | 'deleted',
public input?: AssetInputTypes,
) {
super();
super(entity, type, ctx, input);
}

/**
* Return an asset field to become compatible with the
* deprecated old version of AssetEvent
* @deprecated Use `entity` instead
* @since 1.4
*/
get asset(): Asset {
return this.entity;
}
}
4 changes: 2 additions & 2 deletions packages/core/src/service/services/asset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export class AssetService {
result.tags = tags;
await this.connection.getRepository(ctx, Asset).save(result);
}
this.eventBus.publish(new AssetEvent(ctx, result, 'created'));
this.eventBus.publish(new AssetEvent(ctx, result, 'created', input));
resolve(result);
});
}
Expand All @@ -284,7 +284,7 @@ export class AssetService {
asset.tags = await this.tagService.valuesToTags(ctx, input.tags);
}
const updatedAsset = await this.connection.getRepository(ctx, Asset).save(asset);
this.eventBus.publish(new AssetEvent(ctx, updatedAsset, 'updated'));
this.eventBus.publish(new AssetEvent(ctx, updatedAsset, 'updated', input));
return updatedAsset;
}

Expand Down

0 comments on commit b16e9f6

Please sign in to comment.