Soft delete documents

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-09-08 16:46:36 +02:00
parent 7000ccc246
commit b6254f4758
4 changed files with 35 additions and 17 deletions

View File

@@ -76,6 +76,7 @@ FROM
documents
WHERE
%s
AND deleted_at IS NULL
AND id = @document_id
LIMIT 1;
`
@@ -114,6 +115,7 @@ FROM
documents
WHERE
%s
AND deleted_at IS NULL
AND organization_id = @organization_id
AND %s
`
@@ -156,6 +158,7 @@ FROM
documents
WHERE
%s
AND deleted_at IS NULL
AND organization_id = @organization_id
AND %s
AND %s
@@ -232,18 +235,18 @@ VALUES (
return err
}
func (p Document) Delete(
func (p Document) SoftDelete(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
DELETE FROM documents WHERE %s AND id = @document_id
UPDATE documents SET deleted_at = @deleted_at WHERE %s AND id = @document_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"document_id": p.ID}
args := pgx.StrictNamedArgs{"document_id": p.ID, "deleted_at": time.Now()}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
@@ -284,8 +287,10 @@ SET
document_type = @document_type,
show_on_trust_center = @show_on_trust_center,
updated_at = @updated_at
WHERE %s
AND id = @document_id
WHERE
%s
AND id = @document_id
AND deleted_at IS NULL
`
q = fmt.Sprintf(q, scope.SQLFragment())
@@ -321,7 +326,8 @@ WITH plcs AS (
p.id,
p.tenant_id,
p.search_vector,
p.show_on_trust_center
p.show_on_trust_center,
p.deleted_at
FROM
documents p
INNER JOIN
@@ -333,7 +339,9 @@ SELECT
COUNT(id)
FROM
plcs
WHERE %s
WHERE
%s
AND deleted_at IS NULL
AND %s
`
@@ -373,7 +381,8 @@ WITH plcs AS (
p.current_published_version,
p.show_on_trust_center,
p.created_at,
p.updated_at
p.updated_at,
p.deleted_at
FROM
documents p
INNER JOIN
@@ -393,7 +402,9 @@ SELECT
updated_at
FROM
plcs
WHERE %s
WHERE
%s
AND deleted_at IS NULL
AND %s
AND %s
`
@@ -432,7 +443,8 @@ WITH plcs AS (
p.id,
p.tenant_id,
p.search_vector,
p.show_on_trust_center
p.show_on_trust_center,
p.deleted_at
FROM
documents p
INNER JOIN
@@ -444,7 +456,9 @@ SELECT
COUNT(id)
FROM
plcs
WHERE %s
WHERE
%s
AND deleted_at IS NULL
AND %s
`
@@ -484,7 +498,8 @@ WITH plcs AS (
p.show_on_trust_center,
p.created_at,
p.updated_at,
p.search_vector
p.search_vector,
p.deleted_at
FROM
documents p
INNER JOIN
@@ -504,7 +519,9 @@ SELECT
updated_at
FROM
plcs
WHERE %s
WHERE
%s
AND deleted_at IS NULL
AND %s
AND %s
`

View File

@@ -0,0 +1 @@
ALTER TABLE documents ADD COLUMN deleted_at TIMESTAMP WITH TIME ZONE;

View File

@@ -800,7 +800,7 @@ func (s *DocumentService) DeleteDraft(
)
}
func (s *DocumentService) Delete(
func (s *DocumentService) SoftDelete(
ctx context.Context,
documentID gid.GID,
) error {
@@ -809,7 +809,7 @@ func (s *DocumentService) Delete(
return s.svc.pg.WithConn(
ctx,
func(conn pg.Conn) error {
return document.Delete(ctx, conn, s.svc.scope)
return document.SoftDelete(ctx, conn, s.svc.scope)
},
)
}

View File

@@ -2484,9 +2484,9 @@ func (r *mutationResolver) UpdateDocument(ctx context.Context, input types.Updat
func (r *mutationResolver) DeleteDocument(ctx context.Context, input types.DeleteDocumentInput) (*types.DeleteDocumentPayload, error) {
prb := r.ProboService(ctx, input.DocumentID.TenantID())
err := prb.Documents.Delete(ctx, input.DocumentID)
err := prb.Documents.SoftDelete(ctx, input.DocumentID)
if err != nil {
panic(fmt.Errorf("cannot delete document: %w", err))
panic(fmt.Errorf("cannot soft delete document: %w", err))
}
return &types.DeleteDocumentPayload{