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

Duplicating entities to new channel leads to error if assets are associated #2831

Open
mschipperheyn opened this issue May 3, 2024 · 2 comments
Assignees
Labels
type: bug 🐛 Something isn't working

Comments

@mschipperheyn
Copy link
Contributor

mschipperheyn commented May 3, 2024

Describe the bug
When you copy a product, collection or other entity with associated assets to a new channel, the following error will be thrown:

TypeError: Cannot read properties of undefined (reading 'constructor')
    at AssetService.createOrderableAssets (/Users/mschipperheyn/workspace/maos/node_modules/@vendure/core/src/service/services/asset.service.ts:615:6)
    at AssetService.updateEntityAssets (/Users/mschipperheyn/workspace/maos/node_modules/@vendure/core/src/service/services/asset.service.ts:276:10)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async CollectionService.create (/Users/mschipperheyn/workspace/maos/node_modules/@vendure/core/src/service/services/collection.service.ts:465:9)
    at async EntityDuplicator.duplicate [as duplicateFn] (/Users/mschipperheyn/workspace/maos/node_modules/@vendure/core/src/config/entity/entity-duplicators/collection-duplicator.ts:66:38)
    at async /Users/mschipperheyn/workspace/maos/node_modules/@vendure/core/src/service/helpers/entity-duplicator/entity-duplicator.service.ts:76:35

The reason is this line

The quickest workaround here is to not assume the existence of an asset in the array. However, the more correct solution would be to copy the asset as well.

The core problem, one that is hard to deal with, is that any referenced entity that is on the source channel does not get copied. This means that the copied entity will have references to entities in a different channel and any "findInChannel" style query that gets executed on that entity is a pre or post load / update will not find those associations. If you don't manually copy those associations, problems ensue.

We find ourselves also dealing with facet copy tasks. With post copy actions such as removing copy and (Copy) labels, which is a whole can of worms in itself since it triggers an indexing action that generates errors by itself.

To Reproduce
Steps to reproduce the behavior:

  1. Copy a collection with an associated asset to a new channel

Expected behavior

  1. Associated assets should also be copied to the new channel. At the very least, erors should be prevented.

  2. In general, any copy action should offer the option to copy associations on a per association basis

  3. Ideally, copy actions should offer some kind of post processing function that gives the source and the newly created copied entity. So that any non predictable post processing can be executed.

async afterCopy(ctx: RequestContext, origEntity: T, newEntity: T) {
   [...] copy customField relations, etc.
}
  1. When you are copying to a new channel, it should be possible to disable the copy / (Copy) style label and slug changes through a setting.

  2. Ideally would be nice to be able to disable the indexer for the purpose of these changes until after the whole operation is complete.

Environment (please complete the following information):

  • @vendure/core version: 2.2.2
  • Nodejs version 20
  • Database (mysql/postgres etc): postgres

**Additional context
We are creating a channel copy service that uses a template channel as an input in order to get a pre-configured channel ready for a new seller

@mschipperheyn mschipperheyn added the type: bug 🐛 Something isn't working label May 3, 2024
@mschipperheyn
Copy link
Contributor Author

A workaround for the issue is to assign the to be copied entity to the target channel before copying (and removing after copying). That prevents the asset error occurring. But it doesn't resolve the other issues

@mschipperheyn
Copy link
Contributor Author

A co-worker suggested that since the assets can't really be edited and can therefore just get a channel copy, and a delete on one channel would not impact another channel reference. Didn't check that yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants