Skip to content

feat: on ledger creation return the existing ledger if exists#904

Merged
turip merged 6 commits intomainfrom
feature/upsert-ledger-2
May 15, 2024
Merged

feat: on ledger creation return the existing ledger if exists#904
turip merged 6 commits intomainfrom
feature/upsert-ledger-2

Conversation

@turip
Copy link
Copy Markdown
Member

@turip turip commented May 14, 2024

Overview

If a ledger for the specified subject already exists, let's return the existing ledger ID as a 409 response, so that no new query will be needed to fetch the ledger by subject.

Example response:

{
    "type": "about:blank",
    "title": "Conflict",
    "status": 409,
    "detail": "ledger (default.01HXVNDJR532E8GTBVSC2XK5D4) already exitst for subject subject-1",
    "instance": "urn:request:Peters-MacBook-Pro-2.local/ZzyNVpzatY-000001",
    "conflictingEntity": {
        "id": "01HXVNDJR532E8GTBVSC2XK5D4",
        "subject": "subject-1"
    }
}

Fixes #(issue)

Notes for reviewer

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@turip turip added kind/feature New feature or request area/api labels May 14, 2024
@turip turip changed the title Feature/upsert ledger 2 feat: on ledger creation return the existing ledger if exists May 14, 2024
@turip turip marked this pull request as ready for review May 14, 2024 20:18
@turip turip force-pushed the feature/upsert-ledger-2 branch from 00760c5 to edc222d Compare May 14, 2024 21:33
Comment thread api/openapi.yaml
description: Ledger Exists
x-go-type-import:
path: github.com/openmeterio/openmeter/internal/credit
x-go-type: credit.LedgerAlreadyExistsProblemResponse
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplication of type required due to this: oapi-codegen/oapi-codegen#1610

Comment thread pkg/models/problem.go
// code. The Problem's Status field will be set to match the status argument,
// and the Title will be set to the default Go status text for that code.
func NewStatusProblem(ctx context.Context, err error, status int) Problem {
func NewStatusProblem(ctx context.Context, err error, status int) *StatusProblem {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you change this? (just curious)

Copy link
Copy Markdown
Member Author

@turip turip May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check this: https://go.dev/play/p/GGaWrq1Gu3n

The overal issue is that if an interface is composed with a struct, then json marshaling will treat that as a field member as opposed to being part of the same object.

If we want to define per service problems, like this: https://github.com/openmeterio/openmeter/pull/904/files#diff-f49bfff64b9ead64bbdc915228760a76fb213fcbc0474605aef0c0a5f47c8ad5R19 without putting them into models, we need to have access to the *StatusProblem.

Now I could have done a typecast there, but then if something changes in NewStatusProblem's implementation that might break runtime due to the typecast, so it's cleaner to expose the struct instead.

@turip turip merged commit 42f9659 into main May 15, 2024
@turip turip deleted the feature/upsert-ledger-2 branch May 15, 2024 10:11
Comment thread internal/credit/credit.go

func (e *LedgerAlreadyExistsError) Error() string {
return fmt.Sprintf("ledger (%s.%s) already exitst for subject %s", e.Namespace, e.LedgerID, e.Subject)
return fmt.Sprintf("ledger (%s.%s) already exitst for subject %s", e.Namespace, e.Ledger.ID, e.Ledger.Subject)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we omit the namespace from the error? It's largely transparent anyway. It might be more confusing than useful.

@turip turip mentioned this pull request May 16, 2024
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/api kind/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants