mirror of
https://github.com/hexolan/stocklet.git
synced 2026-03-26 11:41:18 +00:00
chore: initial commit
This commit is contained in:
39
internal/svc/warehouse/api/gateway.go
Normal file
39
internal/svc/warehouse/api/gateway.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/warehouse/v1"
|
||||
"github.com/hexolan/stocklet/internal/pkg/serve"
|
||||
"github.com/hexolan/stocklet/internal/svc/warehouse"
|
||||
)
|
||||
|
||||
func PrepareGateway(cfg *warehouse.ServiceConfig) *runtime.ServeMux {
|
||||
mux, clientOpts := serve.NewGatewayServeBase(&cfg.Shared)
|
||||
|
||||
ctx := context.Background()
|
||||
err := pb.RegisterWarehouseServiceHandlerFromEndpoint(ctx, mux, serve.GetAddrToGrpc("localhost"), clientOpts)
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("failed to register endpoint for gateway")
|
||||
}
|
||||
|
||||
return mux
|
||||
}
|
||||
30
internal/svc/warehouse/api/grpc.go
Normal file
30
internal/svc/warehouse/api/grpc.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"google.golang.org/grpc"
|
||||
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/warehouse/v1"
|
||||
"github.com/hexolan/stocklet/internal/pkg/serve"
|
||||
"github.com/hexolan/stocklet/internal/svc/warehouse"
|
||||
)
|
||||
|
||||
func PrepareGrpc(cfg *warehouse.ServiceConfig, svc *warehouse.WarehouseService) *grpc.Server {
|
||||
svr := serve.NewGrpcServeBase(&cfg.Shared)
|
||||
pb.RegisterWarehouseServiceServer(svr, svc)
|
||||
return svr
|
||||
}
|
||||
42
internal/svc/warehouse/config.go
Normal file
42
internal/svc/warehouse/config.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package warehouse
|
||||
|
||||
import (
|
||||
"github.com/hexolan/stocklet/internal/pkg/config"
|
||||
)
|
||||
|
||||
// Order Service Configuration
|
||||
type ServiceConfig struct {
|
||||
// Core Configuration
|
||||
Shared config.SharedConfig
|
||||
|
||||
// Dynamically loaded configuration
|
||||
Postgres config.PostgresConfig
|
||||
Kafka config.KafkaConfig
|
||||
}
|
||||
|
||||
// load the base service configuration
|
||||
func NewServiceConfig() (*ServiceConfig, error) {
|
||||
cfg := ServiceConfig{}
|
||||
|
||||
// Load the core configuration
|
||||
if err := cfg.Shared.Load(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
161
internal/svc/warehouse/controller/kafka.go
Normal file
161
internal/svc/warehouse/controller/kafka.go
Normal file
@@ -0,0 +1,161 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/twmb/franz-go/pkg/kgo"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/hexolan/stocklet/internal/pkg/messaging"
|
||||
eventpb "github.com/hexolan/stocklet/internal/pkg/protogen/events/v1"
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/warehouse/v1"
|
||||
"github.com/hexolan/stocklet/internal/svc/warehouse"
|
||||
)
|
||||
|
||||
type kafkaController struct {
|
||||
cl *kgo.Client
|
||||
|
||||
svc pb.WarehouseServiceServer
|
||||
|
||||
ctx context.Context
|
||||
ctxCancel context.CancelFunc
|
||||
}
|
||||
|
||||
func NewKafkaController(cl *kgo.Client) warehouse.ConsumerController {
|
||||
// Create a cancellable context for the consumer
|
||||
ctx, ctxCancel := context.WithCancel(context.Background())
|
||||
|
||||
// Ensure the required Kafka topics exist
|
||||
err := messaging.EnsureKafkaTopics(
|
||||
cl,
|
||||
|
||||
messaging.Warehouse_Stock_Created_Topic,
|
||||
messaging.Warehouse_Stock_Added_Topic,
|
||||
messaging.Warehouse_Stock_Removed_Topic,
|
||||
messaging.Warehouse_Reservation_Failed_Topic,
|
||||
messaging.Warehouse_Reservation_Reserved_Topic,
|
||||
messaging.Warehouse_Reservation_Returned_Topic,
|
||||
messaging.Warehouse_Reservation_Consumed_Topic,
|
||||
|
||||
messaging.Order_State_Pending_Topic,
|
||||
messaging.Shipping_Shipment_Allocation_Topic,
|
||||
messaging.Payment_Processing_Topic,
|
||||
)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("kafka: raised attempting to ensure svc topics")
|
||||
}
|
||||
|
||||
// Add the consumption topics
|
||||
cl.AddConsumeTopics(
|
||||
messaging.Order_State_Pending_Topic,
|
||||
messaging.Shipping_Shipment_Allocation_Topic,
|
||||
messaging.Payment_Processing_Topic,
|
||||
)
|
||||
|
||||
return &kafkaController{cl: cl, ctx: ctx, ctxCancel: ctxCancel}
|
||||
}
|
||||
|
||||
func (c *kafkaController) Attach(svc pb.WarehouseServiceServer) {
|
||||
c.svc = svc
|
||||
}
|
||||
|
||||
func (c *kafkaController) Start() {
|
||||
if c.svc == nil {
|
||||
log.Panic().Msg("consumer: no service interface attached")
|
||||
}
|
||||
|
||||
for {
|
||||
fetches := c.cl.PollFetches(c.ctx)
|
||||
if errs := fetches.Errors(); len(errs) > 0 {
|
||||
log.Panic().Any("kafka-errs", errs).Msg("consumer: unrecoverable kafka errors")
|
||||
}
|
||||
|
||||
fetches.EachTopic(func(ft kgo.FetchTopic) {
|
||||
switch ft.Topic {
|
||||
case messaging.Order_State_Pending_Topic:
|
||||
c.consumeOrderPendingEventTopic(ft)
|
||||
case messaging.Shipping_Shipment_Allocation_Topic:
|
||||
c.consumeShipmentAllocationEventTopic(ft)
|
||||
case messaging.Payment_Processing_Topic:
|
||||
c.consumePaymentProcessedEventTopic(ft)
|
||||
default:
|
||||
log.Warn().Str("topic", ft.Topic).Msg("consumer: recieved records from unexpected topic")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (c *kafkaController) Stop() {
|
||||
// Cancel the consumer context
|
||||
c.ctxCancel()
|
||||
}
|
||||
|
||||
func (c *kafkaController) consumeOrderPendingEventTopic(ft kgo.FetchTopic) {
|
||||
log.Info().Str("topic", ft.Topic).Msg("consumer: recieved records from topic")
|
||||
|
||||
// Process each message from the topic
|
||||
ft.EachRecord(func(record *kgo.Record) {
|
||||
// Unmarshal the event
|
||||
var event eventpb.OrderPendingEvent
|
||||
err := proto.Unmarshal(record.Value, &event)
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("consumer: failed to unmarshal event")
|
||||
}
|
||||
|
||||
// Process the event
|
||||
ctx := context.Background()
|
||||
c.svc.ProcessOrderPendingEvent(ctx, &event)
|
||||
})
|
||||
}
|
||||
|
||||
func (c *kafkaController) consumeShipmentAllocationEventTopic(ft kgo.FetchTopic) {
|
||||
log.Info().Str("topic", ft.Topic).Msg("consumer: recieved records from topic")
|
||||
|
||||
// Process each message from the topic
|
||||
ft.EachRecord(func(record *kgo.Record) {
|
||||
// Unmarshal the event
|
||||
var event eventpb.ShipmentAllocationEvent
|
||||
err := proto.Unmarshal(record.Value, &event)
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("consumer: failed to unmarshal event")
|
||||
}
|
||||
|
||||
// Process the event
|
||||
ctx := context.Background()
|
||||
c.svc.ProcessShipmentAllocationEvent(ctx, &event)
|
||||
})
|
||||
}
|
||||
|
||||
func (c *kafkaController) consumePaymentProcessedEventTopic(ft kgo.FetchTopic) {
|
||||
log.Info().Str("topic", ft.Topic).Msg("consumer: recieved records from topic")
|
||||
|
||||
// Process each message from the topic
|
||||
ft.EachRecord(func(record *kgo.Record) {
|
||||
// Unmarshal the event
|
||||
var event eventpb.PaymentProcessedEvent
|
||||
err := proto.Unmarshal(record.Value, &event)
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("consumer: failed to unmarshal event")
|
||||
}
|
||||
|
||||
// Process the event
|
||||
ctx := context.Background()
|
||||
c.svc.ProcessPaymentProcessedEvent(ctx, &event)
|
||||
})
|
||||
}
|
||||
500
internal/svc/warehouse/controller/postgres.go
Normal file
500
internal/svc/warehouse/controller/postgres.go
Normal file
@@ -0,0 +1,500 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/hexolan/stocklet/internal/pkg/errors"
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/warehouse/v1"
|
||||
"github.com/hexolan/stocklet/internal/svc/warehouse"
|
||||
)
|
||||
|
||||
const (
|
||||
pgProductStockBaseQuery string = "SELECT product_id, quantity FROM product_stock"
|
||||
pgReservationBaseQuery string = "SELECT id, order_id, created_at FROM reservations"
|
||||
pgReservationItemsBaseQuery string = "SELECT product_id, quantity FROM reservation_items"
|
||||
)
|
||||
|
||||
type postgresController struct {
|
||||
cl *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewPostgresController(cl *pgxpool.Pool) warehouse.StorageController {
|
||||
return postgresController{cl: cl}
|
||||
}
|
||||
|
||||
func (c postgresController) GetProductStock(ctx context.Context, productId string) (*pb.ProductStock, error) {
|
||||
return c.getProductStock(ctx, nil, productId)
|
||||
}
|
||||
|
||||
func (c postgresController) getProductStock(ctx context.Context, tx *pgx.Tx, productId string) (*pb.ProductStock, error) {
|
||||
// Determine if a db transaction is being used
|
||||
var row pgx.Row
|
||||
const query = pgProductStockBaseQuery + " WHERE product_id=$1"
|
||||
if tx == nil {
|
||||
row = c.cl.QueryRow(ctx, query, productId)
|
||||
} else {
|
||||
row = (*tx).QueryRow(ctx, query, productId)
|
||||
}
|
||||
|
||||
// Scan row to protobuf obj
|
||||
stock, err := scanRowToProductStock(row)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return stock, nil
|
||||
}
|
||||
|
||||
func (c postgresController) GetReservation(ctx context.Context, reservationId string) (*pb.Reservation, error) {
|
||||
return c.getReservation(ctx, nil, reservationId)
|
||||
}
|
||||
|
||||
func (c postgresController) getReservation(ctx context.Context, tx *pgx.Tx, reservationId string) (*pb.Reservation, error) {
|
||||
// Determine if a db transaction is being used
|
||||
var row pgx.Row
|
||||
const query = pgReservationBaseQuery + " WHERE id=$1"
|
||||
if tx == nil {
|
||||
row = c.cl.QueryRow(ctx, query, reservationId)
|
||||
} else {
|
||||
row = (*tx).QueryRow(ctx, query, reservationId)
|
||||
}
|
||||
|
||||
// Scan row to protobuf obj
|
||||
reservation, err := scanRowToReservation(row)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Append items to reservation
|
||||
reservation, err = c.appendItemsToReservation(ctx, tx, reservation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return reservation, nil
|
||||
}
|
||||
|
||||
func (c postgresController) getReservationByOrderId(ctx context.Context, tx *pgx.Tx, orderId string) (*pb.Reservation, error) {
|
||||
// Determine if a db transaction is being used
|
||||
var row pgx.Row
|
||||
const query = pgReservationBaseQuery + " WHERE order_id=$1"
|
||||
if tx == nil {
|
||||
row = c.cl.QueryRow(ctx, query, orderId)
|
||||
} else {
|
||||
row = (*tx).QueryRow(ctx, query, orderId)
|
||||
}
|
||||
|
||||
// Scan row to protobuf obj
|
||||
reservation, err := scanRowToReservation(row)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Append items to reservation
|
||||
reservation, err = c.appendItemsToReservation(ctx, tx, reservation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return reservation, nil
|
||||
}
|
||||
|
||||
func (c postgresController) CreateProductStock(ctx context.Context, productId string, startingQuantity int32) error {
|
||||
// Begin a DB transaction
|
||||
tx, err := c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
// Create stock
|
||||
_, err = tx.Exec(ctx, "INSERT INTO product_stock (product_id, quantity) VALUES ($1, $2)", productId, startingQuantity)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to create product stock", err)
|
||||
}
|
||||
|
||||
// Add the event to the outbox table with the transaction
|
||||
evt, evtTopic, err := warehouse.PrepareStockCreatedEvent(&pb.ProductStock{ProductId: productId, Quantity: startingQuantity})
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", productId, evtTopic, evt)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
// Commit the transaction
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c postgresController) ReserveOrderStock(ctx context.Context, orderId string, orderMetadata warehouse.EventOrderMetadata, productQuantities map[string]int32) error {
|
||||
// Begin a DB transaction
|
||||
tx, err := c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
// Create reservation
|
||||
var reservationId string
|
||||
err = tx.QueryRow(ctx, "INSERT INTO reservations (order_id) VALUES ($1) RETURNING id", orderId).Scan(&reservationId)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to create reservation", err)
|
||||
}
|
||||
|
||||
// Reserve the items
|
||||
insufficientStockProductIds := []string{}
|
||||
for productId, quantity := range productQuantities {
|
||||
err = c.reserveStock(ctx, &tx, reservationId, productId, quantity)
|
||||
if err != nil {
|
||||
insufficientStockProductIds = append(insufficientStockProductIds, productId)
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure that all of the stock was reserved
|
||||
if len(insufficientStockProductIds) > 0 {
|
||||
// Add the event to the outbox table with the transaction
|
||||
evt, evtTopic, err := warehouse.PrepareStockReservationEvent_Failed(orderId, orderMetadata, insufficientStockProductIds)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = c.cl.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", orderId, evtTopic, evt)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Add the event to the outbox table with the transaction
|
||||
evt, evtTopic, err := warehouse.PrepareStockReservationEvent_Reserved(orderId, orderMetadata, reservationId, productQuantities)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", reservationId, evtTopic, evt)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
// Commit the transaction
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c postgresController) reserveStock(ctx context.Context, tx *pgx.Tx, reservationId string, productId string, quantity int32) error {
|
||||
// Determine if a transaction has already been provided
|
||||
var (
|
||||
funcTx pgx.Tx
|
||||
err error
|
||||
)
|
||||
if tx != nil {
|
||||
funcTx = *tx
|
||||
} else {
|
||||
funcTx, err = c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer funcTx.Rollback(ctx)
|
||||
}
|
||||
|
||||
// Subtract from quantity
|
||||
_, err = funcTx.Exec(ctx, "UPDATE product_stock SET quantity = quantity - $1 WHERE product_id=$2", quantity, productId)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to update product stock", err)
|
||||
}
|
||||
|
||||
// Get updated stock
|
||||
stock, err := c.getProductStock(ctx, tx, productId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Ensure that the stock is not negative
|
||||
if stock.Quantity < 0 {
|
||||
return errors.NewServiceError(errors.ErrCodeInvalidArgument, "insufficient stock")
|
||||
}
|
||||
|
||||
// Add as reservation item
|
||||
_, err = funcTx.Exec(ctx, "INSERT INTO reservation_items (reservation_id, product_id, quantity) VALUES ($1, $2, $3)", reservationId, productId, quantity)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to add as reservation item", err)
|
||||
}
|
||||
|
||||
// Commit the transaction (if created in this func)
|
||||
if tx == nil {
|
||||
err = funcTx.Commit(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c postgresController) ReturnReservedOrderStock(ctx context.Context, orderId string) error {
|
||||
// Begin a DB transaction
|
||||
tx, err := c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
// Get the reservation
|
||||
reservation, err := c.getReservationByOrderId(ctx, &tx, orderId)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to locate order reservation", err)
|
||||
}
|
||||
|
||||
// Return all of the reserved stock
|
||||
for _, reservedStock := range reservation.ReservedStock {
|
||||
err = c.returnReservedStock(ctx, &tx, reservation.Id, reservedStock.ProductId, reservedStock.Quantity)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to return reserved stock", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete the reservation
|
||||
_, err = tx.Exec(ctx, "DELETE FROM reservations WHERE id=$1", reservation.Id)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to delete reservation", err)
|
||||
}
|
||||
|
||||
// Prepare and add reservation consumed event to outbox
|
||||
evt, evtTopic, err := warehouse.PrepareStockReservationEvent_Returned(reservation.OrderId, reservation.Id, reservation.ReservedStock)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", reservation.Id, evtTopic, evt)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
// Commit the transaction
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c postgresController) returnReservedStock(ctx context.Context, tx *pgx.Tx, reservationId string, productId string, quantity int32) error {
|
||||
// Determine if a transaction has already been provided
|
||||
var (
|
||||
funcTx pgx.Tx
|
||||
err error
|
||||
)
|
||||
if tx != nil {
|
||||
funcTx = *tx
|
||||
} else {
|
||||
funcTx, err = c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer funcTx.Rollback(ctx)
|
||||
}
|
||||
|
||||
// Add back to stock quantity
|
||||
_, err = funcTx.Exec(ctx, "UPDATE product_stock SET quantity = quantity + $1 WHERE product_id=$2", quantity, productId)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to update product stock", err)
|
||||
}
|
||||
|
||||
// Delete reservation item
|
||||
_, err = funcTx.Exec(ctx, "DELETE FROM reservation_items WHERE reservation_id=$1 AND product_id=$2", reservationId, productId)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to add as reservation item", err)
|
||||
}
|
||||
|
||||
// Commit the transaction (if created in this func)
|
||||
if tx == nil {
|
||||
err = funcTx.Commit(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c postgresController) ConsumeReservedOrderStock(ctx context.Context, orderId string) error {
|
||||
// Begin a DB transaction
|
||||
tx, err := c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
// Get the reservation
|
||||
reservation, err := c.getReservationByOrderId(ctx, &tx, orderId)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to locate order reservation", err)
|
||||
}
|
||||
|
||||
// Delete the reservation
|
||||
_, err = tx.Exec(ctx, "DELETE FROM reservations WHERE id=$1", reservation.Id)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to delete reservation", err)
|
||||
}
|
||||
|
||||
// Dispatch stock removed events
|
||||
for _, reservedStock := range reservation.ReservedStock {
|
||||
evt, evtTopic, err := warehouse.PrepareStockRemovedEvent(reservedStock.ProductId, reservedStock.Quantity, &reservation.Id)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", reservedStock.ProductId, evtTopic, evt)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare and add reservation consumed event to outbox
|
||||
evt, evtTopic, err := warehouse.PrepareStockReservationEvent_Consumed(reservation.OrderId, reservation.Id, reservation.ReservedStock)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", reservation.Id, evtTopic, evt)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
// Commit the transaction
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c postgresController) getReservationItems(ctx context.Context, tx *pgx.Tx, reservationId string) ([]*pb.ReservationStock, error) {
|
||||
// Determine if transaction is being used.
|
||||
var rows pgx.Rows
|
||||
var err error
|
||||
const query = pgReservationItemsBaseQuery + " WHERE reservation_id=$1"
|
||||
if tx == nil {
|
||||
rows, err = c.cl.Query(ctx, query, reservationId)
|
||||
} else {
|
||||
rows, err = (*tx).Query(ctx, query, reservationId)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeService, "query error whilst fetching reserved items", err)
|
||||
}
|
||||
|
||||
items := []*pb.ReservationStock{}
|
||||
for rows.Next() {
|
||||
var reservStock pb.ReservationStock
|
||||
|
||||
err := rows.Scan(
|
||||
&reservStock.ProductId,
|
||||
&reservStock.Quantity,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeService, "failed to scan a reservation item", err)
|
||||
}
|
||||
|
||||
items = append(items, &reservStock)
|
||||
}
|
||||
|
||||
if rows.Err() != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeService, "error scanning item rows", rows.Err())
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
// Append items to the reservation
|
||||
func (c postgresController) appendItemsToReservation(ctx context.Context, tx *pgx.Tx, reservation *pb.Reservation) (*pb.Reservation, error) {
|
||||
reservedItems, err := c.getReservationItems(ctx, tx, reservation.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Add the items to the reservation protobuf
|
||||
reservation.ReservedStock = reservedItems
|
||||
|
||||
return reservation, nil
|
||||
}
|
||||
|
||||
// Scan a postgres row to a protobuf object
|
||||
func scanRowToProductStock(row pgx.Row) (*pb.ProductStock, error) {
|
||||
var stock pb.ProductStock
|
||||
|
||||
err := row.Scan(
|
||||
&stock.ProductId,
|
||||
&stock.Quantity,
|
||||
)
|
||||
if err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeNotFound, "stock not found", err)
|
||||
} else {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "something went wrong scanning object", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &stock, nil
|
||||
}
|
||||
|
||||
// Scan a postgres row to a protobuf object
|
||||
func scanRowToReservation(row pgx.Row) (*pb.Reservation, error) {
|
||||
var reservation pb.Reservation
|
||||
|
||||
// Temporary variables that require conversion
|
||||
var tmpCreatedAt pgtype.Timestamp
|
||||
|
||||
err := row.Scan(
|
||||
&reservation.Id,
|
||||
&reservation.OrderId,
|
||||
&tmpCreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeNotFound, "reservation not found", err)
|
||||
} else {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "something went wrong scanning object", err)
|
||||
}
|
||||
}
|
||||
|
||||
// convert postgres timestamps to unix format
|
||||
if tmpCreatedAt.Valid {
|
||||
reservation.CreatedAt = tmpCreatedAt.Time.Unix()
|
||||
}
|
||||
|
||||
return &reservation, nil
|
||||
}
|
||||
141
internal/svc/warehouse/event.go
Normal file
141
internal/svc/warehouse/event.go
Normal file
@@ -0,0 +1,141 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package warehouse
|
||||
|
||||
import (
|
||||
"github.com/hexolan/stocklet/internal/pkg/messaging"
|
||||
eventspb "github.com/hexolan/stocklet/internal/pkg/protogen/events/v1"
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/warehouse/v1"
|
||||
)
|
||||
|
||||
type EventOrderMetadata struct {
|
||||
CustomerId string
|
||||
ItemsPrice float32
|
||||
TotalPrice float32
|
||||
}
|
||||
|
||||
func PrepareStockCreatedEvent(productStock *pb.ProductStock) ([]byte, string, error) {
|
||||
topic := messaging.Warehouse_Stock_Created_Topic
|
||||
event := &eventspb.StockCreatedEvent{
|
||||
Revision: 1,
|
||||
|
||||
ProductId: productStock.ProductId,
|
||||
Quantity: productStock.Quantity,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareStockAddedEvent(productId string, amount int32, reservationId *string) ([]byte, string, error) {
|
||||
topic := messaging.Warehouse_Stock_Added_Topic
|
||||
event := &eventspb.StockAddedEvent{
|
||||
Revision: 1,
|
||||
|
||||
ProductId: productId,
|
||||
Amount: amount,
|
||||
ReservationId: reservationId,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareStockRemovedEvent(productId string, amount int32, reservationId *string) ([]byte, string, error) {
|
||||
topic := messaging.Warehouse_Stock_Removed_Topic
|
||||
event := &eventspb.StockRemovedEvent{
|
||||
Revision: 1,
|
||||
|
||||
ProductId: productId,
|
||||
Amount: amount,
|
||||
ReservationId: reservationId,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareStockReservationEvent_Failed(orderId string, orderMetadata EventOrderMetadata, insufficientStockProductIds []string) ([]byte, string, error) {
|
||||
topic := messaging.Warehouse_Reservation_Failed_Topic
|
||||
event := &eventspb.StockReservationEvent{
|
||||
Revision: 1,
|
||||
|
||||
Type: eventspb.StockReservationEvent_TYPE_INSUFFICIENT_STOCK,
|
||||
OrderId: orderId,
|
||||
OrderMetadata: &eventspb.StockReservationEvent_OrderMetadata{
|
||||
CustomerId: orderMetadata.CustomerId,
|
||||
ItemsPrice: orderMetadata.ItemsPrice,
|
||||
TotalPrice: orderMetadata.TotalPrice,
|
||||
},
|
||||
InsufficientStock: insufficientStockProductIds,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareStockReservationEvent_Reserved(orderId string, orderMetadata EventOrderMetadata, reservationId string, reservationStock map[string]int32) ([]byte, string, error) {
|
||||
topic := messaging.Warehouse_Reservation_Reserved_Topic
|
||||
event := &eventspb.StockReservationEvent{
|
||||
Revision: 1,
|
||||
|
||||
Type: eventspb.StockReservationEvent_TYPE_STOCK_RESERVED,
|
||||
OrderId: orderId,
|
||||
OrderMetadata: &eventspb.StockReservationEvent_OrderMetadata{
|
||||
CustomerId: orderMetadata.CustomerId,
|
||||
ItemsPrice: orderMetadata.ItemsPrice,
|
||||
TotalPrice: orderMetadata.TotalPrice,
|
||||
},
|
||||
ReservationId: reservationId,
|
||||
ReservationStock: reservationStock,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareStockReservationEvent_Returned(orderId string, reservationId string, reservedStock []*pb.ReservationStock) ([]byte, string, error) {
|
||||
reservationStock := make(map[string]int32)
|
||||
for _, item := range reservedStock {
|
||||
reservationStock[item.ProductId] = item.Quantity
|
||||
}
|
||||
|
||||
topic := messaging.Warehouse_Reservation_Returned_Topic
|
||||
event := &eventspb.StockReservationEvent{
|
||||
Revision: 1,
|
||||
|
||||
Type: eventspb.StockReservationEvent_TYPE_STOCK_RESERVED,
|
||||
OrderId: orderId,
|
||||
ReservationId: reservationId,
|
||||
ReservationStock: reservationStock,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareStockReservationEvent_Consumed(orderId string, reservationId string, reservedStock []*pb.ReservationStock) ([]byte, string, error) {
|
||||
reservationStock := make(map[string]int32)
|
||||
for _, item := range reservedStock {
|
||||
reservationStock[item.ProductId] = item.Quantity
|
||||
}
|
||||
|
||||
topic := messaging.Warehouse_Reservation_Consumed_Topic
|
||||
event := &eventspb.StockReservationEvent{
|
||||
Revision: 1,
|
||||
|
||||
Type: eventspb.StockReservationEvent_TYPE_STOCK_CONSUMED,
|
||||
OrderId: orderId,
|
||||
ReservationId: reservationId,
|
||||
ReservationStock: reservationStock,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
168
internal/svc/warehouse/warehouse.go
Normal file
168
internal/svc/warehouse/warehouse.go
Normal file
@@ -0,0 +1,168 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package warehouse
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/bufbuild/protovalidate-go"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
|
||||
"github.com/hexolan/stocklet/internal/pkg/errors"
|
||||
"github.com/hexolan/stocklet/internal/pkg/messaging"
|
||||
commonpb "github.com/hexolan/stocklet/internal/pkg/protogen/common/v1"
|
||||
eventpb "github.com/hexolan/stocklet/internal/pkg/protogen/events/v1"
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/warehouse/v1"
|
||||
)
|
||||
|
||||
// Interface for the service
|
||||
type WarehouseService struct {
|
||||
pb.UnimplementedWarehouseServiceServer
|
||||
|
||||
store StorageController
|
||||
pbVal *protovalidate.Validator
|
||||
}
|
||||
|
||||
// Interface for database methods
|
||||
// Flexibility for implementing seperate controllers for different databases (e.g. Postgres, MongoDB, etc)
|
||||
type StorageController interface {
|
||||
GetProductStock(ctx context.Context, productId string) (*pb.ProductStock, error)
|
||||
GetReservation(ctx context.Context, reservationId string) (*pb.Reservation, error)
|
||||
|
||||
CreateProductStock(ctx context.Context, productId string, startingQuantity int32) error
|
||||
|
||||
ReserveOrderStock(ctx context.Context, orderId string, orderMetadata EventOrderMetadata, productQuantities map[string]int32) error
|
||||
ReturnReservedOrderStock(ctx context.Context, orderId string) error
|
||||
ConsumeReservedOrderStock(ctx context.Context, orderId string) error
|
||||
}
|
||||
|
||||
// Interface for event consumption
|
||||
// Flexibility for seperate controllers for different messaging systems (e.g. Kafka, NATS, etc)
|
||||
type ConsumerController interface {
|
||||
messaging.ConsumerController
|
||||
|
||||
Attach(svc pb.WarehouseServiceServer)
|
||||
}
|
||||
|
||||
// Create the shipping service
|
||||
func NewWarehouseService(cfg *ServiceConfig, store StorageController) *WarehouseService {
|
||||
// Initialise the protobuf validator
|
||||
pbVal, err := protovalidate.New()
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("failed to initialise protobuf validator")
|
||||
}
|
||||
|
||||
// Initialise the service
|
||||
return &WarehouseService{
|
||||
store: store,
|
||||
pbVal: pbVal,
|
||||
}
|
||||
}
|
||||
|
||||
func (svc WarehouseService) ServiceInfo(ctx context.Context, req *commonpb.ServiceInfoRequest) (*commonpb.ServiceInfoResponse, error) {
|
||||
return &commonpb.ServiceInfoResponse{
|
||||
Name: "warehouse",
|
||||
Source: "https://github.com/hexolan/stocklet",
|
||||
SourceLicense: "AGPL-3.0",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (svc WarehouseService) ViewProductStock(ctx context.Context, req *pb.ViewProductStockRequest) (*pb.ViewProductStockResponse, error) {
|
||||
// Validate the request args
|
||||
if err := svc.pbVal.Validate(req); err != nil {
|
||||
// Provide the validation error to the user.
|
||||
return nil, errors.NewServiceError(errors.ErrCodeInvalidArgument, "invalid request: "+err.Error())
|
||||
}
|
||||
|
||||
// Get stock from db
|
||||
stock, err := svc.store.GetProductStock(ctx, req.ProductId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.ViewProductStockResponse{Stock: stock}, nil
|
||||
}
|
||||
|
||||
func (svc WarehouseService) ViewReservation(ctx context.Context, req *pb.ViewReservationRequest) (*pb.ViewReservationResponse, error) {
|
||||
// Validate the request args
|
||||
if err := svc.pbVal.Validate(req); err != nil {
|
||||
// Provide the validation error to the user.
|
||||
return nil, errors.NewServiceError(errors.ErrCodeInvalidArgument, "invalid request: "+err.Error())
|
||||
}
|
||||
|
||||
// Get reservation from db
|
||||
reservation, err := svc.store.GetReservation(ctx, req.ReservationId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.ViewReservationResponse{Reservation: reservation}, nil
|
||||
}
|
||||
|
||||
func (svc WarehouseService) ProcessProductCreatedEvent(ctx context.Context, req *eventpb.ProductCreatedEvent) (*emptypb.Empty, error) {
|
||||
err := svc.store.CreateProductStock(ctx, req.ProductId, 0)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "error processing event", err)
|
||||
}
|
||||
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func (svc WarehouseService) ProcessOrderPendingEvent(ctx context.Context, req *eventpb.OrderPendingEvent) (*emptypb.Empty, error) {
|
||||
err := svc.store.ReserveOrderStock(
|
||||
ctx,
|
||||
req.OrderId,
|
||||
EventOrderMetadata{
|
||||
CustomerId: req.CustomerId,
|
||||
ItemsPrice: req.ItemsPrice,
|
||||
TotalPrice: req.TotalPrice,
|
||||
},
|
||||
req.ItemQuantities,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "error processing event", err)
|
||||
}
|
||||
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func (svc WarehouseService) ProcessShipmentAllocationEvent(ctx context.Context, req *eventpb.ShipmentAllocationEvent) (*emptypb.Empty, error) {
|
||||
if req.Type == eventpb.ShipmentAllocationEvent_TYPE_FAILED {
|
||||
err := svc.store.ReturnReservedOrderStock(ctx, req.OrderId)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "error processing event", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func (svc WarehouseService) ProcessPaymentProcessedEvent(ctx context.Context, req *eventpb.PaymentProcessedEvent) (*emptypb.Empty, error) {
|
||||
if req.Type == eventpb.PaymentProcessedEvent_TYPE_FAILED {
|
||||
err := svc.store.ReturnReservedOrderStock(ctx, req.OrderId)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "error processing event", err)
|
||||
}
|
||||
} else if req.Type == eventpb.PaymentProcessedEvent_TYPE_SUCCESS {
|
||||
err := svc.store.ConsumeReservedOrderStock(ctx, req.OrderId)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "error processing event", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user