Skip to content

Commit ebf3e7e

Browse files
committed
Deactivate subscription affiliations on cancellation
1 parent d9108f2 commit ebf3e7e

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

apps/labrinth/migrations/20251024182919_subscription_affiliations.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
CREATE TABLE users_subscriptions_affiliations (
2-
id BIGSERIAL NOT NULL PRIMARY KEY,
32
subscription_id BIGINT NOT NULL REFERENCES users_subscriptions(id),
43
affiliate_code BIGINT NOT NULL REFERENCES affiliate_codes(id),
54
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
6-
deactivated_at TIMESTAMPTZ
5+
deactivated_at TIMESTAMPTZ,
6+
UNIQUE (subscription_id)
77
);
88

99
CREATE TABLE users_subscriptions_affiliations_payouts(

apps/labrinth/src/database/models/users_subscriptions_affiliations.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,43 @@ use crate::database::models::{
77

88
#[derive(Debug, Clone, Serialize, Deserialize)]
99
pub struct DBUsersSubscriptionsAffiliations {
10-
pub id: i64,
1110
pub subscription_id: DBUserSubscriptionId,
1211
pub affiliate_code: DBAffiliateCodeId,
1312
pub deactivated_at: Option<DateTime<Utc>>,
1413
}
1514

1615
impl DBUsersSubscriptionsAffiliations {
17-
pub async fn insert<'a, E>(&mut self, exec: E) -> sqlx::Result<()>
16+
pub async fn insert<'a, E>(&self, exec: E) -> sqlx::Result<()>
1817
where
1918
E: sqlx::PgExecutor<'a>,
2019
{
21-
let id = sqlx::query_scalar!(
20+
sqlx::query_scalar!(
2221
"
2322
INSERT INTO users_subscriptions_affiliations
2423
(subscription_id, affiliate_code, deactivated_at)
2524
VALUES ($1, $2, $3)
26-
RETURNING id
2725
",
2826
self.subscription_id.0,
2927
self.affiliate_code.0,
3028
self.deactivated_at,
3129
)
3230
.fetch_one(exec)
3331
.await?;
34-
35-
self.id = id;
3632
Ok(())
3733
}
3834

39-
pub async fn update<'a, E>(&mut self, exec: E) -> sqlx::Result<()>
35+
pub async fn deactivate<'a, E>(
36+
subscription_id: DBUserSubscriptionId,
37+
exec: E,
38+
) -> sqlx::Result<()>
4039
where
4140
E: sqlx::PgExecutor<'a>,
4241
{
4342
sqlx::query!(
4443
"UPDATE users_subscriptions_affiliations
45-
SET subscription_id = $1, affiliate_code = $2, deactivated_at = $3
46-
WHERE id = $4",
47-
self.subscription_id.0,
48-
self.affiliate_code.0,
49-
self.deactivated_at,
50-
self.id
44+
SET deactivated_at = NOW()
45+
WHERE subscription_id = $1",
46+
subscription_id.0,
5147
)
5248
.execute(exec)
5349
.await?;

apps/labrinth/src/queue/payouts/affiliate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ pub async fn process_affiliate_payouts(postgres: &PgPool) -> Result<()> {
4848
INNER JOIN users_subscriptions_affiliations usa
4949
ON c.subscription_id = usa.subscription_id
5050
AND c.subscription_id IS NOT NULL
51+
AND usa.deactivated_at IS NULL
5152
-- ...which have an affiliate code...
5253
INNER JOIN affiliate_codes ac
5354
ON usa.affiliate_code = ac.id
54-
AND usa.deactivated_at IS NULL
5555
-- ...and where no payout to an affiliate has been made for this charge yet
5656
LEFT JOIN users_subscriptions_affiliations_payouts usap
5757
ON c.id = usap.charge_id

apps/labrinth/src/routes/internal/billing.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,11 @@ pub async fn edit_subscription(
616616
..
617617
} if open_charge.status == ChargeStatus::Failed => {
618618
if cancelled {
619+
DBUsersSubscriptionsAffiliations::deactivate(
620+
subscription.id,
621+
&mut *transaction,
622+
)
623+
.await?;
619624
open_charge.status = ChargeStatus::Cancelled;
620625
} else {
621626
// Forces another resubscription attempt
@@ -635,6 +640,11 @@ pub async fn edit_subscription(
635640
) =>
636641
{
637642
open_charge.status = if cancelled {
643+
DBUsersSubscriptionsAffiliations::deactivate(
644+
subscription.id,
645+
&mut *transaction,
646+
)
647+
.await?;
638648
ChargeStatus::Cancelled
639649
} else {
640650
ChargeStatus::Open
@@ -2073,7 +2083,6 @@ pub async fn stripe_webhook(
20732083
.and_then(|m| m.affiliate_code)
20742084
{
20752085
DBUsersSubscriptionsAffiliations {
2076-
id: 0,
20772086
subscription_id: subscription.id,
20782087
affiliate_code: DBAffiliateCodeId::from(
20792088
affiliate_code,
@@ -2083,8 +2092,6 @@ pub async fn stripe_webhook(
20832092
.insert(&mut *transaction)
20842093
.await?;
20852094
}
2086-
2087-
// TODO affiliate code
20882095
};
20892096

20902097
subscription.status = SubscriptionStatus::Provisioned;

0 commit comments

Comments
 (0)