fix(companies): restore backend build
This commit is contained in:
parent
349673b7f8
commit
9b3bc98b38
3 changed files with 24 additions and 24 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -804,6 +804,7 @@ dependencies = [
|
||||||
"db",
|
"db",
|
||||||
"email",
|
"email",
|
||||||
"redis",
|
"redis",
|
||||||
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
|
|
|
||||||
|
|
@ -21,4 +21,5 @@ storage = { path = "../../crates/storage" }
|
||||||
bytes = { workspace = true }
|
bytes = { workspace = true }
|
||||||
cache = { path = "../../crates/cache" }
|
cache = { path = "../../crates/cache" }
|
||||||
redis = { workspace = true }
|
redis = { workspace = true }
|
||||||
|
reqwest = { workspace = true }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::AppState;
|
use crate::AppState;
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, Query, State},
|
extract::{Query, State},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::IntoResponse,
|
response::IntoResponse,
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
|
|
@ -88,15 +88,14 @@ async fn get_ai_credits(
|
||||||
(StatusCode::BAD_REQUEST, "Invalid company ID".to_string())
|
(StatusCode::BAD_REQUEST, "Invalid company ID".to_string())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let credits = sqlx::query_as!(
|
let credits = sqlx::query_as::<_, CompanyAICredits>(
|
||||||
CompanyAICredits,
|
|
||||||
r#"
|
r#"
|
||||||
SELECT company_id, credits_balance, updated_at
|
SELECT company_id, credits_balance, updated_at
|
||||||
FROM company_ai_credits
|
FROM company_ai_credits
|
||||||
WHERE company_id = $1
|
WHERE company_id = $1
|
||||||
"#,
|
"#,
|
||||||
company_id
|
|
||||||
)
|
)
|
||||||
|
.bind(company_id)
|
||||||
.fetch_optional(&state.pool)
|
.fetch_optional(&state.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
|
|
@ -136,15 +135,15 @@ async fn generate_ai(
|
||||||
);
|
);
|
||||||
|
|
||||||
// Check credits
|
// Check credits
|
||||||
let credits = sqlx::query_scalar!(
|
let credits = sqlx::query_scalar::<_, i32>(
|
||||||
r#"
|
r#"
|
||||||
SELECT credits_balance
|
SELECT credits_balance
|
||||||
FROM company_ai_credits
|
FROM company_ai_credits
|
||||||
WHERE company_id = $1
|
WHERE company_id = $1
|
||||||
FOR UPDATE
|
FOR UPDATE
|
||||||
"#,
|
"#,
|
||||||
company_id
|
|
||||||
)
|
)
|
||||||
|
.bind(company_id)
|
||||||
.fetch_optional(&state.pool)
|
.fetch_optional(&state.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
|
|
@ -159,7 +158,7 @@ async fn generate_ai(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deduct credit
|
// Deduct credit
|
||||||
sqlx::query!(
|
sqlx::query(
|
||||||
r#"
|
r#"
|
||||||
UPDATE company_ai_credits
|
UPDATE company_ai_credits
|
||||||
SET credits_balance = credits_balance - 1,
|
SET credits_balance = credits_balance - 1,
|
||||||
|
|
@ -167,8 +166,8 @@ async fn generate_ai(
|
||||||
WHERE company_id = $1
|
WHERE company_id = $1
|
||||||
RETURNING credits_balance
|
RETURNING credits_balance
|
||||||
"#,
|
"#,
|
||||||
company_id
|
|
||||||
)
|
)
|
||||||
|
.bind(company_id)
|
||||||
.fetch_one(&state.pool)
|
.fetch_one(&state.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
|
|
@ -181,20 +180,20 @@ async fn generate_ai(
|
||||||
let prompt_preview = request.prompt.chars().take(100).collect::<String>();
|
let prompt_preview = request.prompt.chars().take(100).collect::<String>();
|
||||||
let result_preview = "AI generated response".chars().take(100).collect::<String>();
|
let result_preview = "AI generated response".chars().take(100).collect::<String>();
|
||||||
|
|
||||||
sqlx::query!(
|
sqlx::query(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO ai_usage_log (id, company_id, request_type, credits_used, prompt_preview, result_preview, model_used, status, created_at)
|
INSERT INTO ai_usage_log (id, company_id, request_type, credits_used, prompt_preview, result_preview, model_used, status, created_at)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW())
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW())
|
||||||
"#,
|
"#,
|
||||||
request_id,
|
|
||||||
company_id,
|
|
||||||
request.request_type,
|
|
||||||
1_i32,
|
|
||||||
prompt_preview,
|
|
||||||
result_preview,
|
|
||||||
"gemma3:270m",
|
|
||||||
"success"
|
|
||||||
)
|
)
|
||||||
|
.bind(request_id)
|
||||||
|
.bind(company_id)
|
||||||
|
.bind(&request.request_type)
|
||||||
|
.bind(1_i32)
|
||||||
|
.bind(prompt_preview)
|
||||||
|
.bind(result_preview)
|
||||||
|
.bind("gemma3:270m")
|
||||||
|
.bind("success")
|
||||||
.execute(&state.pool)
|
.execute(&state.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
|
|
@ -242,12 +241,12 @@ async fn get_usage_history(
|
||||||
let offset = (page - 1) * per_page;
|
let offset = (page - 1) * per_page;
|
||||||
|
|
||||||
// Get total count
|
// Get total count
|
||||||
let total = sqlx::query_scalar!(
|
let total = sqlx::query_scalar::<_, i64>(
|
||||||
r#"
|
r#"
|
||||||
SELECT COUNT(*) FROM ai_usage_log WHERE company_id = $1
|
SELECT COUNT(*) FROM ai_usage_log WHERE company_id = $1
|
||||||
"#,
|
"#,
|
||||||
company_id
|
|
||||||
)
|
)
|
||||||
|
.bind(company_id)
|
||||||
.fetch_one(&state.pool)
|
.fetch_one(&state.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
|
|
@ -256,8 +255,7 @@ async fn get_usage_history(
|
||||||
}).unwrap_or(0);
|
}).unwrap_or(0);
|
||||||
|
|
||||||
// Get entries
|
// Get entries
|
||||||
let entries = sqlx::query_as!(
|
let entries = sqlx::query_as::<_, UsageEntry>(
|
||||||
UsageEntry,
|
|
||||||
r#"
|
r#"
|
||||||
SELECT id, request_type, credits_used, prompt_preview, result_preview,
|
SELECT id, request_type, credits_used, prompt_preview, result_preview,
|
||||||
model_used, status, error_message, created_at
|
model_used, status, error_message, created_at
|
||||||
|
|
@ -266,10 +264,10 @@ async fn get_usage_history(
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
LIMIT $2 OFFSET $3
|
LIMIT $2 OFFSET $3
|
||||||
"#,
|
"#,
|
||||||
company_id,
|
|
||||||
per_page,
|
|
||||||
offset
|
|
||||||
)
|
)
|
||||||
|
.bind(company_id)
|
||||||
|
.bind(per_page)
|
||||||
|
.bind(offset)
|
||||||
.fetch_all(&state.pool)
|
.fetch_all(&state.pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue