Add baa on vendors

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-08-12 17:26:27 +02:00
parent e78a7beaa3
commit 3839d2000d
22 changed files with 3954 additions and 144 deletions

View File

@@ -39,4 +39,6 @@ const (
ReportEntityType
TrustCenterEntityType
TrustCenterAccessEntityType
VendorBusinessAssociateAgreementEntityType
FileEntityType
)

176
pkg/coredata/file.go Normal file
View File

@@ -0,0 +1,176 @@
// 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"
"fmt"
"maps"
"time"
"github.com/getprobo/probo/pkg/gid"
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
)
type (
File struct {
ID gid.GID `db:"id"`
BucketName string `db:"bucket_name"`
MimeType string `db:"mime_type"`
FileName string `db:"file_name"`
FileKey string `db:"file_key"`
FileSize int `db:"file_size"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
DeletedAt *time.Time `db:"deleted_at"`
}
Files []*File
)
func (f *File) LoadByID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
fileID gid.GID,
) error {
q := `
SELECT
id,
bucket_name,
mime_type,
file_name,
file_key,
file_size,
created_at,
updated_at,
deleted_at
FROM
files
WHERE
%s
AND id = @file_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"file_id": fileID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query file: %w", err)
}
defer rows.Close()
file, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[File])
if err != nil {
return fmt.Errorf("cannot collect file: %w", err)
}
*f = file
return nil
}
func (f File) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
files (
id,
tenant_id,
bucket_name,
mime_type,
file_name,
file_key,
file_size,
created_at,
updated_at,
deleted_at
)
VALUES (
@file_id,
@tenant_id,
@bucket_name,
@mime_type,
@file_name,
@file_key,
@file_size,
@created_at,
@updated_at,
@deleted_at
)
`
args := pgx.StrictNamedArgs{
"file_id": f.ID,
"tenant_id": scope.GetTenantID(),
"bucket_name": f.BucketName,
"mime_type": f.MimeType,
"file_name": f.FileName,
"file_key": f.FileKey,
"file_size": f.FileSize,
"created_at": f.CreatedAt,
"updated_at": f.UpdatedAt,
"deleted_at": f.DeletedAt,
}
_, err := conn.Exec(ctx, q, args)
return err
}
func (f File) Delete(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
DELETE FROM files WHERE %s AND id = @file_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"file_id": f.ID}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
return err
}
func (f File) SoftDelete(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
UPDATE files SET deleted_at = @deleted_at, updated_at = @updated_at WHERE %s AND id = @file_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"file_id": f.ID,
"updated_at": time.Now(),
"deleted_at": time.Now(),
}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
return err
}

View File

@@ -0,0 +1,42 @@
CREATE TABLE files (
id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
bucket_name TEXT NOT NULL,
mime_type TEXT NOT NULL,
file_name TEXT NOT NULL,
file_key UUID NOT NULL UNIQUE,
file_size INTEGER NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
deleted_at TIMESTAMP WITH TIME ZONE
);
CREATE TABLE vendor_business_associate_agreements (
id TEXT PRIMARY KEY,
organization_id TEXT NOT NULL,
tenant_id TEXT NOT NULL,
vendor_id TEXT NOT NULL,
valid_from DATE,
valid_until DATE,
file_id TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT vendor_business_associate_agreements_organization_vendor_unique
UNIQUE (organization_id, vendor_id),
CONSTRAINT vendor_business_associate_agreements_file_id_unique
UNIQUE (file_id),
CONSTRAINT vendor_business_associate_agreements_organization_id_fkey
FOREIGN KEY (organization_id)
REFERENCES organizations(id)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT vendor_business_associate_agreements_file_id_fkey
FOREIGN KEY (file_id)
REFERENCES files(id)
ON UPDATE CASCADE
ON DELETE CASCADE
);

View File

@@ -0,0 +1,277 @@
// 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"
"fmt"
"maps"
"time"
"github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
)
type (
VendorBusinessAssociateAgreement struct {
ID gid.GID `db:"id"`
OrganizationID gid.GID `db:"organization_id"`
VendorID gid.GID `db:"vendor_id"`
ValidFrom *time.Time `db:"valid_from"`
ValidUntil *time.Time `db:"valid_until"`
FileID gid.GID `db:"file_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
VendorBusinessAssociateAgreements []*VendorBusinessAssociateAgreement
)
func (v VendorBusinessAssociateAgreement) CursorKey(orderBy VendorBusinessAssociateAgreementOrderField) page.CursorKey {
switch orderBy {
case VendorBusinessAssociateAgreementOrderFieldValidFrom:
return page.NewCursorKey(v.ID, v.ValidFrom)
case VendorBusinessAssociateAgreementOrderFieldCreatedAt:
return page.NewCursorKey(v.ID, v.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (vbaa *VendorBusinessAssociateAgreement) LoadByVendorID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
vendorID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
vendor_id,
valid_from,
valid_until,
file_id,
created_at,
updated_at
FROM
vendor_business_associate_agreements
WHERE
%s
AND vendor_id = @vendor_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.NamedArgs{"vendor_id": vendorID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query vendor business associate agreement: %w", err)
}
vendorBusinessAssociateAgreement, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[VendorBusinessAssociateAgreement])
if err != nil {
return fmt.Errorf("cannot collect vendor business associate agreement: %w", err)
}
*vbaa = vendorBusinessAssociateAgreement
return nil
}
func (vbaa *VendorBusinessAssociateAgreement) LoadByID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
vendorBusinessAssociateAgreementID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
vendor_id,
valid_from,
valid_until,
file_id,
created_at,
updated_at
FROM
vendor_business_associate_agreements
WHERE
%s
AND id = @id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.NamedArgs{"id": vendorBusinessAssociateAgreementID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query vendor business associate agreement: %w", err)
}
vendorBusinessAssociateAgreement, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[VendorBusinessAssociateAgreement])
if err != nil {
return fmt.Errorf("cannot collect vendor business associate agreement: %w", err)
}
*vbaa = vendorBusinessAssociateAgreement
return nil
}
func (vbaa *VendorBusinessAssociateAgreement) Update(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
UPDATE
vendor_business_associate_agreements
SET
valid_from = @valid_from,
valid_until = @valid_until,
file_id = @file_id,
updated_at = @updated_at
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": vbaa.ID,
"valid_from": vbaa.ValidFrom,
"valid_until": vbaa.ValidUntil,
"file_id": vbaa.FileID,
"updated_at": vbaa.UpdatedAt,
}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot update vendor business associate agreement: %w", err)
}
return nil
}
func (vbaa *VendorBusinessAssociateAgreement) Upsert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
vendor_business_associate_agreements (
id,
tenant_id,
organization_id,
vendor_id,
valid_from,
valid_until,
file_id,
created_at,
updated_at
)
VALUES (
@id,
@tenant_id,
@organization_id,
@vendor_id,
@valid_from,
@valid_until,
@file_id,
@created_at,
@updated_at
)
ON CONFLICT (organization_id, vendor_id) DO UPDATE SET
id = EXCLUDED.id,
valid_from = EXCLUDED.valid_from,
valid_until = EXCLUDED.valid_until,
file_id = EXCLUDED.file_id,
updated_at = EXCLUDED.updated_at
`
args := pgx.StrictNamedArgs{
"id": vbaa.ID,
"tenant_id": scope.GetTenantID(),
"vendor_id": vbaa.VendorID,
"organization_id": vbaa.OrganizationID,
"valid_from": vbaa.ValidFrom,
"valid_until": vbaa.ValidUntil,
"file_id": vbaa.FileID,
"created_at": vbaa.CreatedAt,
"updated_at": vbaa.UpdatedAt,
}
_, err := conn.Exec(ctx, q, args)
return err
}
func (vbaa *VendorBusinessAssociateAgreement) Delete(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
DELETE
FROM
vendor_business_associate_agreements
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"id": vbaa.ID}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
return err
}
func (vbaa *VendorBusinessAssociateAgreement) DeleteByVendorID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
vendorID gid.GID,
) error {
q := `
DELETE
FROM
vendor_business_associate_agreements
WHERE
%s
AND vendor_id = @vendor_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"vendor_id": vendorID}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
return err
}

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 (
VendorBusinessAssociateAgreementOrderField string
)
const (
VendorBusinessAssociateAgreementOrderFieldValidFrom VendorBusinessAssociateAgreementOrderField = "VALID_FROM"
VendorBusinessAssociateAgreementOrderFieldCreatedAt VendorBusinessAssociateAgreementOrderField = "CREATED_AT"
)
func (p VendorBusinessAssociateAgreementOrderField) Column() string {
return string(p)
}
func (p VendorBusinessAssociateAgreementOrderField) String() string {
return string(p)
}
func (p VendorBusinessAssociateAgreementOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *VendorBusinessAssociateAgreementOrderField) UnmarshalText(text []byte) error {
*p = VendorBusinessAssociateAgreementOrderField(text)
return nil
}