From 308ed326ea5e6c23d7db9c280e71f39ce8ec2656 Mon Sep 17 00:00:00 2001
From: vhu-axelor <146069039+vhu-axelor@users.noreply.github.com>
Date: Tue, 10 Dec 2024 18:00:09 +0100
Subject: [PATCH] feat: create generic criteria to manage company
---
changelogs/unreleased/88234.json | 5 ++
packages/core/src/apiProviders/Model/utils.ts | 5 ++
.../apiProviders/Standard/requests.helper.ts | 58 ++++++++++++++++++-
3 files changed, 65 insertions(+), 3 deletions(-)
create mode 100644 changelogs/unreleased/88234.json
diff --git a/changelogs/unreleased/88234.json b/changelogs/unreleased/88234.json
new file mode 100644
index 0000000000..55f7ac0822
--- /dev/null
+++ b/changelogs/unreleased/88234.json
@@ -0,0 +1,5 @@
+{
+ "title": "Company management: add generic tools to create criterias",
+ "type": "feat",
+ "packages": "core"
+}
diff --git a/packages/core/src/apiProviders/Model/utils.ts b/packages/core/src/apiProviders/Model/utils.ts
index 854f96f73d..c5030818f6 100644
--- a/packages/core/src/apiProviders/Model/utils.ts
+++ b/packages/core/src/apiProviders/Model/utils.ts
@@ -68,3 +68,8 @@ export interface RequestResponse {
data: any[];
};
}
+
+export type Domain = {
+ domain: string;
+ domainContext: any;
+};
diff --git a/packages/core/src/apiProviders/Standard/requests.helper.ts b/packages/core/src/apiProviders/Standard/requests.helper.ts
index be1b7d0c52..e09bcbbc2d 100644
--- a/packages/core/src/apiProviders/Standard/requests.helper.ts
+++ b/packages/core/src/apiProviders/Standard/requests.helper.ts
@@ -16,7 +16,7 @@
* along with this program. If not, see .
*/
-import {Criteria, getModelApi} from '../Model';
+import {Criteria, Domain, getModelApi} from '../Model';
import {axiosApiProvider} from './AxiosProvider';
import {getObjectFields, getSortFields} from './ObjectFieldsProvider';
@@ -30,6 +30,10 @@ interface SearchProps {
page: number;
numberElementsByPage?: number;
provider?: 'axios' | 'model';
+ companyId?: number;
+ isCompanyM2M?: boolean;
+ companyFieldName?: string;
+ companySetFieldName?: string;
}
interface FetchProps {
@@ -65,6 +69,10 @@ class RequestBuilder {
page = 0,
numberElementsByPage,
provider = 'axios',
+ companyId,
+ isCompanyM2M = false,
+ companyFieldName,
+ companySetFieldName,
}: SearchProps): Promise => {
if (model == null || fieldKey == null) {
return null;
@@ -75,6 +83,10 @@ class RequestBuilder {
? numberElementsByPage
: this.requestLimit;
+ if (companyId && !isCompanyM2M) {
+ criteria.push(getCompanyCriteria(companyId, companyFieldName));
+ }
+
let data: any = {
criteria: [
{
@@ -82,11 +94,26 @@ class RequestBuilder {
criteria: criteria,
},
],
+ _domain: '',
};
+ if (companyId && isCompanyM2M) {
+ const companyDomain = getCompanyDomain(companyId, companySetFieldName);
+
+ data._domain = companyDomain.domain;
+ data._domainContext = companyDomain.domainContext;
+ }
+
if (domain != null && domain !== '') {
- data._domain = domain;
- data._domainContext = domainContext;
+ if (data._domain !== '') {
+ data._domain += ' AND ';
+ }
+
+ data._domain += domain;
+ data._domainContext = {
+ ...data._domainContext,
+ ...domainContext,
+ };
}
if (provider === 'axios') {
@@ -150,6 +177,31 @@ class RequestBuilder {
export const requestBuilder = new RequestBuilder();
+export const getCompanyCriteria = (
+ companyId: number,
+ companyFieldName: string = 'company',
+): Criteria => {
+ return {
+ fieldName: `${companyFieldName}.id`,
+ operator: '=',
+ value: companyId,
+ };
+};
+
+export const getCompanyDomain = (
+ companyId: number,
+ companySetFieldName: string = 'companySet',
+): Domain => {
+ return {
+ domain: `:company MEMBER OF self.${companySetFieldName}`,
+ domainContext: {
+ company: {
+ id: companyId,
+ },
+ },
+ };
+};
+
export const createStandardSearch = (
searchOptions: SearchProps,
): Promise => {