use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct EmvQrConfigId(pub Uuid);
impl EmvQrConfigId {
pub fn new(id: Uuid) -> Self { Self(id) }
pub fn generate() -> Self { Self(Uuid::new_v4()) }
pub fn into_inner(self) -> Uuid { self.0 }
}
impl std::fmt::Display for EmvQrConfigId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for EmvQrConfigId {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Uuid::parse_str(s)?))
}
}
impl From<Uuid> for EmvQrConfigId {
fn from(id: Uuid) -> Self { Self(id) }
}
impl From<EmvQrConfigId> for Uuid {
fn from(id: EmvQrConfigId) -> Self { id.0 }
}
impl AsRef<Uuid> for EmvQrConfigId {
fn as_ref(&self) -> &Uuid { &self.0 }
}
impl std::ops::Deref for EmvQrConfigId {
type Target = Uuid;
fn deref(&self) -> &Self::Target { &self.0 }
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct EmvQrConfig {
pub id: Uuid,
pub merchant_name: String,
pub merchant_city: String,
pub country_code: String,
pub mcc: String,
pub gui: String,
pub merchant_identifier: String,
pub currency: String,
pub initiation_method: String,
pub bank_account_id: Option<Uuid>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub metadata: serde_json::Value,
}
impl EmvQrConfig {
pub fn builder() -> EmvQrConfigBuilder {
<EmvQrConfigBuilder as Default>::default()
}
pub fn new(merchant_name: String, merchant_city: String, country_code: String, mcc: String, gui: String, merchant_identifier: String, currency: String, initiation_method: String, metadata: serde_json::Value) -> Self {
Self {
id: Uuid::new_v4(),
merchant_name,
merchant_city,
country_code,
mcc,
gui,
merchant_identifier,
currency,
initiation_method,
bank_account_id: None,
created_at: Utc::now(),
updated_at: Utc::now(),
metadata,
}
}
pub fn id(&self) -> &Uuid {
&self.id
}
pub fn typed_id(&self) -> EmvQrConfigId {
EmvQrConfigId(self.id)
}
pub fn created_at(&self) -> &DateTime<Utc> {
&self.created_at
}
pub fn updated_at(&self) -> &DateTime<Utc> {
&self.updated_at
}
pub fn with_bank_account_id(mut self, value: Uuid) -> Self {
self.bank_account_id = Some(value);
self
}
pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
for (key, value) in fields {
match key.as_str() {
"merchant_name" => {
if let Ok(v) = serde_json::from_value(value) { self.merchant_name = v; }
}
"merchant_city" => {
if let Ok(v) = serde_json::from_value(value) { self.merchant_city = v; }
}
"country_code" => {
if let Ok(v) = serde_json::from_value(value) { self.country_code = v; }
}
"mcc" => {
if let Ok(v) = serde_json::from_value(value) { self.mcc = v; }
}
"gui" => {
if let Ok(v) = serde_json::from_value(value) { self.gui = v; }
}
"merchant_identifier" => {
if let Ok(v) = serde_json::from_value(value) { self.merchant_identifier = v; }
}
"currency" => {
if let Ok(v) = serde_json::from_value(value) { self.currency = v; }
}
"initiation_method" => {
if let Ok(v) = serde_json::from_value(value) { self.initiation_method = v; }
}
"bank_account_id" => {
if let Ok(v) = serde_json::from_value(value) { self.bank_account_id = v; }
}
"metadata" => {
if let Ok(v) = serde_json::from_value(value) { self.metadata = v; }
}
_ => {} }
}
}
}
impl super::Entity for EmvQrConfig {
type Id = Uuid;
fn entity_id(&self) -> &Self::Id {
&self.id
}
fn entity_type() -> &'static str {
"EmvQrConfig"
}
}
impl backbone_core::PersistentEntity for EmvQrConfig {
fn write_protected_fields() -> &'static [&'static str] {
&["created_at"]
}
fn entity_id(&self) -> String {
self.id.to_string()
}
fn set_entity_id(&mut self, id: String) {
if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
self.id = uuid;
}
}
fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
Some(self.created_at)
}
fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.created_at = ts;
}
fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
Some(self.updated_at)
}
fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
self.updated_at = ts;
}
fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
None
}
fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
let _ = ts;
}
}
impl backbone_orm::EntityRepoMeta for EmvQrConfig {
fn column_types() -> std::collections::HashMap<String, String> {
let mut m = std::collections::HashMap::new();
m.insert("id".to_string(), "uuid".to_string());
m.insert("bank_account_id".to_string(), "uuid".to_string());
m.insert("created_at".to_string(), "timestamptz".to_string());
m.insert("updated_at".to_string(), "timestamptz".to_string());
m
}
fn search_fields() -> &'static [&'static str] {
&["merchant_name", "merchant_city", "country_code", "mcc", "gui", "merchant_identifier", "currency", "initiation_method"]
}
}
#[derive(Debug, Clone, Default)]
pub struct EmvQrConfigBuilder {
merchant_name: Option<String>,
merchant_city: Option<String>,
country_code: Option<String>,
mcc: Option<String>,
gui: Option<String>,
merchant_identifier: Option<String>,
currency: Option<String>,
initiation_method: Option<String>,
bank_account_id: Option<Uuid>,
metadata: Option<serde_json::Value>,
}
impl EmvQrConfigBuilder {
pub fn merchant_name(mut self, value: String) -> Self {
self.merchant_name = Some(value);
self
}
pub fn merchant_city(mut self, value: String) -> Self {
self.merchant_city = Some(value);
self
}
pub fn country_code(mut self, value: String) -> Self {
self.country_code = Some(value);
self
}
pub fn mcc(mut self, value: String) -> Self {
self.mcc = Some(value);
self
}
pub fn gui(mut self, value: String) -> Self {
self.gui = Some(value);
self
}
pub fn merchant_identifier(mut self, value: String) -> Self {
self.merchant_identifier = Some(value);
self
}
pub fn currency(mut self, value: String) -> Self {
self.currency = Some(value);
self
}
pub fn initiation_method(mut self, value: String) -> Self {
self.initiation_method = Some(value);
self
}
pub fn bank_account_id(mut self, value: Uuid) -> Self {
self.bank_account_id = Some(value);
self
}
pub fn metadata(mut self, value: serde_json::Value) -> Self {
self.metadata = Some(value);
self
}
pub fn build(self) -> Result<EmvQrConfig, String> {
let merchant_name = self.merchant_name.ok_or_else(|| "merchant_name is required".to_string())?;
let merchant_city = self.merchant_city.ok_or_else(|| "merchant_city is required".to_string())?;
let mcc = self.mcc.ok_or_else(|| "mcc is required".to_string())?;
let merchant_identifier = self.merchant_identifier.ok_or_else(|| "merchant_identifier is required".to_string())?;
Ok(EmvQrConfig {
id: Uuid::new_v4(),
merchant_name,
merchant_city,
country_code: self.country_code.unwrap_or("ID".to_string()),
mcc,
gui: self.gui.unwrap_or("ID.CO.QRIS.WWW".to_string()),
merchant_identifier,
currency: self.currency.unwrap_or("IDR".to_string()),
initiation_method: self.initiation_method.unwrap_or("11".to_string()),
bank_account_id: self.bank_account_id,
created_at: Utc::now(),
updated_at: Utc::now(),
metadata: self.metadata.unwrap_or(serde_json::json!({})),
})
}
}