Files
xinmi/internal/workbench/deletion.go
T

45 lines
1.2 KiB
Go
Raw Normal View History

package workbench
import (
"context"
"gorm.io/gorm"
)
func lockResourceForDeletion(ctx context.Context, tx *gorm.DB, kind, id string) (*Resource, error) {
return lockResource(ctx, tx, kind, id)
}
// The caller holds both resource locks and has checked that the task is idle.
// Only references change; historical prose and import metadata are preserved.
func detachResourceForDeletion(ctx context.Context, tx *gorm.DB, parent, task *Resource) error {
tenant, actor, admin, err := identity(ctx)
if err != nil {
return err
}
if parent.TenantID != tenant || task.TenantID != tenant || task.Kind != "content-tasks" {
return ErrForbidden
}
if !admin && (parent.OwnerID != actor || task.OwnerID != actor) {
return ErrForbidden
}
d, err := decode[TaskData](task.Data)
if err != nil {
return ErrInvalid
}
if (parent.Kind != "flows" || d.FlowID != parent.ID) && (parent.Kind != "training-maps" || d.MapID != parent.ID) {
return ErrInvalid
}
if task.OwnerID != actor {
if err := archiveResource(ctx, tx, task); err != nil {
return err
}
}
if parent.Kind == "flows" {
task.Data["flow_id"] = ""
} else {
task.Data["map_id"] = ""
}
task.Status = "needs_configuration"
return persistResource(ctx, tx, task, task.Version)
}