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:
File diff suppressed because it is too large
Load Diff
82
apps/web/supabase/tests/database/memberships.test.sql
Normal file
82
apps/web/supabase/tests/database/memberships.test.sql
Normal 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;
|
||||||
@@ -12,7 +12,7 @@ INSERT INTO public.billing_customers(account_id, provider, customer_id)
|
|||||||
VALUES (tests.get_supabase_uid('primary_owner'), 'stripe', 'cus_test');
|
VALUES (tests.get_supabase_uid('primary_owner'), 'stripe', 'cus_test');
|
||||||
|
|
||||||
-- Call the upsert_order function
|
-- 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
|
-- Verify that the order was created correctly
|
||||||
SELECT is(
|
SELECT is(
|
||||||
@@ -29,7 +29,7 @@ SELECT row_eq(
|
|||||||
);
|
);
|
||||||
|
|
||||||
-- Call the upsert_order function again to update the order
|
-- 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
|
-- Verify that the order was updated correctly
|
||||||
SELECT is(
|
SELECT is(
|
||||||
|
|||||||
@@ -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');
|
VALUES (makerkit.get_account_id_by_slug('makerkit'), 'stripe', 'cus_test');
|
||||||
|
|
||||||
-- Call the upsert_order function
|
-- 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
|
-- Verify that the order was created correctly
|
||||||
SELECT is(
|
SELECT is(
|
||||||
@@ -29,7 +29,7 @@ SELECT row_eq(
|
|||||||
);
|
);
|
||||||
|
|
||||||
-- Call the upsert_order function again to update the order
|
-- 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
|
-- Verify that the order was updated correctly
|
||||||
SELECT is(
|
SELECT is(
|
||||||
|
|||||||
@@ -19,6 +19,12 @@ import { createStripeBillingPortalSession } from './create-stripe-billing-portal
|
|||||||
import { createStripeCheckout } from './create-stripe-checkout';
|
import { createStripeCheckout } from './create-stripe-checkout';
|
||||||
import { createStripeClient } from './stripe-sdk';
|
import { createStripeClient } from './stripe-sdk';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name StripeBillingStrategyService
|
||||||
|
* @description The Stripe billing strategy service
|
||||||
|
* @class StripeBillingStrategyService
|
||||||
|
* @implements {BillingStrategyProviderService}
|
||||||
|
*/
|
||||||
export class StripeBillingStrategyService
|
export class StripeBillingStrategyService
|
||||||
implements BillingStrategyProviderService
|
implements BillingStrategyProviderService
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ export class StripeWebhookHandlerService
|
|||||||
private readonly namespace = 'billing.stripe';
|
private readonly namespace = 'billing.stripe';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @name verifyWebhookSignature
|
||||||
* @description Verifies the webhook signature - should throw an error if the signature is invalid
|
* @description Verifies the webhook signature - should throw an error if the signature is invalid
|
||||||
*/
|
*/
|
||||||
async verifyWebhookSignature(request: Request) {
|
async verifyWebhookSignature(request: Request) {
|
||||||
@@ -57,14 +58,6 @@ export class StripeWebhookHandlerService
|
|||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async loadStripe() {
|
|
||||||
if (!this.stripe) {
|
|
||||||
this.stripe = await createStripeClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.stripe;
|
|
||||||
}
|
|
||||||
|
|
||||||
async handleWebhookEvent(
|
async handleWebhookEvent(
|
||||||
event: Stripe.Event,
|
event: Stripe.Event,
|
||||||
params: {
|
params: {
|
||||||
@@ -354,6 +347,14 @@ export class StripeWebhookHandlerService
|
|||||||
|
|
||||||
return onInvoicePaid(payload);
|
return onInvoicePaid(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async loadStripe() {
|
||||||
|
if (!this.stripe) {
|
||||||
|
this.stripe = await createStripeClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.stripe;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getISOString(date: number | null) {
|
function getISOString(date: number | null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user