feat(purchase
This commit is contained in:
@@ -193,6 +193,21 @@ func validatePurchaseTeamSelection(scope *string, categoryID *uuid.UUID, allowCl
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
// validatePurchaseTeamFilter accepts the values the team picker hands back: Pusat,
|
||||
// no team at all, or the id of the parent category a purchase is charged to.
|
||||
func validatePurchaseTeamFilter(team string) (error, string) {
|
||||
switch team {
|
||||
case constants.PurchaseTeamScopeCentral, constants.PurchaseTeamNone:
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
if categoryID, err := uuid.Parse(team); err != nil || categoryID == uuid.Nil {
|
||||
return errors.New("team must be one of: central, none, or a category id"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func (v *PurchaseOrderValidatorImpl) ValidateListPurchaseOrdersRequest(req *contract.ListPurchaseOrdersRequest) (error, string) {
|
||||
if req == nil {
|
||||
return errors.New("request body is required"), constants.MissingFieldErrorCode
|
||||
@@ -213,6 +228,16 @@ func (v *PurchaseOrderValidatorImpl) ValidateListPurchaseOrdersRequest(req *cont
|
||||
}
|
||||
}
|
||||
|
||||
if req.Team != "" {
|
||||
if req.TeamScope != "" || req.TeamCategoryID != nil {
|
||||
return errors.New("team cannot be combined with team_scope or team_category_id"), constants.MalformedFieldErrorCode
|
||||
}
|
||||
|
||||
if err, code := validatePurchaseTeamFilter(req.Team); err != nil {
|
||||
return err, code
|
||||
}
|
||||
}
|
||||
|
||||
if req.TeamScope != "" {
|
||||
validScopes := []string{constants.PurchaseTeamScopeCategory, constants.PurchaseTeamScopeCentral}
|
||||
if !contains(validScopes, req.TeamScope) {
|
||||
|
||||
@@ -195,6 +195,48 @@ func TestPurchaseOrderValidatorUpdateRejectsClearingTeamWithCategory(t *testing.
|
||||
require.Equal(t, constants.MalformedFieldErrorCode, code)
|
||||
}
|
||||
|
||||
func TestPurchaseOrderValidatorListAcceptsTeamFilter(t *testing.T) {
|
||||
validator := NewPurchaseOrderValidator()
|
||||
|
||||
for _, team := range []string{constants.PurchaseTeamScopeCentral, constants.PurchaseTeamNone, uuid.New().String()} {
|
||||
err, code := validator.ValidateListPurchaseOrdersRequest(&contract.ListPurchaseOrdersRequest{
|
||||
Page: 1,
|
||||
Limit: 10,
|
||||
Team: team,
|
||||
})
|
||||
|
||||
require.NoError(t, err, team)
|
||||
require.Empty(t, code, team)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPurchaseOrderValidatorListRejectsUnknownTeamFilter(t *testing.T) {
|
||||
validator := NewPurchaseOrderValidator()
|
||||
|
||||
err, code := validator.ValidateListPurchaseOrdersRequest(&contract.ListPurchaseOrdersRequest{
|
||||
Page: 1,
|
||||
Limit: 10,
|
||||
Team: "marketing",
|
||||
})
|
||||
|
||||
require.Error(t, err)
|
||||
require.Equal(t, constants.MalformedFieldErrorCode, code)
|
||||
}
|
||||
|
||||
func TestPurchaseOrderValidatorListRejectsTeamWithScope(t *testing.T) {
|
||||
validator := NewPurchaseOrderValidator()
|
||||
|
||||
err, code := validator.ValidateListPurchaseOrdersRequest(&contract.ListPurchaseOrdersRequest{
|
||||
Page: 1,
|
||||
Limit: 10,
|
||||
Team: constants.PurchaseTeamNone,
|
||||
TeamScope: constants.PurchaseTeamScopeCentral,
|
||||
})
|
||||
|
||||
require.Error(t, err)
|
||||
require.Equal(t, constants.MalformedFieldErrorCode, code)
|
||||
}
|
||||
|
||||
func TestPurchaseOrderValidatorListRejectsCentralScopeWithCategory(t *testing.T) {
|
||||
validator := NewPurchaseOrderValidator()
|
||||
categoryID := uuid.New()
|
||||
|
||||
Reference in New Issue
Block a user