4
pkg/coredata/migrations/20250412T100700Z.sql
Normal file
4
pkg/coredata/migrations/20250412T100700Z.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
CREATE TYPE risk_treatment AS ENUM ('MITIGATED', 'TRANSFERRED', 'AVOIDED', 'ACCEPTED');
|
||||
|
||||
ALTER TABLE risks ADD COLUMN treatment risk_treatment NOT NULL DEFAULT 'MITIGATED';
|
||||
ALTER TABLE risks ALTER COLUMN treatment DROP DEFAULT;
|
||||
@@ -28,16 +28,17 @@ import (
|
||||
|
||||
type (
|
||||
Risk struct {
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
Name string `db:"name"`
|
||||
Description string `db:"description"`
|
||||
InherentLikelihood float64 `db:"inherent_likelihood"`
|
||||
InherentImpact float64 `db:"inherent_impact"`
|
||||
ResidualLikelihood float64 `db:"residual_likelihood"`
|
||||
ResidualImpact float64 `db:"residual_impact"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
Name string `db:"name"`
|
||||
Description string `db:"description"`
|
||||
Treatment RiskTreatment `db:"treatment"`
|
||||
InherentLikelihood float64 `db:"inherent_likelihood"`
|
||||
InherentImpact float64 `db:"inherent_impact"`
|
||||
ResidualLikelihood float64 `db:"residual_likelihood"`
|
||||
ResidualImpact float64 `db:"residual_impact"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
Risks []*Risk
|
||||
@@ -72,9 +73,9 @@ WITH rsks AS (
|
||||
SELECT
|
||||
r.id,
|
||||
r.organization_id,
|
||||
r.tenant_id,
|
||||
r.name,
|
||||
r.description,
|
||||
r.treatment,
|
||||
r.inherent_likelihood,
|
||||
r.inherent_impact,
|
||||
r.residual_likelihood,
|
||||
@@ -93,6 +94,7 @@ SELECT
|
||||
organization_id,
|
||||
name,
|
||||
description,
|
||||
treatment,
|
||||
inherent_likelihood,
|
||||
inherent_impact,
|
||||
residual_likelihood,
|
||||
@@ -137,6 +139,7 @@ SELECT
|
||||
organization_id,
|
||||
name,
|
||||
description,
|
||||
treatment,
|
||||
inherent_likelihood,
|
||||
inherent_impact,
|
||||
residual_likelihood,
|
||||
@@ -180,6 +183,7 @@ SELECT
|
||||
organization_id,
|
||||
name,
|
||||
description,
|
||||
treatment,
|
||||
inherent_likelihood,
|
||||
inherent_impact,
|
||||
residual_likelihood,
|
||||
@@ -217,8 +221,8 @@ func (r *Risk) Insert(
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO risks (id, tenant_id, organization_id, name, description, inherent_likelihood, inherent_impact, residual_likelihood, residual_impact, created_at, updated_at)
|
||||
VALUES (@id, @tenant_id, @organization_id, @name, @description, @inherent_likelihood, @inherent_impact, @residual_likelihood, @residual_impact, @created_at, @updated_at)
|
||||
INSERT INTO risks (id, tenant_id, organization_id, name, description, treatment, inherent_likelihood, inherent_impact, residual_likelihood, residual_impact, created_at, updated_at)
|
||||
VALUES (@id, @tenant_id, @organization_id, @name, @description, @treatment, @inherent_likelihood, @inherent_impact, @residual_likelihood, @residual_impact, @created_at, @updated_at)
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
@@ -227,6 +231,7 @@ VALUES (@id, @tenant_id, @organization_id, @name, @description, @inherent_likeli
|
||||
"organization_id": r.OrganizationID,
|
||||
"name": r.Name,
|
||||
"description": r.Description,
|
||||
"treatment": r.Treatment,
|
||||
"inherent_likelihood": r.InherentLikelihood,
|
||||
"inherent_impact": r.InherentImpact,
|
||||
"residual_likelihood": r.ResidualLikelihood,
|
||||
@@ -249,6 +254,7 @@ UPDATE risks
|
||||
SET
|
||||
name = @name,
|
||||
description = @description,
|
||||
treatment = @treatment,
|
||||
inherent_likelihood = @inherent_likelihood,
|
||||
inherent_impact = @inherent_impact,
|
||||
residual_likelihood = @residual_likelihood,
|
||||
@@ -263,6 +269,7 @@ WHERE %s
|
||||
"risk_id": r.ID,
|
||||
"name": r.Name,
|
||||
"description": r.Description,
|
||||
"treatment": r.Treatment,
|
||||
"inherent_likelihood": r.InherentLikelihood,
|
||||
"inherent_impact": r.InherentImpact,
|
||||
"residual_likelihood": r.ResidualLikelihood,
|
||||
|
||||
84
pkg/coredata/risk_treatment.go
Normal file
84
pkg/coredata/risk_treatment.go
Normal file
@@ -0,0 +1,84 @@
|
||||
// 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 (
|
||||
RiskTreatment string
|
||||
)
|
||||
|
||||
const (
|
||||
RiskTreatmentMitigated RiskTreatment = "MITIGATED"
|
||||
RiskTreatmentAccepted RiskTreatment = "ACCEPTED"
|
||||
RiskTreatmentAvoided RiskTreatment = "AVOIDED"
|
||||
RiskTreatmentTransferred RiskTreatment = "TRANSFERRED"
|
||||
)
|
||||
|
||||
func (rt RiskTreatment) MarshalText() ([]byte, error) {
|
||||
return []byte(rt.String()), nil
|
||||
}
|
||||
|
||||
func (rt *RiskTreatment) UnmarshalText(data []byte) error {
|
||||
val := string(data)
|
||||
|
||||
switch val {
|
||||
case RiskTreatmentMitigated.String():
|
||||
*rt = RiskTreatmentMitigated
|
||||
case RiskTreatmentAccepted.String():
|
||||
*rt = RiskTreatmentAccepted
|
||||
case RiskTreatmentAvoided.String():
|
||||
*rt = RiskTreatmentAvoided
|
||||
case RiskTreatmentTransferred.String():
|
||||
*rt = RiskTreatmentTransferred
|
||||
default:
|
||||
return fmt.Errorf("invalid RiskTreatment value: %q", val)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rt RiskTreatment) String() string {
|
||||
var val string
|
||||
|
||||
switch rt {
|
||||
case RiskTreatmentMitigated:
|
||||
val = "MITIGATED"
|
||||
case RiskTreatmentAccepted:
|
||||
val = "ACCEPTED"
|
||||
case RiskTreatmentAvoided:
|
||||
val = "AVOIDED"
|
||||
case RiskTreatmentTransferred:
|
||||
val = "TRANSFERRED"
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
func (rt *RiskTreatment) Scan(value any) error {
|
||||
val, ok := value.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid scan source for RiskTreatment, expected string got %T", value)
|
||||
}
|
||||
|
||||
return rt.UnmarshalText([]byte(val))
|
||||
}
|
||||
|
||||
func (rt RiskTreatment) Value() (driver.Value, error) {
|
||||
return rt.String(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user