Add delete vendor action

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-02-12 15:50:01 -08:00
parent 32e6391c80
commit fe95902c81
8 changed files with 330 additions and 15 deletions

View File

@@ -56,6 +56,12 @@ const createVendorMutation = graphql`
}
`;
const deleteVendorMutation = graphql`
mutation VendorListPageDeleteVendorMutation($input: DeleteVendorInput!) {
deleteVendor(input: $input)
}
`;
// TODO: Remove this once we have a real list of vendors
const vendorsList = [
{ id: '1', name: 'Amazon Web Services', createdAt: new Date().toISOString() },
@@ -94,6 +100,7 @@ function VendorListContent({
const [searchTerm, setSearchTerm] = useState("");
const [filteredVendors, setFilteredVendors] = useState<Array<any>>([]);
const [createVendor] = useMutation(createVendorMutation);
const [deleteVendor] = useMutation(deleteVendorMutation);
const vendors = data.node?.vendors?.edges?.map(edge => edge?.node) ?? [];
const pageInfo = data.node?.vendors?.pageInfo;
@@ -213,19 +220,21 @@ function VendorListContent({
<div className="space-y-2">
{vendors.map((vendor) => (
<div key={vendor?.id} className="block hover:bg-accent/50 transition-colors">
<Link
to={`/vendors/${vendor?.id}`}
className="flex items-center justify-between p-4 rounded-lg border bg-card text-card-foreground shadow-sm"
>
<div className="flex items-center gap-4">
<Avatar className="h-10 w-10">
<AvatarFallback>{vendor?.name?.[0]}</AvatarFallback>
</Avatar>
<div className="space-y-1">
<p className="text-sm font-medium leading-none">{vendor?.name}</p>
<p className="text-sm text-muted-foreground">Vendor since {new Date(vendor?.createdAt).toLocaleDateString()}</p>
<div className="flex items-center justify-between p-4 rounded-lg border bg-card text-card-foreground shadow-sm">
<Link
to={`/vendors/${vendor?.id}`}
className="flex items-center gap-4 flex-1"
>
<div className="flex items-center gap-4">
<Avatar className="h-10 w-10">
<AvatarFallback>{vendor?.name?.[0]}</AvatarFallback>
</Avatar>
<div className="space-y-1">
<p className="text-sm font-medium leading-none">{vendor?.name}</p>
<p className="text-sm text-muted-foreground">Vendor since {new Date(vendor?.createdAt).toLocaleDateString()}</p>
</div>
</div>
</div>
</Link>
<div className="flex items-center gap-4">
<Badge variant="secondary" className="font-medium">
Active
@@ -238,8 +247,35 @@ function VendorListContent({
<Badge variant="outline" className="text-muted-foreground">
Not assessed
</Badge>
<Button
variant="ghost"
size="sm"
className="text-destructive hover:text-destructive hover:bg-destructive/10"
onClick={(e) => {
e.preventDefault();
if (window.confirm('Are you sure you want to delete this vendor?')) {
deleteVendor({
variables: {
input: {
vendorId: vendor.id
}
},
onCompleted() {
loadQuery({
first: ITEMS_PER_PAGE,
after: undefined,
last: undefined,
before: undefined,
}, { fetchPolicy: 'network-only' });
},
});
}
}}
>
Delete
</Button>
</div>
</Link>
</div>
</div>
))}

View File

@@ -0,0 +1,79 @@
/**
* @generated SignedSource<<b97adcd0ae4816dc772fcbf3b3c6b3d7>>
* @lightSyntaxTransform
* @nogrep
*/
/* tslint:disable */
/* eslint-disable */
// @ts-nocheck
import { ConcreteRequest } from 'relay-runtime';
export type DeleteVendorInput = {
vendorId: string;
};
export type VendorListPageDeleteVendorMutation$variables = {
input: DeleteVendorInput;
};
export type VendorListPageDeleteVendorMutation$data = {
readonly deleteVendor: any;
};
export type VendorListPageDeleteVendorMutation = {
response: VendorListPageDeleteVendorMutation$data;
variables: VendorListPageDeleteVendorMutation$variables;
};
const node: ConcreteRequest = (function(){
var v0 = [
{
"defaultValue": null,
"kind": "LocalArgument",
"name": "input"
}
],
v1 = [
{
"alias": null,
"args": [
{
"kind": "Variable",
"name": "input",
"variableName": "input"
}
],
"kind": "ScalarField",
"name": "deleteVendor",
"storageKey": null
}
];
return {
"fragment": {
"argumentDefinitions": (v0/*: any*/),
"kind": "Fragment",
"metadata": null,
"name": "VendorListPageDeleteVendorMutation",
"selections": (v1/*: any*/),
"type": "Mutation",
"abstractKey": null
},
"kind": "Request",
"operation": {
"argumentDefinitions": (v0/*: any*/),
"kind": "Operation",
"name": "VendorListPageDeleteVendorMutation",
"selections": (v1/*: any*/)
},
"params": {
"cacheID": "6b9ec06210106cc8f8905fbfda5c60b0",
"id": null,
"metadata": {},
"name": "VendorListPageDeleteVendorMutation",
"operationKind": "mutation",
"text": "mutation VendorListPageDeleteVendorMutation(\n $input: DeleteVendorInput!\n) {\n deleteVendor(input: $input)\n}\n"
}
};
})();
(node as any).hash = "a435b00067d595fc8a1dec393ed1d0b6";
export default node;

View File

@@ -289,9 +289,14 @@ type Query {
type Mutation {
createVendor(input: CreateVendorInput!): Vendor!
deleteVendor(input: DeleteVendorInput!): Void!
}
input CreateVendorInput {
organizationId: ID!
name: String!
}
}
input DeleteVendorInput {
vendorId: ID!
}

View File

@@ -155,6 +155,7 @@ type ComplexityRoot struct {
Mutation struct {
CreateVendor func(childComplexity int, input types.CreateVendorInput) int
DeleteVendor func(childComplexity int, input types.DeleteVendorInput) int
}
Organization struct {
@@ -268,6 +269,7 @@ type FrameworkResolver interface {
}
type MutationResolver interface {
CreateVendor(ctx context.Context, input types.CreateVendorInput) (*types.Vendor, error)
DeleteVendor(ctx context.Context, input types.DeleteVendorInput) (string, error)
}
type OrganizationResolver interface {
Frameworks(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.FrameworkConnection, error)
@@ -711,6 +713,18 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
return e.complexity.Mutation.CreateVendor(childComplexity, args["input"].(types.CreateVendorInput)), true
case "Mutation.deleteVendor":
if e.complexity.Mutation.DeleteVendor == nil {
break
}
args, err := ec.field_Mutation_deleteVendor_args(context.TODO(), rawArgs)
if err != nil {
return 0, false
}
return e.complexity.Mutation.DeleteVendor(childComplexity, args["input"].(types.DeleteVendorInput)), true
case "Organization.createdAt":
if e.complexity.Organization.CreatedAt == nil {
break
@@ -1121,6 +1135,7 @@ func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler {
ec := executionContext{opCtx, e, 0, 0, make(chan graphql.DeferredResult)}
inputUnmarshalMap := graphql.BuildUnmarshalerMap(
ec.unmarshalInputCreateVendorInput,
ec.unmarshalInputDeleteVendorInput,
)
first := true
@@ -1509,12 +1524,18 @@ type Query {
type Mutation {
createVendor(input: CreateVendorInput!): Vendor!
deleteVendor(input: DeleteVendorInput!): Void!
}
input CreateVendorInput {
organizationId: ID!
name: String!
}`, BuiltIn: false},
}
input DeleteVendorInput {
vendorId: ID!
}
`, BuiltIn: false},
}
var parsedSchema = gqlparser.MustLoadSchema(sources...)
@@ -1853,6 +1874,29 @@ func (ec *executionContext) field_Mutation_createVendor_argsInput(
return zeroVal, nil
}
func (ec *executionContext) field_Mutation_deleteVendor_args(ctx context.Context, rawArgs map[string]any) (map[string]any, error) {
var err error
args := map[string]any{}
arg0, err := ec.field_Mutation_deleteVendor_argsInput(ctx, rawArgs)
if err != nil {
return nil, err
}
args["input"] = arg0
return args, nil
}
func (ec *executionContext) field_Mutation_deleteVendor_argsInput(
ctx context.Context,
rawArgs map[string]any,
) (types.DeleteVendorInput, error) {
ctx = graphql.WithPathContext(ctx, graphql.NewPathWithField("input"))
if tmp, ok := rawArgs["input"]; ok {
return ec.unmarshalNDeleteVendorInput2githubᚗcomᚋgetproboᚋproboᚋpkgᚋapiᚋconsoleᚋv1ᚋtypesᚐDeleteVendorInput(ctx, tmp)
}
var zeroVal types.DeleteVendorInput
return zeroVal, nil
}
func (ec *executionContext) field_Organization_frameworks_args(ctx context.Context, rawArgs map[string]any) (map[string]any, error) {
var err error
args := map[string]any{}
@@ -4633,6 +4677,49 @@ func (ec *executionContext) fieldContext_Mutation_createVendor(ctx context.Conte
return fc, nil
}
func (ec *executionContext) _Mutation_deleteVendor(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
fc, err := ec.fieldContext_Mutation_deleteVendor(ctx, field)
if err != nil {
return graphql.Null
}
ctx = graphql.WithFieldContext(ctx, fc)
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (any, error) {
ctx = rctx // use context from middleware stack in children
return ec.resolvers.Mutation().DeleteVendor(rctx, fc.Args["input"].(types.DeleteVendorInput))
})
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
if resTmp == nil {
if !graphql.HasFieldError(ctx, fc) {
ec.Errorf(ctx, "must not be null")
}
return graphql.Null
}
res := resTmp.(string)
fc.Result = res
return ec.marshalNVoid2string(ctx, field.Selections, res)
}
func (ec *executionContext) fieldContext_Mutation_deleteVendor(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
fc = &graphql.FieldContext{
Object: "Mutation",
Field: field,
IsMethod: true,
IsResolver: true,
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return nil, errors.New("field of type Void does not have child fields")
},
}
ctx = graphql.WithFieldContext(ctx, fc)
if fc.Args, err = ec.field_Mutation_deleteVendor_args(ctx, field.ArgumentMap(ec.Variables)); err != nil {
ec.Error(ctx, err)
return fc, err
}
return fc, nil
}
func (ec *executionContext) _Organization_id(ctx context.Context, field graphql.CollectedField, obj *types.Organization) (ret graphql.Marshaler) {
fc, err := ec.fieldContext_Organization_id(ctx, field)
if err != nil {
@@ -8511,6 +8598,33 @@ func (ec *executionContext) unmarshalInputCreateVendorInput(ctx context.Context,
return it, nil
}
func (ec *executionContext) unmarshalInputDeleteVendorInput(ctx context.Context, obj any) (types.DeleteVendorInput, error) {
var it types.DeleteVendorInput
asMap := map[string]any{}
for k, v := range obj.(map[string]any) {
asMap[k] = v
}
fieldsInOrder := [...]string{"vendorId"}
for _, k := range fieldsInOrder {
v, ok := asMap[k]
if !ok {
continue
}
switch k {
case "vendorId":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("vendorId"))
data, err := ec.unmarshalNID2githubᚗcomᚋgetproboᚋproboᚋpkgᚋgidᚐGID(ctx, v)
if err != nil {
return it, err
}
it.VendorID = data
}
}
return it, nil
}
// endregion **************************** input.gotpl *****************************
// region ************************** interface.gotpl ***************************
@@ -9475,6 +9589,13 @@ func (ec *executionContext) _Mutation(ctx context.Context, sel ast.SelectionSet)
if out.Values[i] == graphql.Null {
out.Invalids++
}
case "deleteVendor":
out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) {
return ec._Mutation_deleteVendor(ctx, field)
})
if out.Values[i] == graphql.Null {
out.Invalids++
}
default:
panic("unknown field " + strconv.Quote(field.Name))
}
@@ -10949,6 +11070,11 @@ func (ec *executionContext) marshalNDatetime2timeᚐTime(ctx context.Context, se
return res
}
func (ec *executionContext) unmarshalNDeleteVendorInput2githubᚗcomᚋgetproboᚋproboᚋpkgᚋapiᚋconsoleᚋv1ᚋtypesᚐDeleteVendorInput(ctx context.Context, v any) (types.DeleteVendorInput, error) {
res, err := ec.unmarshalInputDeleteVendorInput(ctx, v)
return res, graphql.ErrorOnPath(ctx, err)
}
func (ec *executionContext) marshalNEvidence2ᚖgithubᚗcomᚋgetproboᚋproboᚋpkgᚋapiᚋconsoleᚋv1ᚋtypesᚐEvidence(ctx context.Context, sel ast.SelectionSet, v *types.Evidence) graphql.Marshaler {
if v == nil {
if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) {
@@ -11574,6 +11700,21 @@ func (ec *executionContext) marshalNVendorEdge2ᚖgithubᚗcomᚋgetproboᚋprob
return ec._VendorEdge(ctx, sel, v)
}
func (ec *executionContext) unmarshalNVoid2string(ctx context.Context, v any) (string, error) {
res, err := graphql.UnmarshalString(v)
return res, graphql.ErrorOnPath(ctx, err)
}
func (ec *executionContext) marshalNVoid2string(ctx context.Context, sel ast.SelectionSet, v string) graphql.Marshaler {
res := graphql.MarshalString(v)
if res == graphql.Null {
if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) {
ec.Errorf(ctx, "the requested element is null which the schema does not allow")
}
}
return res
}
func (ec *executionContext) marshalN__Directive2githubᚗcomᚋ99designsᚋgqlgenᚋgraphqlᚋintrospectionᚐDirective(ctx context.Context, sel ast.SelectionSet, v introspection.Directive) graphql.Marshaler {
return ec.___Directive(ctx, sel, &v)
}

View File

@@ -65,6 +65,10 @@ type CreateVendorInput struct {
Name string `json:"name"`
}
type DeleteVendorInput struct {
VendorID gid.GID `json:"vendorId"`
}
type Evidence struct {
ID gid.GID `json:"id"`
FileURL string `json:"fileUrl"`

View File

@@ -78,6 +78,16 @@ func (r *mutationResolver) CreateVendor(ctx context.Context, input types.CreateV
return types.NewVendor(vendor), nil
}
// DeleteVendor is the resolver for the deleteVendor field.
func (r *mutationResolver) DeleteVendor(ctx context.Context, input types.DeleteVendorInput) (string, error) {
err := r.svc.DeleteVendor(ctx, input.VendorID)
if err != nil {
return "", fmt.Errorf("cannot delete vendor: %w", err)
}
return "", nil
}
// Frameworks is the resolver for the frameworks field.
func (r *organizationResolver) Frameworks(ctx context.Context, obj *types.Organization, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.FrameworkConnection, error) {
cursor := types.NewCursor(first, after, last, before)

View File

@@ -123,6 +123,24 @@ VALUES (
return err
}
func (v Vendor) Delete(
ctx context.Context,
conn pg.Conn,
scope *Scope,
) error {
q := `
DELETE FROM vendors WHERE %s AND id = @vendor_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.NamedArgs{"vendor_id": v.ID}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
return err
}
func (v *Vendors) LoadByOrganizationID(
ctx context.Context,
conn pg.Conn,

View File

@@ -0,0 +1,22 @@
package probo
import (
"context"
"github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/probo/coredata"
"go.gearno.de/kit/pg"
)
func (s Service) DeleteVendor(
ctx context.Context,
vendorID gid.GID,
) error {
vendor := coredata.Vendor{ID: vendorID}
return s.pg.WithConn(
ctx,
func(conn pg.Conn) error {
return vendor.Delete(ctx, conn, s.scope)
},
)
}