chore: initial commit

This commit is contained in:
2024-04-16 22:27:52 +01:00
commit 531b5dabe2
194 changed files with 27071 additions and 0 deletions

View 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/shipping/v1"
"github.com/hexolan/stocklet/internal/pkg/serve"
"github.com/hexolan/stocklet/internal/svc/shipping"
)
func PrepareGateway(cfg *shipping.ServiceConfig) *runtime.ServeMux {
mux, clientOpts := serve.NewGatewayServeBase(&cfg.Shared)
ctx := context.Background()
err := pb.RegisterShippingServiceHandlerFromEndpoint(ctx, mux, serve.GetAddrToGrpc("localhost"), clientOpts)
if err != nil {
log.Panic().Err(err).Msg("failed to register endpoint for gateway")
}
return mux
}

View 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/shipping/v1"
"github.com/hexolan/stocklet/internal/pkg/serve"
"github.com/hexolan/stocklet/internal/svc/shipping"
)
func PrepareGrpc(cfg *shipping.ServiceConfig, svc *shipping.ShippingService) *grpc.Server {
svr := serve.NewGrpcServeBase(&cfg.Shared)
pb.RegisterShippingServiceServer(svr, svc)
return svr
}

View 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 shipping
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
}

View File

@@ -0,0 +1,135 @@
// 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/shipping/v1"
"github.com/hexolan/stocklet/internal/svc/shipping"
)
type kafkaController struct {
cl *kgo.Client
svc pb.ShippingServiceServer
ctx context.Context
ctxCancel context.CancelFunc
}
func NewKafkaController(cl *kgo.Client) shipping.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.Shipping_Shipment_Allocation_Topic,
messaging.Shipping_Shipment_Dispatched_Topic,
messaging.Warehouse_Reservation_Reserved_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.Warehouse_Reservation_Reserved_Topic,
messaging.Payment_Processing_Topic,
)
return &kafkaController{cl: cl, ctx: ctx, ctxCancel: ctxCancel}
}
func (c *kafkaController) Attach(svc pb.ShippingServiceServer) {
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.Warehouse_Reservation_Reserved_Topic:
c.consumeStockReservationEventTopic(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) consumeStockReservationEventTopic(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.StockReservationEvent
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.ProcessStockReservationEvent(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)
})
}

View File

