MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / SoftDelete

Method SoftDelete

app/controlplane/pkg/data/group.go:400–464  ·  view source on GitHub ↗

SoftDelete soft-deletes a group by setting the DeletedAt field to the current time. It also marks all group memberships as deleted and removes any pending invitations related to the group.

(ctx context.Context, orgID uuid.UUID, groupID uuid.UUID)

Source from the content-addressed store, hash-verified

398// SoftDelete soft-deletes a group by setting the DeletedAt field to the current time.
399// It also marks all group memberships as deleted and removes any pending invitations related to the group.
400func (g GroupRepo) SoftDelete(ctx context.Context, orgID uuid.UUID, groupID uuid.UUID) error {
401 ctx, span := otelx.Start(ctx, groupRepoTracer, "GroupRepo.SoftDelete")
402 defer span.End()
403
404 return WithTx(ctx, g.data.DB, func(tx *ent.Tx) error {
405 now := time.Now()
406
407 // Softly delete the group by setting the DeletedAt field
408 _, err := tx.Group.UpdateOneID(groupID).
409 SetDeletedAt(now).
410 Where(group.OrganizationIDEQ(orgID), group.DeletedAtIsNil()).
411 Save(ctx)
412 if err != nil {
413 if ent.IsNotFound(err) {
414 return biz.NewErrNotFound("group")
415 }
416 return fmt.Errorf("failed to mark group as deleted: %w", err)
417 }
418
419 // Mark as deleted all group memberships for this group
420 _, err = tx.GroupMembership.Update().
421 Where(
422 groupmembership.GroupID(groupID),
423 groupmembership.DeletedAtIsNil(),
424 ).
425 SetDeletedAt(now).
426 Save(ctx)
427 if err != nil && !ent.IsNotFound(err) {
428 return fmt.Errorf("failed to mark group memberships as deleted: %w", err)
429 }
430
431 // Delete all memberships where this group is either the member or the resource
432 _, err = tx.Membership.Delete().Where(
433 membership.HasOrganizationWith(organization.ID(orgID)),
434 membership.Or(
435 membership.MemberID(groupID),
436 membership.And(
437 membership.ResourceID(groupID),
438 membership.ResourceTypeEQ(authz.ResourceTypeGroup),
439 ),
440 ),
441 ).Exec(ctx)
442 if err != nil && !ent.IsNotFound(err) {
443 return fmt.Errorf("failed to delete group memberships: %w", err)
444 }
445
446 // Mark as deleted any pending invitations for this group
447 _, err = tx.OrgInvitation.Update().
448 Where(
449 orginvitation.OrganizationIDEQ(orgID),
450 orginvitation.DeletedAtIsNil(),
451 orginvitation.StatusEQ(biz.OrgInvitationStatusPending),
452 func(s *sql.Selector) {
453 s.Where(sqljson.ValueEQ(orginvitation.FieldContext, groupID.String(), sqljson.DotPath("group_id_to_join")))
454 },
455 ).
456 SetDeletedAt(now).
457 Save(ctx)

Callers

nothing calls this directly

Implementers 1

GroupRepoapp/controlplane/pkg/data/group.go

Calls 15

StartFunction · 0.92
OrganizationIDEQFunction · 0.92
DeletedAtIsNilFunction · 0.92
IsNotFoundFunction · 0.92
NewErrNotFoundFunction · 0.92
GroupIDFunction · 0.92
DeletedAtIsNilFunction · 0.92
HasOrganizationWithFunction · 0.92
IDFunction · 0.92
OrFunction · 0.92
MemberIDFunction · 0.92
AndFunction · 0.92

Tested by

no test coverage detected