Skip to content

Commit

Permalink
feat: add sku_variant field
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbrm committed Oct 23, 2024
1 parent 339ec8b commit 1a7c04c
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 23 deletions.
2 changes: 1 addition & 1 deletion libs/datastore/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
}
dbs = map[string]*sqlx.DB{}
// CurrentMigrationVersion holds the default migration version
CurrentMigrationVersion = uint(68)
CurrentMigrationVersion = uint(69)
// MigrationTracks holds the migration version for a given track (eyeshade, promotion, wallet)
MigrationTracks = map[string]uint{
"eyeshade": 20,
Expand Down
1 change: 1 addition & 0 deletions migrations/0069_order_items_add_sku_variant.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE order_items DROP COLUMN sku_variant;
4 changes: 4 additions & 0 deletions migrations/0069_order_items_add_sku_variant.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE order_items ADD COLUMN sku_variant text DEFAULT NULL;

-- Will be executed manually.
-- UPDATE order_items SET sku_variant=sku;
1 change: 1 addition & 0 deletions services/skus/controllers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,7 @@ func (suite *ControllersTestSuite) TestCreateOrder_RadomPayable() {
{
Quantity: 1,
SKU: "sku",
SKUVnt: "sku_vnt",
Location: "https://example.com",
Description: "description",
CredentialType: timeLimitedV2,
Expand Down
4 changes: 4 additions & 0 deletions services/skus/handler/handler_pvt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestCollectValidationErrors_CreateOrderRequestNew(t *testing.T) {
{
Quantity: 1,
SKU: "sku",
SKUVnt: "sku_vnt",
Location: "location",
Description: "description",
CredentialType: "credential_type",
Expand Down Expand Up @@ -72,6 +73,7 @@ func TestCollectValidationErrors_CreateOrderRequestNew(t *testing.T) {
{
Quantity: 1,
SKU: "sku",
SKUVnt: "sku_vnt",
Location: "location",
Description: "description",
CredentialType: "credential_type",
Expand Down Expand Up @@ -100,6 +102,7 @@ func TestCollectValidationErrors_CreateOrderRequestNew(t *testing.T) {
{
Quantity: 1,
SKU: "sku",
SKUVnt: "sku_vnt",
Location: "location",
Description: "description",
CredentialType: "credential_type",
Expand Down Expand Up @@ -133,6 +136,7 @@ func TestCollectValidationErrors_CreateOrderRequestNew(t *testing.T) {
{
Quantity: 1,
SKU: "sku",
SKUVnt: "sku_vnt",
Location: "location",
Description: "description",
CredentialType: "credential_type",
Expand Down
5 changes: 5 additions & 0 deletions services/skus/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ func TestOrder_CreateNew(t *testing.T) {
{
"quantity": 1,
"sku": "sku",
"sku_variant": "sku_vnt",
"location": "location",
"description": "description",
"credential_type": "credential_type",
Expand Down Expand Up @@ -348,6 +349,7 @@ func TestOrder_CreateNew(t *testing.T) {
{
"quantity": 1,
"sku": "sku",
"sku_variant": "sku_vnt",
"location": "location",
"description": "description",
"credential_type": "credential_type",
Expand Down Expand Up @@ -384,6 +386,7 @@ func TestOrder_CreateNew(t *testing.T) {
Items: []model.OrderItem{
{
SKU: "sku",
SKUVnt: "sku_vnt",
Quantity: 1,
Price: mustDecimalFromString("1"),
Subtotal: mustDecimalFromString("1"),
Expand Down Expand Up @@ -425,6 +428,7 @@ func TestOrder_CreateNew(t *testing.T) {
{
"quantity": 1,
"sku": "sku",
"sku_variant": "sku_vnt",
"location": "location",
"description": "description",
"credential_type": "credential_type",
Expand All @@ -448,6 +452,7 @@ func TestOrder_CreateNew(t *testing.T) {
Items: []model.OrderItem{
{
SKU: "sku",
SKUVnt: "sku_vnt",
Quantity: 1,
Price: mustDecimalFromString("1"),
Subtotal: mustDecimalFromString("1"),
Expand Down
2 changes: 2 additions & 0 deletions services/skus/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ type OrderItem struct {
ID uuid.UUID `json:"id" db:"id"`
OrderID uuid.UUID `json:"orderId" db:"order_id"`
SKU string `json:"sku" db:"sku"`
SKUVnt string `json:"sku_variant" db:"sku_variant"`
CreatedAt *time.Time `json:"createdAt" db:"created_at"`
UpdatedAt *time.Time `json:"updatedAt" db:"updated_at"`
Currency string `json:"currency" db:"currency"`
Expand Down Expand Up @@ -409,6 +410,7 @@ type CreateOrderRequestNew struct {
type OrderItemRequestNew struct {
Quantity int `json:"quantity" validate:"required,gte=1"`
SKU string `json:"sku" validate:"required"`
SKUVnt string `json:"sku_variant" validate:"required"`
Period string `json:"period"` // Not used yet.
Location string `json:"location" validate:"required"`
Description string `json:"description" validate:"required"`
Expand Down
4 changes: 4 additions & 0 deletions services/skus/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func (s *Service) CreateOrderItemFromMacaroon(ctx context.Context, sku string, q
switch key {
case "sku":
orderItem.SKU = value

// Legacy, non-Premium orders.
// Use the same value.
orderItem.SKUVnt = value
case "price", "amount":
orderItem.Price, err = decimal.NewFromString(value)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions services/skus/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func (suite *OrderTestSuite) assertSuccess(item *OrderItem, apm []string, expCfg
suite.Assert().Equal("stripe", strings.Join(apm, ","))
suite.Assert().Equal("usd", item.Currency)
suite.Assert().Equal("sku", item.SKU)
suite.Assert().Equal("sku", item.SKUVnt)
suite.Assert().Equal("5.01", item.Price.String())
suite.Assert().Equal("coffee", item.Description.String)
suite.Assert().Equal("brave.com", item.Location.String)
Expand Down
3 changes: 2 additions & 1 deletion services/skus/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2593,7 +2593,8 @@ func createOrderItem(req *model.OrderItemRequestNew) (*model.OrderItem, error) {
}

result := &model.OrderItem{
SKU: req.SKU,
SKU: req.SKU,
SKUVnt: req.SKUVnt,
// Set Currency separately as it should be at the Order level.
CredentialType: req.CredentialType,
ValidFor: &validFor,
Expand Down
2 changes: 2 additions & 0 deletions services/skus/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ func TestCreateOrderItem(t *testing.T) {
name: "full_example",
given: &model.OrderItemRequestNew{
SKU: "sku",
SKUVnt: "sku_vnt",
CredentialType: "credential_type",
CredentialValidDuration: "P1M",
CredentialValidDurationEach: ptr.To("P1D"),
Expand All @@ -242,6 +243,7 @@ func TestCreateOrderItem(t *testing.T) {
exp: tcExpected{
result: &model.OrderItem{
SKU: "sku",
SKUVnt: "sku_vnt",
CredentialType: "credential_type",
ValidFor: mustDurationFromISO("P1M"),
ValidForISO: ptr.To("P1M"),
Expand Down
10 changes: 5 additions & 5 deletions services/skus/storage/repository/order_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewOrderItem() *OrderItem { return &OrderItem{} }
func (r *OrderItem) Get(ctx context.Context, dbi sqlx.QueryerContext, id uuid.UUID) (*model.OrderItem, error) {
const q = `
SELECT
id, order_id, sku, created_at, updated_at, currency,
id, order_id, sku, sku_variant, created_at, updated_at, currency,
quantity, price, (quantity * price) as subtotal,
location, description, credential_type,metadata, valid_for_iso, issuance_interval
FROM order_items WHERE id = $1`
Expand All @@ -40,7 +40,7 @@ func (r *OrderItem) Get(ctx context.Context, dbi sqlx.QueryerContext, id uuid.UU
func (r *OrderItem) FindByOrderID(ctx context.Context, dbi sqlx.QueryerContext, orderID uuid.UUID) ([]model.OrderItem, error) {
const q = `
SELECT
id, order_id, sku, created_at, updated_at, currency,
id, order_id, sku, sku_variant, created_at, updated_at, currency,
quantity, price, (quantity * price) as subtotal,
location, description, credential_type, metadata, valid_for_iso, issuance_interval
FROM order_items WHERE order_id = $1`
Expand All @@ -61,10 +61,10 @@ func (r *OrderItem) InsertMany(ctx context.Context, dbi sqlx.ExtContext, items .

const q = `
INSERT INTO order_items (
order_id, sku, quantity, price, currency, subtotal, location, description, credential_type, metadata, valid_for, valid_for_iso, issuance_interval
order_id, sku, sku_variant, quantity, price, currency, subtotal, location, description, credential_type, metadata, valid_for, valid_for_iso, issuance_interval
) VALUES (
:order_id, :sku, :quantity, :price, :currency, :subtotal, :location, :description, :credential_type, :metadata, :valid_for, :valid_for_iso, :issuance_interval
) RETURNING id, order_id, sku, created_at, updated_at, currency, quantity, price, location, description, credential_type, (quantity * price) as subtotal, metadata, valid_for`
:order_id, :sku, :sku_variant, :quantity, :price, :currency, :subtotal, :location, :description, :credential_type, :metadata, :valid_for, :valid_for_iso, :issuance_interval
) RETURNING id, order_id, sku, sku_variant, created_at, updated_at, currency, quantity, price, location, description, credential_type, (quantity * price) as subtotal, metadata, valid_for`

rows, err := sqlx.NamedQueryContext(ctx, dbi, q, items)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions services/skus/storage/repository/order_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestOrderItem_InsertMany(t *testing.T) {
given: []model.OrderItem{
{
SKU: "sku_01_01",
SKUVnt: "sku_vnt_01_01",
Quantity: 1,
Price: mustDecimalFromString("2"),
Currency: "USD",
Expand All @@ -56,6 +57,7 @@ func TestOrderItem_InsertMany(t *testing.T) {
exp: []model.OrderItem{
{
SKU: "sku_01_01",
SKUVnt: "sku_vnt_01_01",
Quantity: 1,
Price: mustDecimalFromString("2"),
Currency: "USD",
Expand All @@ -70,6 +72,7 @@ func TestOrderItem_InsertMany(t *testing.T) {
given: []model.OrderItem{
{
SKU: "sku_02_01",
SKUVnt: "sku_vnt_02_01",
Quantity: 2,
Price: mustDecimalFromString("3"),
Currency: "USD",
Expand All @@ -79,6 +82,7 @@ func TestOrderItem_InsertMany(t *testing.T) {

{
SKU: "sku_02_02",
SKUVnt: "sku_vnt_02_02",
Quantity: 3,
Price: mustDecimalFromString("4"),
Currency: "USD",
Expand All @@ -90,6 +94,7 @@ func TestOrderItem_InsertMany(t *testing.T) {
exp: []model.OrderItem{
{
SKU: "sku_02_01",
SKUVnt: "sku_vnt_02_01",
Quantity: 2,
Price: mustDecimalFromString("3"),
Currency: "USD",
Expand All @@ -99,6 +104,7 @@ func TestOrderItem_InsertMany(t *testing.T) {

{
SKU: "sku_02_02",
SKUVnt: "sku_vnt_02_02",
Quantity: 3,
Price: mustDecimalFromString("4"),
Currency: "USD",
Expand Down
5 changes: 5 additions & 0 deletions services/skus/storage/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ func TestOrder_GetExpiresAtAfterISOPeriod(t *testing.T) {
items: []model.OrderItem{
{
SKU: "sku_01_01",
SKUVnt: "sku_vnt_01_01",
Quantity: 1,
Price: mustDecimalFromString("2"),
Currency: "USD",
Expand All @@ -621,6 +622,7 @@ func TestOrder_GetExpiresAtAfterISOPeriod(t *testing.T) {
items: []model.OrderItem{
{
SKU: "sku_02_01",
SKUVnt: "sku_vnt_02_01",
Quantity: 2,
Price: mustDecimalFromString("3"),
Currency: "USD",
Expand All @@ -631,6 +633,7 @@ func TestOrder_GetExpiresAtAfterISOPeriod(t *testing.T) {

{
SKU: "sku_02_02",
SKUVnt: "sku_vnt_02_02",
Quantity: 3,
Price: mustDecimalFromString("4"),
Currency: "USD",
Expand All @@ -652,6 +655,7 @@ func TestOrder_GetExpiresAtAfterISOPeriod(t *testing.T) {
items: []model.OrderItem{
{
SKU: "sku_02_01",
SKUVnt: "sku_vnt_02_01",
Quantity: 2,
Price: mustDecimalFromString("3"),
Currency: "USD",
Expand All @@ -661,6 +665,7 @@ func TestOrder_GetExpiresAtAfterISOPeriod(t *testing.T) {

{
SKU: "sku_02_02",
SKUVnt: "sku_vnt_02_02",
Quantity: 3,
Price: mustDecimalFromString("4"),
Currency: "USD",
Expand Down
Loading

0 comments on commit 1a7c04c

Please sign in to comment.