use actix_web::{HttpRequest, HttpResponse, put, web::Json};
use serde_json::{Value, json};
use tracing::info;
use uuid::Uuid;
use crate::api::headers::x_athena_client::x_athena_client;
use crate::api::headers::x_company_id::get_x_company_id;
use crate::api::headers::x_organization_id::get_x_organization_id;
use crate::api::headers::x_user_id::get_x_user_id;
use crate::utils::request_logging::log_request;
#[derive(Debug, serde::Deserialize)]
struct UpdateRequest {
table_name: String,
insert_body: Value,
#[serde(default)]
#[allow(unused)]
update_body: Option<Value>,
}
#[put("/gateway/insert")]
async fn insert_data(req: HttpRequest, body: Json<UpdateRequest>) -> HttpResponse {
log_request(req.clone());
#[allow(unused)]
let client_name: String = x_athena_client(&req.clone());
let _user_id: String = match get_x_user_id(&req) {
Some(id) => id,
None => {
return HttpResponse::BadRequest()
.json(json!({"error": "X-User-Id header not found in the request"}));
}
};
let _company_id: String = match get_x_company_id(&req) {
Some(id) => id,
None => {
return HttpResponse::BadRequest()
.json(json!({"error": "X-Company-Id header not found in the request"}));
}
};
let _organization_id: String = match get_x_organization_id(&req) {
Some(id) => id,
None => {
return HttpResponse::BadRequest()
.json(json!({"error": "X-Organization-Id header not found in the request"}));
}
};
let table_name: String = body.table_name.clone();
let mut insert_body: Value = body.insert_body.clone();
let id_key: &str = crate::api::gateway::update::table_id_map::get_resource_id_key(&table_name);
let new_uuid: String = Uuid::new_v4().to_string();
if let Some(obj) = insert_body.as_object_mut() {
obj.insert(id_key.to_string(), Value::String(new_uuid.clone()));
}
HttpResponse::Ok().json(json!({
"status": "success",
"success": true,
"message": "Data inserted successfully",
"data": insert_body,
"resource_id": new_uuid,
"table_name": table_name,
}))
}
pub mod table_id_map;