fix: add deleted flag to Product aggregate and ignore deleted Products while checking Product.name uniqueness #40

Merged
realaravinth merged 2 commits from deleted-flag into master 2024-07-15 23:54:00 +05:30
9 changed files with 55 additions and 20 deletions

View file

@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "SELECT \n name,\n description,\n image,\n product_id,\n category_id,\n price_major,\n price_minor,\n price_currency,\n sku_able,\n quantity_unit,\n quantity_number\n FROM\n cqrs_inventory_product_query\n WHERE\n product_id = $1;",
"query": "SELECT \n name,\n description,\n image,\n product_id,\n category_id,\n price_major,\n price_minor,\n price_currency,\n sku_able,\n quantity_unit,\n quantity_number,\n deleted\n FROM\n cqrs_inventory_product_query\n WHERE\n product_id = $1;",
"describe": {
"columns": [
{
@ -57,6 +57,11 @@
"ordinal": 10,
"name": "quantity_number",
"type_info": "Int4"
},
{
"ordinal": 11,
"name": "deleted",
"type_info": "Bool"
}
],
"parameters": {
@ -75,8 +80,9 @@
false,
false,
false,
false,
false
]
},
"hash": "398a5dc0ffc9f892255ebfd67e1583f5cf092d871a9a710ca4f59e14d08aaf8e"
"hash": "3f83167782a2de1be7d87d35f63a3e530373595ce83cd36612ee48d3c36c81f3"
}

View file

@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "INSERT INTO cqrs_inventory_product_query (\n version,\n name,\n description,\n image,\n product_id,\n category_id,\n price_major,\n price_minor,\n price_currency,\n sku_able,\n quantity_unit,\n quantity_number\n ) VALUES (\n $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12\n );",
"query": "INSERT INTO cqrs_inventory_product_query (\n version,\n name,\n description,\n image,\n product_id,\n category_id,\n price_major,\n price_minor,\n price_currency,\n sku_able,\n quantity_unit,\n quantity_number,\n deleted\n ) VALUES (\n $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13\n );",
"describe": {
"columns": [],
"parameters": {
@ -16,10 +16,11 @@
"Text",
"Bool",
"Text",
"Int4"
"Int4",
"Bool"
]
},
"nullable": []
},
"hash": "727e8a82469f1887a7136900d45cfb4e3b6c0ddeb0c3610ec5a9f80e0b1c227e"
"hash": "9390910c71001ef77313e594dea0e4f0682f740778e483a2db91d6c73ac6bc6d"
}

View file

@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE\n cqrs_inventory_product_query\n SET\n version = $1,\n name = $2,\n description = $3,\n image = $4,\n product_id = $5,\n category_id = $6,\n price_major = $7,\n price_minor = $8,\n price_currency = $9,\n sku_able = $10,\n quantity_unit = $11,\n quantity_number = $12;",
"query": "UPDATE\n cqrs_inventory_product_query\n SET\n version = $1,\n name = $2,\n description = $3,\n image = $4,\n product_id = $5,\n category_id = $6,\n price_major = $7,\n price_minor = $8,\n price_currency = $9,\n sku_able = $10,\n quantity_unit = $11,\n quantity_number = $12,\n deleted = $13;",
"describe": {
"columns": [],
"parameters": {
@ -16,10 +16,11 @@
"Text",
"Bool",
"Text",
"Int4"
"Int4",
"Bool"
]
},
"nullable": []
},
"hash": "60fb3bb1d459b64f900cf02d7d2444b2183bd4d1a4af3a93336091f3c2bdcace"
"hash": "f55fe530202bb369fdea3baaf91f86bbc866218e4146b6d608255c1b929073b4"
}

View file

@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_inventory_product_query\n WHERE\n name = $1\n AND\n category_id = $2\n );",
"query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_inventory_product_query\n WHERE\n name = $1\n AND\n category_id = $2\n AND\n deleted = false\n );",
"describe": {
"columns": [
{
@ -19,5 +19,5 @@
null
]
},
"hash": "53a5e7e87387ffc14013067a24c80b9d14c9864875177af5320c926d68cfb4ae"
"hash": "ff4bf8b635cd7144238c2df8bb4b13c3e090dc7d6455464b656ff44e0c2c5ebe"
}

View file

