Optimize code for accounts and roles management

The large update includes optimization for account and role management. The code has been revised for better readability and efficiency. Changes include formatting updates and enhancements to the creation and handling of accounts, roles, and memberships. Privacy settings have also been adjusted to provide more secure handling of user data. Code comments have been updated for better understanding of the functions.
This commit is contained in:
giancarlo
2024-04-28 12:55:01 +07:00
parent 2382d6485c
commit 3efbf6029f
6 changed files with 1023 additions and 647 deletions

View File

@@ -0,0 +1,82 @@
begin;
create extension "basejump-supabase_test_helpers" version '0.0.6';
select no_plan();
select makerkit.set_identifier('primary_owner', 'test@makerkit.dev');
select makerkit.set_identifier('owner', 'owner@makerkit.dev');
select makerkit.set_identifier('member', 'member@makerkit.dev');
select makerkit.set_identifier('custom', 'custom@makerkit.dev');
-- another user not in the team
select tests.create_supabase_user('test', 'test@supabase.com');
select tests.authenticate_as('owner');
-- Can check if an account is a team member
-- Primary owner
select is(
(select public.is_team_member(
makerkit.get_account_id_by_slug('makerkit'),
tests.get_supabase_uid('member')
)),
true,
'The primary account owner can check if a member is a team member'
);
select tests.authenticate_as('member');
-- Member
select is(
(select public.is_team_member(
makerkit.get_account_id_by_slug('makerkit'),
tests.get_supabase_uid('owner')
)),
true,
'The member can check if another member is a team member'
);
select is(
(select public.has_role_on_account(
makerkit.get_account_id_by_slug('makerkit')
)),
true,
'The member can check if they have a role on the account'
);
select tests.authenticate_as('test');
-- Foreigners
-- Cannot query the team account memberships
select is(
(select public.is_team_member(
makerkit.get_account_id_by_slug('makerkit'),
tests.get_supabase_uid('owner')
)),
false,
'The foreigner cannot check if a member is a team member'
);
-- Does not have a role on the account
select is(
(select public.has_role_on_account(
makerkit.get_account_id_by_slug('makerkit')
)),
false,
'The foreigner does not have a role on the account'
);
select is_empty(
$$ select * from public.accounts_memberships where account_id = makerkit.get_account_id_by_slug('makerkit') $$,
'The foreigner cannot query the team account memberships'
);
select is_empty(
$$ select * from public.accounts where id = makerkit.get_account_id_by_slug('makerkit') $$,
'The foreigner cannot query the team account'
);
select * from finish();
rollback;

View File

@@ -12,7 +12,7 @@ INSERT INTO public.billing_customers(account_id, provider, customer_id)
VALUES (tests.get_supabase_uid('primary_owner'), 'stripe', 'cus_test');
-- Call the upsert_order function
SELECT public.upsert_order(tests.get_supabase_uid('primary_owner'), 'cus_test', 'order_test', 'pending', 'stripe', 100, 'usd', '[{"product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 1}]');
SELECT public.upsert_order(tests.get_supabase_uid('primary_owner'), 'cus_test', 'order_test', 'pending', 'stripe', 100, 'usd', '[{"id":"order_item_1", "product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 1}]');
-- Verify that the order was created correctly
SELECT is(
@@ -29,7 +29,7 @@ SELECT row_eq(
);
-- Call the upsert_order function again to update the order
SELECT public.upsert_order(tests.get_supabase_uid('primary_owner'), 'cus_test', 'order_test', 'succeeded', 'stripe', 100, 'usd', '[{"product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 10}]');
SELECT public.upsert_order(tests.get_supabase_uid('primary_owner'), 'cus_test', 'order_test', 'succeeded', 'stripe', 100, 'usd', '[{"id":"order_item_1", "product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 10}]');
-- Verify that the order was updated correctly
SELECT is(

View File

@@ -12,7 +12,7 @@ INSERT INTO public.billing_customers(account_id, provider, customer_id)
VALUES (makerkit.get_account_id_by_slug('makerkit'), 'stripe', 'cus_test');
-- Call the upsert_order function
SELECT public.upsert_order(makerkit.get_account_id_by_slug('makerkit'), 'cus_test', 'order_test', 'pending', 'stripe', 100, 'usd', '[{"product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 1}]');
SELECT public.upsert_order(makerkit.get_account_id_by_slug('makerkit'), 'cus_test', 'order_test', 'pending', 'stripe', 100, 'usd', '[{"id":"order_item_1", "product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 1}]');
-- Verify that the order was created correctly
SELECT is(
@@ -29,7 +29,7 @@ SELECT row_eq(
);
-- Call the upsert_order function again to update the order
SELECT public.upsert_order(makerkit.get_account_id_by_slug('makerkit'), 'cus_test', 'order_test', 'succeeded', 'stripe', 100, 'usd', '[{"product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 10}]');
SELECT public.upsert_order(makerkit.get_account_id_by_slug('makerkit'), 'cus_test', 'order_test', 'succeeded', 'stripe', 100, 'usd', '[{"id":"order_item_1", "product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 10}]');
-- Verify that the order was updated correctly
SELECT is(