Add service order id

This commit is contained in:
eraden 2022-05-04 21:34:26 +02:00
parent b4dc801820
commit 252f5373ff
18 changed files with 58 additions and 7 deletions

View File

@ -31,7 +31,7 @@ pub(crate) async fn all_account_orders(
) -> Result<Vec<AccountOrder>> {
sqlx::query_as(
r#"
SELECT id, buyer_id, status, order_ext_id
SELECT id, buyer_id, status, order_ext_id, service_order_id
FROM account_orders
ORDER BY id DESC
"#,
@ -74,7 +74,7 @@ pub(crate) async fn create_account_order(
r#"
INSERT INTO account_orders (buyer_id, status)
VALUES ($1, $2, $3)
RETURNING id, buyer_id, status, order_ext_id
RETURNING id, buyer_id, status, order_ext_id, service_order_id
"#,
)
.bind(msg.buyer_id)
@ -148,7 +148,7 @@ pub(crate) async fn update_account_order(
UPDATE account_orders
SET buyer_id = $2 AND status = $3 AND order_id = $4
WHERE id = $1
RETURNING id, buyer_id, status, order_ext_id
RETURNING id, buyer_id, status, order_ext_id, service_order_id
"#,
)
.bind(msg.id)
@ -185,7 +185,7 @@ pub(crate) async fn update_account_order_by_ext(
UPDATE account_orders
SET status = $2
WHERE order_ext_id = $1
RETURNING id, buyer_id, status, order_ext_id
RETURNING id, buyer_id, status, order_ext_id, service_order_id
"#,
)
.bind(msg.order_ext_id)
@ -209,7 +209,7 @@ db_async_handler!(FindAccountOrder, find_account_order, AccountOrder);
pub(crate) async fn find_account_order(msg: FindAccountOrder, db: PgPool) -> Result<AccountOrder> {
sqlx::query_as(
r#"
SELECT id, buyer_id, status, order_ext_id
SELECT id, buyer_id, status, order_ext_id, service_order_id
FROM account_orders
WHERE id = $1
"#,
@ -222,3 +222,34 @@ WHERE id = $1
super::Error::AccountOrder(Error::NotExists)
})
}
#[derive(actix::Message)]
#[rtype(result = "Result<AccountOrder>")]
pub struct SetOrderServiceId {
pub id: AccountOrderId,
pub service_order_id: String,
}
db_async_handler!(SetOrderServiceId, set_order_service_id, AccountOrder);
pub(crate) async fn set_order_service_id(
msg: SetOrderServiceId,
db: PgPool,
) -> Result<AccountOrder> {
sqlx::query_as(
r#"
UPDATE account_orders
SET service_order_id = $2
WHERE id = $1
RETURNING id, buyer_id, status, order_ext_id, service_order_id
"#,
)
.bind(msg.id)
.bind(msg.service_order_id)
.fetch_one(&db)
.await
.map_err(|e| {
log::error!("{e:?}");
super::Error::AccountOrder(Error::NotExists)
})
}

View File

@ -236,7 +236,7 @@ pub(crate) async fn request_payment(
let pay_u::res::CreateOrder {
status: _,
redirect_uri,
order_id: _,
order_id,
ext_order_id: _,
} = {
client
@ -265,6 +265,15 @@ pub(crate) async fn request_payment(
.await?
};
query_db!(
db,
database::SetOrderServiceId {
service_order_id: order_id.0,
id: db_order.id,
},
Error::CreateOrder
);
let order_items = query_db!(
db,
database::OrderItems {

View File

@ -100,7 +100,7 @@ async fn migrate(opts: MigrateOpts) -> Result<()> {
let config = config::default_load(&opts);
let db = database::Database::build(config).await?;
let res: std::result::Result<(), MigrateError> =
sqlx::migrate!("../db/migrate").run(db.pool()).await;
sqlx::migrate!("../migrations").run(db.pool()).await;
match res {
Ok(()) => Ok(()),
Err(e) => {

View File

@ -535,6 +535,7 @@ pub struct AccountOrder {
pub status: OrderStatus,
pub order_id: Option<OrderId>,
pub order_ext_id: uuid::Uuid,
pub service_order_id: Option<String>,
}
#[derive(sqlx::FromRow, Serialize, Deserialize)]
@ -553,6 +554,7 @@ impl From<AccountOrder> for PublicAccountOrder {
status,
order_id,
order_ext_id: _,
service_order_id: _,
}: AccountOrder,
) -> Self {
Self {

View File

@ -19,6 +19,7 @@ impl From<(Vec<model::AccountOrder>, Vec<model::OrderItem>)> for AccountOrders {
status,
order_id,
order_ext_id: _,
service_order_id: _,
}| {
AccountOrder {
id,
@ -43,6 +44,7 @@ impl From<(model::AccountOrder, Vec<model::OrderItem>)> for AccountOrder {
status,
order_id,
order_ext_id: _,
service_order_id: _,
},
mut items,
): (model::AccountOrder, Vec<model::OrderItem>),

5
build.rs Normal file
View File

@ -0,0 +1,5 @@
// generated by `sqlx migrate build-script`
fn main() {
// trigger recompilation when a new migration is added
println!("cargo:rerun-if-changed=migrations");
}

View File

@ -0,0 +1,2 @@
alter table account_orders
ADD COLUMN service_order_id TEXT UNIQUE;