Skip to content

Commit da9b01c

Browse files
committed
fix: add explicit enum/numeric casts in coupons SQL for PostgreSQL type resolution
1 parent 1d843d9 commit da9b01c

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

  • rustbill/crates/rustbill-server/src/routes/billing

rustbill/crates/rustbill-server/src/routes/billing/coupons.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async fn create(
5555
) -> ApiResult<(StatusCode, Json<serde_json::Value>)> {
5656
let row = sqlx::query_scalar::<_, serde_json::Value>(
5757
r#"INSERT INTO coupons (id, code, name, discount_type, discount_value, currency, max_redemptions, times_redeemed, valid_from, valid_until, active, applies_to, created_at, updated_at)
58-
VALUES (gen_random_uuid()::text, $1, $2, $3, $4, COALESCE($5, 'USD'), $6, 0, COALESCE($7::timestamp, now()), $8::timestamp, COALESCE($9, true), $10, now(), now())
58+
VALUES (gen_random_uuid()::text, $1, $2, $3::discount_type, $4::numeric, COALESCE($5, 'USD'), $6, 0, COALESCE($7::timestamp, now()), $8::timestamp, COALESCE($9, true), $10::jsonb, now(), now())
5959
RETURNING to_jsonb(coupons)"#,
6060
)
6161
.bind(body["code"].as_str())
@@ -67,7 +67,7 @@ async fn create(
6767
.bind(body["validFrom"].as_str())
6868
.bind(body["validUntil"].as_str())
6969
.bind(body["active"].as_bool())
70-
.bind(body.get("appliesTo"))
70+
.bind(body.get("appliesTo").map(|v| v.to_string()))
7171
.fetch_one(&state.db)
7272
.await
7373
.map_err(rustbill_core::error::BillingError::from)?;
@@ -85,13 +85,13 @@ async fn update(
8585
r#"UPDATE coupons SET
8686
code = COALESCE($2, code),
8787
name = COALESCE($3, name),
88-
discount_type = COALESCE($4, discount_type),
89-
discount_value = COALESCE($5, discount_value),
88+
discount_type = COALESCE($4::discount_type, discount_type),
89+
discount_value = COALESCE($5::numeric, discount_value),
9090
currency = COALESCE($6, currency),
9191
max_redemptions = COALESCE($7, max_redemptions),
9292
valid_until = COALESCE($8::timestamp, valid_until),
9393
active = COALESCE($9, active),
94-
applies_to = COALESCE($10, applies_to),
94+
applies_to = COALESCE($10::jsonb, applies_to),
9595
updated_at = now()
9696
WHERE id = $1 AND deleted_at IS NULL
9797
RETURNING to_jsonb(coupons)"#,
@@ -105,7 +105,7 @@ async fn update(
105105
.bind(body["maxRedemptions"].as_i64().map(|v| v as i32))
106106
.bind(body["validUntil"].as_str())
107107
.bind(body["active"].as_bool())
108-
.bind(body.get("appliesTo"))
108+
.bind(body.get("appliesTo").map(|v| v.to_string()))
109109
.fetch_optional(&state.db)
110110
.await
111111
.map_err(rustbill_core::error::BillingError::from)?

0 commit comments

Comments
 (0)