2022-05-19 16:13:27 +02:00
|
|
|
use crate::{db_async_handler, Result};
|
|
|
|
|
2022-06-04 16:55:29 +02:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, serde::Serialize, thiserror::Error)]
|
2022-05-19 16:13:27 +02:00
|
|
|
pub enum Error {
|
|
|
|
#[error("Can't load account addresses")]
|
|
|
|
AccountAddresses,
|
|
|
|
#[error("Failed to save account address")]
|
|
|
|
CreateAccountAddress,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(actix::Message)]
|
2022-05-23 14:11:56 +02:00
|
|
|
#[rtype(result = "Result<Vec<model::AccountAddress>>")]
|
2022-05-19 16:13:27 +02:00
|
|
|
pub struct AccountAddresses {
|
|
|
|
pub account_id: model::AccountId,
|
|
|
|
}
|
|
|
|
|
|
|
|
db_async_handler!(
|
|
|
|
AccountAddresses,
|
|
|
|
account_addresses,
|
2022-05-23 14:11:56 +02:00
|
|
|
Vec<model::AccountAddress>,
|
2022-05-19 16:13:27 +02:00
|
|
|
inner_account_addresses
|
|
|
|
);
|
|
|
|
|
|
|
|
pub(crate) async fn account_addresses(
|
|
|
|
msg: AccountAddresses,
|
|
|
|
pool: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
2022-05-23 14:11:56 +02:00
|
|
|
) -> Result<Vec<model::AccountAddress>> {
|
2022-05-19 16:13:27 +02:00
|
|
|
sqlx::query_as(
|
|
|
|
r#"
|
2022-05-29 13:36:25 +02:00
|
|
|
SELECT id, name, email, phone, street, city, country, zip, account_id, is_default
|
2022-05-19 16:13:27 +02:00
|
|
|
FROM account_addresses
|
|
|
|
WHERE account_id = $1
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(msg.account_id)
|
|
|
|
.fetch_all(pool)
|
|
|
|
.await
|
|
|
|
.map_err(|_| Error::AccountAddresses.into())
|
|
|
|
}
|
2022-05-28 14:03:14 +02:00
|
|
|
|
|
|
|
#[derive(actix::Message)]
|
|
|
|
#[rtype(result = "Result<model::AccountAddress>")]
|
|
|
|
pub struct FindAccountAddress {
|
|
|
|
pub account_id: model::AccountId,
|
|
|
|
pub address_id: model::AddressId,
|
|
|
|
}
|
|
|
|
|
|
|
|
db_async_handler!(
|
|
|
|
FindAccountAddress,
|
|
|
|
find_account_address,
|
|
|
|
model::AccountAddress,
|
|
|
|
inner_find_account_address
|
|
|
|
);
|
|
|
|
|
|
|
|
pub(crate) async fn find_account_address(
|
|
|
|
msg: FindAccountAddress,
|
|
|
|
pool: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
|
|
|
) -> Result<model::AccountAddress> {
|
|
|
|
sqlx::query_as(
|
|
|
|
r#"
|
2022-05-29 13:36:25 +02:00
|
|
|
SELECT id, name, email, phone, street, city, country, zip, account_id, is_default
|
2022-05-28 14:03:14 +02:00
|
|
|
FROM account_addresses
|
|
|
|
WHERE account_id = $1 AND id = $2
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(msg.account_id)
|
|
|
|
.bind(msg.address_id)
|
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
|
|
|
.map_err(|_| Error::AccountAddresses.into())
|
|
|
|
}
|
|
|
|
|
2022-05-19 16:13:27 +02:00
|
|
|
#[derive(actix::Message)]
|
2022-05-23 14:11:56 +02:00
|
|
|
#[rtype(result = "Result<model::AccountAddress>")]
|
|
|
|
pub struct DefaultAccountAddress {
|
|
|
|
pub account_id: model::AccountId,
|
|
|
|
}
|
|
|
|
|
|
|
|
db_async_handler!(
|
|
|
|
DefaultAccountAddress,
|
|
|
|
default_account_address,
|
|
|
|
model::AccountAddress,
|
|
|
|
inner_default_account_address
|
|
|
|
);
|
|
|
|
|
|
|
|
pub(crate) async fn default_account_address(
|
|
|
|
msg: DefaultAccountAddress,
|
|
|
|
pool: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
|
|
|
) -> Result<model::AccountAddress> {
|
|
|
|
sqlx::query_as(
|
|
|
|
r#"
|
2022-05-29 13:36:25 +02:00
|
|
|
SELECT id, name, email, phone, street, city, country, zip, account_id, is_default
|
2022-05-23 14:11:56 +02:00
|
|
|
FROM account_addresses
|
|
|
|
WHERE account_id = $1 AND is_default
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(msg.account_id)
|
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
|
|
|
.map_err(|_| Error::AccountAddresses.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(actix::Message)]
|
|
|
|
#[rtype(result = "Result<model::AccountAddress>")]
|
2022-05-19 16:13:27 +02:00
|
|
|
pub struct CreateAccountAddress {
|
|
|
|
pub name: model::Name,
|
|
|
|
pub email: model::Email,
|
2022-05-29 13:36:25 +02:00
|
|
|
pub phone: model::Phone,
|
2022-05-19 16:13:27 +02:00
|
|
|
pub street: model::Street,
|
|
|
|
pub city: model::City,
|
|
|
|
pub country: model::Country,
|
|
|
|
pub zip: model::Zip,
|
2022-05-23 14:11:56 +02:00
|
|
|
pub account_id: Option<model::AccountId>,
|
|
|
|
pub is_default: bool,
|
2022-05-19 16:13:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
db_async_handler!(
|
|
|
|
CreateAccountAddress,
|
|
|
|
create_address,
|
2022-05-23 14:11:56 +02:00
|
|
|
model::AccountAddress,
|
2022-05-19 16:13:27 +02:00
|
|
|
inner_create_address
|
|
|
|
);
|
|
|
|
|
|
|
|
pub(crate) async fn create_address(
|
|
|
|
msg: CreateAccountAddress,
|
|
|
|
pool: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
2022-05-23 14:11:56 +02:00
|
|
|
) -> Result<model::AccountAddress> {
|
2022-05-29 13:36:25 +02:00
|
|
|
if msg.is_default && msg.account_id.is_some() {
|
2022-05-23 14:11:56 +02:00
|
|
|
if let Err(e) = sqlx::query(
|
|
|
|
r#"
|
|
|
|
UPDATE account_addresses
|
|
|
|
SET is_default = FALSE
|
|
|
|
WHERE account_id = $1
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(msg.account_id)
|
|
|
|
.fetch_all(&mut *pool)
|
|
|
|
.await
|
|
|
|
{
|
2022-06-07 11:36:01 +02:00
|
|
|
tracing::error!("{e}");
|
2022-06-06 15:09:13 +02:00
|
|
|
dbg!(e);
|
2022-05-23 14:11:56 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-29 13:36:25 +02:00
|
|
|
|
2022-05-19 16:13:27 +02:00
|
|
|
sqlx::query_as(
|
|
|
|
r#"
|
2022-06-05 17:44:47 +02:00
|
|
|
INSERT INTO account_addresses ( name, email, phone, street, city, country, zip, account_id, is_default)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
2022-05-29 13:36:25 +02:00
|
|
|
RETURNING id, name, email, phone, street, city, country, zip, account_id, is_default
|
2022-05-19 16:13:27 +02:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(msg.name)
|
|
|
|
.bind(msg.email)
|
2022-05-29 13:36:25 +02:00
|
|
|
.bind(msg.phone)
|
2022-05-19 16:13:27 +02:00
|
|
|
.bind(msg.street)
|
|
|
|
.bind(msg.city)
|
|
|
|
.bind(msg.country)
|
|
|
|
.bind(msg.zip)
|
|
|
|
.bind(msg.account_id)
|
2022-05-29 13:36:25 +02:00
|
|
|
.bind(msg.is_default)
|
2022-05-19 16:13:27 +02:00
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
2022-06-05 17:44:47 +02:00
|
|
|
.map_err(|e| {
|
2022-06-07 11:36:01 +02:00
|
|
|
tracing::error!("{e}");
|
2022-06-06 15:09:13 +02:00
|
|
|
dbg!(e);
|
2022-06-05 17:44:47 +02:00
|
|
|
Error::CreateAccountAddress.into()
|
|
|
|
})
|
2022-05-19 16:13:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(actix::Message)]
|
2022-05-23 14:11:56 +02:00
|
|
|
#[rtype(result = "Result<model::AccountAddress>")]
|
2022-05-19 16:13:27 +02:00
|
|
|
pub struct UpdateAccountAddress {
|
|
|
|
pub id: model::AddressId,
|
|
|
|
pub name: model::Name,
|
|
|
|
pub email: model::Email,
|
2022-05-29 13:36:25 +02:00
|
|
|
pub phone: model::Phone,
|
2022-05-19 16:13:27 +02:00
|
|
|
pub street: model::Street,
|
|
|
|
pub city: model::City,
|
|
|
|
pub country: model::Country,
|
|
|
|
pub zip: model::Zip,
|
|
|
|
pub account_id: model::AccountId,
|
2022-05-23 14:11:56 +02:00
|
|
|
pub is_default: bool,
|
2022-05-19 16:13:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
db_async_handler!(
|
|
|
|
UpdateAccountAddress,
|
|
|
|
update_account_address,
|
2022-05-23 14:11:56 +02:00
|
|
|
model::AccountAddress,
|
2022-05-19 16:13:27 +02:00
|
|
|
inner_update_account_address
|
|
|
|
);
|
|
|
|
|
|
|
|
pub(crate) async fn update_account_address(
|
|
|
|
msg: UpdateAccountAddress,
|
|
|
|
pool: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
2022-05-23 14:11:56 +02:00
|
|
|
) -> Result<model::AccountAddress> {
|
2022-05-19 16:13:27 +02:00
|
|
|
sqlx::query_as(
|
|
|
|
r#"
|
|
|
|
UPDATE account_addresses
|
2022-05-29 13:36:25 +02:00
|
|
|
SET name = $2, email = $3, street = $4, city = $5, country = $6, zip = $7, account_id = $8, is_default = $9, phone = $10
|
2022-05-19 16:13:27 +02:00
|
|
|
WHERE id = $1
|
2022-05-29 13:36:25 +02:00
|
|
|
RETURNING id, name, email, phone, street, city, country, zip, account_id, is_default
|
2022-05-19 16:13:27 +02:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.bind(msg.id)
|
|
|
|
.bind(msg.name)
|
|
|
|
.bind(msg.email)
|
|
|
|
.bind(msg.street)
|
|
|
|
.bind(msg.city)
|
|
|
|
.bind(msg.country)
|
|
|
|
.bind(msg.zip)
|
|
|
|
.bind(msg.account_id)
|
2022-05-23 14:11:56 +02:00
|
|
|
.bind(msg.is_default)
|
2022-05-29 13:36:25 +02:00
|
|
|
.bind(msg.phone)
|
2022-05-19 16:13:27 +02:00
|
|
|
.fetch_one(pool)
|
|
|
|
.await
|
|
|
|
.map_err(|_| Error::CreateAccountAddress.into())
|
|
|
|
}
|
2022-05-29 13:36:25 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use config::*;
|
|
|
|
use fake::Fake;
|
|
|
|
use model::*;
|
|
|
|
|
|
|
|
use crate::*;
|
|
|
|
|
|
|
|
pub struct NoOpts;
|
|
|
|
|
|
|
|
impl UpdateConfig for NoOpts {}
|
|
|
|
|
2022-06-05 17:44:47 +02:00
|
|
|
async fn test_create_account(pool: &mut sqlx::Transaction<'_, sqlx::Postgres>) -> FullAccount {
|
2022-05-29 13:36:25 +02:00
|
|
|
let login: String = fake::faker::internet::en::Username().fake();
|
|
|
|
let email: String = fake::faker::internet::en::FreeEmail().fake();
|
|
|
|
let hash: String = fake::faker::internet::en::Password(10..20).fake();
|
|
|
|
|
2022-06-05 17:44:47 +02:00
|
|
|
crate::create_account(
|
|
|
|
CreateAccount {
|
|
|
|
email: Email::new(email),
|
|
|
|
login: Login::new(login),
|
|
|
|
pass_hash: PassHash::new(hash),
|
|
|
|
role: Role::Admin,
|
|
|
|
},
|
|
|
|
pool,
|
|
|
|
)
|
2022-05-29 13:36:25 +02:00
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix::test]
|
|
|
|
async fn full_check() {
|
2022-06-09 15:28:15 +02:00
|
|
|
testx::db_t_ref!(t);
|
2022-05-29 13:36:25 +02:00
|
|
|
|
|
|
|
// account
|
2022-06-05 17:44:47 +02:00
|
|
|
let account = test_create_account(&mut t).await;
|
2022-05-29 13:36:25 +02:00
|
|
|
|
|
|
|
// address
|
|
|
|
let mut address: AccountAddress = {
|
|
|
|
let name: String = fake::faker::name::en::Name().fake();
|
|
|
|
let email: String = fake::faker::internet::en::FreeEmail().fake();
|
|
|
|
let phone: String = fake::faker::phone_number::en::PhoneNumber().fake();
|
|
|
|
let street: String = fake::faker::address::en::StreetName().fake();
|
|
|
|
let city: String = fake::faker::address::en::CityName().fake();
|
|
|
|
let country: String = fake::faker::address::en::CountryName().fake();
|
|
|
|
let zip: String = fake::faker::address::en::ZipCode().fake();
|
|
|
|
let account_id = Some(account.id);
|
2022-06-05 17:44:47 +02:00
|
|
|
let is_default: bool = true;
|
|
|
|
|
|
|
|
let address = super::create_address(
|
|
|
|
CreateAccountAddress {
|
|
|
|
name: Name::new(name.clone()),
|
|
|
|
email: Email::new(email.clone()),
|
|
|
|
phone: Phone::new(phone.clone()),
|
|
|
|
street: Street::new(street.clone()),
|
|
|
|
city: City::new(city.clone()),
|
|
|
|
country: Country::new(country.clone()),
|
|
|
|
zip: Zip::new(zip.clone()),
|
2022-05-29 13:36:25 +02:00
|
|
|
account_id: account_id.clone(),
|
2022-06-05 17:44:47 +02:00
|
|
|
is_default,
|
|
|
|
},
|
|
|
|
&mut t,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-05-29 13:36:25 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
address,
|
2022-06-05 17:44:47 +02:00
|
|
|
AccountAddress {
|
2022-06-04 16:05:18 +02:00
|
|
|
id: address.id,
|
2022-06-05 17:44:47 +02:00
|
|
|
name: Name::new(name.clone()),
|
|
|
|
email: Email::new(email.clone()),
|
|
|
|
phone: Phone::new(phone.clone()),
|
|
|
|
street: Street::new(street.clone()),
|
|
|
|
city: City::new(city.clone()),
|
|
|
|
country: Country::new(country.clone()),
|
|
|
|
zip: Zip::new(zip.clone()),
|
2022-05-29 13:36:25 +02:00
|
|
|
account_id: account.id,
|
|
|
|
is_default,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
address
|
|
|
|
};
|
|
|
|
|
2022-06-05 17:44:47 +02:00
|
|
|
let found = super::find_account_address(
|
|
|
|
FindAccountAddress {
|
2022-05-29 13:36:25 +02:00
|
|
|
account_id: account.id,
|
|
|
|
address_id: address.id,
|
2022-06-05 17:44:47 +02:00
|
|
|
},
|
|
|
|
&mut t,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-05-29 13:36:25 +02:00
|
|
|
|
|
|
|
assert_eq!(found, address);
|
|
|
|
|
2022-06-05 17:44:47 +02:00
|
|
|
let changed = super::update_account_address(
|
|
|
|
UpdateAccountAddress {
|
2022-05-29 13:36:25 +02:00
|
|
|
id: address.id,
|
|
|
|
name: address.name.clone(),
|
|
|
|
email: address.email.clone(),
|
|
|
|
phone: address.phone.clone(),
|
|
|
|
street: address.street.clone(),
|
|
|
|
city: address.city.clone(),
|
|
|
|
country: address.country.clone(),
|
|
|
|
zip: address.zip.clone(),
|
|
|
|
account_id: address.account_id.clone(),
|
|
|
|
is_default: true,
|
2022-06-05 17:44:47 +02:00
|
|
|
},
|
|
|
|
&mut t,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-05-29 13:36:25 +02:00
|
|
|
|
|
|
|
address.is_default = true;
|
|
|
|
|
|
|
|
assert_eq!(changed, address);
|
|
|
|
|
2022-06-05 17:44:47 +02:00
|
|
|
let default_address = super::default_account_address(
|
|
|
|
DefaultAccountAddress {
|
2022-05-29 13:36:25 +02:00
|
|
|
account_id: account.id,
|
2022-06-05 17:44:47 +02:00
|
|
|
},
|
|
|
|
&mut t,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-05-29 13:36:25 +02:00
|
|
|
|
2022-06-05 21:03:22 +02:00
|
|
|
testx::db_rollback!(t);
|
2022-05-29 13:36:25 +02:00
|
|
|
assert_eq!(default_address, address);
|
|
|
|
}
|
|
|
|
}
|