@ -87,7 +87,6 @@ pub mod tests {
// state exists
assert!(db.category_name_exists_for_store(&category).await.unwrap());
// Set category.deleted = true; now db.category_name_exists_for_store must return false
sqlx::query!(
"UPDATE cqrs_inventory_category_query SET deleted = true WHERE category_id = $1;",
@ -98,7 +97,6 @@ pub mod tests {
.unwrap();
assert!(!db.category_name_exists_for_store(&category).await.unwrap());
settings.drop_db().await;
}
}

View file

@ -84,9 +84,10 @@ pub mod tests {
price_currency,
sku_able,
quantity_unit,
quantity_number
quantity_number,
deleted
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
);",
1,
p.name(),
@ -100,6 +101,7 @@ pub mod tests {
p.sku_able().clone(),
p.quantity().unit().to_string(),
p.quantity().number().clone() as i32,
p.deleted().clone()
)
.execute(&db.pool)
.await

View file

@ -19,6 +19,8 @@ impl ProductNameExistsForCategoryDBPort for InventoryDBPostgresAdapter {
name = $1
AND
category_id = $2
AND
deleted = false
);",
s.name(),
s.category_id(),
@ -74,6 +76,17 @@ mod tests {
// state exists
assert!(db.product_name_exists_for_category(&product).await.unwrap());
// Set product.deleted = true; now db.product_name_exists_for_category must return false
sqlx::query!(
"UPDATE cqrs_inventory_product_query SET deleted = true WHERE product_id = $1;",
product.product_id(),
)
.execute(&db.pool)
.await
.unwrap();
assert!(!db.product_name_exists_for_category(&product).await.unwrap());
settings.drop_db().await;
}
}

View file

@ -38,6 +38,8 @@ pub struct ProductView {
quantity_number: i32,
category_id: Uuid,
deleted: bool,
}
impl From<ProductView> for Product {
@ -64,6 +66,7 @@ impl From<ProductView> for Product {
.category_id(v.category_id)
.quantity(quantity)
.product_id(v.product_id)
.deleted(v.deleted)
.build()
.unwrap()
}
@ -90,6 +93,8 @@ impl View<Product> for ProductView {
self.quantity_number = val.quantity().number().clone() as i32;
self.quantity_unit = val.quantity().unit().to_string();
self.deleted = false;
}
_ => (),
}
@ -117,7 +122,8 @@ impl ViewRepository<ProductView, Product> for InventoryDBPostgresAdapter {
price_currency,
sku_able,
quantity_unit,
quantity_number
quantity_number,
deleted
FROM
cqrs_inventory_product_query
WHERE
@ -152,7 +158,8 @@ impl ViewRepository<ProductView, Product> for InventoryDBPostgresAdapter {
price_currency,
sku_able,
quantity_unit,
quantity_number
quantity_number,
deleted
FROM
cqrs_inventory_product_query
WHERE
@ -207,9 +214,10 @@ impl ViewRepository<ProductView, Product> for InventoryDBPostgresAdapter {
price_currency,
sku_able,
quantity_unit,
quantity_number
quantity_number,
deleted
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
);",
version,
view.name,
@ -223,6 +231,7 @@ impl ViewRepository<ProductView, Product> for InventoryDBPostgresAdapter {
view.sku_able,
view.quantity_unit,
view.quantity_number,
view.deleted
)
.execute(&self.pool)
.await
@ -245,7 +254,8 @@ impl ViewRepository<ProductView, Product> for InventoryDBPostgresAdapter {
price_currency = $9,
sku_able = $10,
quantity_unit = $11,
quantity_number = $12;",
quantity_number = $12,
deleted = $13;",
version,
view.name,
view.description,
@ -257,7 +267,8 @@ impl ViewRepository<ProductView, Product> for InventoryDBPostgresAdapter {
view.price_currency,
view.sku_able,
view.quantity_unit,
view.quantity_number
view.quantity_number,
view.deleted
)
.execute(&self.pool)
.await

View file

@ -127,6 +127,8 @@ pub struct Product {
category_id: Uuid,
sku_able: bool,
product_id: Uuid,
#[builder(default = "false")]
deleted: bool,
}
#[async_trait]
@ -169,6 +171,7 @@ impl Aggregate for Product {
.sku_able(e.sku_able().clone())
.product_id(e.product_id().clone())
.quantity(e.quantity().clone())
.deleted(false)
.build()
.unwrap();
}