From 252f5373ffb9f87a44ac14508b4db2b30cdcb6aa Mon Sep 17 00:00:00 2001 From: eraden Date: Wed, 4 May 2022 21:34:26 +0200 Subject: [PATCH] Add service order id --- api/src/actors/database/account_orders.rs | 41 ++++++++++++++++--- api/src/actors/payment_manager.rs | 11 ++++- api/src/main.rs | 2 +- api/src/model.rs | 2 + api/src/model/api.rs | 2 + build.rs | 5 +++ .../202204131841_init.sql | 0 .../202204160624_create_shopping_cart.sql | 0 .../202204161233_add_cart_state.sql | 0 .../202204172215_delivery_and_price.sql | 0 .../202204180708_add_payment_fields.sql | 0 .../202204181325_create_tokens.sql | 0 .../202204182135_add_uniq_add_time_format.sql | 0 .../202204191430_change_price.sql | 0 .../202204191555_add_account_state.sql | 0 .../202204271359_add_order_ext_id.sql | 0 .../202204300704_photos.sql | 0 ...04192613_add_service_order_id_to_order.sql | 2 + 18 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 build.rs rename {db/migrate => migrations}/202204131841_init.sql (100%) rename {db/migrate => migrations}/202204160624_create_shopping_cart.sql (100%) rename {db/migrate => migrations}/202204161233_add_cart_state.sql (100%) rename {db/migrate => migrations}/202204172215_delivery_and_price.sql (100%) rename {db/migrate => migrations}/202204180708_add_payment_fields.sql (100%) rename {db/migrate => migrations}/202204181325_create_tokens.sql (100%) rename {db/migrate => migrations}/202204182135_add_uniq_add_time_format.sql (100%) rename {db/migrate => migrations}/202204191430_change_price.sql (100%) rename {db/migrate => migrations}/202204191555_add_account_state.sql (100%) rename {db/migrate => migrations}/202204271359_add_order_ext_id.sql (100%) rename {db/migrate => migrations}/202204300704_photos.sql (100%) create mode 100644 migrations/20220504192613_add_service_order_id_to_order.sql diff --git a/api/src/actors/database/account_orders.rs b/api/src/actors/database/account_orders.rs index d7415c4..4ec2326 100644 --- a/api/src/actors/database/account_orders.rs +++ b/api/src/actors/database/account_orders.rs @@ -31,7 +31,7 @@ pub(crate) async fn all_account_orders( ) -> Result> { 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 { 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")] +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 { + 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) + }) +} diff --git a/api/src/actors/payment_manager.rs b/api/src/actors/payment_manager.rs index 67d9366..108d7b2 100644 --- a/api/src/actors/payment_manager.rs +++ b/api/src/actors/payment_manager.rs @@ -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 { diff --git a/api/src/main.rs b/api/src/main.rs index f7b1249..416121e 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -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) => { diff --git a/api/src/model.rs b/api/src/model.rs index 5cd9c5c..d820608 100644 --- a/api/src/model.rs +++ b/api/src/model.rs @@ -535,6 +535,7 @@ pub struct AccountOrder { pub status: OrderStatus, pub order_id: Option, pub order_ext_id: uuid::Uuid, + pub service_order_id: Option, } #[derive(sqlx::FromRow, Serialize, Deserialize)] @@ -553,6 +554,7 @@ impl From for PublicAccountOrder { status, order_id, order_ext_id: _, + service_order_id: _, }: AccountOrder, ) -> Self { Self { diff --git a/api/src/model/api.rs b/api/src/model/api.rs index a421c61..684ec77 100644 --- a/api/src/model/api.rs +++ b/api/src/model/api.rs @@ -19,6 +19,7 @@ impl From<(Vec, Vec)> for AccountOrders { status, order_id, order_ext_id: _, + service_order_id: _, }| { AccountOrder { id, @@ -43,6 +44,7 @@ impl From<(model::AccountOrder, Vec)> for AccountOrder { status, order_id, order_ext_id: _, + service_order_id: _, }, mut items, ): (model::AccountOrder, Vec), diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..7609593 --- /dev/null +++ b/build.rs @@ -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"); +} \ No newline at end of file diff --git a/db/migrate/202204131841_init.sql b/migrations/202204131841_init.sql similarity index 100% rename from db/migrate/202204131841_init.sql rename to migrations/202204131841_init.sql diff --git a/db/migrate/202204160624_create_shopping_cart.sql b/migrations/202204160624_create_shopping_cart.sql similarity index 100% rename from db/migrate/202204160624_create_shopping_cart.sql rename to migrations/202204160624_create_shopping_cart.sql diff --git a/db/migrate/202204161233_add_cart_state.sql b/migrations/202204161233_add_cart_state.sql similarity index 100% rename from db/migrate/202204161233_add_cart_state.sql rename to migrations/202204161233_add_cart_state.sql diff --git a/db/migrate/202204172215_delivery_and_price.sql b/migrations/202204172215_delivery_and_price.sql similarity index 100% rename from db/migrate/202204172215_delivery_and_price.sql rename to migrations/202204172215_delivery_and_price.sql diff --git a/db/migrate/202204180708_add_payment_fields.sql b/migrations/202204180708_add_payment_fields.sql similarity index 100% rename from db/migrate/202204180708_add_payment_fields.sql rename to migrations/202204180708_add_payment_fields.sql diff --git a/db/migrate/202204181325_create_tokens.sql b/migrations/202204181325_create_tokens.sql similarity index 100% rename from db/migrate/202204181325_create_tokens.sql rename to migrations/202204181325_create_tokens.sql diff --git a/db/migrate/202204182135_add_uniq_add_time_format.sql b/migrations/202204182135_add_uniq_add_time_format.sql similarity index 100% rename from db/migrate/202204182135_add_uniq_add_time_format.sql rename to migrations/202204182135_add_uniq_add_time_format.sql diff --git a/db/migrate/202204191430_change_price.sql b/migrations/202204191430_change_price.sql similarity index 100% rename from db/migrate/202204191430_change_price.sql rename to migrations/202204191430_change_price.sql diff --git a/db/migrate/202204191555_add_account_state.sql b/migrations/202204191555_add_account_state.sql similarity index 100% rename from db/migrate/202204191555_add_account_state.sql rename to migrations/202204191555_add_account_state.sql diff --git a/db/migrate/202204271359_add_order_ext_id.sql b/migrations/202204271359_add_order_ext_id.sql similarity index 100% rename from db/migrate/202204271359_add_order_ext_id.sql rename to migrations/202204271359_add_order_ext_id.sql diff --git a/db/migrate/202204300704_photos.sql b/migrations/202204300704_photos.sql similarity index 100% rename from db/migrate/202204300704_photos.sql rename to migrations/202204300704_photos.sql diff --git a/migrations/20220504192613_add_service_order_id_to_order.sql b/migrations/20220504192613_add_service_order_id_to_order.sql new file mode 100644 index 0000000..f212586 --- /dev/null +++ b/migrations/20220504192613_add_service_order_id_to_order.sql @@ -0,0 +1,2 @@ +alter table account_orders +ADD COLUMN service_order_id TEXT UNIQUE;