@@ -0,0 +1,256 @@
// 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/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/postgres"
"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/shipping/v1"
"github.com/hexolan/stocklet/internal/svc/shipping"
)
const (
pgShipmentBaseQuery string = "SELECT id, order_id, dispatched, created_at FROM shipments"
pgShipmentItemsBaseQuery string = "SELECT shipment_id, product_id, quantity FROM shipment_items"
)
type postgresController struct {
cl *pgxpool.Pool
}
func NewPostgresController(cl *pgxpool.Pool) shipping.StorageController {
return postgresController{cl: cl}
}
func (c postgresController) GetShipment(ctx context.Context, shipmentId string) (*pb.Shipment, error) {
return c.getShipment(ctx, nil, shipmentId)
}
func (c postgresController) getShipment(ctx context.Context, tx *pgx.Tx, shipmentId string) (*pb.Shipment, error) {
// Determine if a db transaction is being used
var row pgx.Row
const query = pgShipmentBaseQuery + " WHERE id=$1"
if tx == nil {
row = c.cl.QueryRow(ctx, query, shipmentId)
} else {
row = (*tx).QueryRow(ctx, query, shipmentId)
}
// Scan row to protobuf obj
shipment, err := scanRowToShipment(row)
if err != nil {
return nil, err
}
return shipment, nil
}
func (c postgresController) getShipmentByOrderId(ctx context.Context, tx *pgx.Tx, orderId string) (*pb.Shipment, error) {
// Determine if a db transaction is being used
var row pgx.Row
const query = pgShipmentBaseQuery + " 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
shipment, err := scanRowToShipment(row)
if err != nil {
return nil, err
}
return shipment, nil
}
func (c postgresController) GetShipmentItems(ctx context.Context, shipmentId string) ([]*pb.ShipmentItem, error) {
return c.getShipmentItems(ctx, nil, shipmentId)
}
func (c postgresController) getShipmentItems(ctx context.Context, tx *pgx.Tx, shipmentId string) ([]*pb.ShipmentItem, error) {
// Determine if transaction is being used
var rows pgx.Rows
var err error
if tx == nil {
rows, err = c.cl.Query(ctx, pgShipmentItemsBaseQuery+" WHERE shipment_id=$1", shipmentId)
} else {
rows, err = (*tx).Query(ctx, pgShipmentItemsBaseQuery+" WHERE shipment_id=$1", shipmentId)
}
if err != nil {
return nil, errors.WrapServiceError(errors.ErrCodeService, "query error whilst fetching items", err)
}
shipmentItems := []*pb.ShipmentItem{}
for rows.Next() {
var shipmentItem pb.ShipmentItem
shipmentItem.ShipmentId = shipmentId
err := rows.Scan(
&shipmentItem.ProductId,
&shipmentItem.Quantity,
)
if err != nil {
return nil, errors.WrapServiceError(errors.ErrCodeService, "failed to scan an order item", err)
}
shipmentItems = append(shipmentItems, &shipmentItem)
}
if rows.Err() != nil {
return nil, errors.WrapServiceError(errors.ErrCodeService, "error whilst scanning order item rows", rows.Err())
}
return shipmentItems, nil
}
func (c postgresController) AllocateOrderShipment(ctx context.Context, orderId string, orderMetadata shipping.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 shipment
var shipmentId string
err = tx.QueryRow(ctx, "INSERT INTO shipments (order_id) VALUES ($1) RETURNING id", orderId).Scan(&shipmentId)
if err != nil {
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to create shipment", err)
}
// Add shipment items
vals := [][]interface{}{}
for productId, quantity := range productQuantities {
vals = append(
vals,
goqu.Vals{shipmentId, productId, quantity},
)
}
statement, args, err := goqu.Dialect("postgres").From("shipment_items").Insert().Cols("shipment_id", "product_id", "quantity").Vals(vals...).Prepared(true).ToSQL()
if err != nil {
return errors.WrapServiceError(errors.ErrCodeService, "failed to build statement", err)
}
_, err = tx.Exec(ctx, statement, args...)
if err != nil {
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to add shipment items", err)
}
// Prepare and append shipment allocated event to transaction
evt, evtTopic, err := shipping.PrepareShipmentAllocationEvent_Allocated(orderId, orderMetadata, shipmentId, 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)", shipmentId, 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) CancelOrderShipment(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 shipment
shipment, err := c.getShipmentByOrderId(ctx, &tx, orderId)
if err != nil {
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to fetch shipment info", err)
}
// Get the shipment items
shipmentItems, err := c.getShipmentItems(ctx, &tx, shipment.Id)
if err != nil {
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to fetch shipment manifest", err)
}
// Delete shipment
_, err = tx.Exec(ctx, "DELETE FROM shipments WHERE id=$1", shipment.Id)
if err != nil {
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to delete shipment", err)
}
// Add the event to the outbox table with the transaction
evt, evtTopic, err := shipping.PrepareShipmentAllocationEvent_AllocationReleased(orderId, shipment.Id, shipmentItems)
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)", shipment.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
}
// Scan a postgres row to a protobuf object
func scanRowToShipment(row pgx.Row) (*pb.Shipment, error) {
var shipment pb.Shipment
// Temporary variables that require conversion
var tmpCreatedAt pgtype.Timestamp
err := row.Scan(
&shipment.Id,
&shipment.OrderId,
&shipment.Dispatched,
&tmpCreatedAt,
)
if err != nil {
if err == pgx.ErrNoRows {
return nil, errors.WrapServiceError(errors.ErrCodeNotFound, "shipment 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 {
shipment.CreatedAt = tmpCreatedAt.Time.Unix()
} else {
return nil, errors.NewServiceError(errors.ErrCodeUnknown, "something went wrong scanning object (timestamp conversion error)")
}
return &shipment, nil
}

View File

@@ -0,0 +1,97 @@
// 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 shipping
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/shipping/v1"
)
type EventOrderMetadata struct {
CustomerId string
ItemsPrice float32
TotalPrice float32
}
func PrepareShipmentAllocationEvent_Failed(orderId string, orderMetadata EventOrderMetadata, productQuantities map[string]int32) ([]byte, string, error) {
topic := messaging.Shipping_Shipment_Allocation_Topic
event := &eventspb.ShipmentAllocationEvent{
Revision: 1,
Type: eventspb.ShipmentAllocationEvent_TYPE_FAILED,
OrderId: orderId,
OrderMetadata: &eventspb.ShipmentAllocationEvent_OrderMetadata{
CustomerId: orderMetadata.CustomerId,
ItemsPrice: orderMetadata.ItemsPrice,
TotalPrice: orderMetadata.TotalPrice,
},
ProductQuantities: productQuantities,
}
return messaging.MarshalEvent(event, topic)
}
func PrepareShipmentAllocationEvent_Allocated(orderId string, orderMetadata EventOrderMetadata, shipmentId string, productQuantities map[string]int32) ([]byte, string, error) {
topic := messaging.Shipping_Shipment_Allocation_Topic
event := &eventspb.ShipmentAllocationEvent{
Revision: 1,
Type: eventspb.ShipmentAllocationEvent_TYPE_ALLOCATED,
OrderId: orderId,
OrderMetadata: &eventspb.ShipmentAllocationEvent_OrderMetadata{
CustomerId: orderMetadata.CustomerId,
ItemsPrice: orderMetadata.ItemsPrice,
TotalPrice: orderMetadata.TotalPrice,
},
ShipmentId: shipmentId,
ProductQuantities: productQuantities,
}
return messaging.MarshalEvent(event, topic)
}
func PrepareShipmentAllocationEvent_AllocationReleased(orderId string, shipmentId string, shipmentItems []*pb.ShipmentItem) ([]byte, string, error) {
productQuantities := make(map[string]int32)
for _, item := range shipmentItems {
productQuantities[item.ProductId] = item.Quantity
}
topic := messaging.Shipping_Shipment_Allocation_Topic
event := &eventspb.ShipmentAllocationEvent{
Revision: 1,
Type: eventspb.ShipmentAllocationEvent_TYPE_ALLOCATION_RELEASED,
OrderId: orderId,
ShipmentId: shipmentId,
ProductQuantities: productQuantities,
}
return messaging.MarshalEvent(event, topic)
}
func PrepareShipmentDispatchedEvent(orderId string, shipmentId string, productQuantities map[string]int32) ([]byte, string, error) {
topic := messaging.Shipping_Shipment_Dispatched_Topic
event := &eventspb.ShipmentDispatchedEvent{
Revision: 1,
OrderId: orderId,
ShipmentId: shipmentId,
ProductQuantities: productQuantities,
}
return messaging.MarshalEvent(event, topic)
}

View File

@@ -0,0 +1,147 @@
// 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 shipping
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/shipping/v1"
)
// Interface for the service
type ShippingService struct {
pb.UnimplementedShippingServiceServer
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 {
GetShipment(ctx context.Context, shipmentId string) (*pb.Shipment, error)
GetShipmentItems(ctx context.Context, shipmentId string) ([]*pb.ShipmentItem, error)
AllocateOrderShipment(ctx context.Context, orderId string, orderMetadata EventOrderMetadata, productQuantities map[string]int32) error
CancelOrderShipment(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.ShippingServiceServer)
}
// Create the shipping service
func NewShippingService(cfg *ServiceConfig, store StorageController) *ShippingService {
// 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 &ShippingService{
store: store,
pbVal: pbVal,
}
}
func (svc ShippingService) ServiceInfo(ctx context.Context, req *commonpb.ServiceInfoRequest) (*commonpb.ServiceInfoResponse, error) {
return &commonpb.ServiceInfoResponse{
Name: "shipping",
Source: "https://github.com/hexolan/stocklet",
SourceLicense: "AGPL-3.0",
}, nil
}
func (svc ShippingService) ViewShipment(ctx context.Context, req *pb.ViewShipmentRequest) (*pb.ViewShipmentResponse, 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())
}
// todo: permission checking?
// Get shipment from DB
shipment, err := svc.store.GetShipment(ctx, req.ShipmentId)
if err != nil {
return nil, err
}
return &pb.ViewShipmentResponse{Shipment: shipment}, nil
}
func (svc ShippingService) ViewShipmentManifest(ctx context.Context, req *pb.ViewShipmentManifestRequest) (*pb.ViewShipmentManifestResponse, 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())
}
// todo: permission checking?
shipmentItems, err := svc.store.GetShipmentItems(ctx, req.ShipmentId)
if err != nil {
return nil, err
}
return &pb.ViewShipmentManifestResponse{Manifest: shipmentItems}, nil
}
func (svc ShippingService) ProcessStockReservationEvent(ctx context.Context, req *eventpb.StockReservationEvent) (*emptypb.Empty, error) {
if req.Type == eventpb.StockReservationEvent_TYPE_STOCK_RESERVED {
err := svc.store.AllocateOrderShipment(
ctx,
req.OrderId,
EventOrderMetadata{
CustomerId: req.OrderMetadata.CustomerId,
ItemsPrice: req.OrderMetadata.ItemsPrice,
TotalPrice: req.OrderMetadata.TotalPrice,
},
req.ReservationStock,
)
if err != nil {
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to update in response to event", err)
}
}
return &emptypb.Empty{}, nil
}
func (svc ShippingService) ProcessPaymentProcessedEvent(ctx context.Context, req *eventpb.PaymentProcessedEvent) (*emptypb.Empty, error) {
if req.Type == eventpb.PaymentProcessedEvent_TYPE_FAILED {
err := svc.store.CancelOrderShipment(ctx, req.OrderId)
if err != nil {
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to update in response to event", err)
}
}
return &emptypb.Empty{}, nil
}