Add vendor services

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-08-18 18:26:17 +02:00
parent 0262bd1c3d
commit 54c14a1816
25 changed files with 4638 additions and 68 deletions

View File

@@ -45,4 +45,5 @@ const (
VendorDataPrivacyAgreementEntityType
NonconformityRegistryEntityType
ComplianceRegistryEntityType
VendorServiceEntityType
)

View File

@@ -0,0 +1,15 @@
CREATE TABLE vendor_services (
id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
vendor_id TEXT NOT NULL,
name TEXT NOT NULL,
description TEXT,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT vendor_services_vendor_id_fkey
FOREIGN KEY (vendor_id)
REFERENCES vendors(id)
ON UPDATE CASCADE
ON DELETE CASCADE
);

View File

@@ -0,0 +1,259 @@
// 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/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
)
type (
VendorService struct {
ID gid.GID `db:"id"`
VendorID gid.GID `db:"vendor_id"`
Name string `db:"name"`
Description *string `db:"description"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
VendorServices []*VendorService
ErrVendorServiceNotFound struct {
Identifier string
}
)
func (e ErrVendorServiceNotFound) Error() string {
return fmt.Sprintf("vendor service not found: %s", e.Identifier)
}
func (vs VendorService) CursorKey(orderBy VendorServiceOrderField) page.CursorKey {
switch orderBy {
case VendorServiceOrderFieldCreatedAt:
return page.CursorKey{ID: vs.ID, Value: vs.CreatedAt}
case VendorServiceOrderFieldName:
return page.CursorKey{ID: vs.ID, Value: vs.Name}
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (vs *VendorService) LoadByID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
vendorServiceID gid.GID,
) error {
q := `
SELECT
id,
vendor_id,
name,
description,
created_at,
updated_at
FROM
vendor_services
WHERE
%s
AND id = @vendor_service_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"vendor_service_id": vendorServiceID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query vendor service: %w", err)
}
defer rows.Close()
vendorService, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[VendorService])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return &ErrVendorServiceNotFound{Identifier: vendorServiceID.String()}
}
return fmt.Errorf("cannot collect vendor service: %w", err)
}
*vs = vendorService
return nil
}
func (vs *VendorServices) LoadByVendorID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
vendorID gid.GID,
cursor *page.Cursor[VendorServiceOrderField],
) error {
q := `
SELECT
id,
vendor_id,
name,
description,
created_at,
updated_at
FROM
vendor_services
WHERE
%s
AND vendor_id = @vendor_id
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{
"vendor_id": vendorID,
}
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 vendor services: %w", err)
}
defer rows.Close()
vendorServices, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[VendorService])
if err != nil {
return fmt.Errorf("cannot collect vendor services: %w", err)
}
*vs = vendorServices
return nil
}
func (vs VendorService) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
vendor_services (
tenant_id,
id,
vendor_id,
name,
description,
created_at,
updated_at
)
VALUES (
@tenant_id,
@vendor_service_id,
@vendor_id,
@name,
@description,
@created_at,
@updated_at
)
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"vendor_service_id": vs.ID,
"vendor_id": vs.VendorID,
"name": vs.Name,
"description": vs.Description,
"created_at": vs.CreatedAt,
"updated_at": vs.UpdatedAt,
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot insert vendor service: %w", err)
}
return nil
}
func (vs VendorService) Update(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
UPDATE
vendor_services
SET
name = @name,
description = @description,
updated_at = @updated_at
WHERE
%s
AND id = @vendor_service_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"vendor_service_id": vs.ID,
"name": vs.Name,
"description": vs.Description,
"updated_at": vs.UpdatedAt,
}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot update vendor service: %w", err)
}
return nil
}
func (vs VendorService) Delete(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
DELETE FROM
vendor_services
WHERE
%s
AND id = @vendor_service_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"vendor_service_id": vs.ID}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot delete vendor service: %w", err)
}
return nil
}

View File

@@ -0,0 +1,51 @@
// 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 (
"fmt"
)
type (
VendorServiceOrderField string
)
const (
VendorServiceOrderFieldCreatedAt VendorServiceOrderField = "CREATED_AT"
VendorServiceOrderFieldName VendorServiceOrderField = "NAME"
)
func (p VendorServiceOrderField) Column() string {
return string(p)
}
func (p VendorServiceOrderField) String() string {
return string(p)
}
func (p VendorServiceOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *VendorServiceOrderField) UnmarshalText(text []byte) error {
val := string(text)
switch val {
case string(VendorServiceOrderFieldCreatedAt),
string(VendorServiceOrderFieldName):
*p = VendorServiceOrderField(val)
return nil
}
return fmt.Errorf("invalid VendorServiceOrderField value: %q", val)
}