Skip to content

Commit 8bb485c

Browse files
committed
stock management experiments
1 parent 6e1bc5c commit 8bb485c

File tree

7 files changed

+29
-8
lines changed

7 files changed

+29
-8
lines changed

bookstore-server/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/gorilla/mux v1.8.0
88
github.com/joho/godotenv v1.3.0
99
github.com/klauspost/compress v1.11.4 // indirect
10-
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224192621-2c3e5eadc134
10+
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224203702-6e1bc5c24f96
1111
github.com/rs/cors v1.7.0
1212
go.mongodb.org/mongo-driver v1.4.4
1313
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect

bookstore-server/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kN
7979
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
8080
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224192621-2c3e5eadc134 h1:EsUTCoMd8IRWiGKPHVuPVcKLcAHJ1xzr9/F6YmN0rRo=
8181
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224192621-2c3e5eadc134/go.mod h1:MVzZrcV+oOYjwGRmps0a4y2PE/3ABwWv+8G6Vsg5QQo=
82+
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224201644-9daa352f5789 h1:dnrXgtv5akRpQrlmHFStBS/rOhppRzGISBBgFHc+L+o=
83+
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224201644-9daa352f5789/go.mod h1:MVzZrcV+oOYjwGRmps0a4y2PE/3ABwWv+8G6Vsg5QQo=
84+
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224203702-6e1bc5c24f96 h1:icOWuFuP6uE5o7JC6tpl1KpX74MNDD3VpSqkb9ZL8kE=
85+
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224203702-6e1bc5c24f96/go.mod h1:MVzZrcV+oOYjwGRmps0a4y2PE/3ABwWv+8G6Vsg5QQo=
8286
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
8387
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
8488
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

