directus / directus

The flexible backend for all your projects 🐰 Turn your DB into a headless CMS, admin panels, or apps with a custom UI, instant APIs, auth & more.
https://directus.io
Other
28.05k stars 3.91k forks source link

Filter() Hook should be able to modify nested entities #20746

Closed Be-P closed 10 months ago

Be-P commented 11 months ago

Describe the Bug

I want to modify on a filter() hook the payload that's being used to update nested entities. For example, imagine a School has a one2Many with Students. From the School detail I can modify the student e.g. name, and I'll receive in the filter() hook a payload like this: { id: 1, students: {create:[], delete: [], update: [{id:1, name:"foo bar"}]} Now, in the hook I would like to modify it as follows:
{ id: 1, students: {create:[], delete: [], update: [{id:1, name:"foo",surname:"bar"}]} and return it. I've tried to do so, but the modification does not have effect: in the Student filter() hook I still get the original, not modified value.

To Reproduce

Create an o2m relation, modify the payload, and see it doesn't actually update the nested relation entity

Directus Version

10.8.2

Hosting Strategy

Self-Hosted (Docker Image)

Be-P commented 11 months ago
                        // api/src/services/items.ts:680
            for (const key of keys) {
                const { revisions, nestedActionEvents: nestedActionEventsO2M } = await payloadService.processO2M(
                    **payload**, // This should became something like **payloadAfterHooks**
                    key,
                    opts
                );

                childrenRevisions.push(...revisions);
                nestedActionEvents.push(...nestedActionEventsO2M);
            }
Be-P commented 11 months ago

@br41nslug do you want me to create a PR?

br41nslug commented 11 months ago

I've triaged the issue so you definitely try your hand at fixing it 😄 want me to assign you @Be-P ?

Be-P commented 11 months ago

which branch should I point to with the fixing PR? let me first try to create a PR as exercise, I don't want to slow down your lifecycle if I mess up with something eheh

mahendraHegde commented 10 months ago

@Be-P which api is causing this issue? i tried with PATCH /items/:collection, its working fine for me, i was able to update data in filter hook

mahendraHegde commented 10 months ago

@br41nslug i have added a fix for it. lmk your thoughts.