-
Notifications
You must be signed in to change notification settings - Fork 36
fix(affiliates): sync manual approval counts #317
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
Merged
ralyodio
merged 2 commits into
profullstack:master
from
Jorel97:codex/fix-affiliate-counts-275
May 29, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
supabase/migrations/20260529195000_adjust_affiliate_offer_total_affiliates.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| CREATE OR REPLACE FUNCTION public.moderate_affiliate_application( | ||
| p_offer_id UUID, | ||
| p_application_id UUID, | ||
| p_seller_id UUID, | ||
| p_status TEXT | ||
| ) | ||
| RETURNS public.affiliate_applications | ||
| LANGUAGE plpgsql | ||
| SECURITY DEFINER | ||
| SET search_path = public | ||
| AS $$ | ||
| DECLARE | ||
| v_offer public.affiliate_offers%ROWTYPE; | ||
| v_existing public.affiliate_applications%ROWTYPE; | ||
| v_updated public.affiliate_applications%ROWTYPE; | ||
| v_delta INTEGER := 0; | ||
| BEGIN | ||
| IF p_status NOT IN ('approved', 'rejected') THEN | ||
| RAISE EXCEPTION 'Invalid affiliate application status'; | ||
| END IF; | ||
|
|
||
| SELECT * | ||
| INTO v_offer | ||
| FROM public.affiliate_offers | ||
| WHERE id = p_offer_id | ||
| FOR UPDATE; | ||
|
|
||
| IF v_offer.id IS NULL OR v_offer.seller_id <> p_seller_id THEN | ||
| RAISE EXCEPTION 'Not found or not authorized'; | ||
| END IF; | ||
|
|
||
| SELECT * | ||
| INTO v_existing | ||
| FROM public.affiliate_applications | ||
| WHERE id = p_application_id | ||
| AND offer_id = p_offer_id | ||
| FOR UPDATE; | ||
|
|
||
| IF v_existing.id IS NULL THEN | ||
| RAISE EXCEPTION 'Application not found'; | ||
| END IF; | ||
|
|
||
| IF v_existing.status <> 'approved' AND p_status = 'approved' THEN | ||
| v_delta := 1; | ||
| ELSIF v_existing.status = 'approved' AND p_status <> 'approved' THEN | ||
| v_delta := -1; | ||
| END IF; | ||
|
|
||
| UPDATE public.affiliate_applications | ||
| SET | ||
| status = p_status::public.affiliate_application_status, | ||
| approved_at = CASE | ||
| WHEN p_status = 'approved' THEN COALESCE(approved_at, NOW()) | ||
| ELSE NULL | ||
| END, | ||
| updated_at = NOW() | ||
| WHERE id = p_application_id | ||
| AND offer_id = p_offer_id | ||
| RETURNING * INTO v_updated; | ||
|
|
||
| IF v_delta <> 0 THEN | ||
| UPDATE public.affiliate_offers | ||
| SET | ||
| total_affiliates = GREATEST(0, total_affiliates + v_delta), | ||
| updated_at = NOW() | ||
| WHERE id = p_offer_id; | ||
| END IF; | ||
|
|
||
| RETURN v_updated; | ||
| END; | ||
| $$; | ||
|
|
||
| REVOKE ALL ON FUNCTION public.moderate_affiliate_application(UUID, UUID, UUID, TEXT) FROM PUBLIC; | ||
| REVOKE ALL ON FUNCTION public.moderate_affiliate_application(UUID, UUID, UUID, TEXT) FROM anon; | ||
| REVOKE ALL ON FUNCTION public.moderate_affiliate_application(UUID, UUID, UUID, TEXT) FROM authenticated; | ||
| GRANT EXECUTE ON FUNCTION public.moderate_affiliate_application(UUID, UUID, UUID, TEXT) TO service_role; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.