-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from appuio/feat/sales-order-creation
Reconcile organizations to create sales orders where needed
Showing
11 changed files
with
859 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.uber.org/multierr" | ||
apimeta "k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/tools/record" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
|
||
organizationv1 "github.com/appuio/control-api/apis/organization/v1" | ||
"github.com/appuio/control-api/controllers/saleorder" | ||
) | ||
|
||
// SaleOrderReconciler reconciles invitations and adds a token to the status if required. | ||
type SaleOrderReconciler struct { | ||
client.Client | ||
|
||
Recorder record.EventRecorder | ||
Scheme *runtime.Scheme | ||
|
||
SaleOrderStorage saleorder.SaleOrderStorage | ||
} | ||
|
||
//+kubebuilder:rbac:groups="rbac.appuio.io",resources=organizations,verbs=get;list;watch | ||
//+kubebuilder:rbac:groups="user.appuio.io",resources=organizations,verbs=get;list;watch | ||
//+kubebuilder:rbac:groups="rbac.appuio.io",resources=organizations/status,verbs=get;update;patch | ||
//+kubebuilder:rbac:groups="user.appuio.io",resources=organizations/status,verbs=get;update;patch | ||
|
||
// Reconcile reacts to Organizations and creates Sale Orders if necessary | ||
func (r *SaleOrderReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
log := log.FromContext(ctx) | ||
log.V(1).WithValues("request", req).Info("Reconciling") | ||
|
||
org := organizationv1.Organization{} | ||
if err := r.Get(ctx, req.NamespacedName, &org); err != nil { | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
if org.Spec.BillingEntityRef == "" { | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
if org.Status.SaleOrderName != "" { | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
if org.Status.SaleOrderID != "" { | ||
// ID is present, but Name is not. Update name. | ||
soName, err := r.SaleOrderStorage.GetSaleOrderName(org) | ||
if err != nil { | ||
log.V(0).Error(err, "Error getting sale order name") | ||
apimeta.SetStatusCondition(&org.Status.Conditions, metav1.Condition{ | ||
Type: organizationv1.ConditionSaleOrderNameUpdated, | ||
Status: metav1.ConditionFalse, | ||
Reason: organizationv1.ConditionReasonGetNameFailed, | ||
Message: err.Error(), | ||
}) | ||
return ctrl.Result{}, multierr.Append(err, r.Client.Status().Update(ctx, &org)) | ||
} | ||
apimeta.SetStatusCondition(&org.Status.Conditions, metav1.Condition{ | ||
Type: organizationv1.ConditionSaleOrderNameUpdated, | ||
Status: metav1.ConditionTrue, | ||
}) | ||
org.Status.SaleOrderName = soName | ||
return ctrl.Result{}, r.Client.Status().Update(ctx, &org) | ||
} | ||
|
||
// Neither ID nor Name is present. Create new SO. | ||
soId, err := r.SaleOrderStorage.CreateSaleOrder(org) | ||
|
||
if err != nil { | ||
log.V(0).Error(err, "Error creating sale order") | ||
apimeta.SetStatusCondition(&org.Status.Conditions, metav1.Condition{ | ||
Type: organizationv1.ConditionSaleOrderCreated, | ||
Status: metav1.ConditionFalse, | ||
Reason: organizationv1.ConditionReasonCreateFailed, | ||
Message: err.Error(), | ||
}) | ||
return ctrl.Result{}, multierr.Append(err, r.Client.Status().Update(ctx, &org)) | ||
} | ||
|
||
apimeta.SetStatusCondition(&org.Status.Conditions, metav1.Condition{ | ||
Type: organizationv1.ConditionSaleOrderCreated, | ||
Status: metav1.ConditionTrue, | ||
}) | ||
|
||
org.Status.SaleOrderID = fmt.Sprint(soId) | ||
return ctrl.Result{}, r.Client.Status().Update(ctx, &org) | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *SaleOrderReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&organizationv1.Organization{}). | ||
Complete(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
package controllers_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.uber.org/mock/gomock" | ||
apimeta "k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/tools/record" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
||
organizationv1 "github.com/appuio/control-api/apis/organization/v1" | ||
. "github.com/appuio/control-api/controllers" | ||
"github.com/appuio/control-api/controllers/saleorder/mock_saleorder" | ||
) | ||
|
||
func Test_SaleOrderReconciler_Reconcile_Create_Success(t *testing.T) { | ||
ctx := context.Background() | ||
mctrl := gomock.NewController(t) | ||
mock := mock_saleorder.NewMockSaleOrderStorage(mctrl) | ||
|
||
subject := organizationv1.Organization{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "subject", | ||
}, | ||
Spec: organizationv1.OrganizationSpec{ | ||
BillingEntityRef: "be-0000", | ||
}, | ||
} | ||
c := prepareTest(t, &subject) | ||
|
||
gomock.InOrder( | ||
mock.EXPECT().CreateSaleOrder(gomock.Any()).Return("123", nil), | ||
) | ||
|
||
_, err := (&SaleOrderReconciler{ | ||
Client: c, | ||
Scheme: c.Scheme(), | ||
Recorder: record.NewFakeRecorder(3), | ||
SaleOrderStorage: mock, | ||
}).Reconcile(ctx, ctrl.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: subject.Name, | ||
}, | ||
}) | ||
|
||
require.NoError(t, err) | ||
require.NoError(t, c.Get(ctx, types.NamespacedName{Name: subject.Name}, &subject)) | ||
require.Equal(t, "123", subject.Status.SaleOrderID) | ||
cond := apimeta.FindStatusCondition(subject.Status.Conditions, organizationv1.ConditionSaleOrderCreated) | ||
require.Equal(t, metav1.ConditionTrue, cond.Status) | ||
} | ||
|
||
func Test_SaleOrderReconciler_Reconcile_UpdateName_Success(t *testing.T) { | ||
ctx := context.Background() | ||
mctrl := gomock.NewController(t) | ||
mock := mock_saleorder.NewMockSaleOrderStorage(mctrl) | ||
|
||
subject := organizationv1.Organization{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "subject", | ||
}, | ||
Spec: organizationv1.OrganizationSpec{ | ||
BillingEntityRef: "be-0000", | ||
}, | ||
Status: organizationv1.OrganizationStatus{ | ||
SaleOrderID: "123", | ||
}, | ||
} | ||
c := prepareTest(t, &subject) | ||
|
||
gomock.InOrder( | ||
mock.EXPECT().GetSaleOrderName(gomock.Any()).Return("SO123", nil), | ||
) | ||
|
||
_, err := (&SaleOrderReconciler{ | ||
Client: c, | ||
Scheme: c.Scheme(), | ||
Recorder: record.NewFakeRecorder(3), | ||
SaleOrderStorage: mock, | ||
}).Reconcile(ctx, ctrl.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: subject.Name, | ||
}, | ||
}) | ||
|
||
require.NoError(t, err) | ||
require.NoError(t, c.Get(ctx, types.NamespacedName{Name: subject.Name}, &subject)) | ||
require.Equal(t, "123", subject.Status.SaleOrderID) | ||
require.Equal(t, "SO123", subject.Status.SaleOrderName) | ||
cond := apimeta.FindStatusCondition(subject.Status.Conditions, organizationv1.ConditionSaleOrderNameUpdated) | ||
require.Equal(t, metav1.ConditionTrue, cond.Status) | ||
} | ||
|
||
func Test_SaleOrderReconciler_Reconcile_NoAction_Success(t *testing.T) { | ||
ctx := context.Background() | ||
mctrl := gomock.NewController(t) | ||
mock := mock_saleorder.NewMockSaleOrderStorage(mctrl) | ||
|
||
subject := organizationv1.Organization{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "subject", | ||
}, | ||
Spec: organizationv1.OrganizationSpec{ | ||
BillingEntityRef: "be-0000", | ||
}, | ||
Status: organizationv1.OrganizationStatus{ | ||
SaleOrderID: "123", | ||
SaleOrderName: "SO123", | ||
}, | ||
} | ||
c := prepareTest(t, &subject) | ||
|
||
mock.EXPECT().CreateSaleOrder(gomock.Any()).Times(0) | ||
mock.EXPECT().GetSaleOrderName(gomock.Any()).Times(0) | ||
|
||
_, err := (&SaleOrderReconciler{ | ||
Client: c, | ||
Scheme: c.Scheme(), | ||
Recorder: record.NewFakeRecorder(3), | ||
SaleOrderStorage: mock, | ||
}).Reconcile(ctx, ctrl.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: subject.Name, | ||
}, | ||
}) | ||
|
||
require.NoError(t, err) | ||
require.NoError(t, c.Get(ctx, types.NamespacedName{Name: subject.Name}, &subject)) | ||
} | ||
|
||
func Test_SaleOrderReconciler_Reconcile_NoBillingEntity_Success(t *testing.T) { | ||
ctx := context.Background() | ||
mctrl := gomock.NewController(t) | ||
mock := mock_saleorder.NewMockSaleOrderStorage(mctrl) | ||
|
||
subject := organizationv1.Organization{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "subject", | ||
}, | ||
} | ||
c := prepareTest(t, &subject) | ||
|
||
mock.EXPECT().CreateSaleOrder(gomock.Any()).Times(0) | ||
mock.EXPECT().GetSaleOrderName(gomock.Any()).Times(0) | ||
|
||
_, err := (&SaleOrderReconciler{ | ||
Client: c, | ||
Scheme: c.Scheme(), | ||
Recorder: record.NewFakeRecorder(3), | ||
SaleOrderStorage: mock, | ||
}).Reconcile(ctx, ctrl.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: subject.Name, | ||
}, | ||
}) | ||
|
||
require.NoError(t, err) | ||
require.NoError(t, c.Get(ctx, types.NamespacedName{Name: subject.Name}, &subject)) | ||
} | ||
|
||
func Test_SaleOrderReconciler_Create_Error(t *testing.T) { | ||
ctx := context.Background() | ||
mctrl := gomock.NewController(t) | ||
mock := mock_saleorder.NewMockSaleOrderStorage(mctrl) | ||
|
||
subject := organizationv1.Organization{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "subject", | ||
}, | ||
Spec: organizationv1.OrganizationSpec{ | ||
BillingEntityRef: "be-0000", | ||
}, | ||
} | ||
c := prepareTest(t, &subject) | ||
|
||
gomock.InOrder( | ||
mock.EXPECT().CreateSaleOrder(gomock.Any()).Return("", errors.New("An unanticipated fault has come to pass.")), | ||
) | ||
|
||
_, err := (&SaleOrderReconciler{ | ||
Client: c, | ||
Scheme: c.Scheme(), | ||
Recorder: record.NewFakeRecorder(3), | ||
SaleOrderStorage: mock, | ||
}).Reconcile(ctx, ctrl.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: subject.Name, | ||
}, | ||
}) | ||
|
||
require.Error(t, err) | ||
require.NoError(t, c.Get(ctx, types.NamespacedName{Name: subject.Name}, &subject)) | ||
cond := apimeta.FindStatusCondition(subject.Status.Conditions, organizationv1.ConditionSaleOrderCreated) | ||
require.Equal(t, metav1.ConditionFalse, cond.Status) | ||
require.Equal(t, organizationv1.ConditionReasonCreateFailed, cond.Reason) | ||
require.Equal(t, "An unanticipated fault has come to pass.", cond.Message) | ||
} | ||
|
||
func Test_SaleOrderReconciler_UpdateName_Error(t *testing.T) { | ||
ctx := context.Background() | ||
mctrl := gomock.NewController(t) | ||
mock := mock_saleorder.NewMockSaleOrderStorage(mctrl) | ||
|
||
subject := organizationv1.Organization{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "subject", | ||
}, | ||
Spec: organizationv1.OrganizationSpec{ | ||
BillingEntityRef: "be-0000", | ||
}, | ||
Status: organizationv1.OrganizationStatus{ | ||
SaleOrderID: "123", | ||
}, | ||
} | ||
c := prepareTest(t, &subject) | ||
|
||
gomock.InOrder( | ||
mock.EXPECT().GetSaleOrderName(gomock.Any()).Return("", errors.New("An unanticipated fault has come to pass.")), | ||
) | ||
|
||
_, err := (&SaleOrderReconciler{ | ||
Client: c, | ||
Scheme: c.Scheme(), | ||
Recorder: record.NewFakeRecorder(3), | ||
SaleOrderStorage: mock, | ||
}).Reconcile(ctx, ctrl.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: subject.Name, | ||
}, | ||
}) | ||
|
||
require.Error(t, err) | ||
require.NoError(t, c.Get(ctx, types.NamespacedName{Name: subject.Name}, &subject)) | ||
cond := apimeta.FindStatusCondition(subject.Status.Conditions, organizationv1.ConditionSaleOrderNameUpdated) | ||
require.Equal(t, metav1.ConditionFalse, cond.Status) | ||
require.Equal(t, organizationv1.ConditionReasonGetNameFailed, cond.Reason) | ||
require.Equal(t, "An unanticipated fault has come to pass.", cond.Message) | ||
} |
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package saleorder | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
organizationv1 "github.com/appuio/control-api/apis/organization/v1" | ||
odooclient "github.com/appuio/go-odoo" | ||
) | ||
|
||
type Odoo16Credentials = odooclient.ClientConfig | ||
|
||
type Odoo16Options struct { | ||
SaleOrderClientReferencePrefix string | ||
SaleOrderInternalNote string | ||
} | ||
|
||
const defaultSaleOrderState = "sale" | ||
|
||
type SaleOrderStorage interface { | ||
CreateSaleOrder(organizationv1.Organization) (string, error) | ||
GetSaleOrderName(organizationv1.Organization) (string, error) | ||
} | ||
|
||
type Odoo16Client interface { | ||
Read(string, []int64, *odooclient.Options, interface{}) error | ||
CreateSaleOrder(*odooclient.SaleOrder) (int64, error) | ||
} | ||
|
||
type Odoo16SaleOrderStorage struct { | ||
client Odoo16Client | ||
options *Odoo16Options | ||
} | ||
|
||
func NewOdoo16Storage(credentials *Odoo16Credentials, options *Odoo16Options) (SaleOrderStorage, error) { | ||
client, err := odooclient.NewClient(credentials) | ||
return &Odoo16SaleOrderStorage{ | ||
client: client, | ||
options: options, | ||
}, err | ||
} | ||
|
||
func NewOdoo16StorageFromClient(client Odoo16Client, options *Odoo16Options) SaleOrderStorage { | ||
return &Odoo16SaleOrderStorage{ | ||
client: client, | ||
options: options, | ||
} | ||
} | ||
|
||
func (s *Odoo16SaleOrderStorage) CreateSaleOrder(org organizationv1.Organization) (string, error) { | ||
beID, err := k8sIDToOdooID(org.Spec.BillingEntityRef) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
fetchPartnerFieldOpts := odooclient.NewOptions().FetchFields( | ||
"id", | ||
"parent_id", | ||
) | ||
|
||
beRecords := []odooclient.ResPartner{} | ||
err = s.client.Read(odooclient.ResPartnerModel, []int64{int64(beID)}, fetchPartnerFieldOpts, &beRecords) | ||
if err != nil { | ||
return "", fmt.Errorf("fetching accounting contact by ID: %w", err) | ||
} | ||
|
||
if len(beRecords) <= 0 { | ||
return "", fmt.Errorf("no results when fetching accounting contact by ID") | ||
} | ||
beRecord := beRecords[0] | ||
|
||
if beRecord.ParentId == nil { | ||
return "", fmt.Errorf("accounting contact %d has no parent", beRecord.Id.Get()) | ||
} | ||
|
||
var clientRef string | ||
if org.Spec.DisplayName != "" { | ||
clientRef = fmt.Sprintf("%s (%s)", s.options.SaleOrderClientReferencePrefix, org.Spec.DisplayName) | ||
} else { | ||
clientRef = fmt.Sprintf("%s (%s)", s.options.SaleOrderClientReferencePrefix, org.ObjectMeta.Name) | ||
} | ||
|
||
newSaleOrder := odooclient.SaleOrder{ | ||
PartnerInvoiceId: odooclient.NewMany2One(beRecord.Id.Get(), ""), | ||
PartnerId: odooclient.NewMany2One(beRecord.ParentId.ID, ""), | ||
State: odooclient.NewSelection(defaultSaleOrderState), | ||
ClientOrderRef: odooclient.NewString(clientRef), | ||
InternalNote: odooclient.NewString(s.options.SaleOrderInternalNote), | ||
} | ||
|
||
soID, err := s.client.CreateSaleOrder(&newSaleOrder) | ||
if err != nil { | ||
return "", fmt.Errorf("creating new sale order: %w", err) | ||
} | ||
|
||
return fmt.Sprint(soID), nil | ||
} | ||
|
||
func (s *Odoo16SaleOrderStorage) GetSaleOrderName(org organizationv1.Organization) (string, error) { | ||
fetchOrderFieldOpts := odooclient.NewOptions().FetchFields( | ||
"id", | ||
"name", | ||
) | ||
id, err := strconv.Atoi(org.Status.SaleOrderID) | ||
if err != nil { | ||
return "", fmt.Errorf("error parsing saleOrderID %q from organization status: %w", org.Status.SaleOrderID, err) | ||
} | ||
soRecords := []odooclient.SaleOrder{} | ||
err = s.client.Read(odooclient.SaleOrderModel, []int64{int64(id)}, fetchOrderFieldOpts, &soRecords) | ||
if err != nil { | ||
return "", fmt.Errorf("fetching sale order by ID: %w", err) | ||
} | ||
|
||
if len(soRecords) <= 0 { | ||
return "", fmt.Errorf("no results when fetching sale orders with ID %q", id) | ||
} | ||
|
||
return soRecords[0].Name.Get(), nil | ||
|
||
} | ||
|
||
func k8sIDToOdooID(id string) (int, error) { | ||
if !strings.HasPrefix(id, "be-") { | ||
return 0, fmt.Errorf("invalid ID, missing prefix: %s", id) | ||
} | ||
|
||
return strconv.Atoi(id[3:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package saleorder_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/mock/gomock" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
organizationv1 "github.com/appuio/control-api/apis/organization/v1" | ||
"github.com/appuio/control-api/controllers/saleorder" | ||
"github.com/appuio/control-api/controllers/saleorder/mock_saleorder" | ||
odooclient "github.com/appuio/go-odoo" | ||
) | ||
|
||
func TestCreate(t *testing.T) { | ||
ctrl, mock, subject := createStorage(t) | ||
defer ctrl.Finish() | ||
|
||
tn := time.Now() | ||
st, _ := time.Parse(time.RFC3339, "2023-04-18T14:07:55Z") | ||
statusTime := st.Local() | ||
|
||
gomock.InOrder( | ||
mock.EXPECT().Read(gomock.Any(), []int64{int64(123)}, gomock.Any(), gomock.Any()).SetArg(3, []odooclient.ResPartner{{ | ||
Id: odooclient.NewInt(456), | ||
CreateDate: odooclient.NewTime(tn), | ||
ParentId: odooclient.NewMany2One(123, ""), | ||
Email: odooclient.NewString("accounting@test.com, notifications@test.com"), | ||
VshnControlApiMetaStatus: odooclient.NewString("{\"conditions\":[{\"type\":\"ConditionFoo\",\"status\":\"False\",\"lastTransitionTime\":\"" + statusTime.Format(time.RFC3339) + "\",\"reason\":\"Whatever\",\"message\":\"Hello World\"}]}"), | ||
}}).Return(nil), | ||
mock.EXPECT().CreateSaleOrder(gomock.Any()).Return(int64(149), nil), | ||
) | ||
|
||
soid, err := subject.CreateSaleOrder(organizationv1.Organization{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "myorg", | ||
}, | ||
Spec: organizationv1.OrganizationSpec{ | ||
BillingEntityRef: "be-123", | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
assert.Equal(t, "149", soid) | ||
} | ||
|
||
func TestGet(t *testing.T) { | ||
ctrl, mock, subject := createStorage(t) | ||
defer ctrl.Finish() | ||
|
||
gomock.InOrder( | ||
mock.EXPECT().Read(gomock.Any(), []int64{int64(149)}, gomock.Any(), gomock.Any()).SetArg(3, []odooclient.SaleOrder{{ | ||
Id: odooclient.NewInt(456), | ||
Name: odooclient.NewString("SO149"), | ||
}}).Return(nil), | ||
) | ||
|
||
soid, err := subject.GetSaleOrderName(organizationv1.Organization{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "myorg", | ||
}, | ||
Spec: organizationv1.OrganizationSpec{ | ||
BillingEntityRef: "be-123", | ||
}, | ||
Status: organizationv1.OrganizationStatus{ | ||
SaleOrderID: "149", | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
assert.Equal(t, "SO149", soid) | ||
} | ||
|
||
func TestCreateAttributes(t *testing.T) { | ||
ctrl, mock, subject := createStorage(t) | ||
defer ctrl.Finish() | ||
|
||
tn := time.Now() | ||
st, _ := time.Parse(time.RFC3339, "2023-04-18T14:07:55Z") | ||
statusTime := st.Local() | ||
|
||
gomock.InOrder( | ||
mock.EXPECT().Read(gomock.Any(), []int64{int64(123)}, gomock.Any(), gomock.Any()).SetArg(3, []odooclient.ResPartner{{ | ||
Id: odooclient.NewInt(456), | ||
CreateDate: odooclient.NewTime(tn), | ||
ParentId: odooclient.NewMany2One(123, ""), | ||
Email: odooclient.NewString("accounting@test.com, notifications@test.com"), | ||
VshnControlApiMetaStatus: odooclient.NewString("{\"conditions\":[{\"type\":\"ConditionFoo\",\"status\":\"False\",\"lastTransitionTime\":\"" + statusTime.Format(time.RFC3339) + "\",\"reason\":\"Whatever\",\"message\":\"Hello World\"}]}"), | ||
}}).Return(nil), | ||
mock.EXPECT().CreateSaleOrder(SaleOrderMatcher{ | ||
PartnerId: int64(123), | ||
PartnerInvoiceId: int64(456), | ||
State: "sale", | ||
ClientOrderRef: "client-ref (myorg)", | ||
InternalNote: "internal-note", | ||
}).Return(int64(149), nil), | ||
) | ||
|
||
soid, err := subject.CreateSaleOrder(organizationv1.Organization{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "myorg", | ||
}, | ||
Spec: organizationv1.OrganizationSpec{ | ||
BillingEntityRef: "be-123", | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
assert.Equal(t, "149", soid) | ||
} | ||
|
||
type SaleOrderMatcher struct { | ||
PartnerId int64 | ||
PartnerInvoiceId int64 | ||
State string | ||
ClientOrderRef string | ||
InternalNote string | ||
} | ||
|
||
func (s SaleOrderMatcher) Matches(x interface{}) bool { | ||
so := x.(*odooclient.SaleOrder) | ||
return so.PartnerId.ID == s.PartnerId && so.PartnerInvoiceId.ID == s.PartnerInvoiceId && so.State.Get() == s.State && so.ClientOrderRef.Get() == s.ClientOrderRef && so.InternalNote.Get() == s.InternalNote | ||
} | ||
func (s SaleOrderMatcher) String() string { | ||
return fmt.Sprintf("{PartnerId:%d PartnerInvoiceId:%d State:%s ClientOrderRef:%s InternalNote:%s}", s.PartnerId, s.PartnerInvoiceId, s.State, s.ClientOrderRef, s.InternalNote) | ||
} | ||
|
||
func createStorage(t *testing.T) (*gomock.Controller, *mock_saleorder.MockOdoo16Client, saleorder.SaleOrderStorage) { | ||
ctrl := gomock.NewController(t) | ||
mock := mock_saleorder.NewMockOdoo16Client(ctrl) | ||
|
||
return ctrl, mock, saleorder.NewOdoo16StorageFromClient( | ||
mock, | ||
&saleorder.Odoo16Options{ | ||
SaleOrderClientReferencePrefix: "client-ref", | ||
SaleOrderInternalNote: "internal-note", | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters