Compare commits
40 commits
convert-qu
...
master
Author | SHA1 | Date | |
---|---|---|---|
23a87480b7 | |||
88f4dd6825 | |||
eba2f7204c | |||
7c3676e84d | |||
5f466fce80 | |||
c5d5dcb323 | |||
df64f434be | |||
5a050fde0e | |||
d265412d06 | |||
272e8f68c6 | |||
4a51a3d629 | |||
503920fc5f | |||
ca7defe724 | |||
88446b94c4 | |||
73d829f3f8 | |||
d727d0b5b0 | |||
5245cf02e0 | |||
ac6da029a5 | |||
2853bc5a75 | |||
4580aea18b | |||
e14c42023a | |||
7d66d9d5b8 | |||
3746eb211f | |||
9bd2849695 | |||
2017ca5e97 | |||
28e3da0bbc | |||
d09581d9bc | |||
1660da90a7 | |||
dfc080f26a | |||
d4acccde9d | |||
39edaead04 | |||
f0da898e62 | |||
384dae69f5 | |||
364763fc35 | |||
dbbbb86a8c | |||
1c64b62d5b | |||
5e48eed486 | |||
6b6084a0e9 | |||
24078a201e | |||
d62beabb62 |
165 changed files with 9732 additions and 1193 deletions
|
@ -0,0 +1,22 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_billing_store_query\n WHERE\n name = $1\n AND\n deleted = false\n );",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "exists",
|
||||||
|
"type_info": "Bool"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Text"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
null
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "03b31d653e96f3b28ff15c86ff5801fe319ba86a15c09aacc4990a7a58849081"
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT \n store_id, version\n FROM\n cqrs_billing_store_query\n WHERE\n store_id = $1;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "store_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "version",
|
||||||
|
"type_info": "Int8"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "0bf69e4af7657572bf84633ecf8e2649a7b5baa3fc3abbb6ee7f522fedf3062e"
|
||||||
|
}
|
|
@ -0,0 +1,94 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT \n product_name,\n product_id,\n line_item_id,\n quantity_minor_unit,\n quantity_minor_number,\n quantity_major_unit,\n quantity_major_number,\n created_time,\n bill_id,\n price_per_unit_minor,\n price_per_unit_major,\n price_per_unit_currency,\n deleted\n FROM\n cqrs_billing_line_item_query\n WHERE\n line_item_id = $1;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "product_name",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "product_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "line_item_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
|
"name": "quantity_minor_unit",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 4,
|
||||||
|
"name": "quantity_minor_number",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 5,
|
||||||
|
"name": "quantity_major_unit",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 6,
|
||||||
|
"name": "quantity_major_number",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 7,
|
||||||
|
"name": "created_time",
|
||||||
|
"type_info": "Timestamptz"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 8,
|
||||||
|
"name": "bill_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 9,
|
||||||
|
"name": "price_per_unit_minor",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 10,
|
||||||
|
"name": "price_per_unit_major",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 11,
|
||||||
|
"name": "price_per_unit_currency",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 12,
|
||||||
|
"name": "deleted",
|
||||||
|
"type_info": "Bool"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "0d6ba3039f3419dd34a609ae207ac7ca9cf88099aa273972271040852794caa5"
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "INSERT INTO cqrs_billing_bill_next_token_id\n (token_number, store_id)\n VALUES\n ($1, $2);",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Int4",
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "0e4316d20c96a7fcfcb68a98865de8e3c0e33e22f2d2cd67fdd671b1f94ab8ea"
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "UPDATE\n cqrs_ordering_category_query\n SET\n version = $1,\n name = $2,\n description = $3,\n category_id = $4,\n store_id = $5,\n deleted = $6;",
|
"query": "UPDATE\n cqrs_ordering_category_query\n SET\n version = $1,\n name = $2,\n description = $3,\n store_id = $4,\n deleted = $5;",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
|
@ -9,11 +9,10 @@
|
||||||
"Text",
|
"Text",
|
||||||
"Text",
|
"Text",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Uuid",
|
|
||||||
"Bool"
|
"Bool"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": []
|
"nullable": []
|
||||||
},
|
},
|
||||||
"hash": "d896f6ffb486efad5ed10a9c824656d863de0c9140054de66eef32491ace9ddb"
|
"hash": "289e6d9fe105ae9e91d947c22d0a5979786aac15b0c78359b3f634b74c4686d4"
|
||||||
}
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_billing_bill_next_token_id\n WHERE\n store_id = $1\n );",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "exists",
|
||||||
|
"type_info": "Bool"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
null
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "2af9418499c040df68031e79f7bcbdaa293f1d363f06f5926adfd038783171c4"
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "INSERT INTO cqrs_ordering_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_minor_unit,\n quantity_minor_number,\n quantity_major_unit,\n quantity_major_number,\n\n deleted\n ) VALUES (\n $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15\n );",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Int8",
|
||||||
|
"Text",
|
||||||
|
"Text",
|
||||||
|
"Text",
|
||||||
|
"Uuid",
|
||||||
|
"Uuid",
|
||||||
|
"Int4",
|
||||||
|
"Int4",
|
||||||
|
"Text",
|
||||||
|
"Bool",
|
||||||
|
"Text",
|
||||||
|
"Int4",
|
||||||
|
"Text",
|
||||||
|
"Int4",
|
||||||
|
"Bool"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "3d45cb28b61bac689d137c7415fcd620552e1390a443f4c8907b344a8526ff07"
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "SELECT \n customer_name,\n order_id,\n created_time,\n deleted\n FROM\n cqrs_ordering_order_query\n WHERE\n order_id = $1;",
|
"query": "SELECT \n customer_name,\n order_id,\n created_time,\n store_id,\n deleted\n FROM\n cqrs_ordering_order_query\n WHERE\n order_id = $1;",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
|
@ -20,6 +20,11 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ordinal": 3,
|
"ordinal": 3,
|
||||||
|
"name": "store_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 4,
|
||||||
"name": "deleted",
|
"name": "deleted",
|
||||||
"type_info": "Bool"
|
"type_info": "Bool"
|
||||||
}
|
}
|
||||||
|
@ -30,11 +35,12 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": [
|
"nullable": [
|
||||||
|
true,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
false
|
false
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "dbe1e41f04a81b2a504b9179911201ec52340d09d45041addd54eb349af82488"
|
"hash": "4f8a2294b40e4285fa6d4c7bcf648be7320f99002b9a9279981053a5062b0ed8"
|
||||||
}
|
}
|
|
@ -0,0 +1,94 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT \n product_name,\n product_id,\n line_item_id,\n quantity_minor_unit,\n quantity_minor_number,\n quantity_major_unit,\n quantity_major_number,\n created_time,\n bill_id,\n price_per_unit_minor,\n price_per_unit_major,\n price_per_unit_currency,\n deleted\n FROM cqrs_billing_line_item_query\n WHERE\n bill_id = $1;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "product_name",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "product_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "line_item_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
|
"name": "quantity_minor_unit",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 4,
|
||||||
|
"name": "quantity_minor_number",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 5,
|
||||||
|
"name": "quantity_major_unit",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 6,
|
||||||
|
"name": "quantity_major_number",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 7,
|
||||||
|
"name": "created_time",
|
||||||
|
"type_info": "Timestamptz"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 8,
|
||||||
|
"name": "bill_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 9,
|
||||||
|
"name": "price_per_unit_minor",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 10,
|
||||||
|
"name": "price_per_unit_major",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 11,
|
||||||
|
"name": "price_per_unit_currency",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 12,
|
||||||
|
"name": "deleted",
|
||||||
|
"type_info": "Bool"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "538d43c832702b03da4a51e0b0794785adfb14b4b8ff0ed7c4a7079e711b8ce7"
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "INSERT INTO cqrs_billing_line_item_query (\n version,\n product_name,\n product_id,\n line_item_id,\n quantity_minor_unit,\n quantity_minor_number,\n quantity_major_unit,\n quantity_major_number,\n created_time,\n bill_id,\n price_per_unit_minor,\n price_per_unit_major,\n price_per_unit_currency,\n deleted\n ) VALUES (\n $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14\n );",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Int8",
|
||||||
|
"Text",
|
||||||
|
"Uuid",
|
||||||
|
"Uuid",
|
||||||
|
"Text",
|
||||||
|
"Int4",
|
||||||
|
"Text",
|
||||||
|
"Int4",
|
||||||
|
"Timestamptz",
|
||||||
|
"Uuid",
|
||||||
|
"Int4",
|
||||||
|
"Int4",
|
||||||
|
"Text",
|
||||||
|
"Bool"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "6ac0c3c64749d1eb4a93f8727b56d6a2c9e9b1e49258b38ed1dbebf2f601e99e"
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "INSERT INTO cqrs_billing_bill_query (\n version,\n created_time,\n store_id,\n bill_id,\n token_number,\n total_price_major,\n total_price_minor,\n total_price_currency,\n deleted\n\n ) VALUES (\n $1, $2, $3, $4, $5, $6, $7, $8, $9\n );",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Int8",
|
||||||
|
"Timestamptz",
|
||||||
|
"Uuid",
|
||||||
|
"Uuid",
|
||||||
|
"Int4",
|
||||||
|
"Int4",
|
||||||
|
"Int4",
|
||||||
|
"Text",
|
||||||
|
"Bool"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "6d9b6f386f4425d45d6aa8f7c2331c44587f54c3583849d07d81e87e1f773c2b"
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "INSERT INTO cqrs_billing_store_query (\n version, name, address, store_id, owner, deleted\n ) VALUES (\n $1, $2, $3, $4, $5, $6\n );",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Int8",
|
||||||
|
"Text",
|
||||||
|
"Text",
|
||||||
|
"Uuid",
|
||||||
|
"Uuid",
|
||||||
|
"Bool"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "78008f1b0d3f366937c72a181b19217cd9f28f1ebe73ddc081ec2f7bec5dc1b3"
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "INSERT INTO cqrs_ordering_order_query (\n version,\n customer_name,\n order_id,\n created_time,\n deleted\n\n ) VALUES (\n $1, $2, $3, $4, $5\n );",
|
"query": "INSERT INTO cqrs_ordering_order_query (\n version,\n customer_name,\n order_id,\n created_time,\n store_id,\n deleted\n\n ) VALUES (\n $1, $2, $3, $4, $5, $6\n );",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
|
@ -9,10 +9,11 @@
|
||||||
"Text",
|
"Text",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Timestamptz",
|
"Timestamptz",
|
||||||
|
"Uuid",
|
||||||
"Bool"
|
"Bool"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": []
|
"nullable": []
|
||||||
},
|
},
|
||||||
"hash": "a342003149f2991ef0280d008f18e664c96299edfd9a11d08487e4db10a10e8b"
|
"hash": "7e2e91418e136fc5091b27c5f61158cca61e4853bee64e5afc44679e685cd744"
|
||||||
}
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT \n bill_id, version\n FROM\n cqrs_billing_bill_query\n WHERE\n bill_id = $1;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "bill_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "version",
|
||||||
|
"type_info": "Int8"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "835a3afa5e16c762d021c4ca889b8c9ce62cf39e4a516511f436cba04520fddd"
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "DELETE FROM cqrs_billing_bill_next_token_id WHERE store_id = $1",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "859920462d8e008b34bfc7467140d382b164f80a255dfe805abcd12cd43e17f3"
|
||||||
|
}
|
|
@ -1,18 +1,18 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "UPDATE\n cqrs_ordering_order_query\n SET\n version = $1,\n customer_name = $2,\n order_id = $3,\n created_time = $4,\n deleted = $5;",
|
"query": "UPDATE\n cqrs_ordering_order_query\n SET\n version = $1,\n customer_name = $2,\n created_time = $3,\n store_id= $4,\n deleted = $5;",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"Left": [
|
"Left": [
|
||||||
"Int8",
|
"Int8",
|
||||||
"Text",
|
"Text",
|
||||||
"Uuid",
|
|
||||||
"Timestamptz",
|
"Timestamptz",
|
||||||
|
"Uuid",
|
||||||
"Bool"
|
"Bool"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": []
|
"nullable": []
|
||||||
},
|
},
|
||||||
"hash": "bc7d17aab113d0519c53e5f612116a6e72bb0007a298cdba17f45f4b8bed5f56"
|
"hash": "876ca2c177175439a1604a2d5aeec785e41473116ceda238a8089bcb33588e3d"
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "UPDATE\n cqrs_ordering_store_query\n SET\n version = $1,\n name = $2,\n address = $3,\n store_id = $4,\n owner = $5,\n deleted = $6;",
|
"query": "UPDATE\n cqrs_ordering_store_query\n SET\n version = $1,\n name = $2,\n address = $3,\n owner = $4,\n deleted = $5;",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
|
@ -9,11 +9,10 @@
|
||||||
"Text",
|
"Text",
|
||||||
"Text",
|
"Text",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Uuid",
|
|
||||||
"Bool"
|
"Bool"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": []
|
"nullable": []
|
||||||
},
|
},
|
||||||
"hash": "d0580ff6dc77150cb00186302f20460dc3be59be1f8f5588bdc3d0f4489eb613"
|
"hash": "8a7958c4f8419e1fd95b2d0c75a3bab76f5962f37e58c7dee4e5f9341dca8c0e"
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "UPDATE\n cqrs_inventory_store_query\n SET\n version = $1,\n name = $2,\n address = $3,\n store_id = $4,\n owner = $5,\n deleted = $6;",
|
"query": "UPDATE\n cqrs_inventory_store_query\n SET\n version = $1,\n name = $2,\n address = $3,\n owner = $4,\n deleted = $5;",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
|
@ -9,11 +9,10 @@
|
||||||
"Text",
|
"Text",
|
||||||
"Text",
|
"Text",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Uuid",
|
|
||||||
"Bool"
|
"Bool"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": []
|
"nullable": []
|
||||||
},
|
},
|
||||||
"hash": "0ccd86643c13d9d7c793f362aedc2d720d9dda982a0416849bf97291fc645ea5"
|
"hash": "8d62d7b612fd7f323aee586047dec9603f05782f55f72d21fc29954a65622b5d"
|
||||||
}
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_billing_bill_query\n WHERE\n bill_id = $1\n );",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "exists",
|
||||||
|
"type_info": "Bool"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
null
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "91a8472f079a5221a94cdf5bf1a4da078ac5fe27b022d41948ceb5d554b7f0fa"
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT \n product_id, version\n FROM\n cqrs_ordering_product_query\n WHERE\n product_id = $1;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "product_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "version",
|
||||||
|
"type_info": "Int8"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "9645865fd899d0bb983bb9da43567e172b2c49855971ee58692bed595d347228"
|
||||||
|
}
|
|
@ -1,18 +1,17 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "UPDATE\n cqrs_ordering_kot_query\n SET\n version = $1,\n order_id = $2,\n kot_id = $3,\n created_time = $4,\n deleted = $5;",
|
"query": "UPDATE\n cqrs_ordering_kot_query\n SET\n version = $1,\n order_id = $2,\n created_time = $3,\n deleted = $4;",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"Left": [
|
"Left": [
|
||||||
"Int8",
|
"Int8",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Uuid",
|
|
||||||
"Timestamptz",
|
"Timestamptz",
|
||||||
"Bool"
|
"Bool"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": []
|
"nullable": []
|
||||||
},
|
},
|
||||||
"hash": "4b7100e5c7442066dbb4ea7c9733e038af635766f0be61a86ca7084b34a2109d"
|
"hash": "97c136fd927b9a153a6093da6594e5fc2837b3943b8bbcbade97920a99019c86"
|
||||||
}
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "UPDATE\n cqrs_billing_line_item_query\n SET\n version = $1,\n product_name = $2,\n product_id = $3,\n quantity_minor_unit = $4,\n quantity_minor_number = $5,\n quantity_major_unit = $6,\n quantity_major_number = $7,\n created_time = $8,\n bill_id = $9,\n price_per_unit_minor = $10 ,\n price_per_unit_major = $11,\n price_per_unit_currency = $12,\n deleted = $13;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Int8",
|
||||||
|
"Text",
|
||||||
|
"Uuid",
|
||||||
|
"Text",
|
||||||
|
"Int4",
|
||||||
|
"Text",
|
||||||
|
"Int4",
|
||||||
|
"Timestamptz",
|
||||||
|
"Uuid",
|
||||||
|
"Int4",
|
||||||
|
"Int4",
|
||||||
|
"Text",
|
||||||
|
"Bool"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "995cca627c711a87b30723c6ceefd3fcdd1fc63bdcd95f8a974823089652aa51"
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "UPDATE\n cqrs_ordering_product_query\n SET\n version = $1,\n name = $2,\n description = $3,\n image = $4,\n category_id = $5,\n price_major = $6,\n price_minor = $7,\n price_currency = $8,\n sku_able = $9,\n quantity_minor_unit = $10,\n quantity_minor_number = $11,\n quantity_major_unit = $12,\n quantity_major_number = $13,\n deleted = $14;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Int8",
|
||||||
|
"Text",
|
||||||
|
"Text",
|
||||||
|
"Text",
|
||||||
|
"Uuid",
|
||||||
|
"Int4",
|
||||||
|
"Int4",
|
||||||
|
"Text",
|
||||||
|
"Bool",
|
||||||
|
"Text",
|
||||||
|
"Int4",
|
||||||
|
"Text",
|
||||||
|
"Int4",
|
||||||
|
"Bool"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "a3fa1c6271b85d23d70116363f19144190120c13752364bc3b78a92a08bd9157"
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "UPDATE\n cqrs_ordering_product_customizations_query\n SET\n version = $1,\n name = $2,\n customization_id = $3,\n product_id = $4,\n deleted = $5;",
|
"query": "UPDATE\n cqrs_ordering_product_customizations_query\n SET\n version = $1,\n name = $2,\n product_id = $3,\n deleted = $4;",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
|
@ -8,11 +8,10 @@
|
||||||
"Int8",
|
"Int8",
|
||||||
"Text",
|
"Text",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Uuid",
|
|
||||||
"Bool"
|
"Bool"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": []
|
"nullable": []
|
||||||
},
|
},
|
||||||
"hash": "d9c625876e7d398cb48c6278e69b2eb6ad8515e68d5520013634415109309e6e"
|
"hash": "a5a58d14ddbfa78cca3729392faecfab30cc8b01fed9b73b9cc0813750230314"
|
||||||
}
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT \n line_item_id, version\n FROM\n cqrs_billing_line_item_query\n WHERE\n line_item_id = $1;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "line_item_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "version",
|
||||||
|
"type_info": "Int8"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "a80999e0bed9e7535187b20eb501258adf1c319543da07a8d79de67394b83eeb"
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT \n created_time,\n store_id,\n bill_id,\n token_number,\n total_price_major,\n total_price_minor,\n total_price_currency,\n deleted\n FROM\n cqrs_billing_bill_query\n WHERE\n bill_id = $1;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "created_time",
|
||||||
|
"type_info": "Timestamptz"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "store_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "bill_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
|
"name": "token_number",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 4,
|
||||||
|
"name": "total_price_major",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 5,
|
||||||
|
"name": "total_price_minor",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 6,
|
||||||
|
"name": "total_price_currency",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 7,
|
||||||
|
"name": "deleted",
|
||||||
|
"type_info": "Bool"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "b0c2747901658847d7c765401de06cba772713dfdc11e30e92ad4842cbbb8a62"
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_billing_store_query\n WHERE\n store_id = $1\n );",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "exists",
|
||||||
|
"type_info": "Bool"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
null
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "ba895b4310f182a73d88df08255dc89374f95ea0e3967124cb1e414c52d0428a"
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT EXISTS (\n SELECT 1\n FROM cqrs_billing_line_item_query\n WHERE\n line_item_id = $1\n );",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "exists",
|
||||||
|
"type_info": "Bool"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
null
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "c1308bfe8d0d602e154a7d02df7e229e775326bf554e935b688585d0be253934"
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "UPDATE\n cqrs_billing_bill_query\n SET\n version = $1,\n\n created_time = $2,\n store_id = $3,\n token_number = $4,\n total_price_major = $5,\n total_price_minor = $6,\n total_price_currency = $7,\n deleted = $8;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Int8",
|
||||||
|
"Timestamptz",
|
||||||
|
"Uuid",
|
||||||
|
"Int4",
|
||||||
|
"Int4",
|
||||||
|
"Int4",
|
||||||
|
"Text",
|
||||||
|
"Bool"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "c30f49bb293ca6e184c5110bdfe1108b23bdf71dd904bdd5287155161138565d"
|
||||||
|
}
|
|
@ -1,24 +0,0 @@
|
||||||
{
|
|
||||||
"db_name": "PostgreSQL",
|
|
||||||
"query": "UPDATE\n cqrs_ordering_line_item_query\n SET\n version = $1,\n product_name = $2,\n product_id = $3,\n line_item_id = $4,\n quantity_minor_unit = $5,\n quantity_minor_number = $6,\n quantity_major_unit = $7,\n quantity_major_number = $8,\n created_time = $9,\n kot_id = $10,\n deleted = $11;",
|
|
||||||
"describe": {
|
|
||||||
"columns": [],
|
|
||||||
"parameters": {
|
|
||||||
"Left": [
|
|
||||||
"Int8",
|
|
||||||
"Text",
|
|
||||||
"Uuid",
|
|
||||||
"Uuid",
|
|
||||||
"Text",
|
|
||||||
"Int4",
|
|
||||||
"Text",
|
|
||||||
"Int4",
|
|
||||||
"Timestamptz",
|
|
||||||
"Uuid",
|
|
||||||
"Bool"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"nullable": []
|
|
||||||
},
|
|
||||||
"hash": "c348d55dd91acb0d4697c433f61866b288fec93971bf9ab41faec21680e50f71"
|
|
||||||
}
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"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_minor_unit = $11,\n quantity_minor_number = $12,\n quantity_major_unit = $13,\n quantity_major_number = $14,\n deleted = $15;",
|
"query": "UPDATE\n cqrs_inventory_product_query\n SET\n version = $1,\n name = $2,\n description = $3,\n image = $4,\n category_id = $5,\n price_major = $6,\n price_minor = $7,\n price_currency = $8,\n sku_able = $9,\n quantity_minor_unit = $10,\n quantity_minor_number = $11,\n quantity_major_unit = $12,\n quantity_major_number = $13,\n deleted = $14;",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
|
@ -10,7 +10,6 @@
|
||||||
"Text",
|
"Text",
|
||||||
"Text",
|
"Text",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Uuid",
|
|
||||||
"Int4",
|
"Int4",
|
||||||
"Int4",
|
"Int4",
|
||||||
"Text",
|
"Text",
|
||||||
|
@ -24,5 +23,5 @@
|
||||||
},
|
},
|
||||||
"nullable": []
|
"nullable": []
|
||||||
},
|
},
|
||||||
"hash": "e2f9f291a20aac77851774ba8cd37325143a4d98e0980632f097c5885cc71094"
|
"hash": "c358d3b79d35668b3475f29f5bf6767f7209a2443fd944420baf1e1cf5c51ccb"
|
||||||
}
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT \n name, address, store_id, owner, deleted\n FROM\n cqrs_billing_store_query\n WHERE\n store_id = $1;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "name",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "address",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "store_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
|
"name": "owner",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 4,
|
||||||
|
"name": "deleted",
|
||||||
|
"type_info": "Bool"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "c3f26f3816104a510a4630d5d3d27178f9bcf4055cbf176b5e8fb2376a5972ba"
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "UPDATE\n cqrs_inventory_category_query\n SET\n version = $1,\n name = $2,\n description = $3,\n category_id = $4,\n store_id = $5,\n deleted = $6;",
|
"query": "UPDATE\n cqrs_inventory_category_query\n SET\n version = $1,\n name = $2,\n description = $3,\n store_id = $4,\n deleted = $5;",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
|
@ -9,11 +9,10 @@
|
||||||
"Text",
|
"Text",
|
||||||
"Text",
|
"Text",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Uuid",
|
|
||||||
"Bool"
|
"Bool"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": []
|
"nullable": []
|
||||||
},
|
},
|
||||||
"hash": "14d111d7453b89f2346966a9fdd725d269ef36288ed5e7fe1f7ad452deaab0e6"
|
"hash": "c9bee14e15dae80b7af9b0fdca43a97b964024252bed2d0caeebee175acd55d9"
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "UPDATE\n cqrs_inventory_product_customizations_query\n SET\n version = $1,\n name = $2,\n customization_id = $3,\n product_id = $4,\n deleted = $5;",
|
"query": "UPDATE\n cqrs_inventory_product_customizations_query\n SET\n version = $1,\n name = $2,\n product_id = $3,\n deleted = $4;",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [],
|
"columns": [],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
|
@ -8,11 +8,10 @@
|
||||||
"Int8",
|
"Int8",
|
||||||
"Text",
|
"Text",
|
||||||
"Uuid",
|
"Uuid",
|
||||||
"Uuid",
|
|
||||||
"Bool"
|
"Bool"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": []
|
"nullable": []
|
||||||
},
|
},
|
||||||
"hash": "e88a5dae732c3f8180664f306b4bb1d21f97a2f1391860eb8714a52ef4439d81"
|
"hash": "d46bb69f4e2afbae01ab08beb48a67b2cade64d9ebd47f3fc6143b057671cc1b"
|
||||||
}
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "UPDATE\n cqrs_billing_store_query\n SET\n version = $1,\n name = $2,\n address = $3,\n owner = $4,\n deleted = $5;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Int8",
|
||||||
|
"Text",
|
||||||
|
"Text",
|
||||||
|
"Uuid",
|
||||||
|
"Bool"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "d7decc8f70fc4f12d7a1db5009d2190bb9746000067d53990eb7cd646ff5d252"
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT\n token_number\n FROM\n cqrs_billing_bill_next_token_id\n WHERE\n store_id = $1;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "token_number",
|
||||||
|
"type_info": "Int4"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "dbd434995778eab99e753a8fcdce07612061f183c20028ea4995807de6e6fae0"
|
||||||
|
}
|
|
@ -0,0 +1,100 @@
|
||||||
|
{
|
||||||
|
"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_minor_unit,\n quantity_minor_number,\n quantity_major_unit,\n quantity_major_number,\n deleted\n FROM\n cqrs_ordering_product_query\n WHERE\n product_id = $1;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "name",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "description",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "image",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
|
"name": "product_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 4,
|
||||||
|
"name": "category_id",
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 5,
|
||||||
|
"name": "price_major",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 6,
|
||||||
|
"name": "price_minor",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 7,
|
||||||
|
"name": "price_currency",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 8,
|
||||||
|
"name": "sku_able",
|
||||||
|
"type_info": "Bool"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 9,
|
||||||
|
"name": "quantity_minor_unit",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 10,
|
||||||
|
"name": "quantity_minor_number",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 11,
|
||||||
|
"name": "quantity_major_unit",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 12,
|
||||||
|
"name": "quantity_major_number",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 13,
|
||||||
|
"name": "deleted",
|
||||||
|
"type_info": "Bool"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "ddf0a8ae68a2f52e9ae3723d36983dfb42518b07940e620f4b45eb92b8a54a80"
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "UPDATE cqrs_billing_bill_next_token_id\n SET\n token_number = $1\n WHERE\n store_id = $2",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Int4",
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "df0fde518fdde267e2513fdd2c8d35cbdcf0ae01437b469f1acc93ed11702ce7"
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "UPDATE\n cqrs_ordering_line_item_query\n SET\n version = $1,\n product_name = $2,\n product_id = $3,\n quantity_minor_unit = $4,\n quantity_minor_number = $5,\n quantity_major_unit = $6,\n quantity_major_number = $7,\n created_time = $8,\n kot_id = $9,\n deleted = $10;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Int8",
|
||||||
|
"Text",
|
||||||
|
"Uuid",
|
||||||
|
"Text",
|
||||||
|
"Int4",
|
||||||
|
"Text",
|
||||||
|
"Int4",
|
||||||
|
"Timestamptz",
|
||||||
|
"Uuid",
|
||||||
|
"Bool"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "e61db066fe7e66879f8ce8ccd3da94d89c30f8f5d633004ba16a6e971e041a14"
|
||||||
|
}
|
|
@ -8,8 +8,9 @@ CREATE TABLE IF NOT EXISTS cqrs_ordering_order_query
|
||||||
|
|
||||||
created_time timestamp with time zone DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
|
created_time timestamp with time zone DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
|
||||||
order_id UUID NOT NULL UNIQUE,
|
order_id UUID NOT NULL UNIQUE,
|
||||||
|
store_id UUID NOT NULL,
|
||||||
|
|
||||||
customer_name TEXT NOT NULL,
|
customer_name TEXT,
|
||||||
|
|
||||||
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
|
|
||||||
|
|
16
migrations/20240916121242_cqrs_billing_store_query.sql
Normal file
16
migrations/20240916121242_cqrs_billing_store_query.sql
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
--- SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
--
|
||||||
|
-- SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS cqrs_billing_store_query
|
||||||
|
(
|
||||||
|
version bigint CHECK (version >= 0) NOT NULL,
|
||||||
|
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
address TEXT,
|
||||||
|
owner UUID NOT NULL,
|
||||||
|
store_id UUID NOT NULL UNIQUE,
|
||||||
|
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
|
|
||||||
|
PRIMARY KEY (store_id)
|
||||||
|
);
|
29
migrations/20240917082053_cqrs_billing_line_item_query.sql
Normal file
29
migrations/20240917082053_cqrs_billing_line_item_query.sql
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
-- SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
--
|
||||||
|
-- SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS cqrs_billing_line_item_query
|
||||||
|
(
|
||||||
|
version bigint CHECK (version >= 0) NOT NULL,
|
||||||
|
|
||||||
|
created_time timestamp with time zone DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
|
||||||
|
line_item_id UUID NOT NULL UNIQUE,
|
||||||
|
|
||||||
|
bill_id UUID NOT NULL,
|
||||||
|
|
||||||
|
product_name TEXT NOT NULL,
|
||||||
|
product_id UUID NOT NULL,
|
||||||
|
|
||||||
|
quantity_major_number INTEGER NOT NULL,
|
||||||
|
quantity_minor_number INTEGER NOT NULL,
|
||||||
|
quantity_major_unit TEXT NOT NULL,
|
||||||
|
quantity_minor_unit TEXT NOT NULL,
|
||||||
|
|
||||||
|
price_per_unit_minor INTEGER NOT NULL,
|
||||||
|
price_per_unit_major INTEGER NOT NULL,
|
||||||
|
price_per_unit_currency TEXT NOT NULL,
|
||||||
|
|
||||||
|
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
|
|
||||||
|
PRIMARY KEY (line_item_id)
|
||||||
|
);
|
31
migrations/20240917094208_cqrs_billing_bill_query.sql
Normal file
31
migrations/20240917094208_cqrs_billing_bill_query.sql
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
-- SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
--
|
||||||
|
-- SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS cqrs_billing_bill_query
|
||||||
|
(
|
||||||
|
version bigint CHECK (version >= 0) NOT NULL,
|
||||||
|
|
||||||
|
created_time timestamp with time zone DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
|
||||||
|
bill_id UUID NOT NULL UNIQUE,
|
||||||
|
|
||||||
|
|
||||||
|
store_id UUID NOT NULL,
|
||||||
|
token_number INTEGER NOT NULL,
|
||||||
|
|
||||||
|
total_price_minor INTEGER DEFAULT NULL,
|
||||||
|
total_price_major INTEGER DEFAULT NULL,
|
||||||
|
total_price_currency TEXT DEFAULT NULL,
|
||||||
|
|
||||||
|
deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
|
|
||||||
|
PRIMARY KEY (bill_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS cqrs_billing_bill_next_token_id
|
||||||
|
(
|
||||||
|
store_id UUID NOT NULL,
|
||||||
|
token_number INTEGER NOT NULL DEFAULT 1,
|
||||||
|
|
||||||
|
PRIMARY KEY (store_id)
|
||||||
|
);
|
4
src/billing/adapters/output/db/mod.rs
Normal file
4
src/billing/adapters/output/db/mod.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
mod postgres;
|
83
src/billing/adapters/output/db/postgres/bill_id_exists.rs
Normal file
83
src/billing/adapters/output/db/postgres/bill_id_exists.rs
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::BillingDBPostgresAdapter;
|
||||||
|
use crate::billing::application::port::output::db::{bill_id_exists::*, errors::*};
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl BillIDExistsDBPort for BillingDBPostgresAdapter {
|
||||||
|
async fn bill_id_exists(&self, bill_id: &Uuid) -> BillingDBResult<bool> {
|
||||||
|
let res = sqlx::query!(
|
||||||
|
"SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM cqrs_billing_bill_query
|
||||||
|
WHERE
|
||||||
|
bill_id = $1
|
||||||
|
);",
|
||||||
|
bill_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
if let Some(x) = res.exists {
|
||||||
|
Ok(x)
|
||||||
|
} else {
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
// use crate::billing::domain::add_product_command::tests::get_customizations;
|
||||||
|
use crate::billing::domain::bill_aggregate::*;
|
||||||
|
|
||||||
|
pub async fn create_dummy_bill(bill: &Bill, db: &BillingDBPostgresAdapter) {
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO cqrs_billing_bill_query (
|
||||||
|
version,
|
||||||
|
store_id,
|
||||||
|
bill_id,
|
||||||
|
token_number,
|
||||||
|
deleted
|
||||||
|
) VALUES (
|
||||||
|
$1, $2, $3, $4, $5
|
||||||
|
);",
|
||||||
|
1,
|
||||||
|
*bill.store_id(),
|
||||||
|
*bill.bill_id(),
|
||||||
|
*bill.token_number() as i32,
|
||||||
|
bill.deleted().clone(),
|
||||||
|
)
|
||||||
|
.execute(&db.pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_postgres_product_exists() {
|
||||||
|
let settings = crate::settings::tests::get_settings().await;
|
||||||
|
settings.create_db().await;
|
||||||
|
let db = super::BillingDBPostgresAdapter::new(
|
||||||
|
sqlx::postgres::PgPool::connect(&settings.database.url)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let bill = Bill::default();
|
||||||
|
|
||||||
|
// state doesn't exist
|
||||||
|
assert!(!db.bill_id_exists(bill.bill_id()).await.unwrap());
|
||||||
|
|
||||||
|
create_dummy_bill(&bill, &db).await;
|
||||||
|
|
||||||
|
// state exists
|
||||||
|
assert!(db.bill_id_exists(bill.bill_id()).await.unwrap());
|
||||||
|
|
||||||
|
settings.drop_db().await;
|
||||||
|
}
|
||||||
|
}
|
446
src/billing/adapters/output/db/postgres/bill_view.rs
Normal file
446
src/billing/adapters/output/db/postgres/bill_view.rs
Normal file
|
@ -0,0 +1,446 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use cqrs_es::persist::{PersistenceError, ViewContext, ViewRepository};
|
||||||
|
use cqrs_es::{EventEnvelope, Query, View};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
use super::BillingDBPostgresAdapter;
|
||||||
|
use crate::billing::domain::bill_aggregate::{Bill, BillBuilder};
|
||||||
|
use crate::billing::domain::events::BillingEvent;
|
||||||
|
use crate::types::currency::{self, Currency, PriceBuilder};
|
||||||
|
use crate::utils::parse_aggregate_id::parse_aggregate_id;
|
||||||
|
|
||||||
|
pub const NEW_BILL_NON_UUID: &str = "billing_new_bill_non_uuid-asdfa";
|
||||||
|
|
||||||
|
// The view for a Bill query, for a standard http application this should
|
||||||
|
// be designed to reflect the response dto that will be returned to a user.
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct BillView {
|
||||||
|
created_time: OffsetDateTime,
|
||||||
|
store_id: Uuid,
|
||||||
|
bill_id: Uuid,
|
||||||
|
|
||||||
|
token_number: i32,
|
||||||
|
|
||||||
|
total_price_minor: Option<i32>,
|
||||||
|
total_price_major: Option<i32>,
|
||||||
|
total_price_currency: Option<String>,
|
||||||
|
|
||||||
|
deleted: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<BillView> for Bill {
|
||||||
|
fn from(v: BillView) -> Self {
|
||||||
|
let price = match (
|
||||||
|
v.total_price_minor,
|
||||||
|
v.total_price_major,
|
||||||
|
v.total_price_currency,
|
||||||
|
) {
|
||||||
|
(Some(minor), Some(major), Some(currency)) => Some(
|
||||||
|
PriceBuilder::default()
|
||||||
|
.major(major as usize)
|
||||||
|
.minor(minor as usize)
|
||||||
|
.currency(Currency::from_str(¤cy).unwrap())
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
BillBuilder::default()
|
||||||
|
.created_time(v.created_time)
|
||||||
|
.store_id(v.store_id)
|
||||||
|
.bill_id(v.bill_id)
|
||||||
|
.token_number(v.token_number as usize)
|
||||||
|
.total_price(price)
|
||||||
|
.deleted(v.deleted)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for BillView {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
created_time: OffsetDateTime::now_utc(),
|
||||||
|
store_id: Default::default(),
|
||||||
|
bill_id: Default::default(),
|
||||||
|
|
||||||
|
token_number: Default::default(),
|
||||||
|
|
||||||
|
total_price_minor: Default::default(),
|
||||||
|
total_price_major: Default::default(),
|
||||||
|
total_price_currency: Default::default(),
|
||||||
|
|
||||||
|
deleted: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BillView {
|
||||||
|
fn merge(&mut self, bill: &Bill) {
|
||||||
|
self.created_time = bill.created_time().clone();
|
||||||
|
self.store_id = *bill.store_id();
|
||||||
|
self.bill_id = *bill.bill_id();
|
||||||
|
|
||||||
|
self.token_number = *bill.token_number() as i32;
|
||||||
|
|
||||||
|
self.total_price_minor = bill.total_price().as_ref().map(|t| *t.minor() as i32);
|
||||||
|
self.total_price_major = bill.total_price().as_ref().map(|t| *t.major() as i32);
|
||||||
|
self.total_price_currency = bill
|
||||||
|
.total_price()
|
||||||
|
.as_ref()
|
||||||
|
.map(|t| t.currency().to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This updates the view with events as they are committed.
|
||||||
|
// The logic should be minimal here, e.g., don't calculate the account balance,
|
||||||
|
// design the events to carry the balance information instead.
|
||||||
|
impl View<Bill> for BillView {
|
||||||
|
fn update(&mut self, event: &EventEnvelope<Bill>) {
|
||||||
|
match &event.payload {
|
||||||
|
BillingEvent::BillAdded(val) => {
|
||||||
|
self.merge(val.bill());
|
||||||
|
|
||||||
|
self.deleted = false;
|
||||||
|
}
|
||||||
|
BillingEvent::BillUpdated(e) => self.merge(e.new_bill()),
|
||||||
|
BillingEvent::BillTotalPriceComputed(e) => {
|
||||||
|
let total_price = e.total_price().clone();
|
||||||
|
self.total_price_minor = Some(*total_price.minor() as i32);
|
||||||
|
self.total_price_major = Some(*total_price.major() as i32);
|
||||||
|
self.total_price_currency = Some(total_price.currency().to_string());
|
||||||
|
}
|
||||||
|
BillingEvent::BillDeleted(e) => self.deleted = true,
|
||||||
|
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl ViewRepository<BillView, Bill> for BillingDBPostgresAdapter {
|
||||||
|
async fn load(&self, bill_id: &str) -> Result<Option<BillView>, PersistenceError> {
|
||||||
|
let bill_id = match parse_aggregate_id(bill_id, NEW_BILL_NON_UUID)? {
|
||||||
|
Some((val, _)) => return Ok(Some(val)),
|
||||||
|
None => Uuid::parse_str(bill_id).unwrap(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = sqlx::query_as!(
|
||||||
|
BillView,
|
||||||
|
"SELECT
|
||||||
|
created_time,
|
||||||
|
store_id,
|
||||||
|
bill_id,
|
||||||
|
token_number,
|
||||||
|
total_price_major,
|
||||||
|
total_price_minor,
|
||||||
|
total_price_currency,
|
||||||
|
deleted
|
||||||
|
FROM
|
||||||
|
cqrs_billing_bill_query
|
||||||
|
WHERE
|
||||||
|
bill_id = $1;",
|
||||||
|
bill_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
Ok(Some(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn load_with_context(
|
||||||
|
&self,
|
||||||
|
bill_id: &str,
|
||||||
|
) -> Result<Option<(BillView, ViewContext)>, PersistenceError> {
|
||||||
|
let bill_id = match parse_aggregate_id(bill_id, NEW_BILL_NON_UUID)? {
|
||||||
|
Some(val) => return Ok(Some(val)),
|
||||||
|
None => Uuid::parse_str(bill_id).unwrap(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = sqlx::query_as!(
|
||||||
|
BillView,
|
||||||
|
"SELECT
|
||||||
|
created_time,
|
||||||
|
store_id,
|
||||||
|
bill_id,
|
||||||
|
token_number,
|
||||||
|
total_price_major,
|
||||||
|
total_price_minor,
|
||||||
|
total_price_currency,
|
||||||
|
deleted
|
||||||
|
FROM
|
||||||
|
cqrs_billing_bill_query
|
||||||
|
WHERE
|
||||||
|
bill_id = $1;",
|
||||||
|
&bill_id,
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
|
||||||
|
struct Context {
|
||||||
|
version: i64,
|
||||||
|
bill_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
let ctx = sqlx::query_as!(
|
||||||
|
Context,
|
||||||
|
"SELECT
|
||||||
|
bill_id, version
|
||||||
|
FROM
|
||||||
|
cqrs_billing_bill_query
|
||||||
|
WHERE
|
||||||
|
bill_id = $1;",
|
||||||
|
bill_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
|
||||||
|
let view_context = ViewContext::new(ctx.bill_id.to_string(), ctx.version);
|
||||||
|
Ok(Some((res, view_context)))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_view(
|
||||||
|
&self,
|
||||||
|
view: BillView,
|
||||||
|
context: ViewContext,
|
||||||
|
) -> Result<(), PersistenceError> {
|
||||||
|
match context.version {
|
||||||
|
0 => {
|
||||||
|
let version = context.version + 1;
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO cqrs_billing_bill_query (
|
||||||
|
version,
|
||||||
|
created_time,
|
||||||
|
store_id,
|
||||||
|
bill_id,
|
||||||
|
token_number,
|
||||||
|
total_price_major,
|
||||||
|
total_price_minor,
|
||||||
|
total_price_currency,
|
||||||
|
deleted
|
||||||
|
|
||||||
|
) VALUES (
|
||||||
|
$1, $2, $3, $4, $5, $6, $7, $8, $9
|
||||||
|
);",
|
||||||
|
version,
|
||||||
|
view.created_time,
|
||||||
|
view.store_id,
|
||||||
|
view.bill_id,
|
||||||
|
view.token_number,
|
||||||
|
view.total_price_major,
|
||||||
|
view.total_price_minor,
|
||||||
|
view.total_price_currency,
|
||||||
|
view.deleted,
|
||||||
|
)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let version = context.version + 1;
|
||||||
|
sqlx::query!(
|
||||||
|
"UPDATE
|
||||||
|
cqrs_billing_bill_query
|
||||||
|
SET
|
||||||
|
version = $1,
|
||||||
|
|
||||||
|
created_time = $2,
|
||||||
|
store_id = $3,
|
||||||
|
token_number = $4,
|
||||||
|
total_price_major = $5,
|
||||||
|
total_price_minor = $6,
|
||||||
|
total_price_currency = $7,
|
||||||
|
deleted = $8;",
|
||||||
|
version,
|
||||||
|
view.created_time,
|
||||||
|
view.store_id,
|
||||||
|
view.token_number,
|
||||||
|
view.total_price_major,
|
||||||
|
view.total_price_minor,
|
||||||
|
view.total_price_currency,
|
||||||
|
view.deleted,
|
||||||
|
)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SimpleLoggingQuery {}
|
||||||
|
|
||||||
|
// Our simplest query, this is great for debugging but absolutely useless in production.
|
||||||
|
// This query just pretty prints the events as they are processed.
|
||||||
|
#[async_trait]
|
||||||
|
impl Query<Bill> for SimpleLoggingQuery {
|
||||||
|
async fn dispatch(&self, aggregate_id: &str, events: &[EventEnvelope<Bill>]) {
|
||||||
|
for event in events {
|
||||||
|
let payload = serde_json::to_string_pretty(&event.payload).unwrap();
|
||||||
|
println!("{}-{}\n{}", aggregate_id, event.sequence, payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Query<Bill> for BillingDBPostgresAdapter {
|
||||||
|
async fn dispatch(&self, bill_id: &str, events: &[EventEnvelope<Bill>]) {
|
||||||
|
let res = self
|
||||||
|
.load_with_context(bill_id)
|
||||||
|
.await
|
||||||
|
.unwrap_or_else(|_| Some((BillView::default(), ViewContext::new(bill_id.into(), 0))));
|
||||||
|
let (mut view, view_context): (BillView, ViewContext) = res.unwrap();
|
||||||
|
for event in events {
|
||||||
|
view.update(event);
|
||||||
|
}
|
||||||
|
self.update_view(view, view_context).await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use postgres_es::PostgresCqrs;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
billing::{
|
||||||
|
application::services::{
|
||||||
|
add_bill_service::AddBillServiceBuilder, update_bill_service::*,
|
||||||
|
MockBillingServicesInterface,
|
||||||
|
},
|
||||||
|
domain::{
|
||||||
|
add_bill_command::*, commands::BillingCommand, store_aggregate::Store,
|
||||||
|
update_bill_command::*,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
db::migrate::*,
|
||||||
|
tests::bdd::*,
|
||||||
|
utils::uuid::tests::*,
|
||||||
|
};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn pg_query_billing_bill_view() {
|
||||||
|
let settings = crate::settings::tests::get_settings().await;
|
||||||
|
//let settings = crate::settings::Settings::new().unwrap();
|
||||||
|
settings.create_db().await;
|
||||||
|
|
||||||
|
let db = crate::db::sqlx_postgres::Postgres::init(&settings.database.url).await;
|
||||||
|
db.migrate().await;
|
||||||
|
let db = BillingDBPostgresAdapter::new(db.pool.clone());
|
||||||
|
|
||||||
|
let simple_query = SimpleLoggingQuery {};
|
||||||
|
|
||||||
|
let queries: Vec<Box<dyn Query<Bill>>> = vec![Box::new(simple_query), Box::new(db.clone())];
|
||||||
|
|
||||||
|
let mut mock_services = MockBillingServicesInterface::new();
|
||||||
|
|
||||||
|
let store = Store::default();
|
||||||
|
crate::billing::adapters::output::db::postgres::store_id_exists::tests::create_dummy_store_record(&store, &db).await;
|
||||||
|
|
||||||
|
let db2 = db.clone();
|
||||||
|
mock_services
|
||||||
|
.expect_add_bill()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.returning(move || {
|
||||||
|
Arc::new(
|
||||||
|
AddBillServiceBuilder::default()
|
||||||
|
.db_bill_id_exists(Arc::new(db2.clone()))
|
||||||
|
.db_next_token_id(Arc::new(db2.clone()))
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
let db2 = db.clone();
|
||||||
|
mock_services
|
||||||
|
.expect_update_bill()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.returning(move || {
|
||||||
|
Arc::new(
|
||||||
|
UpdateBillServiceBuilder::default()
|
||||||
|
.db_bill_id_exists(Arc::new(db2.clone()))
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
let (cqrs, bill_query): (
|
||||||
|
Arc<PostgresCqrs<Bill>>,
|
||||||
|
Arc<dyn ViewRepository<BillView, Bill>>,
|
||||||
|
) = (
|
||||||
|
Arc::new(postgres_es::postgres_cqrs(
|
||||||
|
db.pool.clone(),
|
||||||
|
queries,
|
||||||
|
Arc::new(mock_services),
|
||||||
|
)),
|
||||||
|
Arc::new(db.clone()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let cmd = AddBillCommandBuilder::default()
|
||||||
|
.adding_by(UUID)
|
||||||
|
.bill_id(UUID)
|
||||||
|
.store_id(*store.store_id())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
cqrs.execute(
|
||||||
|
&cmd.bill_id().to_string(),
|
||||||
|
BillingCommand::AddBill(cmd.clone()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let bill = bill_query
|
||||||
|
.load(&(*cmd.bill_id()).to_string())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let bill: Bill = bill.into();
|
||||||
|
assert_eq!(bill.store_id(), cmd.store_id());
|
||||||
|
assert_eq!(bill.bill_id(), cmd.bill_id());
|
||||||
|
assert!(!bill.deleted());
|
||||||
|
|
||||||
|
let update_bill_cmd = UpdateBillCommandBuilder::default()
|
||||||
|
.adding_by(UUID)
|
||||||
|
.store_id(*store.store_id())
|
||||||
|
.total_price(None)
|
||||||
|
.old_bill(bill.clone())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
cqrs.execute(
|
||||||
|
&cmd.bill_id().to_string(),
|
||||||
|
BillingCommand::UpdateBill(update_bill_cmd.clone()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let bill = bill_query
|
||||||
|
.load(&(*cmd.bill_id()).to_string())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let bill: Bill = bill.into();
|
||||||
|
assert_eq!(bill.store_id(), cmd.store_id());
|
||||||
|
assert_eq!(bill.bill_id(), update_bill_cmd.old_bill().bill_id());
|
||||||
|
assert_eq!(bill.total_price(), update_bill_cmd.total_price());
|
||||||
|
assert!(!bill.deleted());
|
||||||
|
|
||||||
|
settings.drop_db().await;
|
||||||
|
}
|
||||||
|
}
|
79
src/billing/adapters/output/db/postgres/errors.rs
Normal file
79
src/billing/adapters/output/db/postgres/errors.rs
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
use cqrs_es::persist::PersistenceError;
|
||||||
|
use sqlx::Error as SqlxError;
|
||||||
|
|
||||||
|
use crate::billing::application::port::output::db::errors::BillingDBError;
|
||||||
|
|
||||||
|
impl From<SqlxError> for BillingDBError {
|
||||||
|
fn from(e: SqlxError) -> Self {
|
||||||
|
log::error!("[postgres] err: {}", e);
|
||||||
|
if let SqlxError::Database(err) = e {
|
||||||
|
if err.code() == Some(Cow::from("23505")) {
|
||||||
|
let msg = err.message();
|
||||||
|
if msg.contains("cqrs_inventory_store_query_store_id_key") {
|
||||||
|
return Self::DuplicateStoreID;
|
||||||
|
} else {
|
||||||
|
println!("{msg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Self::InternalError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// map custom row not found error to DB error
|
||||||
|
pub fn map_row_not_found_err(e: SqlxError, row_not_found: BillingDBError) -> BillingDBError {
|
||||||
|
if let SqlxError::RowNotFound = e {
|
||||||
|
row_not_found
|
||||||
|
} else {
|
||||||
|
e.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum PostgresAggregateError {
|
||||||
|
OptimisticLock,
|
||||||
|
ConnectionError(Box<dyn std::error::Error + Send + Sync + 'static>),
|
||||||
|
DeserializationError(Box<dyn std::error::Error + Send + Sync + 'static>),
|
||||||
|
UnknownError(Box<dyn std::error::Error + Send + Sync + 'static>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<SqlxError> for PostgresAggregateError {
|
||||||
|
fn from(err: SqlxError) -> Self {
|
||||||
|
// TODO: improve error handling
|
||||||
|
match &err {
|
||||||
|
SqlxError::Database(database_error) => {
|
||||||
|
if let Some(code) = database_error.code() {
|
||||||
|
if code.as_ref() == "23505" {
|
||||||
|
return PostgresAggregateError::OptimisticLock;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PostgresAggregateError::UnknownError(Box::new(err))
|
||||||
|
}
|
||||||
|
SqlxError::Io(_) | SqlxError::Tls(_) => {
|
||||||
|
PostgresAggregateError::ConnectionError(Box::new(err))
|
||||||
|
}
|
||||||
|
_ => PostgresAggregateError::UnknownError(Box::new(err)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<PostgresAggregateError> for PersistenceError {
|
||||||
|
fn from(err: PostgresAggregateError) -> Self {
|
||||||
|
match err {
|
||||||
|
PostgresAggregateError::OptimisticLock => PersistenceError::OptimisticLockError,
|
||||||
|
PostgresAggregateError::ConnectionError(error) => {
|
||||||
|
PersistenceError::ConnectionError(error)
|
||||||
|
}
|
||||||
|
PostgresAggregateError::DeserializationError(error) => {
|
||||||
|
PersistenceError::UnknownError(error)
|
||||||
|
}
|
||||||
|
PostgresAggregateError::UnknownError(error) => PersistenceError::UnknownError(error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,144 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::BillingDBPostgresAdapter;
|
||||||
|
use crate::billing::{
|
||||||
|
application::port::output::db::{errors::*, get_line_items_for_bill_id::*},
|
||||||
|
domain::line_item_aggregate::LineItem,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl GetLineItemsForBillIDDBPort for BillingDBPostgresAdapter {
|
||||||
|
async fn get_line_items_for_bill_id(&self, bill_id: Uuid) -> BillingDBResult<Vec<LineItem>> {
|
||||||
|
let mut res = sqlx::query_as!(
|
||||||
|
super::line_item_view::LineItemView,
|
||||||
|
"SELECT
|
||||||
|
product_name,
|
||||||
|
product_id,
|
||||||
|
line_item_id,
|
||||||
|
quantity_minor_unit,
|
||||||
|
quantity_minor_number,
|
||||||
|
quantity_major_unit,
|
||||||
|
quantity_major_number,
|
||||||
|
created_time,
|
||||||
|
bill_id,
|
||||||
|
price_per_unit_minor,
|
||||||
|
price_per_unit_major,
|
||||||
|
price_per_unit_currency,
|
||||||
|
deleted
|
||||||
|
FROM cqrs_billing_line_item_query
|
||||||
|
WHERE
|
||||||
|
bill_id = $1;",
|
||||||
|
bill_id
|
||||||
|
)
|
||||||
|
.fetch_all(&self.pool)
|
||||||
|
.await?;
|
||||||
|
println!("Got len: {}", res.len());
|
||||||
|
let mut output = Vec::with_capacity(res.len());
|
||||||
|
res.drain(0..).for_each(|r| output.push(r.into()));
|
||||||
|
Ok(output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
// use crate::billing::domain::add_product_command::tests::get_customizations;
|
||||||
|
use crate::{
|
||||||
|
billing::{
|
||||||
|
adapters::output::db::postgres::line_item_view::LineItemView, domain::bill_aggregate::*,
|
||||||
|
},
|
||||||
|
types::currency::*,
|
||||||
|
types::quantity::*,
|
||||||
|
utils::uuid::{tests::*, *},
|
||||||
|
};
|
||||||
|
|
||||||
|
async fn create_dummy_line_item(
|
||||||
|
db: &BillingDBPostgresAdapter,
|
||||||
|
bill_id: Uuid,
|
||||||
|
line_item_id: Uuid,
|
||||||
|
) {
|
||||||
|
let view = LineItemView::default();
|
||||||
|
let version = 0;
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO cqrs_billing_line_item_query (
|
||||||
|
version,
|
||||||
|
product_name,
|
||||||
|
product_id,
|
||||||
|
line_item_id,
|
||||||
|
quantity_minor_unit,
|
||||||
|
quantity_minor_number,
|
||||||
|
quantity_major_unit,
|
||||||
|
quantity_major_number,
|
||||||
|
created_time,
|
||||||
|
bill_id,
|
||||||
|
price_per_unit_minor,
|
||||||
|
price_per_unit_major,
|
||||||
|
price_per_unit_currency,
|
||||||
|
deleted
|
||||||
|
) VALUES (
|
||||||
|
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
|
||||||
|
);",
|
||||||
|
version,
|
||||||
|
view.product_name,
|
||||||
|
view.product_id,
|
||||||
|
line_item_id,
|
||||||
|
QuantityUnit::DiscreteNumber.to_string(),
|
||||||
|
view.quantity_minor_number,
|
||||||
|
QuantityUnit::DiscreteNumber.to_string(),
|
||||||
|
view.quantity_major_number,
|
||||||
|
view.created_time,
|
||||||
|
bill_id,
|
||||||
|
view.price_per_unit_minor,
|
||||||
|
view.price_per_unit_major,
|
||||||
|
Currency::INR.to_string(),
|
||||||
|
view.deleted,
|
||||||
|
)
|
||||||
|
.execute(&db.pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_postgres_get_line_items_for_bill_id() {
|
||||||
|
let settings = crate::settings::tests::get_settings().await;
|
||||||
|
settings.create_db().await;
|
||||||
|
let db = super::BillingDBPostgresAdapter::new(
|
||||||
|
sqlx::postgres::PgPool::connect(&settings.database.url)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let bill_id = UUID;
|
||||||
|
|
||||||
|
// state doesn't exist
|
||||||
|
assert!(db
|
||||||
|
.get_line_items_for_bill_id(bill_id)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.is_empty());
|
||||||
|
|
||||||
|
let u = GenerateUUID;
|
||||||
|
let li_id_1 = u.get_uuid();
|
||||||
|
let li_id_2 = u.get_uuid();
|
||||||
|
create_dummy_line_item(&db, bill_id, li_id_1).await;
|
||||||
|
create_dummy_line_item(&db, bill_id, li_id_2).await;
|
||||||
|
|
||||||
|
// state exists
|
||||||
|
let res = db.get_line_items_for_bill_id(bill_id).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(res.len(), 2);
|
||||||
|
assert!(res
|
||||||
|
.iter()
|
||||||
|
.any(|li| *li.bill_id() == bill_id && *li.line_item_id() == li_id_1));
|
||||||
|
assert!(res
|
||||||
|
.iter()
|
||||||
|
.any(|li| *li.bill_id() == bill_id && *li.line_item_id() == li_id_2));
|
||||||
|
|
||||||
|
settings.drop_db().await;
|
||||||
|
}
|
||||||
|
}
|
109
src/billing/adapters/output/db/postgres/line_item_id_exists.rs
Normal file
109
src/billing/adapters/output/db/postgres/line_item_id_exists.rs
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::BillingDBPostgresAdapter;
|
||||||
|
use crate::billing::application::port::output::db::{errors::*, line_item_id_exists::*};
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl LineItemIDExistsDBPort for BillingDBPostgresAdapter {
|
||||||
|
async fn line_item_id_exists(&self, line_item_id: &Uuid) -> BillingDBResult<bool> {
|
||||||
|
let res = sqlx::query!(
|
||||||
|
"SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM cqrs_billing_line_item_query
|
||||||
|
WHERE
|
||||||
|
line_item_id = $1
|
||||||
|
);",
|
||||||
|
line_item_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
if let Some(x) = res.exists {
|
||||||
|
Ok(x)
|
||||||
|
} else {
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
// use crate::billing::domain::add_product_command::tests::get_customizations;
|
||||||
|
use crate::billing::domain::line_item_aggregate::*;
|
||||||
|
|
||||||
|
async fn create_dummy_line_item(line_item: &LineItem, db: &BillingDBPostgresAdapter) {
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO cqrs_billing_line_item_query (
|
||||||
|
version,
|
||||||
|
product_name,
|
||||||
|
product_id,
|
||||||
|
line_item_id,
|
||||||
|
quantity_minor_unit,
|
||||||
|
quantity_minor_number,
|
||||||
|
quantity_major_unit,
|
||||||
|
quantity_major_number,
|
||||||
|
|
||||||
|
bill_id,
|
||||||
|
|
||||||
|
price_per_unit_minor,
|
||||||
|
price_per_unit_major,
|
||||||
|
price_per_unit_currency,
|
||||||
|
|
||||||
|
|
||||||
|
deleted
|
||||||
|
) VALUES (
|
||||||
|
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
|
||||||
|
);",
|
||||||
|
1,
|
||||||
|
line_item.product_name(),
|
||||||
|
line_item.product_id(),
|
||||||
|
line_item.line_item_id(),
|
||||||
|
line_item.quantity().major().unit().to_string(),
|
||||||
|
line_item.quantity().major().number().clone() as i32,
|
||||||
|
line_item.quantity().minor().unit().to_string(),
|
||||||
|
line_item.quantity().minor().number().clone() as i32,
|
||||||
|
line_item.bill_id(),
|
||||||
|
*line_item.price_per_unit().minor() as i32,
|
||||||
|
*line_item.price_per_unit().major() as i32,
|
||||||
|
line_item.price_per_unit().currency().to_string(),
|
||||||
|
line_item.deleted().clone(),
|
||||||
|
)
|
||||||
|
.execute(&db.pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_postgres_product_exists() {
|
||||||
|
let settings = crate::settings::tests::get_settings().await;
|
||||||
|
settings.create_db().await;
|
||||||
|
let db = super::BillingDBPostgresAdapter::new(
|
||||||
|
sqlx::postgres::PgPool::connect(&settings.database.url)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let line_item = LineItem::default();
|
||||||
|
|
||||||
|
// state doesn't exist
|
||||||
|
assert!(!db
|
||||||
|
.line_item_id_exists(line_item.line_item_id())
|
||||||
|
.await
|
||||||
|
.unwrap());
|
||||||
|
|
||||||
|
create_dummy_line_item(&line_item, &db).await;
|
||||||
|
|
||||||
|
// state exists
|
||||||
|
assert!(db
|
||||||
|
.line_item_id_exists(line_item.product_id())
|
||||||
|
.await
|
||||||
|
.unwrap());
|
||||||
|
|
||||||
|
settings.drop_db().await;
|
||||||
|
}
|
||||||
|
}
|
576
src/billing/adapters/output/db/postgres/line_item_view.rs
Normal file
576
src/billing/adapters/output/db/postgres/line_item_view.rs
Normal file
|
@ -0,0 +1,576 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use cqrs_es::persist::{PersistenceError, ViewContext, ViewRepository};
|
||||||
|
use cqrs_es::{EventEnvelope, Query, View};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
use super::BillingDBPostgresAdapter;
|
||||||
|
use crate::billing::domain::events::BillingEvent;
|
||||||
|
use crate::billing::domain::line_item_aggregate::*;
|
||||||
|
use crate::types::currency::*;
|
||||||
|
use crate::types::quantity::*;
|
||||||
|
use crate::utils::parse_aggregate_id::parse_aggregate_id;
|
||||||
|
|
||||||
|
pub const NEW_LINE_ITEM_NON_UUID: &str = "new_line_item_non_uuid-asdfa-billing";
|
||||||
|
|
||||||
|
// The view for a LineItem query, for a standard http application this should
|
||||||
|
// be designed to reflect the response dto that will be returned to a user.
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct LineItemView {
|
||||||
|
pub product_name: String,
|
||||||
|
pub product_id: Uuid,
|
||||||
|
pub bill_id: Uuid,
|
||||||
|
pub created_time: OffsetDateTime,
|
||||||
|
|
||||||
|
pub line_item_id: Uuid,
|
||||||
|
|
||||||
|
pub quantity_major_number: i32,
|
||||||
|
pub quantity_minor_number: i32,
|
||||||
|
pub quantity_major_unit: String,
|
||||||
|
pub quantity_minor_unit: String,
|
||||||
|
|
||||||
|
pub price_per_unit_major: i32,
|
||||||
|
pub price_per_unit_minor: i32,
|
||||||
|
pub price_per_unit_currency: String,
|
||||||
|
|
||||||
|
pub deleted: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for LineItemView {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
created_time: OffsetDateTime::now_utc(),
|
||||||
|
product_name: Default::default(),
|
||||||
|
product_id: Default::default(),
|
||||||
|
bill_id: Default::default(),
|
||||||
|
|
||||||
|
line_item_id: Default::default(),
|
||||||
|
|
||||||
|
price_per_unit_minor: Default::default(),
|
||||||
|
price_per_unit_major: Default::default(),
|
||||||
|
price_per_unit_currency: Default::default(),
|
||||||
|
|
||||||
|
quantity_major_number: Default::default(),
|
||||||
|
quantity_minor_number: Default::default(),
|
||||||
|
quantity_major_unit: Default::default(),
|
||||||
|
quantity_minor_unit: Default::default(),
|
||||||
|
deleted: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<LineItemView> for LineItem {
|
||||||
|
fn from(v: LineItemView) -> Self {
|
||||||
|
let quantity = QuantityBuilder::default()
|
||||||
|
.minor(
|
||||||
|
QuantityPartBuilder::default()
|
||||||
|
.number(v.quantity_minor_number as usize)
|
||||||
|
.unit(QuantityUnit::from_str(&v.quantity_minor_unit).unwrap())
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.major(
|
||||||
|
QuantityPartBuilder::default()
|
||||||
|
.number(v.quantity_major_number as usize)
|
||||||
|
.unit(QuantityUnit::from_str(&v.quantity_major_unit).unwrap())
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let price_per_unit = PriceBuilder::default()
|
||||||
|
.minor(v.price_per_unit_minor as usize)
|
||||||
|
.major(v.price_per_unit_major as usize)
|
||||||
|
.currency(Currency::from_str(&v.price_per_unit_currency).unwrap())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
LineItemBuilder::default()
|
||||||
|
.product_name(v.product_name)
|
||||||
|
.line_item_id(v.line_item_id)
|
||||||
|
.quantity(quantity)
|
||||||
|
.created_time(v.created_time)
|
||||||
|
.product_id(v.product_id)
|
||||||
|
.price_per_unit(price_per_unit)
|
||||||
|
.bill_id(v.bill_id)
|
||||||
|
.deleted(v.deleted)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This updates the view with events as they are committed.
|
||||||
|
// The logic should be minimal here, e.g., don't calculate the account balance,
|
||||||
|
// design the events to carry the balance information instead.
|
||||||
|
impl View<LineItem> for LineItemView {
|
||||||
|
fn update(&mut self, event: &EventEnvelope<LineItem>) {
|
||||||
|
match &event.payload {
|
||||||
|
BillingEvent::LineItemAdded(val) => {
|
||||||
|
self.product_name = val.line_item().product_name().into();
|
||||||
|
self.product_id = *val.line_item().product_id();
|
||||||
|
self.line_item_id = *val.line_item().line_item_id();
|
||||||
|
|
||||||
|
self.quantity_major_number = *val.line_item().quantity().major().number() as i32;
|
||||||
|
self.quantity_minor_number = *val.line_item().quantity().minor().number() as i32;
|
||||||
|
self.quantity_major_unit = val.line_item().quantity().major().unit().to_string();
|
||||||
|
self.quantity_minor_unit = val.line_item().quantity().minor().unit().to_string();
|
||||||
|
|
||||||
|
self.price_per_unit_major = *val.line_item().price_per_unit().major() as i32;
|
||||||
|
self.price_per_unit_minor = *val.line_item().price_per_unit().minor() as i32;
|
||||||
|
self.price_per_unit_currency =
|
||||||
|
val.line_item().price_per_unit().currency().to_string();
|
||||||
|
|
||||||
|
self.created_time = val.line_item().created_time().clone();
|
||||||
|
self.bill_id = *val.line_item().bill_id();
|
||||||
|
|
||||||
|
self.deleted = false;
|
||||||
|
}
|
||||||
|
BillingEvent::LineItemUpdated(val) => {
|
||||||
|
let new = val.new_line_item();
|
||||||
|
self.product_name = new.product_name().into();
|
||||||
|
self.product_id = *new.product_id();
|
||||||
|
self.line_item_id = *new.line_item_id();
|
||||||
|
|
||||||
|
self.quantity_major_number = *new.quantity().major().number() as i32;
|
||||||
|
self.quantity_minor_number = *new.quantity().minor().number() as i32;
|
||||||
|
self.quantity_major_unit = new.quantity().major().unit().to_string();
|
||||||
|
self.quantity_minor_unit = new.quantity().minor().unit().to_string();
|
||||||
|
|
||||||
|
self.price_per_unit_major = *new.price_per_unit().major() as i32;
|
||||||
|
self.price_per_unit_minor = *new.price_per_unit().minor() as i32;
|
||||||
|
self.price_per_unit_currency = new.price_per_unit().currency().to_string();
|
||||||
|
|
||||||
|
self.created_time = new.created_time().clone();
|
||||||
|
self.bill_id = *new.bill_id();
|
||||||
|
}
|
||||||
|
BillingEvent::LineItemDeleted(_) => self.deleted = true,
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl ViewRepository<LineItemView, LineItem> for BillingDBPostgresAdapter {
|
||||||
|
async fn load(&self, line_item_id: &str) -> Result<Option<LineItemView>, PersistenceError> {
|
||||||
|
let line_item_id = match parse_aggregate_id(line_item_id, NEW_LINE_ITEM_NON_UUID)? {
|
||||||
|
Some((val, _)) => return Ok(Some(val)),
|
||||||
|
None => Uuid::parse_str(line_item_id).unwrap(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = sqlx::query_as!(
|
||||||
|
LineItemView,
|
||||||
|
"SELECT
|
||||||
|
product_name,
|
||||||
|
product_id,
|
||||||
|
line_item_id,
|
||||||
|
quantity_minor_unit,
|
||||||
|
quantity_minor_number,
|
||||||
|
quantity_major_unit,
|
||||||
|
quantity_major_number,
|
||||||
|
created_time,
|
||||||
|
bill_id,
|
||||||
|
price_per_unit_minor,
|
||||||
|
price_per_unit_major,
|
||||||
|
price_per_unit_currency,
|
||||||
|
deleted
|
||||||
|
FROM
|
||||||
|
cqrs_billing_line_item_query
|
||||||
|
WHERE
|
||||||
|
line_item_id = $1;",
|
||||||
|
line_item_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
|
||||||
|
Ok(Some(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn load_with_context(
|
||||||
|
&self,
|
||||||
|
line_item_id: &str,
|
||||||
|
) -> Result<Option<(LineItemView, ViewContext)>, PersistenceError> {
|
||||||
|
let line_item_id = match parse_aggregate_id(line_item_id, NEW_LINE_ITEM_NON_UUID)? {
|
||||||
|
Some(val) => return Ok(Some(val)),
|
||||||
|
None => Uuid::parse_str(line_item_id).unwrap(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = sqlx::query_as!(
|
||||||
|
LineItemView,
|
||||||
|
"SELECT
|
||||||
|
product_name,
|
||||||
|
product_id,
|
||||||
|
line_item_id,
|
||||||
|
quantity_minor_unit,
|
||||||
|
quantity_minor_number,
|
||||||
|
quantity_major_unit,
|
||||||
|
quantity_major_number,
|
||||||
|
created_time,
|
||||||
|
bill_id,
|
||||||
|
price_per_unit_minor,
|
||||||
|
price_per_unit_major,
|
||||||
|
price_per_unit_currency,
|
||||||
|
deleted
|
||||||
|
FROM
|
||||||
|
cqrs_billing_line_item_query
|
||||||
|
WHERE
|
||||||
|
line_item_id = $1;",
|
||||||
|
line_item_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
|
||||||
|
struct Context {
|
||||||
|
version: i64,
|
||||||
|
line_item_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
let ctx = sqlx::query_as!(
|
||||||
|
Context,
|
||||||
|
"SELECT
|
||||||
|
line_item_id, version
|
||||||
|
FROM
|
||||||
|
cqrs_billing_line_item_query
|
||||||
|
WHERE
|
||||||
|
line_item_id = $1;",
|
||||||
|
line_item_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
|
||||||
|
let view_context = ViewContext::new(ctx.line_item_id.to_string(), ctx.version);
|
||||||
|
Ok(Some((res, view_context)))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_view(
|
||||||
|
&self,
|
||||||
|
view: LineItemView,
|
||||||
|
context: ViewContext,
|
||||||
|
) -> Result<(), PersistenceError> {
|
||||||
|
match context.version {
|
||||||
|
0 => {
|
||||||
|
let version = context.version + 1;
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO cqrs_billing_line_item_query (
|
||||||
|
version,
|
||||||
|
product_name,
|
||||||
|
product_id,
|
||||||
|
line_item_id,
|
||||||
|
quantity_minor_unit,
|
||||||
|
quantity_minor_number,
|
||||||
|
quantity_major_unit,
|
||||||
|
quantity_major_number,
|
||||||
|
created_time,
|
||||||
|
bill_id,
|
||||||
|
price_per_unit_minor,
|
||||||
|
price_per_unit_major,
|
||||||
|
price_per_unit_currency,
|
||||||
|
deleted
|
||||||
|
) VALUES (
|
||||||
|
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
|
||||||
|
);",
|
||||||
|
version,
|
||||||
|
view.product_name,
|
||||||
|
view.product_id,
|
||||||
|
view.line_item_id,
|
||||||
|
view.quantity_minor_unit,
|
||||||
|
view.quantity_minor_number,
|
||||||
|
view.quantity_major_unit,
|
||||||
|
view.quantity_major_number,
|
||||||
|
view.created_time,
|
||||||
|
view.bill_id,
|
||||||
|
view.price_per_unit_minor,
|
||||||
|
view.price_per_unit_major,
|
||||||
|
view.price_per_unit_currency,
|
||||||
|
view.deleted,
|
||||||
|
)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => {
|
||||||
|
let version = context.version + 1;
|
||||||
|
sqlx::query!(
|
||||||
|
"UPDATE
|
||||||
|
cqrs_billing_line_item_query
|
||||||
|
SET
|
||||||
|
version = $1,
|
||||||
|
product_name = $2,
|
||||||
|
product_id = $3,
|
||||||
|
quantity_minor_unit = $4,
|
||||||
|
quantity_minor_number = $5,
|
||||||
|
quantity_major_unit = $6,
|
||||||
|
quantity_major_number = $7,
|
||||||
|
created_time = $8,
|
||||||
|
bill_id = $9,
|
||||||
|
price_per_unit_minor = $10 ,
|
||||||
|
price_per_unit_major = $11,
|
||||||
|
price_per_unit_currency = $12,
|
||||||
|
deleted = $13;",
|
||||||
|
version,
|
||||||
|
view.product_name,
|
||||||
|
view.product_id,
|
||||||
|
view.quantity_minor_unit,
|
||||||
|
view.quantity_minor_number,
|
||||||
|
view.quantity_major_unit,
|
||||||
|
view.quantity_major_number,
|
||||||
|
view.created_time,
|
||||||
|
view.bill_id,
|
||||||
|
view.price_per_unit_minor,
|
||||||
|
view.price_per_unit_major,
|
||||||
|
view.price_per_unit_currency,
|
||||||
|
view.deleted,
|
||||||
|
)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Query<LineItem> for BillingDBPostgresAdapter {
|
||||||
|
async fn dispatch(&self, line_item_id: &str, events: &[EventEnvelope<LineItem>]) {
|
||||||
|
let res = self
|
||||||
|
.load_with_context(line_item_id)
|
||||||
|
.await
|
||||||
|
.unwrap_or_else(|_| {
|
||||||
|
Some((
|
||||||
|
LineItemView::default(),
|
||||||
|
ViewContext::new(line_item_id.into(), 0),
|
||||||
|
))
|
||||||
|
});
|
||||||
|
let (mut view, view_context): (LineItemView, ViewContext) = res.unwrap();
|
||||||
|
for event in events {
|
||||||
|
view.update(event);
|
||||||
|
}
|
||||||
|
self.update_view(view, view_context).await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use postgres_es::PostgresCqrs;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
billing::{
|
||||||
|
application::services::{
|
||||||
|
add_line_item_service::AddLineItemServiceBuilder, delete_line_item_service::*,
|
||||||
|
update_line_item_service::*, MockBillingServicesInterface,
|
||||||
|
},
|
||||||
|
domain::{
|
||||||
|
add_line_item_command::*, bill_aggregate::Bill, commands::BillingCommand,
|
||||||
|
delete_line_item_command::DeleteLineItemCommandBuilder,
|
||||||
|
update_line_item_command::*,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
db::migrate::*,
|
||||||
|
tests::bdd::*,
|
||||||
|
types::quantity::*,
|
||||||
|
utils::{
|
||||||
|
random_string::GenerateRandomStringInterface,
|
||||||
|
uuid::{tests::UUID, *},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn pg_query_billing_line_item_view() {
|
||||||
|
let settings = crate::settings::tests::get_settings().await;
|
||||||
|
//let settings = crate::settings::Settings::new().unwrap();
|
||||||
|
settings.create_db().await;
|
||||||
|
|
||||||
|
let db = crate::db::sqlx_postgres::Postgres::init(&settings.database.url).await;
|
||||||
|
db.migrate().await;
|
||||||
|
let db = BillingDBPostgresAdapter::new(db.pool.clone());
|
||||||
|
|
||||||
|
let queries: Vec<Box<dyn Query<LineItem>>> = vec![Box::new(db.clone())];
|
||||||
|
|
||||||
|
let mut mock_services = MockBillingServicesInterface::new();
|
||||||
|
|
||||||
|
let bill = Bill::default();
|
||||||
|
crate::billing::adapters::output::db::postgres::bill_id_exists::tests::create_dummy_bill(
|
||||||
|
&bill, &db,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let db2 = db.clone();
|
||||||
|
mock_services
|
||||||
|
.expect_add_line_item()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.returning(move || {
|
||||||
|
Arc::new(
|
||||||
|
AddLineItemServiceBuilder::default()
|
||||||
|
.db_line_item_id_exists(Arc::new(db2.clone()))
|
||||||
|
.db_bill_id_exists(Arc::new(db2.clone()))
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
let db2 = db.clone();
|
||||||
|
mock_services
|
||||||
|
.expect_update_line_item()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.returning(move || {
|
||||||
|
Arc::new(
|
||||||
|
UpdateLineItemServiceBuilder::default()
|
||||||
|
.db_line_item_id_exists(Arc::new(db2.clone()))
|
||||||
|
.db_bill_id_exists(Arc::new(db2.clone()))
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
let db2 = db.clone();
|
||||||
|
mock_services
|
||||||
|
.expect_delete_line_item()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.returning(move || {
|
||||||
|
Arc::new(
|
||||||
|
DeleteLineItemServiceBuilder::default()
|
||||||
|
.db_line_item_id_exists(Arc::new(db2.clone()))
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
let (cqrs, line_item_query): (
|
||||||
|
Arc<PostgresCqrs<LineItem>>,
|
||||||
|
Arc<dyn ViewRepository<LineItemView, LineItem>>,
|
||||||
|
) = (
|
||||||
|
Arc::new(postgres_es::postgres_cqrs(
|
||||||
|
db.pool.clone(),
|
||||||
|
queries,
|
||||||
|
Arc::new(mock_services),
|
||||||
|
)),
|
||||||
|
Arc::new(db.clone()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let rand = crate::utils::random_string::GenerateRandomString {};
|
||||||
|
let uuid = GenerateUUID {};
|
||||||
|
let line_item_id = uuid.get_uuid();
|
||||||
|
|
||||||
|
let cmd = AddLineItemCommandBuilder::default()
|
||||||
|
.product_name(rand.get_random(10))
|
||||||
|
.adding_by(UUID)
|
||||||
|
.price_per_unit(Price::default())
|
||||||
|
.quantity(Quantity::get_quantity())
|
||||||
|
.product_id(UUID)
|
||||||
|
.bill_id(*bill.bill_id())
|
||||||
|
.line_item_id(line_item_id)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
cqrs.execute(
|
||||||
|
&cmd.line_item_id().to_string(),
|
||||||
|
BillingCommand::AddLineItem(cmd.clone()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let line_item = line_item_query
|
||||||
|
.load(&(*cmd.line_item_id()).to_string())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let line_item: LineItem = line_item.into();
|
||||||
|
assert_eq!(line_item.line_item_id(), cmd.line_item_id());
|
||||||
|
assert_eq!(line_item.product_name(), cmd.product_name());
|
||||||
|
assert_eq!(line_item.product_id(), cmd.product_id());
|
||||||
|
assert_eq!(line_item.quantity(), cmd.quantity());
|
||||||
|
assert!(!line_item.deleted());
|
||||||
|
|
||||||
|
let update_line_item_cmd = UnvalidatedUpdateLineItemCommandBuilder::default()
|
||||||
|
.product_name(rand.get_random(10))
|
||||||
|
.adding_by(UUID)
|
||||||
|
.quantity(Quantity::get_quantity())
|
||||||
|
.product_id(UUID)
|
||||||
|
.bill_id(*bill.bill_id())
|
||||||
|
.old_line_item(line_item.clone())
|
||||||
|
.price_per_unit(Price::default())
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
.validate()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
cqrs.execute(
|
||||||
|
&cmd.line_item_id().to_string(),
|
||||||
|
BillingCommand::UpdateLineItem(update_line_item_cmd.clone()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let line_item = line_item_query
|
||||||
|
.load(&(*cmd.line_item_id()).to_string())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let line_item: LineItem = line_item.into();
|
||||||
|
assert_eq!(
|
||||||
|
line_item.line_item_id(),
|
||||||
|
update_line_item_cmd.old_line_item().line_item_id()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
line_item.product_name(),
|
||||||
|
update_line_item_cmd.product_name()
|
||||||
|
);
|
||||||
|
assert_eq!(line_item.product_id(), update_line_item_cmd.product_id());
|
||||||
|
assert_eq!(line_item.quantity(), update_line_item_cmd.quantity());
|
||||||
|
assert!(!line_item.deleted());
|
||||||
|
|
||||||
|
// delete
|
||||||
|
let delete_line_item_cmd = DeleteLineItemCommandBuilder::default()
|
||||||
|
.line_item(line_item.clone())
|
||||||
|
.adding_by(UUID)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
cqrs.execute(
|
||||||
|
&cmd.line_item_id().to_string(),
|
||||||
|
BillingCommand::DeleteLineItem(delete_line_item_cmd.clone()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let deleted_line_item = line_item_query
|
||||||
|
.load(&(*cmd.line_item_id()).to_string())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let deleted_line_item: LineItem = deleted_line_item.into();
|
||||||
|
assert_eq!(
|
||||||
|
deleted_line_item.line_item_id(),
|
||||||
|
delete_line_item_cmd.line_item().line_item_id()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
deleted_line_item.product_name(),
|
||||||
|
delete_line_item_cmd.line_item().product_name()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
deleted_line_item.product_id(),
|
||||||
|
delete_line_item_cmd.line_item().product_id()
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
deleted_line_item.quantity(),
|
||||||
|
delete_line_item_cmd.line_item().quantity()
|
||||||
|
);
|
||||||
|
assert!(deleted_line_item.deleted());
|
||||||
|
|
||||||
|
settings.drop_db().await;
|
||||||
|
}
|
||||||
|
}
|
34
src/billing/adapters/output/db/postgres/mod.rs
Normal file
34
src/billing/adapters/output/db/postgres/mod.rs
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use sqlx::postgres::PgPool;
|
||||||
|
|
||||||
|
use crate::db::{migrate::RunMigrations, sqlx_postgres::Postgres};
|
||||||
|
|
||||||
|
mod bill_id_exists;
|
||||||
|
mod bill_view;
|
||||||
|
mod errors;
|
||||||
|
mod get_line_items_for_bill_id;
|
||||||
|
mod line_item_id_exists;
|
||||||
|
mod line_item_view;
|
||||||
|
mod next_token_id;
|
||||||
|
mod store_id_exists;
|
||||||
|
mod store_name_exists;
|
||||||
|
mod store_view;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct BillingDBPostgresAdapter {
|
||||||
|
pool: PgPool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BillingDBPostgresAdapter {
|
||||||
|
pub fn new(pool: PgPool) -> Self {
|
||||||
|
Self { pool }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn migratable(&self) -> Arc<dyn RunMigrations> {
|
||||||
|
Arc::new(Postgres::new(self.pool.clone()))
|
||||||
|
}
|
||||||
|
}
|
111
src/billing/adapters/output/db/postgres/next_token_id.rs
Normal file
111
src/billing/adapters/output/db/postgres/next_token_id.rs
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::BillingDBPostgresAdapter;
|
||||||
|
use crate::billing::application::port::output::db::{errors::*, next_token_id::*};
|
||||||
|
|
||||||
|
struct TokenNumber {
|
||||||
|
token_number: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl NextTokenIDDBPort for BillingDBPostgresAdapter {
|
||||||
|
/// delete record for store_id, so that when next_token_id is called, it'll create and set it
|
||||||
|
/// to 1
|
||||||
|
async fn reset(&self, store_id: &Uuid) -> BillingDBResult<()> {
|
||||||
|
sqlx::query!(
|
||||||
|
"DELETE FROM cqrs_billing_bill_next_token_id WHERE store_id = $1",
|
||||||
|
store_id
|
||||||
|
)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// increment token ID and return pre-increment value
|
||||||
|
async fn next_token_id(&self, store_id: &Uuid) -> BillingDBResult<usize> {
|
||||||
|
let res = sqlx::query!(
|
||||||
|
"SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM cqrs_billing_bill_next_token_id
|
||||||
|
WHERE
|
||||||
|
store_id = $1
|
||||||
|
);",
|
||||||
|
store_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
if let Some(true) = res.exists {
|
||||||
|
let res = sqlx::query_as!(
|
||||||
|
TokenNumber,
|
||||||
|
"SELECT
|
||||||
|
token_number
|
||||||
|
FROM
|
||||||
|
cqrs_billing_bill_next_token_id
|
||||||
|
WHERE
|
||||||
|
store_id = $1;",
|
||||||
|
store_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
sqlx::query!(
|
||||||
|
"UPDATE cqrs_billing_bill_next_token_id
|
||||||
|
SET
|
||||||
|
token_number = $1
|
||||||
|
WHERE
|
||||||
|
store_id = $2",
|
||||||
|
res.token_number + 1,
|
||||||
|
store_id
|
||||||
|
)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(res.token_number as usize + 1)
|
||||||
|
} else {
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO cqrs_billing_bill_next_token_id
|
||||||
|
(token_number, store_id)
|
||||||
|
VALUES
|
||||||
|
($1, $2);",
|
||||||
|
1,
|
||||||
|
store_id
|
||||||
|
)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
// use crate::billing::domain::add_product_command::tests::get_customizations;
|
||||||
|
use crate::{billing::domain::bill_aggregate::*, utils::uuid::tests::UUID};
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_postgres_next_token_id() {
|
||||||
|
let settings = crate::settings::tests::get_settings().await;
|
||||||
|
settings.create_db().await;
|
||||||
|
let db = super::BillingDBPostgresAdapter::new(
|
||||||
|
sqlx::postgres::PgPool::connect(&settings.database.url)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let store_id = UUID;
|
||||||
|
|
||||||
|
assert_eq!(db.next_token_id(&store_id).await.unwrap(), 1);
|
||||||
|
assert_eq!(db.next_token_id(&store_id).await.unwrap(), 2);
|
||||||
|
assert!(db.reset(&store_id).await.is_ok());
|
||||||
|
assert_eq!(db.next_token_id(&store_id).await.unwrap(), 1);
|
||||||
|
|
||||||
|
settings.drop_db().await;
|
||||||
|
}
|
||||||
|
}
|
87
src/billing/adapters/output/db/postgres/store_id_exists.rs
Normal file
87
src/billing/adapters/output/db/postgres/store_id_exists.rs
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::BillingDBPostgresAdapter;
|
||||||
|
use crate::billing::application::port::output::db::{errors::*, store_id_exists::*};
|
||||||
|
use crate::billing::domain::store_aggregate::*;
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl StoreIDExistsDBPort for BillingDBPostgresAdapter {
|
||||||
|
async fn store_id_exists(&self, store_id: &Uuid) -> BillingDBResult<bool> {
|
||||||
|
let res = sqlx::query!(
|
||||||
|
"SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM cqrs_billing_store_query
|
||||||
|
WHERE
|
||||||
|
store_id = $1
|
||||||
|
);",
|
||||||
|
store_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
if let Some(x) = res.exists {
|
||||||
|
Ok(x)
|
||||||
|
} else {
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::utils::uuid::tests::UUID;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub async fn create_dummy_store_record(s: &Store, db: &BillingDBPostgresAdapter) {
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO cqrs_billing_store_query
|
||||||
|
(version, name, address, store_id, owner, deleted)
|
||||||
|
VALUES ($1, $2, $3, $4, $5 ,$6);",
|
||||||
|
1,
|
||||||
|
s.name(),
|
||||||
|
s.address().as_ref().map(|s| s.as_str()),
|
||||||
|
s.store_id(),
|
||||||
|
s.owner(),
|
||||||
|
false
|
||||||
|
)
|
||||||
|
.execute(&db.pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_postgres_store_exists() {
|
||||||
|
let store_id = Uuid::new_v4();
|
||||||
|
let settings = crate::settings::tests::get_settings().await;
|
||||||
|
settings.create_db().await;
|
||||||
|
let db = super::BillingDBPostgresAdapter::new(
|
||||||
|
sqlx::postgres::PgPool::connect(&settings.database.url)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let store = StoreBuilder::default()
|
||||||
|
.name("store_name".into())
|
||||||
|
.owner(UUID)
|
||||||
|
.address(Some("store_address".into()))
|
||||||
|
.store_id(store_id)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// state doesn't exist
|
||||||
|
assert!(!db.store_id_exists(store.store_id()).await.unwrap());
|
||||||
|
|
||||||
|
create_dummy_store_record(&store, &db).await;
|
||||||
|
|
||||||
|
// state exists
|
||||||
|
assert!(db.store_id_exists(store.store_id()).await.unwrap());
|
||||||
|
|
||||||
|
settings.drop_db().await;
|
||||||
|
}
|
||||||
|
}
|
81
src/billing/adapters/output/db/postgres/store_name_exists.rs
Normal file
81
src/billing/adapters/output/db/postgres/store_name_exists.rs
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use super::BillingDBPostgresAdapter;
|
||||||
|
use crate::billing::application::port::output::db::{errors::*, store_name_exists::*};
|
||||||
|
use crate::billing::domain::store_aggregate::*;
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl StoreNameExistsDBPort for BillingDBPostgresAdapter {
|
||||||
|
async fn store_name_exists(&self, s: &Store) -> BillingDBResult<bool> {
|
||||||
|
let res = sqlx::query!(
|
||||||
|
"SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM cqrs_billing_store_query
|
||||||
|
WHERE
|
||||||
|
name = $1
|
||||||
|
AND
|
||||||
|
deleted = false
|
||||||
|
);",
|
||||||
|
s.name(),
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
if let Some(x) = res.exists {
|
||||||
|
Ok(x)
|
||||||
|
} else {
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::utils::uuid::tests::UUID;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::billing::adapters::output::db::postgres::store_id_exists::tests::create_dummy_store_record;
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_postgres_store_exists() {
|
||||||
|
let store_id = Uuid::new_v4();
|
||||||
|
let settings = crate::settings::tests::get_settings().await;
|
||||||
|
settings.create_db().await;
|
||||||
|
let db = super::BillingDBPostgresAdapter::new(
|
||||||
|
sqlx::postgres::PgPool::connect(&settings.database.url)
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let store = StoreBuilder::default()
|
||||||
|
.name("store_name".into())
|
||||||
|
.owner(UUID)
|
||||||
|
.address(Some("store_address".into()))
|
||||||
|
.store_id(store_id)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// state doesn't exist
|
||||||
|
assert!(!db.store_name_exists(&store).await.unwrap());
|
||||||
|
|
||||||
|
create_dummy_store_record(&store, &db).await;
|
||||||
|
|
||||||
|
// state exists
|
||||||
|
assert!(db.store_name_exists(&store).await.unwrap());
|
||||||
|
|
||||||
|
// Set store.deleted = true; now db.store_name_exists must return false
|
||||||
|
sqlx::query!(
|
||||||
|
"UPDATE cqrs_billing_store_query SET deleted = true WHERE store_id = $1;",
|
||||||
|
store.store_id()
|
||||||
|
)
|
||||||
|
.execute(&db.pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert!(!db.store_name_exists(&store).await.unwrap());
|
||||||
|
|
||||||
|
settings.drop_db().await;
|
||||||
|
}
|
||||||
|
}
|
353
src/billing/adapters/output/db/postgres/store_view.rs
Normal file
353
src/billing/adapters/output/db/postgres/store_view.rs
Normal file
|
@ -0,0 +1,353 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use cqrs_es::persist::{PersistenceError, ViewContext, ViewRepository};
|
||||||
|
use cqrs_es::{EventEnvelope, Query, View};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
use super::BillingDBPostgresAdapter;
|
||||||
|
use crate::billing::domain::events::BillingEvent;
|
||||||
|
use crate::billing::domain::store_aggregate::*;
|
||||||
|
use crate::utils::parse_aggregate_id::parse_aggregate_id;
|
||||||
|
|
||||||
|
pub const NEW_STORE_NON_UUID: &str = "billing_new_store_non_uuid-asdfa";
|
||||||
|
|
||||||
|
// The view for a Store query, for a standard http application this should
|
||||||
|
// be designed to reflect the response dto that will be returned to a user.
|
||||||
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
||||||
|
pub struct StoreView {
|
||||||
|
name: String,
|
||||||
|
address: Option<String>,
|
||||||
|
store_id: Uuid,
|
||||||
|
owner: Uuid,
|
||||||
|
deleted: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<StoreView> for Store {
|
||||||
|
fn from(value: StoreView) -> Self {
|
||||||
|
StoreBuilder::default()
|
||||||
|
.name(value.name)
|
||||||
|
.address(value.address)
|
||||||
|
.store_id(value.store_id)
|
||||||
|
.owner(value.owner)
|
||||||
|
.deleted(value.deleted)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This updates the view with events as they are committed.
|
||||||
|
// The logic should be minimal here, e.g., don't calculate the account balance,
|
||||||
|
// design the events to carry the balance information instead.
|
||||||
|
impl View<Store> for StoreView {
|
||||||
|
fn update(&mut self, event: &EventEnvelope<Store>) {
|
||||||
|
match &event.payload {
|
||||||
|
BillingEvent::StoreAdded(val) => {
|
||||||
|
self.name = val.name().into();
|
||||||
|
self.address = val.address().clone();
|
||||||
|
self.store_id = *val.store_id();
|
||||||
|
self.owner = *val.owner();
|
||||||
|
self.deleted = false;
|
||||||
|
}
|
||||||
|
BillingEvent::StoreUpdated(e) => {
|
||||||
|
let val = e.new_store();
|
||||||
|
self.name = val.name().into();
|
||||||
|
self.address = val.address().clone();
|
||||||
|
self.store_id = *val.store_id();
|
||||||
|
self.owner = *val.owner();
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl ViewRepository<StoreView, Store> for BillingDBPostgresAdapter {
|
||||||
|
async fn load(&self, store_id: &str) -> Result<Option<StoreView>, PersistenceError> {
|
||||||
|
let store_id = match parse_aggregate_id(store_id, NEW_STORE_NON_UUID)? {
|
||||||
|
Some((val, _)) => return Ok(Some(val)),
|
||||||
|
None => Uuid::parse_str(store_id).unwrap(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = sqlx::query_as!(
|
||||||
|
StoreView,
|
||||||
|
"SELECT
|
||||||
|
name, address, store_id, owner, deleted
|
||||||
|
FROM
|
||||||
|
cqrs_billing_store_query
|
||||||
|
WHERE
|
||||||
|
store_id = $1;",
|
||||||
|
store_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
Ok(Some(res))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn load_with_context(
|
||||||
|
&self,
|
||||||
|
store_id: &str,
|
||||||
|
) -> Result<Option<(StoreView, ViewContext)>, PersistenceError> {
|
||||||
|
let store_id = match parse_aggregate_id(store_id, NEW_STORE_NON_UUID)? {
|
||||||
|
Some(val) => return Ok(Some(val)),
|
||||||
|
None => Uuid::parse_str(store_id).unwrap(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = sqlx::query_as!(
|
||||||
|
StoreView,
|
||||||
|
"SELECT
|
||||||
|
name, address, store_id, owner, deleted
|
||||||
|
FROM
|
||||||
|
cqrs_billing_store_query
|
||||||
|
WHERE
|
||||||
|
store_id = $1;",
|
||||||
|
&store_id,
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
|
||||||
|
struct Context {
|
||||||
|
version: i64,
|
||||||
|
store_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
let ctx = sqlx::query_as!(
|
||||||
|
Context,
|
||||||
|
"SELECT
|
||||||
|
store_id, version
|
||||||
|
FROM
|
||||||
|
cqrs_billing_store_query
|
||||||
|
WHERE
|
||||||
|
store_id = $1;",
|
||||||
|
store_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
|
||||||
|
let view_context = ViewContext::new(ctx.store_id.to_string(), ctx.version);
|
||||||
|
Ok(Some((res, view_context)))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_view(
|
||||||
|
&self,
|
||||||
|
view: StoreView,
|
||||||
|
context: ViewContext,
|
||||||
|
) -> Result<(), PersistenceError> {
|
||||||
|
match context.version {
|
||||||
|
0 => {
|
||||||
|
let version = context.version + 1;
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO cqrs_billing_store_query (
|
||||||
|
version, name, address, store_id, owner, deleted
|
||||||
|
) VALUES (
|
||||||
|
$1, $2, $3, $4, $5, $6
|
||||||
|
);",
|
||||||
|
version,
|
||||||
|
view.name,
|
||||||
|
view.address,
|
||||||
|
view.store_id,
|
||||||
|
view.owner,
|
||||||
|
view.deleted,
|
||||||
|
)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let version = context.version + 1;
|
||||||
|
sqlx::query!(
|
||||||
|
"UPDATE
|
||||||
|
cqrs_billing_store_query
|
||||||
|
SET
|
||||||
|
version = $1,
|
||||||
|
name = $2,
|
||||||
|
address = $3,
|
||||||
|
owner = $4,
|
||||||
|
deleted = $5;",
|
||||||
|
version,
|
||||||
|
view.name,
|
||||||
|
view.address,
|
||||||
|
view.owner,
|
||||||
|
view.deleted,
|
||||||
|
)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(PostgresAggregateError::from)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SimpleLoggingQuery {}
|
||||||
|
|
||||||
|
// Our simplest query, this is great for debugging but absolutely useless in production.
|
||||||
|
// This query just pretty prints the events as they are processed.
|
||||||
|
#[async_trait]
|
||||||
|
impl Query<Store> for SimpleLoggingQuery {
|
||||||
|
async fn dispatch(&self, aggregate_id: &str, events: &[EventEnvelope<Store>]) {
|
||||||
|
for event in events {
|
||||||
|
let payload = serde_json::to_string_pretty(&event.payload).unwrap();
|
||||||
|
println!("{}-{}\n{}", aggregate_id, event.sequence, payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Query<Store> for BillingDBPostgresAdapter {
|
||||||
|
async fn dispatch(&self, store_id: &str, events: &[EventEnvelope<Store>]) {
|
||||||
|
let res = self
|
||||||
|
.load_with_context(store_id)
|
||||||
|
.await
|
||||||
|
.unwrap_or_else(|_| Some((StoreView::default(), ViewContext::new(store_id.into(), 0))));
|
||||||
|
let (mut view, view_context): (StoreView, ViewContext) = res.unwrap();
|
||||||
|
for event in events {
|
||||||
|
view.update(event);
|
||||||
|
}
|
||||||
|
self.update_view(view, view_context).await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use postgres_es::PostgresCqrs;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
billing::{
|
||||||
|
application::services::{
|
||||||
|
add_store_service::AddStoreServiceBuilder, update_store_service::*,
|
||||||
|
MockBillingServicesInterface,
|
||||||
|
},
|
||||||
|
domain::add_store_command::*,
|
||||||
|
domain::commands::BillingCommand,
|
||||||
|
domain::update_store_command::*,
|
||||||
|
},
|
||||||
|
db::migrate::*,
|
||||||
|
tests::bdd::*,
|
||||||
|
utils::{random_string::GenerateRandomStringInterface, uuid::tests::UUID},
|
||||||
|
};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn pg_query_billing_store_view() {
|
||||||
|
let settings = crate::settings::tests::get_settings().await;
|
||||||
|
//let settings = crate::settings::Settings::new().unwrap();
|
||||||
|
settings.create_db().await;
|
||||||
|
|
||||||
|
let db = crate::db::sqlx_postgres::Postgres::init(&settings.database.url).await;
|
||||||
|
db.migrate().await;
|
||||||
|
let db = BillingDBPostgresAdapter::new(db.pool.clone());
|
||||||
|
|
||||||
|
let simple_query = SimpleLoggingQuery {};
|
||||||
|
|
||||||
|
let queries: Vec<Box<dyn Query<Store>>> =
|
||||||
|
vec![Box::new(simple_query), Box::new(db.clone())];
|
||||||
|
|
||||||
|
let mut mock_services = MockBillingServicesInterface::new();
|
||||||
|
|
||||||
|
let db2 = db.clone();
|
||||||
|
mock_services
|
||||||
|
.expect_add_store()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.returning(move || {
|
||||||
|
Arc::new(
|
||||||
|
AddStoreServiceBuilder::default()
|
||||||
|
.db_store_id_exists(Arc::new(db2.clone()))
|
||||||
|
.db_store_name_exists(Arc::new(db2.clone()))
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
let db2 = db.clone();
|
||||||
|
mock_services
|
||||||
|
.expect_update_store()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.returning(move || {
|
||||||
|
Arc::new(
|
||||||
|
UpdateStoreServiceBuilder::default()
|
||||||
|
.db_store_id_exists(Arc::new(db2.clone()))
|
||||||
|
.db_store_name_exists(Arc::new(db2.clone()))
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
let (cqrs, store_query): (
|
||||||
|
Arc<PostgresCqrs<Store>>,
|
||||||
|
Arc<dyn ViewRepository<StoreView, Store>>,
|
||||||
|
) = (
|
||||||
|
Arc::new(postgres_es::postgres_cqrs(
|
||||||
|
db.pool.clone(),
|
||||||
|
queries,
|
||||||
|
Arc::new(mock_services),
|
||||||
|
)),
|
||||||
|
Arc::new(db.clone()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let rand = crate::utils::random_string::GenerateRandomString {};
|
||||||
|
let cmd = AddStoreCommandBuilder::default()
|
||||||
|
.name(rand.get_random(10))
|
||||||
|
.address(None)
|
||||||
|
.owner(UUID)
|
||||||
|
.store_id(UUID)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
cqrs.execute(
|
||||||
|
&cmd.store_id().to_string(),
|
||||||
|
BillingCommand::AddStore(cmd.clone()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let store = store_query
|
||||||
|
.load(&(*cmd.store_id()).to_string())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let store: Store = store.into();
|
||||||
|
assert_eq!(store.name(), cmd.name());
|
||||||
|
assert_eq!(store.address(), cmd.address());
|
||||||
|
assert_eq!(store.owner(), cmd.owner());
|
||||||
|
assert_eq!(store.store_id(), cmd.store_id());
|
||||||
|
assert!(!store.deleted());
|
||||||
|
|
||||||
|
let update_store_cmd = UpdateStoreCommand::new(
|
||||||
|
rand.get_random(10),
|
||||||
|
Some(rand.get_random(10)),
|
||||||
|
UUID,
|
||||||
|
store,
|
||||||
|
UUID,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
cqrs.execute(
|
||||||
|
&cmd.store_id().to_string(),
|
||||||
|
BillingCommand::UpdateStore(update_store_cmd.clone()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let store = store_query
|
||||||
|
.load(&(*cmd.store_id()).to_string())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
let store: Store = store.into();
|
||||||
|
assert_eq!(store.name(), update_store_cmd.name());
|
||||||
|
assert_eq!(store.address(), update_store_cmd.address());
|
||||||
|
assert_eq!(store.owner(), update_store_cmd.owner());
|
||||||
|
assert_eq!(store.store_id(), update_store_cmd.old_store().store_id());
|
||||||
|
assert!(!store.deleted());
|
||||||
|
|
||||||
|
settings.drop_db().await;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
mod db;
|
||||||
|
|
|
@ -2,5 +2,5 @@
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
mod port;
|
pub mod port;
|
||||||
mod services;
|
pub mod services;
|
||||||
|
|
|
@ -2,5 +2,5 @@
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
mod input;
|
pub mod input;
|
||||||
mod output;
|
pub mod output;
|
||||||
|
|
53
src/billing/application/port/output/db/bill_id_exists.rs
Normal file
53
src/billing/application/port/output/db/bill_id_exists.rs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
#[cfg(test)]
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
pub use tests::*;
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait BillIDExistsDBPort: Send + Sync {
|
||||||
|
async fn bill_id_exists(&self, c: &Uuid) -> BillingDBResult<bool>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type BillIDExistsDBPortObj = std::sync::Arc<dyn BillIDExistsDBPort>;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub fn mock_bill_id_exists_db_port_false(times: Option<usize>) -> BillIDExistsDBPortObj {
|
||||||
|
let mut m = MockBillIDExistsDBPort::new();
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_bill_id_exists()
|
||||||
|
.times(times)
|
||||||
|
.returning(|_| Ok(false));
|
||||||
|
} else {
|
||||||
|
m.expect_bill_id_exists().returning(|_| Ok(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mock_bill_id_exists_db_port_true(times: Option<usize>) -> BillIDExistsDBPortObj {
|
||||||
|
let mut m = MockBillIDExistsDBPort::new();
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_bill_id_exists()
|
||||||
|
.times(times)
|
||||||
|
.returning(|_| Ok(true));
|
||||||
|
} else {
|
||||||
|
m.expect_bill_id_exists().returning(|_| Ok(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
}
|
19
src/billing/application/port/output/db/errors.rs
Normal file
19
src/billing/application/port/output/db/errors.rs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_more::Display;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
pub type BillingDBResult<V> = Result<V, BillingDBError>;
|
||||||
|
|
||||||
|
#[derive(Debug, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub enum BillingDBError {
|
||||||
|
DuplicateBillID,
|
||||||
|
DuplicateStoreName,
|
||||||
|
DuplicateStoreID,
|
||||||
|
StoreIDNotFound,
|
||||||
|
DuplicateLineItemID,
|
||||||
|
LineItemIDNotFound,
|
||||||
|
InternalError,
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::billing::domain::line_item_aggregate::LineItem;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
#[cfg(test)]
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
pub use tests::*;
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait GetLineItemsForBillIDDBPort: Send + Sync {
|
||||||
|
async fn get_line_items_for_bill_id(&self, bill_id: Uuid) -> BillingDBResult<Vec<LineItem>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type GetLineItemsForBillIDDBPortObj = std::sync::Arc<dyn GetLineItemsForBillIDDBPort>;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub fn mock_get_line_items_for_bill_id_db_port_no_line_items(
|
||||||
|
times: Option<usize>,
|
||||||
|
) -> GetLineItemsForBillIDDBPortObj {
|
||||||
|
let mut m = MockGetLineItemsForBillIDDBPort::new();
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_get_line_items_for_bill_id()
|
||||||
|
.times(times)
|
||||||
|
.returning(|_| Ok(Vec::default()));
|
||||||
|
} else {
|
||||||
|
m.expect_get_line_items_for_bill_id()
|
||||||
|
.returning(|_| Ok(Vec::default()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mock_get_line_items_for_bill_id_db_port_true(
|
||||||
|
times: Option<usize>,
|
||||||
|
) -> GetLineItemsForBillIDDBPortObj {
|
||||||
|
let mut m = MockGetLineItemsForBillIDDBPort::new();
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_get_line_items_for_bill_id()
|
||||||
|
.times(times)
|
||||||
|
.returning(|_| Ok(vec![LineItem::default()]));
|
||||||
|
} else {
|
||||||
|
m.expect_get_line_items_for_bill_id()
|
||||||
|
.returning(|_| Ok(vec![LineItem::default()]));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
#[cfg(test)]
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
pub use tests::*;
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait LineItemIDExistsDBPort: Send + Sync {
|
||||||
|
async fn line_item_id_exists(&self, line_item_id: &Uuid) -> BillingDBResult<bool>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type LineItemIDExistsDBPortObj = std::sync::Arc<dyn LineItemIDExistsDBPort>;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub fn mock_line_item_id_exists_db_port_false(
|
||||||
|
times: Option<usize>,
|
||||||
|
) -> LineItemIDExistsDBPortObj {
|
||||||
|
let mut m = MockLineItemIDExistsDBPort::new();
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_line_item_id_exists()
|
||||||
|
.times(times)
|
||||||
|
.returning(|_| Ok(false));
|
||||||
|
} else {
|
||||||
|
m.expect_line_item_id_exists().returning(|_| Ok(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mock_line_item_id_exists_db_port_true(
|
||||||
|
times: Option<usize>,
|
||||||
|
) -> LineItemIDExistsDBPortObj {
|
||||||
|
let mut m = MockLineItemIDExistsDBPort::new();
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_line_item_id_exists()
|
||||||
|
.times(times)
|
||||||
|
.returning(|_| Ok(true));
|
||||||
|
} else {
|
||||||
|
m.expect_line_item_id_exists().returning(|_| Ok(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
}
|
10
src/billing/application/port/output/db/mod.rs
Normal file
10
src/billing/application/port/output/db/mod.rs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
pub mod bill_id_exists;
|
||||||
|
pub mod errors;
|
||||||
|
pub mod get_line_items_for_bill_id;
|
||||||
|
pub mod line_item_id_exists;
|
||||||
|
pub mod next_token_id;
|
||||||
|
pub mod store_id_exists;
|
||||||
|
pub mod store_name_exists;
|
43
src/billing/application/port/output/db/next_token_id.rs
Normal file
43
src/billing/application/port/output/db/next_token_id.rs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
#[cfg(test)]
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
pub use tests::*;
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait NextTokenIDDBPort: Send + Sync {
|
||||||
|
/// delete record for store_id, so that when next_token_id is called, it'll create and set it
|
||||||
|
/// to 1
|
||||||
|
async fn reset(&self, store_id: &Uuid) -> BillingDBResult<()>;
|
||||||
|
|
||||||
|
/// increment token ID and return pre-increment value
|
||||||
|
async fn next_token_id(&self, store_id: &Uuid) -> BillingDBResult<usize>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type NextTokenIDDBPortObj = std::sync::Arc<dyn NextTokenIDDBPort>;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub fn mock_next_token_id_db_port(times: Option<usize>) -> NextTokenIDDBPortObj {
|
||||||
|
let mut m = MockNextTokenIDDBPort::new();
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_next_token_id().times(times).returning(|_| Ok(1));
|
||||||
|
} else {
|
||||||
|
m.expect_next_token_id().returning(|_| Ok(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
}
|
55
src/billing/application/port/output/db/store_id_exists.rs
Normal file
55
src/billing/application/port/output/db/store_id_exists.rs
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::billing::domain::store_aggregate::Store;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
#[cfg(test)]
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
pub use tests::*;
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait StoreIDExistsDBPort: Send + Sync {
|
||||||
|
async fn store_id_exists(&self, store_id: &Uuid) -> BillingDBResult<bool>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type StoreIDExistsDBPortObj = std::sync::Arc<dyn StoreIDExistsDBPort>;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub fn mock_store_id_exists_db_port_false(times: Option<usize>) -> StoreIDExistsDBPortObj {
|
||||||
|
let mut m = MockStoreIDExistsDBPort::new();
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_store_id_exists()
|
||||||
|
.times(times)
|
||||||
|
.returning(|_| Ok(false));
|
||||||
|
} else {
|
||||||
|
m.expect_store_id_exists().returning(|_| Ok(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mock_store_id_exists_db_port_true(times: Option<usize>) -> StoreIDExistsDBPortObj {
|
||||||
|
let mut m = MockStoreIDExistsDBPort::new();
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_store_id_exists()
|
||||||
|
.times(times)
|
||||||
|
.returning(|_| Ok(true));
|
||||||
|
} else {
|
||||||
|
m.expect_store_id_exists().returning(|_| Ok(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
}
|
54
src/billing/application/port/output/db/store_name_exists.rs
Normal file
54
src/billing/application/port/output/db/store_name_exists.rs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
|
||||||
|
use crate::billing::domain::store_aggregate::Store;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
#[cfg(test)]
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
pub use tests::*;
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait StoreNameExistsDBPort: Send + Sync {
|
||||||
|
async fn store_name_exists(&self, s: &Store) -> BillingDBResult<bool>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type StoreNameExistsDBPortObj = std::sync::Arc<dyn StoreNameExistsDBPort>;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub fn mock_store_name_exists_db_port_false(times: Option<usize>) -> StoreNameExistsDBPortObj {
|
||||||
|
let mut m = MockStoreNameExistsDBPort::new();
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_store_name_exists()
|
||||||
|
.times(times)
|
||||||
|
.returning(|_| Ok(false));
|
||||||
|
} else {
|
||||||
|
m.expect_store_name_exists().returning(|_| Ok(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mock_store_name_exists_db_port_true(times: Option<usize>) -> StoreNameExistsDBPortObj {
|
||||||
|
let mut m = MockStoreNameExistsDBPort::new();
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_store_name_exists()
|
||||||
|
.times(times)
|
||||||
|
.returning(|_| Ok(true));
|
||||||
|
} else {
|
||||||
|
m.expect_store_name_exists().returning(|_| Ok(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
pub mod db;
|
||||||
|
|
115
src/billing/application/services/add_bill_service.rs
Normal file
115
src/billing/application/services/add_bill_service.rs
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
use crate::billing::{
|
||||||
|
application::port::output::db::{bill_id_exists::*, next_token_id::*},
|
||||||
|
domain::{
|
||||||
|
add_bill_command::AddBillCommand,
|
||||||
|
bill_added_event::{BillAddedEvent, BillAddedEventBuilder},
|
||||||
|
bill_aggregate::*,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait AddBillUseCase: Send + Sync {
|
||||||
|
async fn add_bill(&self, cmd: AddBillCommand) -> BillingResult<BillAddedEvent>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type AddBillServiceObj = Arc<dyn AddBillUseCase>;
|
||||||
|
|
||||||
|
#[derive(Clone, Builder)]
|
||||||
|
pub struct AddBillService {
|
||||||
|
db_bill_id_exists: BillIDExistsDBPortObj,
|
||||||
|
db_next_token_id: NextTokenIDDBPortObj,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl AddBillUseCase for AddBillService {
|
||||||
|
async fn add_bill(&self, cmd: AddBillCommand) -> BillingResult<BillAddedEvent> {
|
||||||
|
if self.db_bill_id_exists.bill_id_exists(cmd.bill_id()).await? {
|
||||||
|
return Err(BillingError::DuplicateBillID);
|
||||||
|
}
|
||||||
|
|
||||||
|
let token_number = self.db_next_token_id.next_token_id(cmd.store_id()).await?;
|
||||||
|
|
||||||
|
let bill = BillBuilder::default()
|
||||||
|
.bill_id(*cmd.bill_id())
|
||||||
|
.token_number(token_number)
|
||||||
|
.created_time(cmd.created_time().clone())
|
||||||
|
.store_id(*cmd.store_id())
|
||||||
|
.total_price(None)
|
||||||
|
.deleted(false)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(BillAddedEventBuilder::default()
|
||||||
|
.added_by_user(*cmd.adding_by())
|
||||||
|
.bill(bill)
|
||||||
|
.build()
|
||||||
|
.unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
// use crate::billing::domain::add_bill_command::tests::get_command;
|
||||||
|
use crate::utils::uuid::tests::UUID;
|
||||||
|
use crate::{tests::bdd::*, utils::uuid::tests::mock_get_uuid};
|
||||||
|
|
||||||
|
pub fn mock_add_bill_service(times: Option<usize>, cmd: AddBillCommand) -> AddBillServiceObj {
|
||||||
|
let mut m = MockAddBillUseCase::new();
|
||||||
|
|
||||||
|
let bill = BillBuilder::default()
|
||||||
|
.bill_id(*cmd.bill_id())
|
||||||
|
.token_number(1)
|
||||||
|
.total_price(None)
|
||||||
|
.created_time(cmd.created_time().clone())
|
||||||
|
.store_id(*cmd.store_id())
|
||||||
|
.deleted(false)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let res = BillAddedEventBuilder::default()
|
||||||
|
.added_by_user(*cmd.adding_by())
|
||||||
|
.bill(bill)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_add_bill()
|
||||||
|
.times(times)
|
||||||
|
.returning(move |_| Ok(res.clone()));
|
||||||
|
} else {
|
||||||
|
m.expect_add_bill().returning(move |_| Ok(res.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_bill_doesnt_exist() {
|
||||||
|
let cmd = AddBillCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = AddBillServiceBuilder::default()
|
||||||
|
.db_bill_id_exists(mock_bill_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.db_next_token_id(mock_next_token_id_db_port(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let res = s.add_bill(cmd.clone()).await.unwrap();
|
||||||
|
assert_eq!(res.bill().created_time(), cmd.created_time());
|
||||||
|
assert_eq!(res.bill().store_id(), cmd.store_id());
|
||||||
|
assert_eq!(res.added_by_user(), cmd.adding_by());
|
||||||
|
}
|
||||||
|
}
|
129
src/billing/application/services/add_line_item_service.rs
Normal file
129
src/billing/application/services/add_line_item_service.rs
Normal file
|
@ -0,0 +1,129 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
use crate::billing::{
|
||||||
|
application::port::output::db::bill_id_exists::*,
|
||||||
|
application::port::output::db::line_item_id_exists::*,
|
||||||
|
domain::{add_line_item_command::*, line_item_added_event::*, line_item_aggregate::*},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait AddLineItemUseCase: Send + Sync {
|
||||||
|
async fn add_line_item(&self, cmd: AddLineItemCommand) -> BillingResult<LineItemAddedEvent>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type AddLineItemServiceObj = Arc<dyn AddLineItemUseCase>;
|
||||||
|
|
||||||
|
#[derive(Clone, Builder)]
|
||||||
|
pub struct AddLineItemService {
|
||||||
|
db_line_item_id_exists: LineItemIDExistsDBPortObj,
|
||||||
|
db_bill_id_exists: BillIDExistsDBPortObj,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl AddLineItemUseCase for AddLineItemService {
|
||||||
|
async fn add_line_item(&self, cmd: AddLineItemCommand) -> BillingResult<LineItemAddedEvent> {
|
||||||
|
if !self.db_bill_id_exists.bill_id_exists(cmd.bill_id()).await? {
|
||||||
|
return Err(BillingError::BillIDNotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
if self
|
||||||
|
.db_line_item_id_exists
|
||||||
|
.line_item_id_exists(cmd.line_item_id())
|
||||||
|
.await?
|
||||||
|
{
|
||||||
|
return Err(BillingError::DuplicateLineItemID);
|
||||||
|
}
|
||||||
|
|
||||||
|
let line_item = LineItemBuilder::default()
|
||||||
|
.created_time(cmd.created_time().clone())
|
||||||
|
.product_name(cmd.product_name().into())
|
||||||
|
.product_id(*cmd.product_id())
|
||||||
|
.bill_id(*cmd.bill_id())
|
||||||
|
.line_item_id(*cmd.line_item_id())
|
||||||
|
.quantity(cmd.quantity().clone())
|
||||||
|
.price_per_unit(cmd.price_per_unit().clone())
|
||||||
|
.deleted(false)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(LineItemAddedEventBuilder::default()
|
||||||
|
.added_by_user(*cmd.adding_by())
|
||||||
|
.line_item(line_item)
|
||||||
|
.build()
|
||||||
|
.unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::billing::domain::line_item_added_event::tests::get_added_line_item_event_from_command;
|
||||||
|
use crate::tests::bdd::*;
|
||||||
|
|
||||||
|
pub fn mock_add_line_item_service(
|
||||||
|
times: Option<usize>,
|
||||||
|
cmd: AddLineItemCommand,
|
||||||
|
) -> AddLineItemServiceObj {
|
||||||
|
let mut m = MockAddLineItemUseCase::new();
|
||||||
|
|
||||||
|
let res = get_added_line_item_event_from_command(&cmd);
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_add_line_item()
|
||||||
|
.times(times)
|
||||||
|
.returning(move |_| Ok(res.clone()));
|
||||||
|
} else {
|
||||||
|
m.expect_add_line_item().returning(move |_| Ok(res.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service() {
|
||||||
|
let cmd = AddLineItemCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = AddLineItemServiceBuilder::default()
|
||||||
|
.db_line_item_id_exists(mock_line_item_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.db_bill_id_exists(mock_bill_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let res = s.add_line_item(cmd.clone()).await.unwrap();
|
||||||
|
assert_eq!(res.line_item().product_name(), cmd.product_name());
|
||||||
|
assert_eq!(res.line_item().product_id(), cmd.product_id());
|
||||||
|
assert_eq!(res.line_item().bill_id(), cmd.bill_id());
|
||||||
|
assert_eq!(res.line_item().quantity(), cmd.quantity());
|
||||||
|
assert_eq!(res.line_item().quantity(), cmd.quantity());
|
||||||
|
assert_eq!(res.line_item().created_time(), cmd.created_time());
|
||||||
|
assert!(!res.line_item().deleted());
|
||||||
|
assert_eq!(res.added_by_user(), cmd.adding_by());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_bill_id_doesnt_exist() {
|
||||||
|
let cmd = AddLineItemCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = AddLineItemServiceBuilder::default()
|
||||||
|
.db_line_item_id_exists(mock_line_item_id_exists_db_port_false(IS_NEVER_CALLED))
|
||||||
|
.db_bill_id_exists(mock_bill_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
s.add_line_item(cmd.clone()).await,
|
||||||
|
Err(BillingError::BillIDNotFound)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
152
src/billing/application/services/add_store_service.rs
Normal file
152
src/billing/application/services/add_store_service.rs
Normal file
|
@ -0,0 +1,152 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
use crate::billing::{
|
||||||
|
application::port::output::db::{store_id_exists::*, store_name_exists::*},
|
||||||
|
domain::{
|
||||||
|
add_store_command::*,
|
||||||
|
store_added_event::{StoreAddedEvent, StoreAddedEventBuilder},
|
||||||
|
store_aggregate::*,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait AddStoreUseCase: Send + Sync {
|
||||||
|
async fn add_store(&self, cmd: AddStoreCommand) -> BillingResult<StoreAddedEvent>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type AddStoreServiceObj = Arc<dyn AddStoreUseCase>;
|
||||||
|
|
||||||
|
#[derive(Clone, Builder)]
|
||||||
|
pub struct AddStoreService {
|
||||||
|
db_store_id_exists: StoreIDExistsDBPortObj,
|
||||||
|
db_store_name_exists: StoreNameExistsDBPortObj,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl AddStoreUseCase for AddStoreService {
|
||||||
|
async fn add_store(&self, cmd: AddStoreCommand) -> BillingResult<StoreAddedEvent> {
|
||||||
|
if self
|
||||||
|
.db_store_id_exists
|
||||||
|
.store_id_exists(cmd.store_id())
|
||||||
|
.await?
|
||||||
|
{
|
||||||
|
return Err(BillingError::DuplicateStoreID);
|
||||||
|
}
|
||||||
|
|
||||||
|
let store = StoreBuilder::default()
|
||||||
|
.name(cmd.name().into())
|
||||||
|
.address(cmd.address().as_ref().map(|s| s.to_string()))
|
||||||
|
.owner(*cmd.owner())
|
||||||
|
.store_id(*cmd.store_id())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
if self.db_store_name_exists.store_name_exists(&store).await? {
|
||||||
|
return Err(BillingError::DuplicateStoreName);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(StoreAddedEventBuilder::default()
|
||||||
|
.name(store.name().into())
|
||||||
|
.address(store.address().as_ref().map(|s| s.to_string()))
|
||||||
|
.owner(*cmd.owner())
|
||||||
|
.store_id(*cmd.store_id())
|
||||||
|
.build()
|
||||||
|
.unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::tests::bdd::*;
|
||||||
|
use crate::utils::uuid::tests::*;
|
||||||
|
|
||||||
|
pub fn mock_add_store_service(
|
||||||
|
times: Option<usize>,
|
||||||
|
cmd: AddStoreCommand,
|
||||||
|
) -> AddStoreServiceObj {
|
||||||
|
let mut m = MockAddStoreUseCase::new();
|
||||||
|
|
||||||
|
let res = StoreAddedEventBuilder::default()
|
||||||
|
.name(cmd.name().into())
|
||||||
|
.address(cmd.address().as_ref().map(|s| s.to_string()))
|
||||||
|
.owner(*cmd.owner())
|
||||||
|
.store_id(*cmd.store_id())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_add_store()
|
||||||
|
.times(times)
|
||||||
|
.returning(move |_| Ok(res.clone()));
|
||||||
|
} else {
|
||||||
|
m.expect_add_store().returning(move |_| Ok(res.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_store_id_doesnt_exist() {
|
||||||
|
let name = "foo";
|
||||||
|
let address = "bar";
|
||||||
|
let owner = UUID;
|
||||||
|
|
||||||
|
let cmd = AddStoreCommandBuilder::default()
|
||||||
|
.name(name.into())
|
||||||
|
.address(Some(address.into()))
|
||||||
|
.owner(owner)
|
||||||
|
.store_id(UUID)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let s = AddStoreServiceBuilder::default()
|
||||||
|
.db_store_id_exists(mock_store_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.db_store_name_exists(mock_store_name_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let res = s.add_store(cmd.clone()).await.unwrap();
|
||||||
|
assert_eq!(res.name(), cmd.name());
|
||||||
|
assert_eq!(res.address(), cmd.address());
|
||||||
|
assert_eq!(res.owner(), cmd.owner());
|
||||||
|
assert_eq!(res.store_id(), cmd.store_id());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_store_name_exists() {
|
||||||
|
let name = "foo";
|
||||||
|
let address = "bar";
|
||||||
|
let owner = UUID;
|
||||||
|
|
||||||
|
let cmd = AddStoreCommandBuilder::default()
|
||||||
|
.name(name.into())
|
||||||
|
.address(Some(address.into()))
|
||||||
|
.owner(owner)
|
||||||
|
.store_id(UUID)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let s = AddStoreServiceBuilder::default()
|
||||||
|
.db_store_id_exists(mock_store_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.db_store_name_exists(mock_store_name_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
s.add_store(cmd.clone()).await,
|
||||||
|
Err(BillingError::DuplicateStoreName)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,210 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
use crate::{
|
||||||
|
billing::{
|
||||||
|
application::port::output::db::bill_id_exists::*,
|
||||||
|
application::port::output::db::get_line_items_for_bill_id::*,
|
||||||
|
domain::{
|
||||||
|
bill_aggregate::*, bill_total_price_computed_event::*,
|
||||||
|
compute_bill_total_price_command::*,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
types::currency::*,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait ComputeBillTotalPriceBillUseCase: Send + Sync {
|
||||||
|
async fn compute_total_price_for_bill(
|
||||||
|
&self,
|
||||||
|
cmd: ComputeBillTotalPriceBillCommand,
|
||||||
|
) -> BillingResult<BillTotalPriceComputedEvent>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type ComputeBillTotalPriceBillServiceObj = Arc<dyn ComputeBillTotalPriceBillUseCase>;
|
||||||
|
|
||||||
|
#[derive(Clone, Builder)]
|
||||||
|
pub struct ComputeBillTotalPriceBillService {
|
||||||
|
db_bill_id_exists: BillIDExistsDBPortObj,
|
||||||
|
db_get_line_items_for_bill_id: GetLineItemsForBillIDDBPortObj,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl ComputeBillTotalPriceBillUseCase for ComputeBillTotalPriceBillService {
|
||||||
|
async fn compute_total_price_for_bill(
|
||||||
|
&self,
|
||||||
|
cmd: ComputeBillTotalPriceBillCommand,
|
||||||
|
) -> BillingResult<BillTotalPriceComputedEvent> {
|
||||||
|
if !self.db_bill_id_exists.bill_id_exists(cmd.bill_id()).await? {
|
||||||
|
return Err(BillingError::BillIDNotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
let line_items = self
|
||||||
|
.db_get_line_items_for_bill_id
|
||||||
|
.get_line_items_for_bill_id(*cmd.bill_id())
|
||||||
|
.await?;
|
||||||
|
if line_items.is_empty() {
|
||||||
|
todo!("Can't compute bill");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut total_price = PriceBuilder::default()
|
||||||
|
.major(0)
|
||||||
|
.minor(0)
|
||||||
|
.currency(Currency::INR)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
for li in line_items.iter() {
|
||||||
|
total_price = total_price.clone() + li.total_price();
|
||||||
|
}
|
||||||
|
// TODO: 3. Tax?
|
||||||
|
|
||||||
|
Ok(BillTotalPriceComputedEventBuilder::default()
|
||||||
|
.added_by_user(*cmd.adding_by())
|
||||||
|
.bill_id(*cmd.bill_id())
|
||||||
|
.total_price(total_price)
|
||||||
|
.build()
|
||||||
|
.unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use time::macros::datetime;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::billing::domain::bill_total_price_computed_event::tests::get_bill_total_computed_event_from_command;
|
||||||
|
use crate::billing::domain::line_item_aggregate::LineItemBuilder;
|
||||||
|
use crate::tests::bdd::*;
|
||||||
|
use crate::types::quantity::*;
|
||||||
|
use crate::utils::uuid::tests::*;
|
||||||
|
|
||||||
|
pub fn mock_compute_bill_total_price_service(
|
||||||
|
times: Option<usize>,
|
||||||
|
cmd: ComputeBillTotalPriceBillCommand,
|
||||||
|
) -> ComputeBillTotalPriceBillServiceObj {
|
||||||
|
let mut m = MockComputeBillTotalPriceBillUseCase::new();
|
||||||
|
|
||||||
|
let res = get_bill_total_computed_event_from_command(&cmd);
|
||||||
|
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_compute_total_price_for_bill()
|
||||||
|
.times(times)
|
||||||
|
.returning(move |_| Ok(res.clone()));
|
||||||
|
} else {
|
||||||
|
m.expect_compute_total_price_for_bill()
|
||||||
|
.returning(move |_| Ok(res.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service() {
|
||||||
|
let cmd = ComputeBillTotalPriceBillCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = ComputeBillTotalPriceBillServiceBuilder::default()
|
||||||
|
.db_bill_id_exists(mock_bill_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||||
|
.db_get_line_items_for_bill_id(mock_get_line_items_for_bill_id_db_port_true(
|
||||||
|
IS_CALLED_ONLY_ONCE,
|
||||||
|
))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let res = s.compute_total_price_for_bill(cmd.clone()).await.unwrap();
|
||||||
|
assert_eq!(res.total_price().clone(), Price::default());
|
||||||
|
assert_eq!(res.bill_id(), cmd.bill_id());
|
||||||
|
assert_eq!(res.added_by_user(), cmd.adding_by());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_sum() {
|
||||||
|
let cmd = ComputeBillTotalPriceBillCommand::get_cmd();
|
||||||
|
|
||||||
|
let li = LineItemBuilder::default()
|
||||||
|
.created_time(datetime!(1970-01-01 0:00 UTC))
|
||||||
|
.product_name("test_product".into())
|
||||||
|
.product_id(UUID)
|
||||||
|
.quantity(
|
||||||
|
QuantityBuilder::default()
|
||||||
|
.major(
|
||||||
|
QuantityPartBuilder::default()
|
||||||
|
.number(2)
|
||||||
|
.unit(QuantityUnit::DiscreteNumber)
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.minor(
|
||||||
|
QuantityPartBuilder::default()
|
||||||
|
.number(1)
|
||||||
|
.unit(QuantityUnit::DiscreteNumber)
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.bill_id(UUID)
|
||||||
|
.price_per_unit(
|
||||||
|
PriceBuilder::default()
|
||||||
|
.major(100)
|
||||||
|
.minor(20)
|
||||||
|
.currency(Currency::INR)
|
||||||
|
.build()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.line_item_id(UUID)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut m = MockGetLineItemsForBillIDDBPort::new();
|
||||||
|
m.expect_get_line_items_for_bill_id()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.returning(move |_| Ok(vec![li.clone(), li.clone()]));
|
||||||
|
|
||||||
|
let s = ComputeBillTotalPriceBillServiceBuilder::default()
|
||||||
|
.db_bill_id_exists(mock_bill_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||||
|
.db_get_line_items_for_bill_id(std::sync::Arc::new(m))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let res = s.compute_total_price_for_bill(cmd.clone()).await.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
res.total_price().clone(),
|
||||||
|
PriceBuilder::default()
|
||||||
|
.major(100 * 2 * 2)
|
||||||
|
.minor(20 * 2 * 2)
|
||||||
|
.currency(Currency::INR)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_bill_id_doesnt_exist() {
|
||||||
|
let cmd = ComputeBillTotalPriceBillCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = ComputeBillTotalPriceBillServiceBuilder::default()
|
||||||
|
.db_bill_id_exists(mock_bill_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.db_get_line_items_for_bill_id(mock_get_line_items_for_bill_id_db_port_true(
|
||||||
|
IS_NEVER_CALLED,
|
||||||
|
))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
s.compute_total_price_for_bill(cmd.clone()).await,
|
||||||
|
Err(BillingError::BillIDNotFound)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
103
src/billing/application/services/delete_bill_service.rs
Normal file
103
src/billing/application/services/delete_bill_service.rs
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
use crate::billing::{
|
||||||
|
application::port::output::db::bill_id_exists::*,
|
||||||
|
domain::{bill_deleted_event::*, delete_bill_command::DeleteBillCommand},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait DeleteBillUseCase: Send + Sync {
|
||||||
|
async fn delete_bill(&self, cmd: DeleteBillCommand) -> BillingResult<BillDeletedEvent>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type DeleteBillServiceObj = Arc<dyn DeleteBillUseCase>;
|
||||||
|
|
||||||
|
#[derive(Clone, Builder)]
|
||||||
|
pub struct DeleteBillService {
|
||||||
|
db_bill_id_exists: BillIDExistsDBPortObj,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl DeleteBillUseCase for DeleteBillService {
|
||||||
|
async fn delete_bill(&self, cmd: DeleteBillCommand) -> BillingResult<BillDeletedEvent> {
|
||||||
|
if !self
|
||||||
|
.db_bill_id_exists
|
||||||
|
.bill_id_exists(cmd.bill().bill_id())
|
||||||
|
.await?
|
||||||
|
{
|
||||||
|
return Err(BillingError::BillIDNotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(BillDeletedEventBuilder::default()
|
||||||
|
.added_by_user(*cmd.adding_by())
|
||||||
|
.bill(cmd.bill().clone())
|
||||||
|
.build()
|
||||||
|
.unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::billing::domain::bill_deleted_event::tests::get_deleted_bill_event_from_command;
|
||||||
|
use crate::tests::bdd::*;
|
||||||
|
|
||||||
|
pub fn mock_delete_bill_service(
|
||||||
|
times: Option<usize>,
|
||||||
|
cmd: DeleteBillCommand,
|
||||||
|
) -> DeleteBillServiceObj {
|
||||||
|
let mut m = MockDeleteBillUseCase::new();
|
||||||
|
|
||||||
|
let res = get_deleted_bill_event_from_command(&cmd);
|
||||||
|
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_delete_bill()
|
||||||
|
.times(times)
|
||||||
|
.returning(move |_| Ok(res.clone()));
|
||||||
|
} else {
|
||||||
|
m.expect_delete_bill().returning(move |_| Ok(res.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service() {
|
||||||
|
let cmd = DeleteBillCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = DeleteBillServiceBuilder::default()
|
||||||
|
.db_bill_id_exists(mock_bill_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let res = s.delete_bill(cmd.clone()).await.unwrap();
|
||||||
|
assert_eq!(res.bill(), cmd.bill());
|
||||||
|
assert_eq!(res.added_by_user(), cmd.adding_by());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_bill_id_doesnt_exist() {
|
||||||
|
let cmd = DeleteBillCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = DeleteBillServiceBuilder::default()
|
||||||
|
.db_bill_id_exists(mock_bill_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
s.delete_bill(cmd.clone()).await,
|
||||||
|
Err(BillingError::BillIDNotFound)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
135
src/billing/application/services/delete_line_item_service.rs
Normal file
135
src/billing/application/services/delete_line_item_service.rs
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
use crate::billing::{
|
||||||
|
application::port::output::db::line_item_id_exists::*,
|
||||||
|
domain::{delete_line_item_command::*, line_item_aggregate::*, line_item_deleted_event::*},
|
||||||
|
};
|
||||||
|
use crate::utils::uuid::*;
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait DeleteLineItemUseCase: Send + Sync {
|
||||||
|
async fn delete_line_item(
|
||||||
|
&self,
|
||||||
|
cmd: DeleteLineItemCommand,
|
||||||
|
) -> BillingResult<LineItemDeletedEvent>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type DeleteLineItemServiceObj = Arc<dyn DeleteLineItemUseCase>;
|
||||||
|
|
||||||
|
#[derive(Clone, Builder)]
|
||||||
|
pub struct DeleteLineItemService {
|
||||||
|
db_line_item_id_exists: LineItemIDExistsDBPortObj,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl DeleteLineItemUseCase for DeleteLineItemService {
|
||||||
|
async fn delete_line_item(
|
||||||
|
&self,
|
||||||
|
cmd: DeleteLineItemCommand,
|
||||||
|
) -> BillingResult<LineItemDeletedEvent> {
|
||||||
|
if !self
|
||||||
|
.db_line_item_id_exists
|
||||||
|
.line_item_id_exists(cmd.line_item().line_item_id())
|
||||||
|
.await?
|
||||||
|
{
|
||||||
|
return Err(BillingError::LineItemIDNotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
let deleted_line_item = LineItemBuilder::default()
|
||||||
|
.created_time(cmd.line_item().created_time().clone())
|
||||||
|
.product_name(cmd.line_item().product_name().into())
|
||||||
|
.product_id(*cmd.line_item().product_id())
|
||||||
|
.line_item_id(*cmd.line_item().line_item_id())
|
||||||
|
.bill_id(*cmd.line_item().bill_id())
|
||||||
|
.quantity(cmd.line_item().quantity().clone())
|
||||||
|
.price_per_unit(cmd.line_item().price_per_unit().clone())
|
||||||
|
.deleted(true)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(LineItemDeletedEventBuilder::default()
|
||||||
|
.added_by_user(*cmd.adding_by())
|
||||||
|
.line_item(deleted_line_item)
|
||||||
|
.build()
|
||||||
|
.unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::billing::domain::line_item_deleted_event::tests::get_deleted_line_item_event_from_command;
|
||||||
|
use crate::tests::bdd::*;
|
||||||
|
|
||||||
|
pub fn mock_delete_line_item_service(
|
||||||
|
times: Option<usize>,
|
||||||
|
cmd: DeleteLineItemCommand,
|
||||||
|
) -> DeleteLineItemServiceObj {
|
||||||
|
let mut m = MockDeleteLineItemUseCase::new();
|
||||||
|
|
||||||
|
let res = get_deleted_line_item_event_from_command(&cmd);
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_delete_line_item()
|
||||||
|
.times(times)
|
||||||
|
.returning(move |_| Ok(res.clone()));
|
||||||
|
} else {
|
||||||
|
m.expect_delete_line_item()
|
||||||
|
.returning(move |_| Ok(res.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service() {
|
||||||
|
let cmd = DeleteLineItemCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = DeleteLineItemServiceBuilder::default()
|
||||||
|
.db_line_item_id_exists(mock_line_item_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let res = s.delete_line_item(cmd.clone()).await.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
res.line_item().product_name(),
|
||||||
|
cmd.line_item().product_name()
|
||||||
|
);
|
||||||
|
assert_eq!(res.line_item().product_id(), cmd.line_item().product_id());
|
||||||
|
assert_eq!(res.line_item().quantity(), cmd.line_item().quantity());
|
||||||
|
assert_eq!(
|
||||||
|
res.line_item().line_item_id(),
|
||||||
|
cmd.line_item().line_item_id()
|
||||||
|
);
|
||||||
|
assert_eq!(res.line_item().bill_id(), cmd.line_item().bill_id());
|
||||||
|
assert!(res.line_item().deleted());
|
||||||
|
|
||||||
|
assert_eq!(res.added_by_user(), cmd.adding_by());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_line_item_id_doesnt_exist() {
|
||||||
|
let cmd = DeleteLineItemCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = DeleteLineItemServiceBuilder::default()
|
||||||
|
.db_line_item_id_exists(mock_line_item_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
s.delete_line_item(cmd.clone()).await,
|
||||||
|
Err(BillingError::LineItemIDNotFound)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
37
src/billing/application/services/errors.rs
Normal file
37
src/billing/application/services/errors.rs
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_more::{Display, Error};
|
||||||
|
use log::error;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::billing::application::port::output::db::errors::BillingDBError;
|
||||||
|
|
||||||
|
pub type BillingResult<V> = Result<V, BillingError>;
|
||||||
|
|
||||||
|
#[derive(Debug, Error, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub enum BillingError {
|
||||||
|
BillIDNotFound,
|
||||||
|
InternalError,
|
||||||
|
DuplicateStoreName,
|
||||||
|
DuplicateBillID,
|
||||||
|
DuplicateLineItemID,
|
||||||
|
DuplicateStoreID,
|
||||||
|
StoreIDNotFound,
|
||||||
|
LineItemIDNotFound,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<BillingDBError> for BillingError {
|
||||||
|
fn from(value: BillingDBError) -> Self {
|
||||||
|
match value {
|
||||||
|
BillingDBError::DuplicateBillID => Self::DuplicateBillID,
|
||||||
|
BillingDBError::DuplicateStoreName => Self::DuplicateStoreName,
|
||||||
|
BillingDBError::DuplicateStoreID => Self::DuplicateStoreID,
|
||||||
|
BillingDBError::StoreIDNotFound => BillingError::StoreIDNotFound,
|
||||||
|
BillingDBError::InternalError => BillingError::InternalError,
|
||||||
|
BillingDBError::DuplicateLineItemID => Self::DuplicateLineItemID,
|
||||||
|
BillingDBError::LineItemIDNotFound => BillingError::LineItemIDNotFound,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,83 @@
|
||||||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
|
||||||
|
pub mod errors;
|
||||||
|
|
||||||
|
// services
|
||||||
|
pub mod add_bill_service;
|
||||||
|
pub mod add_line_item_service;
|
||||||
|
pub mod add_store_service;
|
||||||
|
pub mod compute_bill_total_price_service;
|
||||||
|
pub mod delete_bill_service;
|
||||||
|
pub mod delete_line_item_service;
|
||||||
|
pub mod update_bill_service;
|
||||||
|
pub mod update_line_item_service;
|
||||||
|
pub mod update_store_service;
|
||||||
|
// TODO: 2. reset token number for store_id cronjob
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
pub trait BillingServicesInterface: Send + Sync {
|
||||||
|
fn add_bill(&self) -> add_bill_service::AddBillServiceObj;
|
||||||
|
fn update_bill(&self) -> update_bill_service::UpdateBillServiceObj;
|
||||||
|
fn delete_bill(&self) -> delete_bill_service::DeleteBillServiceObj;
|
||||||
|
fn add_store(&self) -> add_store_service::AddStoreServiceObj;
|
||||||
|
fn update_store(&self) -> update_store_service::UpdateStoreServiceObj;
|
||||||
|
fn add_line_item(&self) -> add_line_item_service::AddLineItemServiceObj;
|
||||||
|
fn update_line_item(&self) -> update_line_item_service::UpdateLineItemServiceObj;
|
||||||
|
fn delete_line_item(&self) -> delete_line_item_service::DeleteLineItemServiceObj;
|
||||||
|
fn compute_total_price_for_bill(
|
||||||
|
&self,
|
||||||
|
) -> compute_bill_total_price_service::ComputeBillTotalPriceBillServiceObj;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Builder)]
|
||||||
|
pub struct BillingServices {
|
||||||
|
add_bill: add_bill_service::AddBillServiceObj,
|
||||||
|
add_store: add_store_service::AddStoreServiceObj,
|
||||||
|
update_store: update_store_service::UpdateStoreServiceObj,
|
||||||
|
add_line_item: add_line_item_service::AddLineItemServiceObj,
|
||||||
|
update_line_item: update_line_item_service::UpdateLineItemServiceObj,
|
||||||
|
delete_line_item: delete_line_item_service::DeleteLineItemServiceObj,
|
||||||
|
update_bill: update_bill_service::UpdateBillServiceObj,
|
||||||
|
delete_bill: delete_bill_service::DeleteBillServiceObj,
|
||||||
|
compute_total_price_for_bill:
|
||||||
|
compute_bill_total_price_service::ComputeBillTotalPriceBillServiceObj,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BillingServicesInterface for BillingServices {
|
||||||
|
fn add_bill(&self) -> add_bill_service::AddBillServiceObj {
|
||||||
|
self.add_bill.clone()
|
||||||
|
}
|
||||||
|
fn update_bill(&self) -> update_bill_service::UpdateBillServiceObj {
|
||||||
|
self.update_bill.clone()
|
||||||
|
}
|
||||||
|
fn delete_bill(&self) -> delete_bill_service::DeleteBillServiceObj {
|
||||||
|
self.delete_bill.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_store(&self) -> add_store_service::AddStoreServiceObj {
|
||||||
|
self.add_store.clone()
|
||||||
|
}
|
||||||
|
fn update_store(&self) -> update_store_service::UpdateStoreServiceObj {
|
||||||
|
self.update_store.clone()
|
||||||
|
}
|
||||||
|
fn add_line_item(&self) -> add_line_item_service::AddLineItemServiceObj {
|
||||||
|
self.add_line_item.clone()
|
||||||
|
}
|
||||||
|
fn update_line_item(&self) -> update_line_item_service::UpdateLineItemServiceObj {
|
||||||
|
self.update_line_item.clone()
|
||||||
|
}
|
||||||
|
fn delete_line_item(&self) -> delete_line_item_service::DeleteLineItemServiceObj {
|
||||||
|
self.delete_line_item.clone()
|
||||||
|
}
|
||||||
|
fn compute_total_price_for_bill(
|
||||||
|
&self,
|
||||||
|
) -> compute_bill_total_price_service::ComputeBillTotalPriceBillServiceObj {
|
||||||
|
self.compute_total_price_for_bill.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
116
src/billing/application/services/update_bill_service.rs
Normal file
116
src/billing/application/services/update_bill_service.rs
Normal file
|
@ -0,0 +1,116 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
use crate::billing::{
|
||||||
|
application::port::output::db::bill_id_exists::*,
|
||||||
|
domain::{bill_aggregate::*, bill_updated_event::*, update_bill_command::UpdateBillCommand},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait UpdateBillUseCase: Send + Sync {
|
||||||
|
async fn update_bill(&self, cmd: UpdateBillCommand) -> BillingResult<BillUpdatedEvent>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type UpdateBillServiceObj = Arc<dyn UpdateBillUseCase>;
|
||||||
|
|
||||||
|
#[derive(Clone, Builder)]
|
||||||
|
pub struct UpdateBillService {
|
||||||
|
db_bill_id_exists: BillIDExistsDBPortObj,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl UpdateBillUseCase for UpdateBillService {
|
||||||
|
async fn update_bill(&self, cmd: UpdateBillCommand) -> BillingResult<BillUpdatedEvent> {
|
||||||
|
if !self
|
||||||
|
.db_bill_id_exists
|
||||||
|
.bill_id_exists(cmd.old_bill().bill_id())
|
||||||
|
.await?
|
||||||
|
{
|
||||||
|
return Err(BillingError::BillIDNotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
let bill = BillBuilder::default()
|
||||||
|
.bill_id(*cmd.old_bill().bill_id())
|
||||||
|
.token_number(*cmd.old_bill().token_number())
|
||||||
|
.total_price(cmd.total_price().clone())
|
||||||
|
.store_id(*cmd.store_id())
|
||||||
|
.deleted(false)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(BillUpdatedEventBuilder::default()
|
||||||
|
.added_by_user(*cmd.adding_by())
|
||||||
|
.new_bill(bill)
|
||||||
|
.old_bill(cmd.old_bill().clone())
|
||||||
|
.build()
|
||||||
|
.unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::billing::domain::bill_updated_event::tests::get_updated_bill_event_from_command;
|
||||||
|
use crate::tests::bdd::*;
|
||||||
|
use crate::utils::uuid::tests::*;
|
||||||
|
|
||||||
|
pub fn mock_update_bill_service(
|
||||||
|
times: Option<usize>,
|
||||||
|
cmd: UpdateBillCommand,
|
||||||
|
) -> UpdateBillServiceObj {
|
||||||
|
let mut m = MockUpdateBillUseCase::new();
|
||||||
|
|
||||||
|
let res = get_updated_bill_event_from_command(&cmd);
|
||||||
|
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_update_bill()
|
||||||
|
.times(times)
|
||||||
|
.returning(move |_| Ok(res.clone()));
|
||||||
|
} else {
|
||||||
|
m.expect_update_bill().returning(move |_| Ok(res.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service() {
|
||||||
|
let cmd = UpdateBillCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = UpdateBillServiceBuilder::default()
|
||||||
|
.db_bill_id_exists(mock_bill_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let res = s.update_bill(cmd.clone()).await.unwrap();
|
||||||
|
assert_eq!(res.new_bill().total_price(), cmd.total_price());
|
||||||
|
assert_eq!(res.new_bill().bill_id(), cmd.old_bill().bill_id());
|
||||||
|
assert_eq!(res.old_bill(), cmd.old_bill());
|
||||||
|
assert_eq!(res.added_by_user(), cmd.adding_by());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_bill_id_doesnt_exist() {
|
||||||
|
let cmd = UpdateBillCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = UpdateBillServiceBuilder::default()
|
||||||
|
.db_bill_id_exists(mock_bill_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
s.update_bill(cmd.clone()).await,
|
||||||
|
Err(BillingError::BillIDNotFound)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
161
src/billing/application/services/update_line_item_service.rs
Normal file
161
src/billing/application/services/update_line_item_service.rs
Normal file
|
@ -0,0 +1,161 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
use crate::billing::{
|
||||||
|
application::port::output::db::bill_id_exists::*,
|
||||||
|
application::port::output::db::line_item_id_exists::*,
|
||||||
|
domain::{line_item_aggregate::*, line_item_updated_event::*, update_line_item_command::*},
|
||||||
|
};
|
||||||
|
use crate::utils::uuid::*;
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait UpdateLineItemUseCase: Send + Sync {
|
||||||
|
async fn update_line_item(
|
||||||
|
&self,
|
||||||
|
cmd: UpdateLineItemCommand,
|
||||||
|
) -> BillingResult<LineItemUpdatedEvent>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type UpdateLineItemServiceObj = Arc<dyn UpdateLineItemUseCase>;
|
||||||
|
|
||||||
|
#[derive(Clone, Builder)]
|
||||||
|
pub struct UpdateLineItemService {
|
||||||
|
db_line_item_id_exists: LineItemIDExistsDBPortObj,
|
||||||
|
db_bill_id_exists: BillIDExistsDBPortObj,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl UpdateLineItemUseCase for UpdateLineItemService {
|
||||||
|
async fn update_line_item(
|
||||||
|
&self,
|
||||||
|
cmd: UpdateLineItemCommand,
|
||||||
|
) -> BillingResult<LineItemUpdatedEvent> {
|
||||||
|
if !self
|
||||||
|
.db_line_item_id_exists
|
||||||
|
.line_item_id_exists(cmd.old_line_item().line_item_id())
|
||||||
|
.await?
|
||||||
|
{
|
||||||
|
return Err(BillingError::LineItemIDNotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
if !self.db_bill_id_exists.bill_id_exists(cmd.bill_id()).await? {
|
||||||
|
return Err(BillingError::BillIDNotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
let new_line_item = LineItemBuilder::default()
|
||||||
|
.created_time(cmd.created_time().clone())
|
||||||
|
.product_name(cmd.product_name().into())
|
||||||
|
.product_id(*cmd.product_id())
|
||||||
|
.line_item_id(*cmd.old_line_item().line_item_id())
|
||||||
|
.bill_id(*cmd.bill_id())
|
||||||
|
.quantity(cmd.quantity().clone())
|
||||||
|
.price_per_unit(cmd.price_per_unit().clone())
|
||||||
|
.deleted(false)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(LineItemUpdatedEventBuilder::default()
|
||||||
|
.added_by_user(*cmd.adding_by())
|
||||||
|
.new_line_item(new_line_item)
|
||||||
|
.old_line_item(cmd.old_line_item().clone())
|
||||||
|
.build()
|
||||||
|
.unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::billing::domain::line_item_updated_event::tests::get_updated_line_item_event_from_command;
|
||||||
|
use crate::utils::uuid::tests::UUID;
|
||||||
|
use crate::{tests::bdd::*, utils::uuid::tests::mock_get_uuid};
|
||||||
|
|
||||||
|
pub fn mock_update_line_item_service(
|
||||||
|
times: Option<usize>,
|
||||||
|
cmd: UpdateLineItemCommand,
|
||||||
|
) -> UpdateLineItemServiceObj {
|
||||||
|
let mut m = MockUpdateLineItemUseCase::new();
|
||||||
|
|
||||||
|
let res = get_updated_line_item_event_from_command(&cmd);
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_update_line_item()
|
||||||
|
.times(times)
|
||||||
|
.returning(move |_| Ok(res.clone()));
|
||||||
|
} else {
|
||||||
|
m.expect_update_line_item()
|
||||||
|
.returning(move |_| Ok(res.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service() {
|
||||||
|
let cmd = UpdateLineItemCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = UpdateLineItemServiceBuilder::default()
|
||||||
|
.db_line_item_id_exists(mock_line_item_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||||
|
.db_bill_id_exists(mock_bill_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let res = s.update_line_item(cmd.clone()).await.unwrap();
|
||||||
|
assert_eq!(res.new_line_item().product_name(), cmd.product_name());
|
||||||
|
assert_eq!(res.new_line_item().product_id(), cmd.product_id());
|
||||||
|
assert_eq!(res.new_line_item().quantity(), cmd.quantity());
|
||||||
|
assert_eq!(
|
||||||
|
res.new_line_item().line_item_id(),
|
||||||
|
cmd.old_line_item().line_item_id()
|
||||||
|
);
|
||||||
|
assert_eq!(res.new_line_item().bill_id(), cmd.old_line_item().bill_id());
|
||||||
|
|
||||||
|
assert!(!res.new_line_item().deleted());
|
||||||
|
|
||||||
|
assert_eq!(res.old_line_item(), cmd.old_line_item());
|
||||||
|
|
||||||
|
assert_eq!(res.added_by_user(), cmd.adding_by());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_line_item_id_doesnt_exist() {
|
||||||
|
let cmd = UpdateLineItemCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = UpdateLineItemServiceBuilder::default()
|
||||||
|
.db_line_item_id_exists(mock_line_item_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.db_bill_id_exists(mock_bill_id_exists_db_port_true(IS_NEVER_CALLED))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
s.update_line_item(cmd.clone()).await,
|
||||||
|
Err(BillingError::LineItemIDNotFound)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_bill_id_doesnt_exist() {
|
||||||
|
let cmd = UpdateLineItemCommand::get_cmd();
|
||||||
|
|
||||||
|
let s = UpdateLineItemServiceBuilder::default()
|
||||||
|
.db_line_item_id_exists(mock_line_item_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||||
|
.db_bill_id_exists(mock_bill_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
s.update_line_item(cmd.clone()).await,
|
||||||
|
Err(BillingError::BillIDNotFound)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
146
src/billing/application/services/update_store_service.rs
Normal file
146
src/billing/application/services/update_store_service.rs
Normal file
|
@ -0,0 +1,146 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use mockall::*;
|
||||||
|
|
||||||
|
use super::errors::*;
|
||||||
|
use crate::billing::{
|
||||||
|
application::port::output::db::{store_id_exists::*, store_name_exists::*},
|
||||||
|
domain::{
|
||||||
|
store_aggregate::*, store_updated_event::*, update_store_command::UpdateStoreCommand,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
use crate::utils::uuid::*;
|
||||||
|
|
||||||
|
#[automock]
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait UpdateStoreUseCase: Send + Sync {
|
||||||
|
async fn update_store(&self, cmd: UpdateStoreCommand) -> BillingResult<StoreUpdatedEvent>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type UpdateStoreServiceObj = Arc<dyn UpdateStoreUseCase>;
|
||||||
|
|
||||||
|
#[derive(Clone, Builder)]
|
||||||
|
pub struct UpdateStoreService {
|
||||||
|
db_store_id_exists: StoreIDExistsDBPortObj,
|
||||||
|
db_store_name_exists: StoreNameExistsDBPortObj,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl UpdateStoreUseCase for UpdateStoreService {
|
||||||
|
async fn update_store(&self, cmd: UpdateStoreCommand) -> BillingResult<StoreUpdatedEvent> {
|
||||||
|
if !self
|
||||||
|
.db_store_id_exists
|
||||||
|
.store_id_exists(cmd.old_store().store_id())
|
||||||
|
.await?
|
||||||
|
{
|
||||||
|
return Err(BillingError::StoreIDNotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
let store = StoreBuilder::default()
|
||||||
|
.name(cmd.name().into())
|
||||||
|
.address(cmd.address().as_ref().map(|s| s.to_string()))
|
||||||
|
.owner(*cmd.owner())
|
||||||
|
.store_id(*cmd.old_store().store_id())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
if cmd.name() != cmd.old_store().name() {
|
||||||
|
if self.db_store_name_exists.store_name_exists(&store).await? {
|
||||||
|
return Err(BillingError::DuplicateStoreName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(StoreUpdatedEventBuilder::default()
|
||||||
|
.added_by_user(*cmd.adding_by())
|
||||||
|
.new_store(store)
|
||||||
|
.old_store(cmd.old_store().clone())
|
||||||
|
.build()
|
||||||
|
.unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::billing::domain::store_updated_event::tests::get_store_updated_event_from_command;
|
||||||
|
use crate::billing::domain::update_store_command::tests::get_update_store_cmd;
|
||||||
|
use crate::tests::bdd::*;
|
||||||
|
use crate::utils::uuid::tests::*;
|
||||||
|
|
||||||
|
pub fn mock_update_store_service(
|
||||||
|
times: Option<usize>,
|
||||||
|
cmd: UpdateStoreCommand,
|
||||||
|
) -> UpdateStoreServiceObj {
|
||||||
|
let mut m = MockUpdateStoreUseCase::new();
|
||||||
|
|
||||||
|
let res = get_store_updated_event_from_command(&cmd);
|
||||||
|
|
||||||
|
if let Some(times) = times {
|
||||||
|
m.expect_update_store()
|
||||||
|
.times(times)
|
||||||
|
.returning(move |_| Ok(res.clone()));
|
||||||
|
} else {
|
||||||
|
m.expect_update_store().returning(move |_| Ok(res.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Arc::new(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service() {
|
||||||
|
let cmd = get_update_store_cmd();
|
||||||
|
|
||||||
|
let s = UpdateStoreServiceBuilder::default()
|
||||||
|
.db_store_id_exists(mock_store_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||||
|
.db_store_name_exists(mock_store_name_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let res = s.update_store(cmd.clone()).await.unwrap();
|
||||||
|
assert_eq!(res.new_store().name(), cmd.name());
|
||||||
|
assert_eq!(res.new_store().address(), cmd.address());
|
||||||
|
assert_eq!(res.new_store().owner(), cmd.owner());
|
||||||
|
assert_eq!(res.new_store().store_id(), cmd.old_store().store_id());
|
||||||
|
assert_eq!(res.old_store(), cmd.old_store());
|
||||||
|
assert_eq!(res.added_by_user(), cmd.adding_by());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_store_name_exists() {
|
||||||
|
let cmd = get_update_store_cmd();
|
||||||
|
|
||||||
|
let s = UpdateStoreServiceBuilder::default()
|
||||||
|
.db_store_id_exists(mock_store_id_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||||
|
.db_store_name_exists(mock_store_name_exists_db_port_true(IS_CALLED_ONLY_ONCE))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
s.update_store(cmd.clone()).await,
|
||||||
|
Err(BillingError::DuplicateStoreName)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_service_store_id_doesnt_exist() {
|
||||||
|
let cmd = get_update_store_cmd();
|
||||||
|
|
||||||
|
let s = UpdateStoreServiceBuilder::default()
|
||||||
|
.db_store_id_exists(mock_store_id_exists_db_port_false(IS_CALLED_ONLY_ONCE))
|
||||||
|
.db_store_name_exists(mock_store_name_exists_db_port_false(IS_NEVER_CALLED))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
s.update_store(cmd.clone()).await,
|
||||||
|
Err(BillingError::StoreIDNotFound)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
62
src/billing/domain/add_bill_command.rs
Normal file
62
src/billing/domain/add_bill_command.rs
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder,
|
||||||
|
)]
|
||||||
|
pub struct AddBillCommand {
|
||||||
|
adding_by: Uuid,
|
||||||
|
|
||||||
|
#[builder(default = "OffsetDateTime::now_utc()")]
|
||||||
|
created_time: OffsetDateTime,
|
||||||
|
|
||||||
|
bill_id: Uuid,
|
||||||
|
store_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use time::macros::datetime;
|
||||||
|
|
||||||
|
use crate::utils::uuid::tests::UUID;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl AddBillCommand {
|
||||||
|
pub fn get_cmd() -> Self {
|
||||||
|
let store_id = UUID;
|
||||||
|
let adding_by = UUID;
|
||||||
|
|
||||||
|
AddBillCommandBuilder::default()
|
||||||
|
.adding_by(adding_by)
|
||||||
|
.created_time(datetime!(1970-01-01 0:00 UTC))
|
||||||
|
.store_id(store_id)
|
||||||
|
.bill_id(UUID)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmd() {
|
||||||
|
let store_id = UUID;
|
||||||
|
let adding_by = UUID;
|
||||||
|
|
||||||
|
let cmd = AddBillCommandBuilder::default()
|
||||||
|
.adding_by(adding_by)
|
||||||
|
.store_id(store_id)
|
||||||
|
.bill_id(UUID)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(*cmd.store_id(), store_id);
|
||||||
|
assert_eq!(*cmd.adding_by(), adding_by);
|
||||||
|
}
|
||||||
|
}
|
152
src/billing/domain/add_line_item_command.rs
Normal file
152
src/billing/domain/add_line_item_command.rs
Normal file
|
@ -0,0 +1,152 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use derive_more::{Display, Error};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::types::{currency::*, quantity::*};
|
||||||
|
use crate::utils::string::empty_string_err;
|
||||||
|
|
||||||
|
#[derive(Debug, Error, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub enum AddLineItemCommandError {
|
||||||
|
QuantityIsEmpty,
|
||||||
|
ProductNameIsEmpty,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Builder, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters,
|
||||||
|
)]
|
||||||
|
#[builder(build_fn(validate = "Self::validate"))]
|
||||||
|
pub struct AddLineItemCommand {
|
||||||
|
#[builder(default = "OffsetDateTime::now_utc()")]
|
||||||
|
created_time: OffsetDateTime,
|
||||||
|
#[builder(setter(custom))]
|
||||||
|
product_name: String,
|
||||||
|
product_id: Uuid,
|
||||||
|
bill_id: Uuid,
|
||||||
|
line_item_id: Uuid,
|
||||||
|
quantity: Quantity,
|
||||||
|
price_per_unit: Price,
|
||||||
|
|
||||||
|
adding_by: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AddLineItemCommandBuilder {
|
||||||
|
pub fn product_name(&mut self, product_name: String) -> &mut Self {
|
||||||
|
self.product_name = Some(product_name.trim().to_owned());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate(&self) -> Result<(), String> {
|
||||||
|
let product_name = self.product_name.as_ref().unwrap().trim().to_owned();
|
||||||
|
if product_name.is_empty() {
|
||||||
|
return Err(AddLineItemCommandError::ProductNameIsEmpty.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.quantity.as_ref().unwrap().is_empty() {
|
||||||
|
return Err(AddLineItemCommandError::QuantityIsEmpty.to_string());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use time::macros::datetime;
|
||||||
|
|
||||||
|
use crate::utils::uuid::tests::UUID;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl AddLineItemCommand {
|
||||||
|
pub fn get_cmd() -> Self {
|
||||||
|
let product_name = "foo";
|
||||||
|
let product_id = UUID;
|
||||||
|
let bill_id = UUID;
|
||||||
|
let adding_by = UUID;
|
||||||
|
let quantity = Quantity::get_quantity();
|
||||||
|
|
||||||
|
AddLineItemCommandBuilder::default()
|
||||||
|
.product_name(product_name.into())
|
||||||
|
.adding_by(adding_by)
|
||||||
|
.created_time(datetime!(1970-01-01 0:00 UTC))
|
||||||
|
.quantity(quantity.clone())
|
||||||
|
.price_per_unit(Price::default())
|
||||||
|
.product_id(product_id)
|
||||||
|
.bill_id(bill_id)
|
||||||
|
.line_item_id(UUID)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmd() {
|
||||||
|
let product_name = "foo";
|
||||||
|
let product_id = UUID;
|
||||||
|
let bill_id = UUID;
|
||||||
|
let adding_by = UUID;
|
||||||
|
let quantity = Quantity::get_quantity();
|
||||||
|
|
||||||
|
let cmd = AddLineItemCommandBuilder::default()
|
||||||
|
.product_name(product_name.into())
|
||||||
|
.adding_by(adding_by)
|
||||||
|
.price_per_unit(Price::default())
|
||||||
|
.quantity(quantity.clone())
|
||||||
|
.product_id(product_id)
|
||||||
|
.bill_id(bill_id)
|
||||||
|
.line_item_id(UUID)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cmd.quantity(), &quantity);
|
||||||
|
assert_eq!(*cmd.product_id(), product_id);
|
||||||
|
assert_eq!(*cmd.adding_by(), adding_by);
|
||||||
|
assert_eq!(cmd.product_name(), product_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmd_product_name_empty() {
|
||||||
|
let product_name = "";
|
||||||
|
let product_id = UUID;
|
||||||
|
let bill_id = UUID;
|
||||||
|
let adding_by = UUID;
|
||||||
|
let quantity = Quantity::get_quantity();
|
||||||
|
|
||||||
|
assert!(AddLineItemCommandBuilder::default()
|
||||||
|
.product_name(product_name.into())
|
||||||
|
.adding_by(adding_by)
|
||||||
|
.quantity(quantity.clone())
|
||||||
|
.price_per_unit(Price::default())
|
||||||
|
.product_id(product_id)
|
||||||
|
.line_item_id(UUID)
|
||||||
|
.bill_id(bill_id)
|
||||||
|
.build()
|
||||||
|
.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmd_quantity_empty() {
|
||||||
|
let product_name = "foo";
|
||||||
|
let product_id = UUID;
|
||||||
|
let bill_id = UUID;
|
||||||
|
let adding_by = UUID;
|
||||||
|
// minor = 0; major = 0;
|
||||||
|
let quantity = Quantity::default();
|
||||||
|
|
||||||
|
assert!(AddLineItemCommandBuilder::default()
|
||||||
|
.product_name(product_name.into())
|
||||||
|
.adding_by(adding_by)
|
||||||
|
.quantity(quantity.clone())
|
||||||
|
.product_id(product_id)
|
||||||
|
.bill_id(bill_id)
|
||||||
|
.price_per_unit(Price::default())
|
||||||
|
.build()
|
||||||
|
.is_err());
|
||||||
|
}
|
||||||
|
}
|
109
src/billing/domain/add_store_command.rs
Normal file
109
src/billing/domain/add_store_command.rs
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use derive_more::{Display, Error};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Error, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub enum AddStoreCommandError {
|
||||||
|
NameIsEmpty,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Builder, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters,
|
||||||
|
)]
|
||||||
|
#[builder(build_fn(validate = "Self::validate"))]
|
||||||
|
pub struct AddStoreCommand {
|
||||||
|
#[builder(setter(custom))]
|
||||||
|
name: String,
|
||||||
|
#[builder(setter(custom))]
|
||||||
|
address: Option<String>,
|
||||||
|
store_id: Uuid,
|
||||||
|
owner: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AddStoreCommandBuilder {
|
||||||
|
pub fn address(&mut self, address: Option<String>) -> &mut Self {
|
||||||
|
self.address = if let Some(address) = address {
|
||||||
|
let address = address.trim();
|
||||||
|
if address.is_empty() {
|
||||||
|
Some(None)
|
||||||
|
} else {
|
||||||
|
Some(Some(address.to_owned()))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Some(None)
|
||||||
|
};
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn name(&mut self, name: String) -> &mut Self {
|
||||||
|
self.name = Some(name.trim().to_owned());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate(&self) -> Result<(), String> {
|
||||||
|
let name = self.name.as_ref().unwrap().trim().to_owned();
|
||||||
|
if name.is_empty() {
|
||||||
|
return Err(AddStoreCommandError::NameIsEmpty.to_string());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::tests::bdd::*;
|
||||||
|
use crate::utils::uuid::tests::*;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmd() {
|
||||||
|
let name = "foo";
|
||||||
|
let address = "bar";
|
||||||
|
let owner = UUID;
|
||||||
|
|
||||||
|
// address = None
|
||||||
|
let cmd = AddStoreCommandBuilder::default()
|
||||||
|
.name(name.into())
|
||||||
|
.address(None)
|
||||||
|
.owner(owner)
|
||||||
|
.store_id(UUID)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
// let cmd = AddStoreCommand::new(name.into(), None, owner, UUID).unwrap();
|
||||||
|
assert_eq!(cmd.name(), name);
|
||||||
|
assert_eq!(cmd.address(), &None);
|
||||||
|
assert_eq!(cmd.owner(), &owner);
|
||||||
|
assert_eq!(*cmd.store_id(), UUID);
|
||||||
|
|
||||||
|
// address = Some
|
||||||
|
let cmd = AddStoreCommandBuilder::default()
|
||||||
|
.name(name.into())
|
||||||
|
.address(Some(address.into()))
|
||||||
|
.owner(owner)
|
||||||
|
.store_id(UUID)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
// let cmd = AddStoreCommand::new(name.into(), Some(address.into()), owner, UUID).unwrap();
|
||||||
|
assert_eq!(cmd.name(), name);
|
||||||
|
assert_eq!(cmd.address(), &Some(address.to_owned()));
|
||||||
|
assert_eq!(cmd.owner(), &owner);
|
||||||
|
assert_eq!(*cmd.store_id(), UUID);
|
||||||
|
|
||||||
|
// AddStoreCommandError::NameIsEmpty
|
||||||
|
|
||||||
|
assert!(AddStoreCommandBuilder::default()
|
||||||
|
.name("".into())
|
||||||
|
.address(Some(address.into()))
|
||||||
|
.owner(owner)
|
||||||
|
.store_id(UUID)
|
||||||
|
.build()
|
||||||
|
.is_err())
|
||||||
|
}
|
||||||
|
}
|
41
src/billing/domain/bill_added_event.rs
Normal file
41
src/billing/domain/bill_added_event.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::bill_aggregate::Bill;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd,
|
||||||
|
)]
|
||||||
|
pub struct BillAddedEvent {
|
||||||
|
added_by_user: Uuid,
|
||||||
|
|
||||||
|
bill: Bill,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use crate::billing::domain::add_bill_command::AddBillCommand;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn get_added_bill_event_from_command(cmd: &AddBillCommand) -> BillAddedEvent {
|
||||||
|
let bill = Bill::get_bill();
|
||||||
|
|
||||||
|
BillAddedEventBuilder::default()
|
||||||
|
.added_by_user(cmd.adding_by().clone())
|
||||||
|
.bill(bill)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_event() {
|
||||||
|
get_added_bill_event_from_command(&AddBillCommand::get_cmd());
|
||||||
|
}
|
||||||
|
}
|
229
src/billing/domain/bill_aggregate.rs
Normal file
229
src/billing/domain/bill_aggregate.rs
Normal file
|
@ -0,0 +1,229 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use cqrs_es::Aggregate;
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::types::currency::*;
|
||||||
|
|
||||||
|
use crate::billing::{
|
||||||
|
application::services::{errors::*, *},
|
||||||
|
domain::{commands::*, events::*},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Builder, Getters,
|
||||||
|
)]
|
||||||
|
pub struct Bill {
|
||||||
|
#[builder(default = "OffsetDateTime::now_utc()")]
|
||||||
|
created_time: OffsetDateTime,
|
||||||
|
// bills: Vec<Bill>,
|
||||||
|
bill_id: Uuid,
|
||||||
|
token_number: usize,
|
||||||
|
#[builder(default = "None")]
|
||||||
|
total_price: Option<Price>,
|
||||||
|
store_id: Uuid,
|
||||||
|
|
||||||
|
#[builder(default = "false")]
|
||||||
|
deleted: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Bill {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
created_time: OffsetDateTime::now_utc(),
|
||||||
|
bill_id: Default::default(),
|
||||||
|
token_number: 1,
|
||||||
|
total_price: None,
|
||||||
|
store_id: Uuid::new_v4(),
|
||||||
|
deleted: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use crate::{billing::domain::add_bill_command::AddBillCommand, utils::uuid::tests::UUID};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl Bill {
|
||||||
|
pub fn get_bill() -> Self {
|
||||||
|
let cmd = AddBillCommand::get_cmd();
|
||||||
|
|
||||||
|
BillBuilder::default()
|
||||||
|
.created_time(cmd.created_time().clone())
|
||||||
|
.bill_id(UUID)
|
||||||
|
.store_id(UUID)
|
||||||
|
.token_number(1)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//
|
||||||
|
#[async_trait]
|
||||||
|
impl Aggregate for Bill {
|
||||||
|
type Command = BillingCommand;
|
||||||
|
type Event = BillingEvent;
|
||||||
|
type Error = BillingError;
|
||||||
|
type Services = std::sync::Arc<dyn BillingServicesInterface>;
|
||||||
|
|
||||||
|
// This identifier should be unique to the system.
|
||||||
|
fn aggregate_type() -> String {
|
||||||
|
"billing.bill".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
// The aggregate logic goes here. Note that this will be the _bulk_ of a CQRS system
|
||||||
|
// so expect to use helper functions elsewhere to keep the code clean.
|
||||||
|
async fn handle(
|
||||||
|
&self,
|
||||||
|
command: Self::Command,
|
||||||
|
services: &Self::Services,
|
||||||
|
) -> Result<Vec<Self::Event>, Self::Error> {
|
||||||
|
match command {
|
||||||
|
BillingCommand::AddBill(cmd) => {
|
||||||
|
let res = services.add_bill().add_bill(cmd).await?;
|
||||||
|
Ok(vec![BillingEvent::BillAdded(res)])
|
||||||
|
}
|
||||||
|
BillingCommand::UpdateBill(cmd) => {
|
||||||
|
let res = services.update_bill().update_bill(cmd).await?;
|
||||||
|
Ok(vec![BillingEvent::BillUpdated(res)])
|
||||||
|
}
|
||||||
|
BillingCommand::DeleteBill(cmd) => {
|
||||||
|
let res = services.delete_bill().delete_bill(cmd).await?;
|
||||||
|
Ok(vec![BillingEvent::BillDeleted(res)])
|
||||||
|
}
|
||||||
|
BillingCommand::ComputeBillTotalPriceBill(cmd) => {
|
||||||
|
let res = services
|
||||||
|
.compute_total_price_for_bill()
|
||||||
|
.compute_total_price_for_bill(cmd)
|
||||||
|
.await?;
|
||||||
|
Ok(vec![BillingEvent::BillTotalPriceComputed(res)])
|
||||||
|
}
|
||||||
|
_ => Ok(Vec::default()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply(&mut self, event: Self::Event) {
|
||||||
|
match event {
|
||||||
|
BillingEvent::BillAdded(e) => *self = e.bill().clone(),
|
||||||
|
BillingEvent::BillUpdated(e) => *self = e.new_bill().clone(),
|
||||||
|
BillingEvent::BillTotalPriceComputed(e) => {
|
||||||
|
self.total_price = Some(e.total_price().clone());
|
||||||
|
}
|
||||||
|
BillingEvent::BillDeleted(e) => *self = e.bill().clone(),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod aggregate_tests {
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use add_bill_service::tests::mock_add_bill_service;
|
||||||
|
use compute_bill_total_price_service::tests::mock_compute_bill_total_price_service;
|
||||||
|
use compute_bill_total_price_service::*;
|
||||||
|
use cqrs_es::test::TestFramework;
|
||||||
|
use delete_bill_service::tests::mock_delete_bill_service;
|
||||||
|
use update_bill_service::tests::mock_update_bill_service;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::billing::domain::bill_deleted_event::tests::get_deleted_bill_event_from_command;
|
||||||
|
use crate::billing::domain::bill_total_price_computed_event::tests::get_bill_total_computed_event_from_command;
|
||||||
|
use crate::billing::domain::bill_total_price_computed_event::BillTotalPriceComputedEvent;
|
||||||
|
use crate::billing::domain::bill_updated_event::tests::get_updated_bill_event_from_command;
|
||||||
|
use crate::billing::domain::delete_bill_command::DeleteBillCommand;
|
||||||
|
use crate::billing::domain::update_bill_command::UpdateBillCommand;
|
||||||
|
use crate::tests::bdd::*;
|
||||||
|
|
||||||
|
use crate::billing::domain::{
|
||||||
|
add_bill_command::*, bill_added_event::tests::get_added_bill_event_from_command,
|
||||||
|
bill_total_price_computed_event::tests::*, compute_bill_total_price_command::*,
|
||||||
|
};
|
||||||
|
|
||||||
|
type BillTestFramework = TestFramework<Bill>;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_add_bill() {
|
||||||
|
let cmd = AddBillCommand::get_cmd();
|
||||||
|
let expected = get_added_bill_event_from_command(&cmd);
|
||||||
|
let expected = BillingEvent::BillAdded(expected);
|
||||||
|
|
||||||
|
let mut services = MockBillingServicesInterface::new();
|
||||||
|
services
|
||||||
|
.expect_add_bill()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.return_const(mock_add_bill_service(IS_CALLED_ONLY_ONCE, cmd.clone()));
|
||||||
|
|
||||||
|
BillTestFramework::with(Arc::new(services))
|
||||||
|
.given_no_previous_events()
|
||||||
|
.when(BillingCommand::AddBill(cmd))
|
||||||
|
.then_expect_events(vec![expected]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_update_bill() {
|
||||||
|
let cmd = UpdateBillCommand::get_cmd();
|
||||||
|
let expected = get_updated_bill_event_from_command(&cmd);
|
||||||
|
let expected = BillingEvent::BillUpdated(expected);
|
||||||
|
|
||||||
|
let mut services = MockBillingServicesInterface::new();
|
||||||
|
services
|
||||||
|
.expect_update_bill()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.return_const(mock_update_bill_service(IS_CALLED_ONLY_ONCE, cmd.clone()));
|
||||||
|
|
||||||
|
BillTestFramework::with(Arc::new(services))
|
||||||
|
.given_no_previous_events()
|
||||||
|
.when(BillingCommand::UpdateBill(cmd))
|
||||||
|
.then_expect_events(vec![expected]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_bill_total_price_computed() {
|
||||||
|
let cmd = ComputeBillTotalPriceBillCommand::get_cmd();
|
||||||
|
let expected = get_bill_total_computed_event_from_command(&cmd);
|
||||||
|
let expected = BillingEvent::BillTotalPriceComputed(expected);
|
||||||
|
|
||||||
|
let mut services = MockBillingServicesInterface::new();
|
||||||
|
services
|
||||||
|
.expect_compute_total_price_for_bill()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.return_const(mock_compute_bill_total_price_service(
|
||||||
|
IS_CALLED_ONLY_ONCE,
|
||||||
|
cmd.clone(),
|
||||||
|
));
|
||||||
|
|
||||||
|
BillTestFramework::with(Arc::new(services))
|
||||||
|
.given_no_previous_events()
|
||||||
|
.when(BillingCommand::ComputeBillTotalPriceBill(cmd))
|
||||||
|
.then_expect_events(vec![expected]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_bill() {
|
||||||
|
let cmd = DeleteBillCommand::get_cmd();
|
||||||
|
let expected = get_deleted_bill_event_from_command(&cmd);
|
||||||
|
let expected = BillingEvent::BillDeleted(expected);
|
||||||
|
|
||||||
|
let mut services = MockBillingServicesInterface::new();
|
||||||
|
services
|
||||||
|
.expect_delete_bill()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.return_const(mock_delete_bill_service(IS_CALLED_ONLY_ONCE, cmd.clone()));
|
||||||
|
|
||||||
|
BillTestFramework::with(Arc::new(services))
|
||||||
|
.given_no_previous_events()
|
||||||
|
.when(BillingCommand::DeleteBill(cmd))
|
||||||
|
.then_expect_events(vec![expected]);
|
||||||
|
}
|
||||||
|
}
|
49
src/billing/domain/bill_deleted_event.rs
Normal file
49
src/billing/domain/bill_deleted_event.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::bill_aggregate::*;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd,
|
||||||
|
)]
|
||||||
|
pub struct BillDeletedEvent {
|
||||||
|
added_by_user: Uuid,
|
||||||
|
|
||||||
|
bill: Bill,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use crate::billing::domain::delete_bill_command::DeleteBillCommand;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn get_deleted_bill_event_from_command(cmd: &DeleteBillCommand) -> BillDeletedEvent {
|
||||||
|
let deleted_bill = BillBuilder::default()
|
||||||
|
.created_time(cmd.bill().created_time().clone())
|
||||||
|
.store_id(*cmd.bill().store_id())
|
||||||
|
.bill_id(*cmd.bill().bill_id())
|
||||||
|
.token_number(1)
|
||||||
|
.deleted(true)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
BillDeletedEventBuilder::default()
|
||||||
|
.added_by_user(cmd.adding_by().clone())
|
||||||
|
.bill(deleted_bill)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_event() {
|
||||||
|
let event = get_deleted_bill_event_from_command(&DeleteBillCommand::get_cmd());
|
||||||
|
assert!(event.bill().deleted());
|
||||||
|
}
|
||||||
|
}
|
44
src/billing/domain/bill_total_price_computed_event.rs
Normal file
44
src/billing/domain/bill_total_price_computed_event.rs
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::bill_aggregate::*;
|
||||||
|
use crate::types::currency::*;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd,
|
||||||
|
)]
|
||||||
|
pub struct BillTotalPriceComputedEvent {
|
||||||
|
added_by_user: Uuid,
|
||||||
|
bill_id: Uuid,
|
||||||
|
|
||||||
|
total_price: Price,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use crate::billing::domain::compute_bill_total_price_command::*;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn get_bill_total_computed_event_from_command(
|
||||||
|
cmd: &ComputeBillTotalPriceBillCommand,
|
||||||
|
) -> BillTotalPriceComputedEvent {
|
||||||
|
BillTotalPriceComputedEventBuilder::default()
|
||||||
|
.added_by_user(cmd.adding_by().clone())
|
||||||
|
.bill_id(*cmd.bill_id())
|
||||||
|
.total_price(Price::default())
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_event() {
|
||||||
|
get_bill_total_computed_event_from_command(&ComputeBillTotalPriceBillCommand::get_cmd());
|
||||||
|
}
|
||||||
|
}
|
49
src/billing/domain/bill_updated_event.rs
Normal file
49
src/billing/domain/bill_updated_event.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::bill_aggregate::*;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd,
|
||||||
|
)]
|
||||||
|
pub struct BillUpdatedEvent {
|
||||||
|
added_by_user: Uuid,
|
||||||
|
|
||||||
|
new_bill: Bill,
|
||||||
|
old_bill: Bill,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use crate::billing::domain::update_bill_command::UpdateBillCommand;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn get_updated_bill_event_from_command(cmd: &UpdateBillCommand) -> BillUpdatedEvent {
|
||||||
|
let new_bill = BillBuilder::default()
|
||||||
|
.created_time(cmd.created_time().clone())
|
||||||
|
.token_number(1)
|
||||||
|
.store_id(*cmd.store_id())
|
||||||
|
.bill_id(*cmd.old_bill().bill_id())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
BillUpdatedEventBuilder::default()
|
||||||
|
.added_by_user(cmd.adding_by().clone())
|
||||||
|
.old_bill(cmd.old_bill().clone())
|
||||||
|
.new_bill(new_bill)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_event() {
|
||||||
|
get_updated_bill_event_from_command(&UpdateBillCommand::get_cmd());
|
||||||
|
}
|
||||||
|
}
|
28
src/billing/domain/commands.rs
Normal file
28
src/billing/domain/commands.rs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use mockall::predicate::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
add_bill_command::AddBillCommand, add_line_item_command::AddLineItemCommand,
|
||||||
|
add_store_command::AddStoreCommand,
|
||||||
|
compute_bill_total_price_command::ComputeBillTotalPriceBillCommand,
|
||||||
|
delete_bill_command::DeleteBillCommand, delete_line_item_command::DeleteLineItemCommand,
|
||||||
|
update_bill_command::UpdateBillCommand, update_line_item_command::UpdateLineItemCommand,
|
||||||
|
update_store_command::UpdateStoreCommand,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
|
pub enum BillingCommand {
|
||||||
|
AddLineItem(AddLineItemCommand),
|
||||||
|
UpdateLineItem(UpdateLineItemCommand),
|
||||||
|
DeleteLineItem(DeleteLineItemCommand),
|
||||||
|
AddBill(AddBillCommand),
|
||||||
|
UpdateBill(UpdateBillCommand),
|
||||||
|
DeleteBill(DeleteBillCommand),
|
||||||
|
ComputeBillTotalPriceBill(ComputeBillTotalPriceBillCommand),
|
||||||
|
AddStore(AddStoreCommand),
|
||||||
|
UpdateStore(UpdateStoreCommand),
|
||||||
|
}
|
66
src/billing/domain/compute_bill_total_price_command.rs
Normal file
66
src/billing/domain/compute_bill_total_price_command.rs
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::bill_aggregate::Bill;
|
||||||
|
use crate::types::currency::*;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder,
|
||||||
|
)]
|
||||||
|
pub struct ComputeBillTotalPriceBillCommand {
|
||||||
|
adding_by: Uuid,
|
||||||
|
|
||||||
|
#[builder(default = "OffsetDateTime::now_utc()")]
|
||||||
|
created_time: OffsetDateTime,
|
||||||
|
|
||||||
|
bill_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use time::macros::datetime;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
billing::{self, domain::bill_aggregate::*},
|
||||||
|
utils::uuid::tests::UUID,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl ComputeBillTotalPriceBillCommand {
|
||||||
|
pub fn get_cmd() -> Self {
|
||||||
|
let bill_id = UUID;
|
||||||
|
let adding_by = UUID;
|
||||||
|
|
||||||
|
ComputeBillTotalPriceBillCommandBuilder::default()
|
||||||
|
.adding_by(adding_by)
|
||||||
|
.bill_id(bill_id)
|
||||||
|
.created_time(datetime!(1970-01-01 0:00 UTC))
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmd() {
|
||||||
|
let bill_id = UUID;
|
||||||
|
let adding_by = UUID;
|
||||||
|
|
||||||
|
let cmd = ComputeBillTotalPriceBillCommandBuilder::default()
|
||||||
|
.adding_by(adding_by)
|
||||||
|
.bill_id(bill_id)
|
||||||
|
.created_time(datetime!(1970-01-01 0:00 UTC))
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(*cmd.bill_id(), bill_id);
|
||||||
|
assert_eq!(*cmd.adding_by(), adding_by);
|
||||||
|
}
|
||||||
|
}
|
37
src/billing/domain/delete_bill_command.rs
Normal file
37
src/billing/domain/delete_bill_command.rs
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::bill_aggregate::Bill;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Builder, Getters,
|
||||||
|
)]
|
||||||
|
pub struct DeleteBillCommand {
|
||||||
|
adding_by: Uuid,
|
||||||
|
bill: Bill,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::utils::uuid::tests::UUID;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl DeleteBillCommand {
|
||||||
|
pub fn get_cmd() -> Self {
|
||||||
|
let adding_by = UUID;
|
||||||
|
|
||||||
|
DeleteBillCommandBuilder::default()
|
||||||
|
.adding_by(adding_by)
|
||||||
|
.bill(Bill::get_bill())
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
37
src/billing/domain/delete_line_item_command.rs
Normal file
37
src/billing/domain/delete_line_item_command.rs
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::line_item_aggregate::LineItem;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Builder, Getters,
|
||||||
|
)]
|
||||||
|
pub struct DeleteLineItemCommand {
|
||||||
|
adding_by: Uuid,
|
||||||
|
line_item: LineItem,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::utils::uuid::tests::UUID;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl DeleteLineItemCommand {
|
||||||
|
pub fn get_cmd() -> Self {
|
||||||
|
let adding_by = UUID;
|
||||||
|
|
||||||
|
DeleteLineItemCommandBuilder::default()
|
||||||
|
.adding_by(adding_by)
|
||||||
|
.line_item(LineItem::get_line_item())
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
49
src/billing/domain/events.rs
Normal file
49
src/billing/domain/events.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use cqrs_es::DomainEvent;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
bill_added_event::BillAddedEvent, bill_deleted_event::BillDeletedEvent,
|
||||||
|
bill_total_price_computed_event::BillTotalPriceComputedEvent,
|
||||||
|
bill_updated_event::BillUpdatedEvent, line_item_added_event::LineItemAddedEvent,
|
||||||
|
line_item_deleted_event::LineItemDeletedEvent, line_item_updated_event::LineItemUpdatedEvent,
|
||||||
|
store_added_event::StoreAddedEvent, store_updated_event::StoreUpdatedEvent,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd)]
|
||||||
|
pub enum BillingEvent {
|
||||||
|
LineItemAdded(LineItemAddedEvent),
|
||||||
|
LineItemUpdated(LineItemUpdatedEvent),
|
||||||
|
LineItemDeleted(LineItemDeletedEvent),
|
||||||
|
BillAdded(BillAddedEvent),
|
||||||
|
BillUpdated(BillUpdatedEvent),
|
||||||
|
BillDeleted(BillDeletedEvent),
|
||||||
|
BillTotalPriceComputed(BillTotalPriceComputedEvent),
|
||||||
|
StoreAdded(StoreAddedEvent),
|
||||||
|
StoreUpdated(StoreUpdatedEvent),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DomainEvent for BillingEvent {
|
||||||
|
fn event_version(&self) -> String {
|
||||||
|
"1.0".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn event_type(&self) -> String {
|
||||||
|
let e: &str = match self {
|
||||||
|
BillingEvent::LineItemAdded { .. } => "BillingLineItemAdded",
|
||||||
|
BillingEvent::LineItemUpdated { .. } => "BillingLineItemUpdated",
|
||||||
|
BillingEvent::LineItemDeleted { .. } => "BillingLineItemDeleted",
|
||||||
|
BillingEvent::BillAdded { .. } => "BillingBillAdded",
|
||||||
|
BillingEvent::BillUpdated { .. } => "BillingBillUpdated",
|
||||||
|
BillingEvent::BillDeleted { .. } => "BillingBillDeleted",
|
||||||
|
BillingEvent::BillTotalPriceComputed { .. } => "BillingBillTotalPriceComputed",
|
||||||
|
BillingEvent::StoreAdded { .. } => "BillingStoreAdded",
|
||||||
|
BillingEvent::StoreUpdated { .. } => "BillingStoreUpdated",
|
||||||
|
};
|
||||||
|
|
||||||
|
e.to_string()
|
||||||
|
}
|
||||||
|
}
|
41
src/billing/domain/line_item_added_event.rs
Normal file
41
src/billing/domain/line_item_added_event.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::line_item_aggregate::LineItem;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd,
|
||||||
|
)]
|
||||||
|
pub struct LineItemAddedEvent {
|
||||||
|
added_by_user: Uuid,
|
||||||
|
|
||||||
|
line_item: LineItem,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use crate::billing::domain::add_line_item_command::AddLineItemCommand;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn get_added_line_item_event_from_command(cmd: &AddLineItemCommand) -> LineItemAddedEvent {
|
||||||
|
let line_item = LineItem::get_line_item();
|
||||||
|
|
||||||
|
LineItemAddedEventBuilder::default()
|
||||||
|
.added_by_user(cmd.adding_by().clone())
|
||||||
|
.line_item(line_item)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_event() {
|
||||||
|
get_added_line_item_event_from_command(&AddLineItemCommand::get_cmd());
|
||||||
|
}
|
||||||
|
}
|
225
src/billing/domain/line_item_aggregate.rs
Normal file
225
src/billing/domain/line_item_aggregate.rs
Normal file
|
@ -0,0 +1,225 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use cqrs_es::Aggregate;
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::billing::{
|
||||||
|
application::services::{errors::*, *},
|
||||||
|
domain::{commands::*, events::*},
|
||||||
|
};
|
||||||
|
use crate::types::{currency::Price, quantity::Quantity};
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Builder, Getters,
|
||||||
|
)]
|
||||||
|
pub struct LineItem {
|
||||||
|
#[builder(default = "OffsetDateTime::now_utc()")]
|
||||||
|
created_time: OffsetDateTime,
|
||||||
|
product_name: String,
|
||||||
|
product_id: Uuid,
|
||||||
|
bill_id: Uuid,
|
||||||
|
line_item_id: Uuid,
|
||||||
|
quantity: Quantity,
|
||||||
|
price_per_unit: Price,
|
||||||
|
#[builder(default = "false")]
|
||||||
|
deleted: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for LineItem {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
created_time: OffsetDateTime::now_utc(),
|
||||||
|
product_name: String::default(),
|
||||||
|
product_id: Default::default(),
|
||||||
|
line_item_id: Default::default(),
|
||||||
|
bill_id: Default::default(),
|
||||||
|
quantity: Default::default(),
|
||||||
|
price_per_unit: Price::default(),
|
||||||
|
deleted: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LineItem {
|
||||||
|
pub fn total_price(&self) -> Price {
|
||||||
|
let price_per_unit_as_minor =
|
||||||
|
self.price_per_unit().major_as_minor() + self.price_per_unit().minor();
|
||||||
|
let total_price_as_minor = if self.quantity().major().is_dividable() {
|
||||||
|
(self.quantity().major_as_minor().unwrap() // TODO: handle err
|
||||||
|
+ self.quantity().minor().number())
|
||||||
|
* price_per_unit_as_minor
|
||||||
|
} else {
|
||||||
|
self.quantity().major().number() * price_per_unit_as_minor
|
||||||
|
};
|
||||||
|
|
||||||
|
Price::from_minor(
|
||||||
|
total_price_as_minor,
|
||||||
|
self.price_per_unit().currency().clone(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use crate::{
|
||||||
|
billing::domain::add_line_item_command::AddLineItemCommand, utils::uuid::tests::UUID,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl LineItem {
|
||||||
|
pub fn get_line_item() -> Self {
|
||||||
|
let cmd = AddLineItemCommand::get_cmd();
|
||||||
|
|
||||||
|
LineItemBuilder::default()
|
||||||
|
.created_time(cmd.created_time().clone())
|
||||||
|
.product_name("test_product".into())
|
||||||
|
.product_id(*cmd.product_id())
|
||||||
|
.quantity(cmd.quantity().clone())
|
||||||
|
.bill_id(*cmd.bill_id())
|
||||||
|
.price_per_unit(cmd.price_per_unit().clone())
|
||||||
|
.line_item_id(*cmd.line_item_id())
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Aggregate for LineItem {
|
||||||
|
type Command = BillingCommand;
|
||||||
|
type Event = BillingEvent;
|
||||||
|
type Error = BillingError;
|
||||||
|
type Services = std::sync::Arc<dyn BillingServicesInterface>;
|
||||||
|
|
||||||
|
// This identifier should be unique to the system.
|
||||||
|
fn aggregate_type() -> String {
|
||||||
|
"billing.line_item".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
// The aggregate logic goes here. Note that this will be the _bulk_ of a CQRS system
|
||||||
|
// so expect to use helper functions elsewhere to keep the code clean.
|
||||||
|
async fn handle(
|
||||||
|
&self,
|
||||||
|
command: Self::Command,
|
||||||
|
services: &Self::Services,
|
||||||
|
) -> Result<Vec<Self::Event>, Self::Error> {
|
||||||
|
match command {
|
||||||
|
BillingCommand::AddLineItem(cmd) => {
|
||||||
|
let res = services.add_line_item().add_line_item(cmd).await?;
|
||||||
|
Ok(vec![BillingEvent::LineItemAdded(res)])
|
||||||
|
}
|
||||||
|
BillingCommand::UpdateLineItem(cmd) => {
|
||||||
|
let res = services.update_line_item().update_line_item(cmd).await?;
|
||||||
|
Ok(vec![BillingEvent::LineItemUpdated(res)])
|
||||||
|
}
|
||||||
|
BillingCommand::DeleteLineItem(cmd) => {
|
||||||
|
let res = services.delete_line_item().delete_line_item(cmd).await?;
|
||||||
|
Ok(vec![BillingEvent::LineItemDeleted(res)])
|
||||||
|
}
|
||||||
|
_ => Ok(Vec::default()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply(&mut self, event: Self::Event) {
|
||||||
|
match event {
|
||||||
|
BillingEvent::LineItemAdded(e) => *self = e.line_item().clone(),
|
||||||
|
BillingEvent::LineItemUpdated(e) => *self = e.new_line_item().clone(),
|
||||||
|
BillingEvent::LineItemDeleted(e) => *self = e.line_item().clone(),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod aggregate_tests {
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use add_line_item_service::tests::mock_add_line_item_service;
|
||||||
|
use cqrs_es::test::TestFramework;
|
||||||
|
use delete_line_item_service::tests::mock_delete_line_item_service;
|
||||||
|
use update_line_item_service::tests::mock_update_line_item_service;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use crate::billing::application::services::*;
|
||||||
|
use crate::billing::domain::delete_line_item_command::DeleteLineItemCommand;
|
||||||
|
use crate::billing::domain::line_item_deleted_event::tests::get_deleted_line_item_event_from_command;
|
||||||
|
use crate::billing::domain::line_item_updated_event::tests::get_updated_line_item_event_from_command;
|
||||||
|
use crate::billing::domain::update_line_item_command::UpdateLineItemCommand;
|
||||||
|
use crate::tests::bdd::*;
|
||||||
|
|
||||||
|
use crate::billing::domain::{
|
||||||
|
add_line_item_command::*,
|
||||||
|
line_item_added_event::tests::get_added_line_item_event_from_command,
|
||||||
|
};
|
||||||
|
|
||||||
|
type LineItemTestFramework = TestFramework<LineItem>;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_add_line_item() {
|
||||||
|
let cmd = AddLineItemCommand::get_cmd();
|
||||||
|
let expected = get_added_line_item_event_from_command(&cmd);
|
||||||
|
let expected = BillingEvent::LineItemAdded(expected);
|
||||||
|
|
||||||
|
let mut services = MockBillingServicesInterface::new();
|
||||||
|
services
|
||||||
|
.expect_add_line_item()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.return_const(mock_add_line_item_service(IS_CALLED_ONLY_ONCE, cmd.clone()));
|
||||||
|
|
||||||
|
LineItemTestFramework::with(Arc::new(services))
|
||||||
|
.given_no_previous_events()
|
||||||
|
.when(BillingCommand::AddLineItem(cmd))
|
||||||
|
.then_expect_events(vec![expected]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_update_line_item() {
|
||||||
|
let cmd = UpdateLineItemCommand::get_cmd();
|
||||||
|
let expected = get_updated_line_item_event_from_command(&cmd);
|
||||||
|
let expected = BillingEvent::LineItemUpdated(expected);
|
||||||
|
|
||||||
|
let mut services = MockBillingServicesInterface::new();
|
||||||
|
services
|
||||||
|
.expect_update_line_item()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.return_const(mock_update_line_item_service(
|
||||||
|
IS_CALLED_ONLY_ONCE,
|
||||||
|
cmd.clone(),
|
||||||
|
));
|
||||||
|
|
||||||
|
LineItemTestFramework::with(Arc::new(services))
|
||||||
|
.given_no_previous_events()
|
||||||
|
.when(BillingCommand::UpdateLineItem(cmd))
|
||||||
|
.then_expect_events(vec![expected]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_line_item() {
|
||||||
|
let cmd = DeleteLineItemCommand::get_cmd();
|
||||||
|
let expected = get_deleted_line_item_event_from_command(&cmd);
|
||||||
|
let expected = BillingEvent::LineItemDeleted(expected);
|
||||||
|
|
||||||
|
let mut services = MockBillingServicesInterface::new();
|
||||||
|
services
|
||||||
|
.expect_delete_line_item()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.return_const(mock_delete_line_item_service(
|
||||||
|
IS_CALLED_ONLY_ONCE,
|
||||||
|
cmd.clone(),
|
||||||
|
));
|
||||||
|
|
||||||
|
LineItemTestFramework::with(Arc::new(services))
|
||||||
|
.given_no_previous_events()
|
||||||
|
.when(BillingCommand::DeleteLineItem(cmd))
|
||||||
|
.then_expect_events(vec![expected]);
|
||||||
|
}
|
||||||
|
}
|
54
src/billing/domain/line_item_deleted_event.rs
Normal file
54
src/billing/domain/line_item_deleted_event.rs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::line_item_aggregate::*;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd,
|
||||||
|
)]
|
||||||
|
pub struct LineItemDeletedEvent {
|
||||||
|
added_by_user: Uuid,
|
||||||
|
|
||||||
|
line_item: LineItem,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use crate::billing::domain::delete_line_item_command::DeleteLineItemCommand;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn get_deleted_line_item_event_from_command(
|
||||||
|
cmd: &DeleteLineItemCommand,
|
||||||
|
) -> LineItemDeletedEvent {
|
||||||
|
let deleted_line_item = LineItemBuilder::default()
|
||||||
|
.created_time(cmd.line_item().created_time().clone())
|
||||||
|
.product_name(cmd.line_item().product_name().into())
|
||||||
|
.product_id(*cmd.line_item().product_id())
|
||||||
|
.bill_id(*cmd.line_item().bill_id())
|
||||||
|
.line_item_id(*cmd.line_item().line_item_id())
|
||||||
|
.price_per_unit(cmd.line_item().price_per_unit().clone())
|
||||||
|
.quantity(cmd.line_item().quantity().clone())
|
||||||
|
.deleted(true)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
LineItemDeletedEventBuilder::default()
|
||||||
|
.added_by_user(cmd.adding_by().clone())
|
||||||
|
.line_item(deleted_line_item)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_event() {
|
||||||
|
let event = get_deleted_line_item_event_from_command(&DeleteLineItemCommand::get_cmd());
|
||||||
|
assert!(event.line_item().deleted());
|
||||||
|
}
|
||||||
|
}
|
54
src/billing/domain/line_item_updated_event.rs
Normal file
54
src/billing/domain/line_item_updated_event.rs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::line_item_aggregate::*;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd,
|
||||||
|
)]
|
||||||
|
pub struct LineItemUpdatedEvent {
|
||||||
|
added_by_user: Uuid,
|
||||||
|
|
||||||
|
new_line_item: LineItem,
|
||||||
|
old_line_item: LineItem,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use crate::billing::domain::update_line_item_command::UpdateLineItemCommand;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn get_updated_line_item_event_from_command(
|
||||||
|
cmd: &UpdateLineItemCommand,
|
||||||
|
) -> LineItemUpdatedEvent {
|
||||||
|
let new_line_item = LineItemBuilder::default()
|
||||||
|
.created_time(cmd.created_time().clone())
|
||||||
|
.product_name(cmd.product_name().clone())
|
||||||
|
.product_id(*cmd.product_id())
|
||||||
|
.bill_id(*cmd.bill_id())
|
||||||
|
.price_per_unit(cmd.price_per_unit().clone())
|
||||||
|
.quantity(cmd.quantity().clone())
|
||||||
|
.line_item_id(*cmd.old_line_item().line_item_id())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
LineItemUpdatedEventBuilder::default()
|
||||||
|
.added_by_user(cmd.adding_by().clone())
|
||||||
|
.old_line_item(cmd.old_line_item().clone())
|
||||||
|
.new_line_item(new_line_item)
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_event() {
|
||||||
|
get_updated_line_item_event_from_command(&UpdateLineItemCommand::get_cmd());
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,32 @@
|
||||||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
// aggregates
|
||||||
|
pub mod bill_aggregate;
|
||||||
|
pub mod line_item_aggregate;
|
||||||
|
pub mod store_aggregate;
|
||||||
|
|
||||||
|
// commands
|
||||||
|
pub mod add_bill_command;
|
||||||
|
pub mod add_line_item_command;
|
||||||
|
pub mod add_store_command;
|
||||||
|
pub mod commands;
|
||||||
|
pub mod compute_bill_total_price_command;
|
||||||
|
pub mod delete_bill_command;
|
||||||
|
pub mod delete_line_item_command;
|
||||||
|
pub mod update_bill_command;
|
||||||
|
pub mod update_line_item_command;
|
||||||
|
pub mod update_store_command;
|
||||||
|
|
||||||
|
// events;
|
||||||
|
pub mod bill_added_event;
|
||||||
|
pub mod bill_deleted_event;
|
||||||
|
pub mod bill_total_price_computed_event;
|
||||||
|
pub mod bill_updated_event;
|
||||||
|
pub mod events;
|
||||||
|
pub mod line_item_added_event;
|
||||||
|
pub mod line_item_deleted_event;
|
||||||
|
pub mod line_item_updated_event;
|
||||||
|
pub mod store_added_event;
|
||||||
|
pub mod store_updated_event;
|
||||||
|
|
18
src/billing/domain/store_added_event.rs
Normal file
18
src/billing/domain/store_added_event.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd,
|
||||||
|
)]
|
||||||
|
pub struct StoreAddedEvent {
|
||||||
|
name: String,
|
||||||
|
address: Option<String>,
|
||||||
|
owner: Uuid,
|
||||||
|
store_id: Uuid,
|
||||||
|
}
|
152
src/billing/domain/store_aggregate.rs
Normal file
152
src/billing/domain/store_aggregate.rs
Normal file
|
@ -0,0 +1,152 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use cqrs_es::Aggregate;
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::billing::application::services::errors::*;
|
||||||
|
use crate::billing::application::services::*;
|
||||||
|
|
||||||
|
use super::{commands::BillingCommand, events::BillingEvent};
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Default, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Builder, Getters,
|
||||||
|
)]
|
||||||
|
pub struct Store {
|
||||||
|
name: String,
|
||||||
|
address: Option<String>,
|
||||||
|
owner: Uuid,
|
||||||
|
store_id: Uuid,
|
||||||
|
#[builder(default = "false")]
|
||||||
|
deleted: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Aggregate for Store {
|
||||||
|
type Command = BillingCommand;
|
||||||
|
type Event = BillingEvent;
|
||||||
|
type Error = BillingError;
|
||||||
|
type Services = std::sync::Arc<dyn BillingServicesInterface>;
|
||||||
|
|
||||||
|
// This identifier should be unique to the system.
|
||||||
|
fn aggregate_type() -> String {
|
||||||
|
"billing.store".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
// The aggregate logic goes here. Note that this will be the _bulk_ of a CQRS system
|
||||||
|
// so expect to use helper functions elsewhere to keep the code clean.
|
||||||
|
async fn handle(
|
||||||
|
&self,
|
||||||
|
command: Self::Command,
|
||||||
|
services: &Self::Services,
|
||||||
|
) -> Result<Vec<Self::Event>, Self::Error> {
|
||||||
|
match command {
|
||||||
|
BillingCommand::AddStore(cmd) => {
|
||||||
|
let res = services.add_store().add_store(cmd).await?;
|
||||||
|
Ok(vec![BillingEvent::StoreAdded(res)])
|
||||||
|
}
|
||||||
|
BillingCommand::UpdateStore(cmd) => {
|
||||||
|
let res = services.update_store().update_store(cmd).await?;
|
||||||
|
Ok(vec![BillingEvent::StoreUpdated(res)])
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => Ok(Vec::default()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply(&mut self, event: Self::Event) {
|
||||||
|
match event {
|
||||||
|
BillingEvent::StoreAdded(e) => {
|
||||||
|
self.name = e.name().into();
|
||||||
|
self.address = e.address().as_ref().map(|s| s.to_string());
|
||||||
|
self.owner = *e.owner();
|
||||||
|
self.store_id = *e.store_id();
|
||||||
|
self.deleted = false;
|
||||||
|
}
|
||||||
|
BillingEvent::StoreUpdated(e) => *self = e.new_store().clone(),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use cqrs_es::test::TestFramework;
|
||||||
|
use update_store_service::tests::mock_update_store_service;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::billing::{
|
||||||
|
application::services::add_store_service::tests::*,
|
||||||
|
domain::{
|
||||||
|
add_store_command::*, commands::BillingCommand, events::BillingEvent,
|
||||||
|
store_added_event::*, store_updated_event::tests::get_store_updated_event_from_command,
|
||||||
|
update_store_command::tests::get_update_store_cmd,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
use crate::tests::bdd::*;
|
||||||
|
use crate::utils::uuid::tests::*;
|
||||||
|
|
||||||
|
// A test framework that will apply our events and command
|
||||||
|
// and verify that the logic works as expected.
|
||||||
|
type StoreTestFramework = TestFramework<Store>;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_create_store() {
|
||||||
|
let name = "store_name";
|
||||||
|
let address = Some("store_address".to_string());
|
||||||
|
let owner = UUID;
|
||||||
|
let store_id = UUID;
|
||||||
|
|
||||||
|
let expected = StoreAddedEventBuilder::default()
|
||||||
|
.name(name.into())
|
||||||
|
.address(address.clone())
|
||||||
|
.store_id(store_id)
|
||||||
|
.owner(owner)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
let expected = BillingEvent::StoreAdded(expected);
|
||||||
|
|
||||||
|
let cmd = AddStoreCommandBuilder::default()
|
||||||
|
.name(name.into())
|
||||||
|
.address(address.clone())
|
||||||
|
.owner(owner)
|
||||||
|
.store_id(UUID)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut services = MockBillingServicesInterface::new();
|
||||||
|
services
|
||||||
|
.expect_add_store()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.return_const(mock_add_store_service(IS_CALLED_ONLY_ONCE, cmd.clone()));
|
||||||
|
|
||||||
|
StoreTestFramework::with(Arc::new(services))
|
||||||
|
.given_no_previous_events()
|
||||||
|
.when(BillingCommand::AddStore(cmd))
|
||||||
|
.then_expect_events(vec![expected]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_update_store() {
|
||||||
|
let cmd = get_update_store_cmd();
|
||||||
|
let expected = BillingEvent::StoreUpdated(get_store_updated_event_from_command(&cmd));
|
||||||
|
|
||||||
|
let mut services = MockBillingServicesInterface::new();
|
||||||
|
services
|
||||||
|
.expect_update_store()
|
||||||
|
.times(IS_CALLED_ONLY_ONCE.unwrap())
|
||||||
|
.return_const(mock_update_store_service(IS_CALLED_ONLY_ONCE, cmd.clone()));
|
||||||
|
|
||||||
|
StoreTestFramework::with(Arc::new(services))
|
||||||
|
.given_no_previous_events()
|
||||||
|
.when(BillingCommand::UpdateStore(cmd))
|
||||||
|
.then_expect_events(vec![expected]);
|
||||||
|
}
|
||||||
|
}
|
43
src/billing/domain/store_updated_event.rs
Normal file
43
src/billing/domain/store_updated_event.rs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::store_aggregate::*;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Builder, Serialize, Deserialize, Getters, Eq, PartialEq, Ord, PartialOrd,
|
||||||
|
)]
|
||||||
|
pub struct StoreUpdatedEvent {
|
||||||
|
added_by_user: Uuid,
|
||||||
|
old_store: Store,
|
||||||
|
new_store: Store,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod tests {
|
||||||
|
use crate::billing::domain::update_store_command::UpdateStoreCommand;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub fn get_store_updated_event_from_command(cmd: &UpdateStoreCommand) -> StoreUpdatedEvent {
|
||||||
|
let new_store = StoreBuilder::default()
|
||||||
|
.name(cmd.name().into())
|
||||||
|
.address(cmd.address().as_ref().map(|s| s.to_string()))
|
||||||
|
.owner(*cmd.owner())
|
||||||
|
.store_id(*cmd.old_store().store_id())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
StoreUpdatedEventBuilder::default()
|
||||||
|
.new_store(new_store)
|
||||||
|
.old_store(cmd.old_store().clone())
|
||||||
|
.added_by_user(*cmd.adding_by())
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
71
src/billing/domain/update_bill_command.rs
Normal file
71
src/billing/domain/update_bill_command.rs
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use derive_getters::Getters;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::bill_aggregate::Bill;
|
||||||
|
use crate::types::currency::*;
|
||||||
|
|
||||||
|
#[derive(
|
||||||
|
Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters, Builder,
|
||||||
|
)]
|
||||||
|
pub struct UpdateBillCommand {
|
||||||
|
adding_by: Uuid,
|
||||||
|
|
||||||
|
#[builder(default = "OffsetDateTime::now_utc()")]
|
||||||
|
created_time: OffsetDateTime,
|
||||||
|
|
||||||
|
store_id: Uuid,
|
||||||
|
total_price: Option<Price>,
|
||||||
|
|
||||||
|
old_bill: Bill,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use time::macros::datetime;
|
||||||
|
|
||||||
|
use crate::{billing::domain::bill_aggregate::*, utils::uuid::tests::UUID};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
impl UpdateBillCommand {
|
||||||
|
pub fn get_cmd() -> Self {
|
||||||
|
let store_id = UUID;
|
||||||
|
let adding_by = UUID;
|
||||||
|
|
||||||
|
UpdateBillCommandBuilder::default()
|
||||||
|
.created_time(datetime!(1970-01-01 0:00 UTC))
|
||||||
|
.adding_by(adding_by)
|
||||||
|
.store_id(store_id)
|
||||||
|
.total_price(None)
|
||||||
|
.old_bill(Bill::get_bill())
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmd() {
|
||||||
|
let store_id = UUID;
|
||||||
|
let adding_by = UUID;
|
||||||
|
let old_bill = Bill::get_bill();
|
||||||
|
|
||||||
|
let cmd = UpdateBillCommandBuilder::default()
|
||||||
|
.adding_by(adding_by)
|
||||||
|
.store_id(store_id)
|
||||||
|
.total_price(None)
|
||||||
|
.old_bill(old_bill.clone())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(*cmd.store_id(), store_id);
|
||||||
|
assert_eq!(*cmd.adding_by(), adding_by);
|
||||||
|
assert_eq!(cmd.old_bill(), &old_bill);
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue