Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(soft-delete): implement soft delete for PaymentOrder and LockPay… #409

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ent/schema/lockpaymentorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type LockPaymentOrder struct {
func (LockPaymentOrder) Mixin() []ent.Mixin {
return []ent.Mixin{
TimeMixin{},
SoftDeleteMixin{},
}
}

Expand Down
14 changes: 14 additions & 0 deletions ent/schema/mixins.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,17 @@ func (TimeMixin) Fields() []ent.Field {
UpdateDefault(time.Now),
}
}

// SoftDeleteMixin implements the ent.Mixin for soft delete functionality.
type SoftDeleteMixin struct {
mixin.Schema
}

// Fields of the SoftDeleteMixin.
func (SoftDeleteMixin) Fields() []ent.Field {
return []ent.Field{
field.Time("deleted_at").
Optional().
Nillable(),
}
}
Comment on lines +35 to +46
Copy link
Contributor

Choose a reason for hiding this comment

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

can you copy all the mixin code that's here? https://entgo.io/docs/interceptors/#soft-delete

the only change should be "delete_time" to "deleted_at"

Copy link
Author

Choose a reason for hiding this comment

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

Ok sir

Copy link
Author

Choose a reason for hiding this comment

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

image
I keep getting this error, do you know of a way around it?

Copy link
Contributor

Choose a reason for hiding this comment

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

@Fidel-wole can you ask under "Aggregator" topic in the dev community https://t.me/+Stx-wLOdj49iNDM0

1 change: 1 addition & 0 deletions ent/schema/paymentorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type PaymentOrder struct {
func (PaymentOrder) Mixin() []ent.Mixin {
return []ent.Mixin{
TimeMixin{},
SoftDeleteMixin{},
}
}

Expand Down
2 changes: 1 addition & 1 deletion ent/schema/senderprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (SenderProfile) Edges() []ent.Edge {
Unique().
Annotations(entsql.OnDelete(entsql.Cascade)),
edge.To("payment_orders", PaymentOrder.Type).
Annotations(entsql.OnDelete(entsql.SetNull)),
Annotations(entsql.OnDelete(entsql.Cascade)),
edge.To("order_tokens", SenderOrderToken.Type).
Annotations(entsql.OnDelete(entsql.Cascade)),
edge.To("linked_address", LinkedAddress.Type).
Expand Down
18 changes: 18 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,3 +532,21 @@ func GetTokenRateFromQueue(tokenSymbol string, orderAmount decimal.Decimal, fiat

return rateResponse, nil
}

type contextKey string

const skipSoftDeleteKey contextKey = "skipSoftDelete"

// SkipSoftDelete adds a context value to skip soft delete logic.
func SkipSoftDelete(ctx context.Context) context.Context {
return context.WithValue(ctx, skipSoftDeleteKey, true)
}

// IsSkipSoftDelete returns true if the context indicates that soft delete should be skipped.
func IsSkipSoftDelete(ctx context.Context) bool {
val := ctx.Value(skipSoftDeleteKey)
if val != nil {
return val.(bool)
}
return false
}
Comment on lines +535 to +552
Copy link
Contributor

Choose a reason for hiding this comment

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

these aren't needed... SkipSoftDelete is already be in the mixin.go snippet from ent docs