diff --git a/api/handler/contract.go b/api/handler/contract.go index d624c1a..405f90f 100644 --- a/api/handler/contract.go +++ b/api/handler/contract.go @@ -58,11 +58,17 @@ func (a contract) Create(c echo.Context) error { } body := model.RequestContractCreate{} + if err := c.Bind(&body); err != nil { common.LogStringError(c, err, "contract: create bind") return httperror.BadRequest400(c) } + if err := c.Validate(body); err != nil { + common.LogStringError(c, err, "contract: create validate") + return httperror.InvalidPayload400(c, err) + } + SanitizeChecksums(&body.Address) m, err := a.service.Create(c.Request().Context(), body, callerId, organizationId) @@ -228,11 +234,17 @@ func (a contract) Update(c echo.Context) error { } body := model.RequestContractUpdate{} - err := c.Bind(&body) - if err != nil { + + if err := c.Bind(&body); err != nil { common.LogStringError(c, err, "contract: update bind") return httperror.BadRequest400(c) } + + if err := c.Validate(body); err != nil { + common.LogStringError(c, err, "contract: update validate") + return httperror.InvalidPayload400(c, err) + } + if body.Address != nil { SanitizeChecksums(body.Address) } diff --git a/pkg/model/entity.go b/pkg/model/entity.go index 72b7912..8147a10 100644 --- a/pkg/model/entity.go +++ b/pkg/model/entity.go @@ -94,6 +94,7 @@ type Contract struct { Name string `json:"name" db:"name"` Address string `json:"address" db:"address"` Functions pq.StringArray `json:"functions" db:"functions" swaggertype:"array,string"` + Type string `json:"type" db:"type"` NetworkId string `json:"networkId" db:"network_id"` OrganizationId string `json:"organizationId" db:"organization_id"` PlatformIds pq.StringArray `json:"platformIds" db:"platform_ids" swaggertype:"array,string"` diff --git a/pkg/model/request.go b/pkg/model/request.go index 881662e..9709179 100644 --- a/pkg/model/request.go +++ b/pkg/model/request.go @@ -3,14 +3,14 @@ package model import "github.com/lib/pq" type RequestOrganizationCreate struct { - OrganizationName string `json:"organizationName" validate:"required"` + OrganizationName string `json:"organizationName" validate:"required,max=100"` Email string `json:"email" validate:"required,email"` - Name string `json:"name" validate:"required"` + Name string `json:"name" validate:"required,max=100"` } type RequestOrganizationUpdate struct { - Name *string `json:"organizationName" db:"name"` - Description *string `json:"description" db:"description"` + Name *string `json:"organizationName" db:"name" validate:"omitempty,max=100"` + Description *string `json:"description" db:"description" validate:"omitempty,max=144"` } type RequestPlatformCreate struct { @@ -56,8 +56,8 @@ type RequestInviteAcceptance struct { type RequestMemberUpdateSelf struct { Name *string `json:"name" db:"name"` - OldPassword *string `json:"oldPassword" validate:"required_with=NewPassword,min=8,max=100"` - NewPassword *string `json:"newPassword" validate:"required_with=OldPassword,min=8,max=100"` + OldPassword *string `json:"oldPassword" validate:"required_with_all=OldPassword,NewPassword,min=8,max=100"` + NewPassword *string `json:"newPassword" validate:"required_with_all=NewPassword,OldPassword,min=8,max=100"` } type RequestMemberUpdateOther struct { @@ -86,18 +86,20 @@ type RequestPasswordReset struct { } type RequestContractCreate struct { - Name string `json:"name" db:"name"` - Address string `json:"address" db:"address"` - Functions pq.StringArray `json:"functions" db:"functions" swaggertype:"array,string"` - NetworkId string `json:"networkId" db:"network_id"` - PlatformIds pq.StringArray `json:"platformIds" db:"platform_ids" swaggertype:"array,string"` + Name string `json:"name" db:"name" validate:"required,max=100"` + Address string `json:"address" db:"address" validate:"required,eth_addr"` + Functions pq.StringArray `json:"functions" db:"functions" validate:"required" swaggertype:"array,string" ` + Type string `json:"type" db:"type" validate:"required,oneof=NFT TOKEN NFT_AND_TOKEN"` + NetworkId string `json:"networkId" db:"network_id" validate:"required,uuid"` + PlatformIds pq.StringArray `json:"platformIds" db:"platform_ids" validate:"required,min=1" swaggertype:"array,string"` OrganizationId string `json:"-" db:"organization_id"` } type RequestContractUpdate struct { - Name *string `json:"name"` - Address *string `json:"address"` + Name *string `json:"name" validate:"omitempty,max=100"` + Address *string `json:"address" validate:"omitempty,eth_addr"` Functions pq.StringArray `json:"functions" swaggertype:"array,string"` - NetworkId *string `json:"networkId"` - PlatformIds pq.StringArray `json:"platformIds"swaggertype:"array,string"` + Type *string `json:"type" validate:"omitempty,oneof=NFT TOKEN NFT_AND_TOKEN"` + NetworkId *string `json:"networkId" validate:"omitempty,uuid"` + PlatformIds pq.StringArray `json:"platformIds" validate:"omitempty,min=1" swaggertype:"array,string"` } diff --git a/pkg/repository/contract.go b/pkg/repository/contract.go index a002357..602aae1 100644 --- a/pkg/repository/contract.go +++ b/pkg/repository/contract.go @@ -48,13 +48,13 @@ func NewContract(db database.Queryable) Contract { func (c contract[T]) Create(ctx context.Context, request model.RequestContractCreate) (contract model.Contract, err error) { rows, err := c.Store.QueryxContext(ctx, ` WITH platforms AS ( - SELECT UNNEST($6::uuid[]) AS platform_id - WHERE EXISTS (SELECT 1 FROM platform WHERE organization_id = $5) + SELECT UNNEST($7::uuid[]) AS platform_id + WHERE EXISTS (SELECT 1 FROM platform WHERE organization_id = $6) ), ins_contract AS ( - INSERT INTO contract (name, address, functions, network_id, organization_id) - VALUES ($1, $2, $3, $4, $5) - RETURNING * + INSERT INTO contract (name, address, functions, type, network_id, organization_id) + VALUES ($1, $2, $3, $4, $5, $6) + RETURNING * ), ins_ctp AS ( INSERT INTO contract_to_platform (platform_id, contract_id) @@ -67,9 +67,9 @@ func (c contract[T]) Create(ctx context.Context, request model.RequestContractCr FROM ins_contract JOIN ins_ctp ON ins_contract.id = ins_ctp.contract_id GROUP BY ins_contract.id, ins_contract.name, ins_contract.address, ins_contract.functions, - ins_contract.network_id, ins_contract.organization_id, ins_contract.created_at, + ins_contract.type, ins_contract.network_id, ins_contract.organization_id, ins_contract.created_at, ins_contract.updated_at, ins_contract.deleted_at, ins_contract.deactivated_at, ins_contract.deleted_at - `, request.Name, request.Address, request.Functions, request.NetworkId, request.OrganizationId, request.PlatformIds) + `, request.Name, request.Address, request.Functions, request.Type, request.NetworkId, request.OrganizationId, request.PlatformIds) if err != nil { var pgErr *pq.Error if errors.As(err, &pgErr) { @@ -203,7 +203,7 @@ func (c contract[T]) Deactivate(ctx context.Context, id string, organizationId s SELECT uc.*, array_agg(jctp.platform_id) AS platform_ids FROM updated_contract uc JOIN contract_to_platform jctp ON uc.id = jctp.contract_id - GROUP BY uc.id, uc.name, uc.address, uc.organization_id, uc.functions, uc.network_id, uc.created_at, uc.updated_at, uc.deactivated_at, uc.deleted_at + GROUP BY uc.id, uc.name, uc.address, uc.organization_id, uc.functions, uc.type, uc.network_id, uc.created_at, uc.updated_at, uc.deactivated_at, uc.deleted_at `, id, organizationId) return model, libcommon.StringError(err) @@ -227,7 +227,7 @@ func (c contract[T]) Activate(ctx context.Context, id string, organizationId str SELECT uc.*, array_agg(jctp.platform_id) AS platform_ids FROM updated_contract uc JOIN contract_to_platform jctp ON uc.id = jctp.contract_id - GROUP BY uc.id, uc.name, uc.address, uc.organization_id, uc.functions, uc.network_id, uc.created_at, uc.updated_at, uc.deactivated_at, uc.deleted_at + GROUP BY uc.id, uc.name, uc.address, uc.organization_id, uc.functions, uc.type, uc.network_id, uc.created_at, uc.updated_at, uc.deactivated_at, uc.deleted_at `, id, organizationId) return model, libcommon.StringError(err) @@ -240,22 +240,23 @@ func (c contract[T]) Update(ctx context.Context, id string, organizationId strin SET name = COALESCE($1, name), address = COALESCE($2, address), functions = COALESCE($3, functions), - network_id = COALESCE($4, network_id) - WHERE id = $5 + type = COALESCE($4, type), + network_id = COALESCE($5, network_id) + WHERE id = $6 AND EXISTS ( SELECT 1 FROM contract_to_platform JOIN platform ON contract_to_platform.platform_id = platform.id WHERE contract_to_platform.contract_id = contract.id - AND platform.organization_id = $6 + AND platform.organization_id = $7 ) AND deleted_at IS NULL RETURNING * ), valid_platforms AS ( - SELECT UNNEST($7::uuid[]) AS platform_id + SELECT UNNEST($8::uuid[]) AS platform_id FROM platform - WHERE organization_id = $6 + WHERE organization_id = $7 ), new_ctp AS ( INSERT INTO contract_to_platform (platform_id, contract_id) @@ -266,11 +267,11 @@ func (c contract[T]) Update(ctx context.Context, id string, organizationId strin SELECT uc.*, array_agg(ctp.platform_id) AS platform_ids FROM updated_contract uc JOIN contract_to_platform ctp ON uc.id = ctp.contract_id - GROUP BY uc.id, uc.name, uc.address, uc.organization_id, uc.functions, uc.network_id, uc.created_at, uc.updated_at, uc.deactivated_at, uc.deleted_at + GROUP BY uc.id, uc.name, uc.address, uc.organization_id, uc.functions, uc.type, uc.network_id, uc.created_at, uc.updated_at, uc.deactivated_at, uc.deleted_at ` err = c.Store.QueryRowxContext(ctx, query, - updates.Name, updates.Address, updates.Functions, updates.NetworkId, id, organizationId, updates.PlatformIds).StructScan(&model) + updates.Name, updates.Address, updates.Functions, updates.Type, updates.NetworkId, id, organizationId, updates.PlatformIds).StructScan(&model) if err != nil { return model, libcommon.StringError(err) }