bookstore-server/service/checkout.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,23 @@ func NewCheckoutService(p *kafka.Producer, topics *CheckoutTopics, collection *m
3131

3232
func (s CheckoutService) CheckoutCart(cart *dto.Cart) *dto.CartResponse {
3333
id := primitive.NewObjectID()
34-
go s.stageStock(cart.Items)
34+
doneUpdate := make(chan bool)
35+
go s.stageStock(cart.Items, doneUpdate)
3536
go s.requestCartShipment(cart, id)
3637
go s.requestCartPayment(cart, id)
3738
cartResponse := dto.CartResponse{CartID: id, Status: "requested"}
39+
<-doneUpdate
3840
return &cartResponse
3941

4042
}
4143

42-
func (s CheckoutService) stageStock(items []dto.CartItem) {
44+
func (s CheckoutService) stageStock(items []dto.CartItem, done chan bool) {
4345
ctx, cancel := context.WithTimeout(context.Background(), CTXTimeout*time.Second)
4446
defer cancel()
47+
defer func() {
48+
done <- true
49+
}()
50+
4551
for _, item := range items {
4652
log.Println("updating stock for book", item.Book)
4753
log.Println("Following qty will be staged", item.Qty)

seed-bookstore/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface Book {
1212
const uri = process.env.MONGO_URI || "mongodb://localhost:27017";
1313
const dbName = process.env.DB_NAME || "bookstore";
1414
const collName = process.env.MONGO_COLL || "books";
15-
const SIZE = 5;
15+
const SIZE = 15;
1616

1717
const client = new MongoClient(uri, { useUnifiedTopology: true });
1818

warehouse-server/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/aws/aws-sdk-go v1.36.15 // indirect
77
github.com/joho/godotenv v1.3.0
88
github.com/klauspost/compress v1.11.4 // indirect
9-
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224192621-2c3e5eadc134
9+
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224203702-6e1bc5c24f96
1010
go.mongodb.org/mongo-driver v1.4.4
1111
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
1212
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect

warehouse-server/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
7272
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
7373
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
7474
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
75+
github.com/mtanzim/event-driven-bookstore v0.0.0-20201224201644-9daa352f5789 h1:/6HfK/QSCR/84iWDNSwP5j+yXkR1rlIRlCA4nqtYF1M=
76+
github.com/mtanzim/event-driven-bookstore v0.0.0-20201224203702-6e1bc5c24f96 h1:8ktrSYjtDygmGChdHSqMlSabaa5kHo2Iy22Yy14PUak=
7577
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201218123655-8698acd0eeba h1:iw7OiWZMjCpE7qFdYs7BzrXsVl+rXqQM3pE0UP6fdaQ=
7678
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201218123655-8698acd0eeba/go.mod h1:oyIArPUvcShGDKt2HibHNtknD+W1PwIPHNnBp/8vsL8=
7779
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201218130814-89d0f3ce984a h1:9WSsk+hlc2SaznxeI2bE27w6IlDnsJgJ5hNnekV81KM=
@@ -82,6 +84,10 @@ github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201221232549-47
8284
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201221232549-4778e055d153/go.mod h1:MVzZrcV+oOYjwGRmps0a4y2PE/3ABwWv+8G6Vsg5QQo=
8385
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224192621-2c3e5eadc134 h1:EsUTCoMd8IRWiGKPHVuPVcKLcAHJ1xzr9/F6YmN0rRo=
8486
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224192621-2c3e5eadc134/go.mod h1:MVzZrcV+oOYjwGRmps0a4y2PE/3ABwWv+8G6Vsg5QQo=
87+
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224201644-9daa352f5789 h1:dnrXgtv5akRpQrlmHFStBS/rOhppRzGISBBgFHc+L+o=
88+
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224201644-9daa352f5789/go.mod h1:MVzZrcV+oOYjwGRmps0a4y2PE/3ABwWv+8G6Vsg5QQo=
89+
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224203702-6e1bc5c24f96 h1:icOWuFuP6uE5o7JC6tpl1KpX74MNDD3VpSqkb9ZL8kE=
90+
github.com/mtanzim/event-driven-bookstore/common-server v0.0.0-20201224203702-6e1bc5c24f96/go.mod h1:MVzZrcV+oOYjwGRmps0a4y2PE/3ABwWv+8G6Vsg5QQo=
8591
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
8692
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
8793
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

warehouse-server/service/payment.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ func (s PaymentStatusService) processPaymentStatus(msg *kafka.Message) {
3535
go s.persist(paymentStatus)
3636
}
3737

38-
// TODO: synchronize? What if payment notification comes before the shipment request is registered
38+
// TODO: synchronize?
3939
// Is this a proper way to build a DLQ?
40-
// Can this be done with Kafka? Come back to this later
40+
// Can this be done with Kafka? Come back to this later.
4141
func (s PaymentStatusService) persist(paymentStatus dto.CartPaymentResponse) {
4242
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
4343
defer cancel()
4444
log.Println("Updating payment status")
4545
log.Println(paymentStatus)
4646

4747
if paymentStatus.Approved {
48-
update := bson.D{{"$set", bson.D{{"paid", true}}}}
4948
filter := bson.M{"_id": paymentStatus.CartID}
49+
update := bson.D{{"$set", bson.D{{"paid", true}}}}
5050
updateRes, err := s.warehouseCollection.UpdateOne(ctx, filter, update)
5151
if err != nil || updateRes.ModifiedCount != 1 {
5252
log.Println("Failed to update shipment payment status for cart id:", paymentStatus.CartID)
@@ -58,5 +58,10 @@ func (s PaymentStatusService) persist(paymentStatus dto.CartPaymentResponse) {
5858
}
5959

6060
}
61+
// TODO: Payment rejection logic
62+
// if rejected, fire of 'unstage' message on broker with []CartItem
63+
// store microservice can update staged and stock qty
64+
// Alternatively store can directly listen for payment rejections?
65+
// Then, store will need cart persistence as well
6166

6267
}

0 commit comments

Comments
 (0)