Add revenue split to affiliate codes v2 (#4672)

* wip: affiliate payouts again

* Implement affiliate payout queue

* Deactivate subscription affiliations on cancellation

* Remove a test that never compiled in the first place

* Update sqlx cache

* address some PR comments

* more comments

* wip: handle refund charges

* cargo sqlx prepare

* Address PR comments

* cargo sqlx prepare
This commit is contained in:
aecsocket
2025-12-02 00:01:24 +00:00
committed by GitHub
parent 60e0953616
commit 783aaa6553
20 changed files with 679 additions and 10 deletions

View File

@@ -0,0 +1,26 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO payouts_values\n (user_id, amount, created,\n date_available, affiliate_code_source)\n VALUES ($1, $2, $3, $4, $5)\n RETURNING id\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Int8"
}
],
"parameters": {
"Left": [
"Int8",
"Numeric",
"Timestamptz",
"Timestamptz",
"Int8"
]
},
"nullable": [
false
]
},
"hash": "08310363d63462bf1d07f950f09b8e3b466c7d6fd7a6efd3984a3cbc87f996bc"
}

View File

@@ -0,0 +1,26 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT\n usap.id as usap_id,\n usap.payout_value_id\n FROM charges refund_charges\n -- find original charges that have been refunded\n INNER JOIN charges original_charges\n ON original_charges.id = refund_charges.parent_charge_id\n -- find affiliate payouts for those original charges\n INNER JOIN users_subscriptions_affiliations_payouts usap\n ON usap.charge_id = original_charges.id\n -- only include payouts that haven't been issued yet (not available as of now)\n INNER JOIN payouts_values pv\n ON pv.id = usap.payout_value_id\n AND pv.date_available > NOW()\n WHERE\n refund_charges.status = 'succeeded'\n -- make sure it's actually a refund charge\n AND refund_charges.charge_type = 'refund'\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "usap_id",
"type_info": "Int8"
},
{
"ordinal": 1,
"name": "payout_value_id",
"type_info": "Int8"
}
],
"parameters": {
"Left": []
},
"nullable": [
false,
false
]
},
"hash": "0d9ae03c785ef21ecd9dc17d7c025431bf14c6e168a8350f1fe7602be4934ece"
}

View File

@@ -0,0 +1,14 @@
{
"db_name": "PostgreSQL",
"query": "\n DELETE FROM payouts_values\n WHERE id = ANY($1::bigint[])\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8Array"
]
},
"nullable": []
},
"hash": "270cf4cb3a56ee14f3671d8d3ad17ed8fbdc0ca791370ac52ff480c0e2d2cbf6"
}

View File

@@ -0,0 +1,17 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO users_subscriptions_affiliations_payouts\n (charge_id, subscription_id,\n affiliate_code, payout_value_id)\n SELECT * FROM UNNEST($1::bigint[], $2::bigint[], $3::bigint[], $4::bigint[])\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8Array",
"Int8Array",
"Int8Array",
"Int8Array"
]
},
"nullable": []
},
"hash": "57fd45648479590a01e8da51b2ecd59cbe76a1ae8acb4c2d7c96ba397739873a"
}

View File

@@ -0,0 +1,16 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO users_subscriptions_affiliations\n (subscription_id, affiliate_code, deactivated_at)\n VALUES ($1, $2, $3)\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8",
"Int8",
"Timestamptz"
]
},
"nullable": []
},
"hash": "64844433bb6c7e5a48890ec42786f56a5e220c21aa9d0fc0c03f57bee864fb63"
}

View File

@@ -0,0 +1,25 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO users_subscriptions_affiliations_payouts\n (charge_id, subscription_id, affiliate_code, payout_value_id)\n VALUES ($1, $2, $3, $4)\n RETURNING id\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Int8"
}
],
"parameters": {
"Left": [
"Int8",
"Int8",
"Int8",
"Int8"
]
},
"nullable": [
false
]
},
"hash": "82a8120805e27f9134ccaa02ea25e7ddaf51f952783f68eda706505a825f25a5"
}

View File

@@ -0,0 +1,14 @@
{
"db_name": "PostgreSQL",
"query": "\n DELETE FROM users_subscriptions_affiliations_payouts\n WHERE id = ANY($1::bigint[])\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8Array"
]
},
"nullable": []
},
"hash": "8b3990f01c62c20fc2ff66566282db593fd6e8990a7e32305c2daa4aac3f6d74"
}

View File

@@ -0,0 +1,14 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE users_subscriptions_affiliations\n SET deactivated_at = NOW()\n WHERE subscription_id = $1",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8"
]
},
"nullable": []
},
"hash": "abdda73294ec06970af162132e49c3b8116b282e0edb2d3c71b8a98e6353ce82"
}

View File

@@ -0,0 +1,68 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT\n c.id as charge_id,\n c.subscription_id as \"subscription_id!\",\n c.net as charge_net,\n c.tax_amount as charge_tax_amount,\n c.last_attempt as charge_last_attempt,\n c.currency_code,\n usa.affiliate_code,\n ac.affiliate as affiliate_user_id,\n ac.revenue_split\n -- get any charges...\n FROM charges c\n -- ...which have a subscription...\n INNER JOIN users_subscriptions_affiliations usa\n ON c.subscription_id = usa.subscription_id\n AND c.subscription_id IS NOT NULL\n AND (usa.deactivated_at IS NULL OR c.last_attempt < usa.deactivated_at)\n -- ...which have an affiliate code...\n INNER JOIN affiliate_codes ac\n ON usa.affiliate_code = ac.id\n -- ...and where no payout to an affiliate has been made for this charge yet\n LEFT JOIN users_subscriptions_affiliations_payouts usap\n ON c.id = usap.charge_id\n WHERE\n c.status = 'succeeded'\n AND c.net > 0\n AND usap.id IS NULL\n -- exclude charges that have refund charges\n AND NOT EXISTS (\n SELECT 1\n FROM charges refund_charges\n WHERE refund_charges.parent_charge_id = c.id\n )\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "charge_id",
"type_info": "Int8"
},
{
"ordinal": 1,
"name": "subscription_id!",
"type_info": "Int8"
},
{
"ordinal": 2,
"name": "charge_net",
"type_info": "Int8"
},
{
"ordinal": 3,
"name": "charge_tax_amount",
"type_info": "Int8"
},
{
"ordinal": 4,
"name": "charge_last_attempt",
"type_info": "Timestamptz"
},
{
"ordinal": 5,
"name": "currency_code",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "affiliate_code",
"type_info": "Int8"
},
{
"ordinal": 7,
"name": "affiliate_user_id",
"type_info": "Int8"
},
{
"ordinal": 8,
"name": "revenue_split",
"type_info": "Float8"
}
],
"parameters": {
"Left": []
},
"nullable": [
false,
true,
true,
false,
true,
false,
false,
false,
true
]
},
"hash": "e8225fa8dae7e1ca57bc8f259cda40c3eb8b12943db05884386a456c5eb91117"
}