carttrigger

package module
v0.0.0-...-3d84a2e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2023 License: MIT-0 Imports: 10 Imported by: 0

README

The Cart Firestore Trigger Function

The Cart Firestore Trigger is a Cloud Function that is invoked in response to Firestore document updates. Specifically, updates of root shopping cart documents written by the gRPC API cart-servce.

Cart creations are ignored, only updates are of interest. As the cart-servce is currently implemented updates only happen in two circumstances: when the cart is checked out or when it is abandoned.

Updates to items in the cart or to the delivery address are not monitored and are not of interest to the trigger.

When a cart is tagged as "checked out" and this Cloud Function is triggered, it will gather the complete data for teh cart, including cart items and delivery address, and submit the resulting package to a Cloud Task for insertion into the "order archive."

Documentation

Overview

Package carttrigger handles Firestore trigger invocations when shopping cart documents are updated.

The handler is not invoked for the addition of cart items or delivery addresses, nor for creation of carts, only for updates to the cart root document. This will almost invariably be due to the cart either being submitted or abandoned.

Index

Constants

This section is empty.

Variables

View Source
var (
	// TopicProjectId is a variable so that unit tests can override it to ensures that test requests are not
	// routed to the live project! See https://firebase.google.com/docs/emulator-suite/connect_firestore
	TopicProjectId string

	// TopicId a variable so that unit tests can override it to force errors to occur
	TopicId string
)

Functions

func CartTrigger

func CartTrigger(ctx context.Context, e FirestoreEvent) error

CartTrigger receives a document update Firestore trigger event. The function is deployed with a trigger configuration (see Makefile) that will notify the handler of all updates to the root document of a Shopping Cart.

Types

type CartServiceClient

type CartServiceClient interface {
	GetCart(ctx context.Context, cartId string) (*pbcart.ShoppingCart, error)
}

CartServiceClient is a wrapper for the cart service that supports lazy loading of the service and unit test error generation.

type CartServiceClientImpl

type CartServiceClientImpl struct {
	CartServiceClient
	// contains filtered or unexported fields
}

CartServiceClientImpl is the default implementation of the CartServiceClient interface. error generation.

func (*CartServiceClientImpl) GetCart

func (c *CartServiceClientImpl) GetCart(ctx context.Context, cartId string) (*pbcart.ShoppingCart, error)

GetCart loads a fully populated shopping cart from Firestore.

type FirestoreCart

type FirestoreCart struct {
	Id     StringValue  `json:"id"`
	Status IntegerValue `json:"status"`
}

FirestoreCart describes the document fields that we need to know about as they will be found in the event data (not as we would prefer them, in the structure that we submitted to the Firestore API to populate the document in the first place :-(

type FirestoreEvent

type FirestoreEvent struct {
	OldValue   FirestoreValue `json:"oldValue"`
	Value      FirestoreValue `json:"value"`
	UpdateMask struct {
		FieldPaths []string `json:"fieldPaths"`
	} `json:"updateMask"`
}

FirestoreEvent is the payload of a Firestore event.

type FirestoreValue

type FirestoreValue struct {
	CreateTime time.Time     `json:"createTime"`
	Fields     FirestoreCart `json:"fields"`
	Name       string        `json:"name"`
	UpdateTime time.Time     `json:"updateTime"`
}

FirestoreValue holds Firestore document fields.

type IntegerValue

type IntegerValue struct {
	IntegerValue string `json:"integerValue"`
}

type PubSubClient

type PubSubClient interface {
	Publish(ctx context.Context, cart *pbcart.ShoppingCart) error
}

PubSubClient is a wrapper for our Google client that supports lazy loading of the client and unit test error generation.

type PubSubClientImpl

type PubSubClientImpl struct {
	PubSubClient
	// contains filtered or unexported fields
}

PubSubClientImpl is the default implementation of the PubSubClient interface.

func (*PubSubClientImpl) Publish

func (c *PubSubClientImpl) Publish(ctx context.Context, cart *pbcart.ShoppingCart) error

Publish submits a binary message to our configured Pub/Sub topic.

type StringValue

type StringValue struct {
	StringValue string `json:"stringValue"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL