Add right requests

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-12-29 15:45:36 +01:00
parent 0fb281e0e9
commit 9626e24370
31 changed files with 6750 additions and 0 deletions

View File

@@ -69,6 +69,7 @@ const (
MeetingEntityType uint16 = 45
DataProtectionImpactAssessmentEntityType uint16 = 46
TransferImpactAssessmentEntityType uint16 = 47
RightsRequestEntityType uint16 = 48
)
type EntityInfo struct {
@@ -269,6 +270,10 @@ var entityRegistry = map[uint16]EntityInfo{
Model: "TransferImpactAssessment",
Table: "processing_activity_transfer_impact_assessments",
},
RightsRequestEntityType: {
Model: "RightsRequest",
Table: "rights_requests",
},
}
func EntityTable(entityType uint16) (string, bool) {

View File

@@ -0,0 +1,37 @@
CREATE TYPE rights_request_type AS ENUM (
'ACCESS',
'DELETION',
'PORTABILITY'
);
CREATE TYPE rights_request_state AS ENUM (
'TODO',
'IN_PROGRESS',
'DONE'
);
CREATE TABLE rights_requests (
id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
organization_id TEXT NOT NULL,
request_type rights_request_type NOT NULL,
request_state rights_request_state NOT NULL,
data_subject TEXT,
contact TEXT,
details TEXT,
deadline DATE,
action_taken TEXT,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT rights_requests_organization_id_fkey
FOREIGN KEY (organization_id)
REFERENCES organizations(id)
ON UPDATE CASCADE
ON DELETE CASCADE
);

View File

@@ -0,0 +1,41 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
type RightsRequestOrderField string
const (
RightsRequestOrderFieldCreatedAt RightsRequestOrderField = "CREATED_AT"
RightsRequestOrderFieldDeadline RightsRequestOrderField = "DEADLINE"
RightsRequestOrderFieldState RightsRequestOrderField = "STATE"
RightsRequestOrderFieldType RightsRequestOrderField = "TYPE"
)
func (p RightsRequestOrderField) Column() string {
return string(p)
}
func (p RightsRequestOrderField) String() string {
return string(p)
}
func (p RightsRequestOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *RightsRequestOrderField) UnmarshalText(text []byte) error {
*p = RightsRequestOrderField(text)
return nil
}

View File

@@ -0,0 +1,68 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import (
"database/sql/driver"
"fmt"
)
type RightsRequestState string
const (
RightsRequestStateTodo RightsRequestState = "TODO"
RightsRequestStateInProgress RightsRequestState = "IN_PROGRESS"
RightsRequestStateDone RightsRequestState = "DONE"
)
func RightsRequestStates() []RightsRequestState {
return []RightsRequestState{
RightsRequestStateTodo,
RightsRequestStateInProgress,
RightsRequestStateDone,
}
}
func (rrs RightsRequestState) String() string {
return string(rrs)
}
func (rrs *RightsRequestState) Scan(value any) error {
var s string
switch v := value.(type) {
case string:
s = v
case []byte:
s = string(v)
default:
return fmt.Errorf("unsupported type for RightsRequestState: %T", value)
}
switch s {
case "TODO":
*rrs = RightsRequestStateTodo
case "IN_PROGRESS":
*rrs = RightsRequestStateInProgress
case "DONE":
*rrs = RightsRequestStateDone
default:
return fmt.Errorf("invalid RightsRequestState value: %q", s)
}
return nil
}
func (rrs RightsRequestState) Value() (driver.Value, error) {
return string(rrs), nil
}

View File

@@ -0,0 +1,68 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import (
"database/sql/driver"
"fmt"
)
type RightsRequestType string
const (
RightsRequestTypeAccess RightsRequestType = "ACCESS"
RightsRequestTypeDeletion RightsRequestType = "DELETION"
RightsRequestTypePortability RightsRequestType = "PORTABILITY"
)
func RightsRequestTypes() []RightsRequestType {
return []RightsRequestType{
RightsRequestTypeAccess,
RightsRequestTypeDeletion,
RightsRequestTypePortability,
}
}
func (rrt RightsRequestType) String() string {
return string(rrt)
}
func (rrt *RightsRequestType) Scan(value any) error {
var s string
switch v := value.(type) {
case string:
s = v
case []byte:
s = string(v)
default:
return fmt.Errorf("unsupported type for RightsRequestType: %T", value)
}
switch s {
case "ACCESS":
*rrt = RightsRequestTypeAccess
case "DELETION":
*rrt = RightsRequestTypeDeletion
case "PORTABILITY":
*rrt = RightsRequestTypePortability
default:
return fmt.Errorf("invalid RightsRequestType value: %q", s)
}
return nil
}
func (rrt RightsRequestType) Value() (driver.Value, error) {
return string(rrt), nil
}

View File

@@ -0,0 +1,327 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import (
"context"
"errors"
"fmt"
"maps"
"time"
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/page"
)
type (
ErrRightsRequestNotFound struct {
Identifier string
}
RightsRequest struct {
ID gid.GID `db:"id"`
OrganizationID gid.GID `db:"organization_id"`
RequestType RightsRequestType `db:"request_type"`
RequestState RightsRequestState `db:"request_state"`
DataSubject *string `db:"data_subject"`
Contact *string `db:"contact"`
Details *string `db:"details"`
Deadline *time.Time `db:"deadline"`
ActionTaken *string `db:"action_taken"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
RightsRequests []*RightsRequest
)
func (e ErrRightsRequestNotFound) Error() string {
return fmt.Sprintf("rights request not found: %q", e.Identifier)
}
func (rr *RightsRequest) CursorKey(field RightsRequestOrderField) page.CursorKey {
switch field {
case RightsRequestOrderFieldCreatedAt:
return page.NewCursorKey(rr.ID, rr.CreatedAt)
case RightsRequestOrderFieldDeadline:
return page.NewCursorKey(rr.ID, rr.Deadline)
case RightsRequestOrderFieldState:
return page.NewCursorKey(rr.ID, rr.RequestState)
case RightsRequestOrderFieldType:
return page.NewCursorKey(rr.ID, rr.RequestType)
}
panic(fmt.Sprintf("unsupported order by: %s", field))
}
func (rr *RightsRequest) LoadByID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
rightsRequestID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
request_type,
request_state,
data_subject,
contact,
details,
deadline,
action_taken,
created_at,
updated_at
FROM
rights_requests
WHERE
%s
AND id = @rights_request_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"rights_request_id": rightsRequestID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query rights request: %w", err)
}
request, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[RightsRequest])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return &ErrRightsRequestNotFound{Identifier: rightsRequestID.String()}
}
return fmt.Errorf("cannot collect rights request: %w", err)
}
*rr = request
return nil
}
func (rrs *RightsRequests) CountByOrganizationID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
organizationID gid.GID,
) (int, error) {
q := `
SELECT
COUNT(id)
FROM
rights_requests
WHERE
%s
AND organization_id = @organization_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"organization_id": organizationID}
maps.Copy(args, scope.SQLArguments())
row := conn.QueryRow(ctx, q, args)
var count int
err := row.Scan(&count)
if err != nil {
return 0, fmt.Errorf("cannot count rights requests: %w", err)
}
return count, nil
}
func (rrs *RightsRequests) LoadByOrganizationID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor[RightsRequestOrderField],
) error {
q := `
SELECT
id,
organization_id,
request_type,
request_state,
data_subject,
contact,
details,
deadline,
action_taken,
created_at,
updated_at
FROM
rights_requests
WHERE
%s
AND organization_id = @organization_id
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{"organization_id": organizationID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, cursor.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query rights requests: %w", err)
}
requests, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[RightsRequest])
if err != nil {
return fmt.Errorf("cannot collect rights requests: %w", err)
}
*rrs = requests
return nil
}
func (rr *RightsRequest) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO rights_requests (
id,
tenant_id,
organization_id,
request_type,
request_state,
data_subject,
contact,
details,
deadline,
action_taken,
created_at,
updated_at
) VALUES (
@id,
@tenant_id,
@organization_id,
@request_type,
@request_state,
@data_subject,
@contact,
@details,
@deadline,
@action_taken,
@created_at,
@updated_at
)
`
args := pgx.StrictNamedArgs{
"id": rr.ID,
"tenant_id": scope.GetTenantID(),
"organization_id": rr.OrganizationID,
"request_type": rr.RequestType,
"request_state": rr.RequestState,
"data_subject": rr.DataSubject,
"contact": rr.Contact,
"details": rr.Details,
"deadline": rr.Deadline,
"action_taken": rr.ActionTaken,
"created_at": rr.CreatedAt,
"updated_at": rr.UpdatedAt,
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot insert rights request: %w", err)
}
return nil
}
func (rr *RightsRequest) Update(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
UPDATE rights_requests SET
request_type = @request_type,
request_state = @request_state,
data_subject = @data_subject,
contact = @contact,
details = @details,
deadline = @deadline,
action_taken = @action_taken,
updated_at = @updated_at
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": rr.ID,
"request_type": rr.RequestType,
"request_state": rr.RequestState,
"data_subject": rr.DataSubject,
"contact": rr.Contact,
"details": rr.Details,
"deadline": rr.Deadline,
"action_taken": rr.ActionTaken,
"updated_at": rr.UpdatedAt,
}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot update rights request: %w", err)
}
return nil
}
func (rr *RightsRequest) Delete(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
DELETE FROM rights_requests
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"id": rr.ID}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot delete rights request: %w", err)
}
return nil
}