Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): fix collection update event generation #1114

Merged
merged 3 commits into from
Sep 28, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions packages/core/src/service/services/collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { AssetService } from './asset.service';
import { ChannelService } from './channel.service';
import { FacetValueService } from './facet-value.service';

type ApplyCollectionFiltersJobData = { ctx: SerializedRequestContext; collectionIds: ID[] };
type ApplyCollectionFiltersJobData = { ctx: SerializedRequestContext; collectionIds: ID[]; applyToChangedVariantsOnly?: boolean; };

@Injectable()
export class CollectionService implements OnModuleInit {
Expand All @@ -64,7 +64,8 @@ export class CollectionService implements OnModuleInit {
private slugValidator: SlugValidator,
private configArgService: ConfigArgService,
private customFieldRelationService: CustomFieldRelationService,
) {}
) {
}

async onModuleInit() {
const productEvents$ = this.eventBus.ofType(ProductEvent);
Expand Down Expand Up @@ -99,7 +100,7 @@ export class CollectionService implements OnModuleInit {
}
completed++;
if (collection) {
const affectedVariantIds = await this.applyCollectionFiltersInternal(collection);
const affectedVariantIds = await this.applyCollectionFiltersInternal(collection, job.data.applyToChangedVariantsOnly);
job.setProgress(Math.ceil((completed / job.data.collectionIds.length) * 100));
this.eventBus.publish(
new CollectionModificationEvent(ctx, collection, affectedVariantIds),
Expand Down Expand Up @@ -362,7 +363,11 @@ export class CollectionService implements OnModuleInit {
await this.applyFiltersQueue.add({
ctx: ctx.serialize(),
collectionIds: [collection.id],
applyToChangedVariantsOnly: false,
});
} else {
const affectedVariantIds = await this.getCollectionProductVariantIds(collection);
this.eventBus.publish(new CollectionModificationEvent(ctx, collection, affectedVariantIds));
}
Izayda marked this conversation as resolved.
Show resolved Hide resolved
return assertFound(this.findOne(ctx, collection.id));
}
Expand Down Expand Up @@ -431,7 +436,7 @@ export class CollectionService implements OnModuleInit {
/**
* Applies the CollectionFilters
*/
private async applyCollectionFiltersInternal(collection: Collection): Promise<ID[]> {
private async applyCollectionFiltersInternal(collection: Collection, applyToChangedVariantsOnly?: boolean): Promise<ID[]> {
Izayda marked this conversation as resolved.
Show resolved Hide resolved
const ancestorFilters = await this.getAncestors(collection.id).then(ancestors =>
ancestors.reduce(
(filters, c) => [...filters, ...(c.filters || [])],
Expand Down Expand Up @@ -460,11 +465,19 @@ export class CollectionService implements OnModuleInit {
}
const preIdsSet = new Set(preIds);
const postIdsSet = new Set(postIds);
const difference = [
...preIds.filter(id => !postIdsSet.has(id)),
...postIds.filter(id => !preIdsSet.has(id)),
];
return difference;

if (applyToChangedVariantsOnly === undefined ? true : applyToChangedVariantsOnly) {
Izayda marked this conversation as resolved.
Show resolved Hide resolved
return [
...preIds.filter(id => !postIdsSet.has(id)),
...postIds.filter(id => !preIdsSet.has(id)),
];
} else {
return [
...preIds.filter(id => !postIdsSet.has(id)),
...postIds,
];
}

}

/**
Expand Down