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

Filter results if joined table has an authorization.recordHandler #1009

Open
chattago2002 opened this issue Jan 18, 2024 · 3 comments
Open
Assignees

Comments

@chattago2002
Copy link

chattago2002 commented Jan 18, 2024

Hi.
I'm trying to have good results from a call with join and filters.

I have 2 tables: "requests_details" and "requests". For the "requests" table I set the "authorization.recordHandler" middleware to add "filter=user_id,eq,$_SESSION['apiUser']['id']" if the user is not "admin".

The API endpoint is "/records/requests_details?join=requests&filter=request_id,nis".

At the moment I have the following response:

{
    "records": [
        {
            "id": 75,
            "request_id": null,
            "full_name": "John Doe",
            "phone": "00390099888",
            "room": "single"
        },
        {
            "id": 219,
            "request_id": {
                "id": 41,
                "full_name": "Melvin Carr",
                "birthday": "1960-06-27",
                "email": "MelvinRCarr@rhyta.com",
                "user_id": 128
            },
            "full_name": "Melvin Carr",
            "phone": "0039444777",
            "room": "double"
        }
    ]
}

For good results I mean those where request_id is not null.
I set "authorization.recordHandler" additional as following:

if ($tableName === 'requests_details') { return ($tableName == 'requests_details') ? 'filter=request_id,nis' : ''; } `

Where I'm wrong?
Is there another way to achieve the desired result?

Thanks

@mevdschee
Copy link
Owner

mevdschee commented Jan 18, 2024

Your code:

if ($tableName === 'requests_details') { return ($tableName == 'requests_details') ? 'filter=request_id,nis' : ''; }

Should be:

return ($tableName == 'requests_details') ? 'filter=request_id,nis' : '';

Other than that I see no code problem.

As for the logic: I can see why the filter doesn't work as the null results are caused by the recordHandler. You can apply an customization.afterHandler if you really want to remove those results from the query. You can also list the requests with their corresponding requests_details instead of the other way around.

@mevdschee mevdschee self-assigned this Jan 18, 2024
@chattago2002
Copy link
Author

chattago2002 commented Jan 19, 2024

I was using that code because I have other conditions for other tables but... my code and your code couldn't be considered very similar?

Anyway I replaced my code with yours but results are the same so I decided to use customization.afterHandler but I cannot find information about it and its parameters. I made the following code:
if ($tableName == 'requests_details') { $arr = json_decode($response->getBody()->getContents(), true); $arr['records'] = array_filter($arr['records'], function ($obj) { return !is_null($obj['request_id']); }); return ResponseFactory::fromObject(200, $arr, 1); }

Results are ok but the question is: Is this the right way?

Another question: in authorization.recordHandler I can add only the filter parameter? Is not possible to use others as excludeor join? I tried but it seems not working.

@mevdschee
Copy link
Owner

mevdschee commented Mar 19, 2024

couldn't be considered very similar?

I agree.

Is this the right way?

It seems good to me.

I tried but it seems not working.

I think only filter is allowed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants