|
|
@@ -155,31 +155,44 @@ export class AssetService {
|
|
|
return updatedAsset;
|
|
|
}
|
|
|
|
|
|
- async delete(ctx: RequestContext, id: ID, force: boolean = false): Promise<DeletionResponse> {
|
|
|
- const asset = await getEntityOrThrow(this.connection, Asset, id);
|
|
|
- const usages = await this.findAssetUsages(asset);
|
|
|
- const hasUsages = !!(usages.products.length || usages.variants.length || usages.collections.length);
|
|
|
+ async delete(ctx: RequestContext, ids: ID[], force: boolean = false): Promise<DeletionResponse> {
|
|
|
+ const assets = await this.connection.getRepository(Asset).findByIds(ids);
|
|
|
+ const usageCount = {
|
|
|
+ products: 0,
|
|
|
+ variants: 0,
|
|
|
+ collections: 0,
|
|
|
+ };
|
|
|
+ for (const asset of assets) {
|
|
|
+ const usages = await this.findAssetUsages(asset);
|
|
|
+ usageCount.products += usages.products.length;
|
|
|
+ usageCount.variants += usages.variants.length;
|
|
|
+ usageCount.collections += usages.collections.length;
|
|
|
+ }
|
|
|
+ const hasUsages = !!(usageCount.products || usageCount.variants || usageCount.collections);
|
|
|
if (hasUsages && !force) {
|
|
|
return {
|
|
|
result: DeletionResult.NOT_DELETED,
|
|
|
message: ctx.translate('message.asset-to-be-deleted-is-featured', {
|
|
|
- products: usages.products.length,
|
|
|
- variants: usages.variants.length,
|
|
|
- collections: usages.collections.length,
|
|
|
+ assetCount: assets.length,
|
|
|
+ products: usageCount.products,
|
|
|
+ variants: usageCount.variants,
|
|
|
+ collections: usageCount.collections,
|
|
|
}),
|
|
|
};
|
|
|
}
|
|
|
- // Create a new asset so that the id is still available
|
|
|
- // after deletion (the .remove() method sets it to undefined)
|
|
|
- const deletedAsset = new Asset(asset);
|
|
|
- await this.connection.getRepository(Asset).remove(asset);
|
|
|
- try {
|
|
|
- await this.configService.assetOptions.assetStorageStrategy.deleteFile(asset.source);
|
|
|
- await this.configService.assetOptions.assetStorageStrategy.deleteFile(asset.preview);
|
|
|
- } catch (e) {
|
|
|
- Logger.error(`error.could-not-delete-asset-file`, undefined, e.stack);
|
|
|
+ for (const asset of assets) {
|
|
|
+ // Create a new asset so that the id is still available
|
|
|
+ // after deletion (the .remove() method sets it to undefined)
|
|
|
+ const deletedAsset = new Asset(asset);
|
|
|
+ await this.connection.getRepository(Asset).remove(asset);
|
|
|
+ try {
|
|
|
+ await this.configService.assetOptions.assetStorageStrategy.deleteFile(asset.source);
|
|
|
+ await this.configService.assetOptions.assetStorageStrategy.deleteFile(asset.preview);
|
|
|
+ } catch (e) {
|
|
|
+ Logger.error(`error.could-not-delete-asset-file`, undefined, e.stack);
|
|
|
+ }
|
|
|
+ this.eventBus.publish(new AssetEvent(ctx, deletedAsset, 'deleted'));
|
|
|
}
|
|
|
- this.eventBus.publish(new AssetEvent(ctx, deletedAsset, 'deleted'));
|
|
|
return {
|
|
|
result: DeletionResult.DELETED,
|
|
|
};
|