skip to Main Content

OK I have a system that generates subscriptions from customers related to

  1. Service,.
  2. Account (email) belonging to that service.
  3. Profile related to the account.
    All this is associated to a client, but to be able to see it at the time of the subscription all this keeps relation to the authenticated user.

The problem I have it when obtaining the profiles.

In my Accounts table I have a field called sale_type that can have only 2 options:
‘complete’ and ‘profile’

I have functions to get the accounts associated to a service_id, so I can get the profiles associated to an account_id.

When an account has sale_type = ‘complete’; its associated profiles are linked through the account_id which in turn has a user_id (account holder) in the accounts table.

But when an account has sale_type = ‘profile”; its profiles are associated to the user through a field called user_id in the profiles table.

In this way; if an account is sale_type = ‘complete and you want to make a subscription, you will get all the profiles associated to that account, while if an account is sale_type = ‘profile’ you will get only the profiles belonging to that account in which the user_id (from the profiles table) is the authenticated user.

profiles table

In the following image I can select the Service, I can select a mail (mabeliñ[email protected]) with the condition sale_type = ‘complete’, and then I can see the profiles it has.

subscription view

But when I choose an account with the condition sale_type = ‘profile’, it does not show me the profiles that the account has on behalf of the authenticated user.

subscripton view

I have the function below that shows me the profiles in the accounts sale_type= ‘complete’ but does not show the profiles in the accounts sale_type= ‘profile’.

public function getProfiles($account_id){
        $data = [];

        // Obtener la cuenta con el id de cuenta dado
        $account = Account::find($account_id);

        // Comprobar si la cuenta existe
        if ($account) {
            $attr = [
                'table' => 'profiles',
                'columns' => [
                    'id',
                    'name',
                    'pin',
                    'user_id' // Agregar user_id para obtener este campo también
                ],
                'compare' => 'account_id',
                'compare_value' => $account_id
            ];
            $response = Helper::getDataSelect($attr);

            // Verificar el sale_type de la cuenta
            if ($account->sale_type == 'complete') {
                // Si sale_type es 'complete', mostrar todos los perfiles
                $data = $response;
            } else if ($account->sale_type == 'profile') {
                // Si sale_type es 'profile', mostrar solo los perfiles para el usuario actual
                if (count($response) > 0){
                    foreach ($response as $res){
                        $profile = Profile::find($res->id);

                        if ($profile && $profile->subscriptions->count() == 0) {
                            if ($profile->user_id == Auth::user()->id) {
                                array_push($data, $res);
                            } 
                        }
                    }
                }
            }
        }

        return response()->json(['data' => $data]);
    }

I have tried changing the function to this:

public function getProfiles($account_id){
    $data = [];
    $attr = [
        'table'=>'profiles',
        'columns'=>[
            'id',
            'name',
            'pin'
        ],
        'compare'=>'account_id',
        'compare_value'=>$account_id
    ];
    $response = Helper::getDataSelect($attr);
    if(count($response) > 0){
        foreach($response as $res){
            $profile = Profile::find($res->id);
            if($profile->subscriptions->count() == 0){

                if($profile->user_id == Auth::user()->id){
                    array_push($data, $res);
                }
            }
        }
    }

    return response()->json(['data'=>$data]);
}

This way it lets me see the profiles of the account sale_type= ‘profile’ but it does not let me see the profiles of the accounts sale_type=’complete’ 🙁

I can see the profiles that belong to me

and for the ‘full’ account I don’t see the profiles

I need help, to allow me to show the profiles depending on the sale_type, because currently if I use one method I can see the sale_type=’complete’ and the others not. and if I change the function I can see the accounts sale_type=’profile’ and the others not 🙁

esta es mi funcion en el Helper:

public static function getDataSelect($attr, $validateUser=false){
            if($attr){
                if($validateUser){
                    $data = DB::table($attr['table'])->select($attr['columns'])->where($attr['compare'],$attr['compare_value'])->where('user_id',Auth::user()->id)->get();
                }else{
                    $data = DB::table($attr['table'])->select($attr['columns'])->where($attr['compare'],$attr['compare_value'])->get();
                }

                return $data;
            }else{
                return null;
            }

        }

2

Answers


  1. Chosen as BEST ANSWER

    I solved the problem this way

    public function getProfiles($account_id)
        {
            $data = [];
            $account = Account::find($account_id);
    
            $attr = [
                'table' => 'profiles',
                'columns' => [
                    'id',
                    'name',
                    'pin',
                    'user_id'
                ],
                'compare' => 'account_id',
                'compare_value' => $account_id
            ];
            $response = Helper::getDataSelect($attr);
            if (count($response) > 0) {
                foreach ($response as $res) {
                    $profile = Profile::find($res->id);
                    if ($profile->subscriptions->count() == 0) {
    
                        if ($profile->user_id == Auth::user()->id) {
                            array_push($data, $res);
                        } else {
                            if ($account) {
                                if ($account->sale_type == 'complete') {
                                    // Si sale_type es 'complete', mostrar todos los perfiles
                                    $data = $response;
                                }
    
                            }
                        }
                    }
                }
            }
    
            return response()->json(['data' => $data]);
        }
    

  2. What if you use the filtering for your second scenario based on authenticated user?

    Here is your code with modifications:

    public function getProfiles($account_id) {
        $data = [];
        $account = Account::find($account_id);
        if ($account) {
            $attr = [
                'table' => 'profiles',
                'columns' => [
                    'id',
                    'name',
                    'pin',
                    'user_id'
                ],
                'compare' => 'account_id',
                'compare_value' => $account_id
            ];
            $response = Helper::getDataSelect($attr);
    
            if ($account->sale_type == 'complete') {
                $data = $response;
            } else if ($account->sale_type == 'profile') {
                foreach ($response as $res) {
                    if ($res->user_id == Auth::user()->id) {
                        $data[] = $res;
                    }
                }
            }
        }
        return response()->json(['data' => $data]);
    }
    

    I hope this help you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search