feat: Implement core monetization features including course pricing, payment preference creation, and transaction management with Mercado Pago integration.

This commit is contained in:
2026-02-15 13:40:48 -03:00
parent f613f34a96
commit 34e72ae985
13 changed files with 895 additions and 194 deletions
@@ -0,0 +1,64 @@
-- Add price and currency to courses table
ALTER TABLE courses ADD COLUMN price NUMERIC(10, 2) DEFAULT 0.00;
ALTER TABLE courses ADD COLUMN currency VARCHAR(10) DEFAULT 'USD';
-- Update fn_create_course to handle price and currency
CREATE OR REPLACE FUNCTION fn_create_course(
p_organization_id UUID,
p_instructor_id UUID,
p_title TEXT,
p_pacing_mode TEXT DEFAULT 'self_paced',
p_price NUMERIC(10, 2) DEFAULT 0.00,
p_currency TEXT DEFAULT 'USD'
) RETURNS SETOF courses AS $$
BEGIN
RETURN QUERY
INSERT INTO courses (
organization_id,
instructor_id,
title,
pacing_mode,
price,
currency
) VALUES (
p_organization_id,
p_instructor_id,
p_title,
p_pacing_mode,
p_price,
p_currency
) RETURNING *;
END;
$$ LANGUAGE plpgsql;
-- Update fn_update_course to handle price and currency
CREATE OR REPLACE FUNCTION fn_update_course(
p_id UUID,
p_organization_id UUID,
p_title TEXT,
p_description TEXT,
p_passing_percentage INTEGER,
p_pacing_mode TEXT,
p_start_date TIMESTAMP WITH TIME ZONE,
p_end_date TIMESTAMP WITH TIME ZONE,
p_certificate_template TEXT,
p_price NUMERIC(10, 2),
p_currency TEXT
) RETURNS SETOF courses AS $$
BEGIN
RETURN QUERY
UPDATE courses SET
title = p_title,
description = p_description,
passing_percentage = p_passing_percentage,
pacing_mode = p_pacing_mode,
start_date = p_start_date,
end_date = p_end_date,
certificate_template = p_certificate_template,
price = p_price,
currency = p_currency,
updated_at = NOW()
WHERE id = p_id AND organization_id = p_organization_id
RETURNING *;
END;
$$ LANGUAGE plpgsql;
+27 -2
View File
@@ -261,11 +261,23 @@ pub async fn create_course(
org_ctx.id
};
let course = sqlx::query_as::<_, Course>("SELECT * FROM fn_create_course($1, $2, $3, $4)")
let price = payload
.get("price")
.and_then(|v| v.as_f64())
.unwrap_or(0.0);
let currency = payload
.get("currency")
.and_then(|v| v.as_str())
.unwrap_or("USD");
let course = sqlx::query_as::<_, Course>("SELECT * FROM fn_create_course($1, $2, $3, $4, $5, $6)")
.bind(target_org_id)
.bind(instructor_id)
.bind(title)
.bind(pacing_mode)
.bind(price)
.bind(currency)
.fetch_one(&mut *tx)
.await
.map_err(|e| {
@@ -358,6 +370,17 @@ pub async fn update_course(
.and_then(|s| s.parse::<DateTime<Utc>>().ok())
.or(existing.end_date);
let price = payload
.get("price")
.and_then(|v| v.as_f64())
.unwrap_or(existing.price);
let currency = payload
.get("currency")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.unwrap_or(existing.currency);
// BEGIN TRANSACTION
let mut tx = pool
.begin()
@@ -375,7 +398,7 @@ pub async fn update_course(
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let course = sqlx::query_as::<_, Course>(
"SELECT * FROM fn_update_course($1, $2, $3, $4, $5, $6, $7, $8, $9)",
"SELECT * FROM fn_update_course($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
)
.bind(id)
.bind(org_ctx.id)
@@ -386,6 +409,8 @@ pub async fn update_course(
.bind(start_date)
.bind(end_date)
.bind(certificate_template)
.bind(price)
.bind(currency)
.fetch_one(&mut *tx)
.await
.map_err(|e| {