-
Notifications
You must be signed in to change notification settings - Fork 348
Add revenue split to affiliate codes v2 #4672
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
base: main
Are you sure you want to change the base?
Changes from all commits
9e88a91
d9108f2
ebf3e7e
4aafbd8
e38e8de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| CREATE TABLE users_subscriptions_affiliations ( | ||
| subscription_id BIGINT NOT NULL REFERENCES users_subscriptions(id), | ||
| affiliate_code BIGINT NOT NULL REFERENCES affiliate_codes(id), | ||
| created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| deactivated_at TIMESTAMPTZ, | ||
| UNIQUE (subscription_id) | ||
| ); | ||
|
|
||
| CREATE TABLE users_subscriptions_affiliations_payouts( | ||
| id BIGSERIAL PRIMARY KEY, | ||
| charge_id BIGINT NOT NULL REFERENCES charges(id), | ||
| subscription_id BIGINT NOT NULL REFERENCES users_subscriptions(id), | ||
| affiliate_code BIGINT NOT NULL REFERENCES affiliate_codes(id), | ||
| payout_value_id BIGSERIAL NOT NULL REFERENCES payouts_values(id), | ||
| UNIQUE (charge_id) | ||
| ); | ||
|
|
||
| ALTER TABLE payouts_values | ||
| ADD COLUMN affiliate_code_source BIGINT; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| use chrono::{DateTime, Utc}; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| use crate::database::models::{ | ||
| DBAffiliateCodeId, DBChargeId, DBUserSubscriptionId, | ||
| }; | ||
|
|
||
| #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| pub struct DBUsersSubscriptionsAffiliations { | ||
| pub subscription_id: DBUserSubscriptionId, | ||
| pub affiliate_code: DBAffiliateCodeId, | ||
| pub deactivated_at: Option<DateTime<Utc>>, | ||
| } | ||
|
|
||
| impl DBUsersSubscriptionsAffiliations { | ||
| pub async fn insert<'a, E>(&self, exec: E) -> sqlx::Result<()> | ||
| where | ||
| E: sqlx::PgExecutor<'a>, | ||
| { | ||
| sqlx::query_scalar!( | ||
| " | ||
| INSERT INTO users_subscriptions_affiliations | ||
| (subscription_id, affiliate_code, deactivated_at) | ||
| VALUES ($1, $2, $3) | ||
| ", | ||
| self.subscription_id.0, | ||
| self.affiliate_code.0, | ||
| self.deactivated_at, | ||
| ) | ||
| .fetch_one(exec) | ||
| .await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub async fn deactivate<'a, E>( | ||
| subscription_id: DBUserSubscriptionId, | ||
| exec: E, | ||
| ) -> sqlx::Result<()> | ||
| where | ||
| E: sqlx::PgExecutor<'a>, | ||
| { | ||
| sqlx::query!( | ||
| "UPDATE users_subscriptions_affiliations | ||
| SET deactivated_at = NOW() | ||
| WHERE subscription_id = $1", | ||
| subscription_id.0, | ||
| ) | ||
| .execute(exec) | ||
| .await?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| pub struct DBUsersSubscriptionsAffiliationsPayouts { | ||
| pub id: i64, | ||
| pub charge_id: DBChargeId, | ||
| pub subscription_id: DBUserSubscriptionId, | ||
| pub affiliate_code: DBAffiliateCodeId, | ||
| pub payout_value_id: i64, | ||
| } | ||
|
|
||
| impl DBUsersSubscriptionsAffiliationsPayouts { | ||
| pub async fn insert<'a, E>(&mut self, exec: E) -> sqlx::Result<()> | ||
| where | ||
| E: sqlx::PgExecutor<'a>, | ||
| { | ||
| let id = sqlx::query_scalar!( | ||
| " | ||
| INSERT INTO users_subscriptions_affiliations_payouts | ||
| (charge_id, subscription_id, affiliate_code, payout_value_id) | ||
| VALUES ($1, $2, $3, $4) | ||
| RETURNING id | ||
| ", | ||
| self.charge_id.0, | ||
| self.subscription_id.0, | ||
| self.affiliate_code.0, | ||
| self.payout_value_id, | ||
| ) | ||
| .fetch_one(exec) | ||
| .await?; | ||
|
|
||
| self.id = id; | ||
| Ok(()) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be setting deactivated_at when a subscription is cancelled in the subscription cancelled route handler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done in
routes/internal/billing.rs- see calls toDBUsersSubscriptionsAffiliations::deactivate. But I'm not sure if I've covered all of the places where this is set. I'd like a review to see if there are any paths I've missed for this.