Skip to content

Commit

Permalink
feat: create generic criteria to manage company (#833)
Browse files Browse the repository at this point in the history
* RM#88234
  • Loading branch information
vhu-axelor authored Dec 12, 2024
1 parent 5304ff6 commit f8d2d5b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
5 changes: 5 additions & 0 deletions changelogs/unreleased/88234.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title": "Company management: add generic tools to create criterias",
"type": "feat",
"packages": "core"
}
5 changes: 5 additions & 0 deletions packages/core/src/apiProviders/Model/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@ export interface RequestResponse {
data: any[];
};
}

export type Domain = {
domain: string;
domainContext: any;
};
58 changes: 55 additions & 3 deletions packages/core/src/apiProviders/Standard/requests.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import {Criteria, getModelApi} from '../Model';
import {Criteria, Domain, getModelApi} from '../Model';
import {axiosApiProvider} from './AxiosProvider';
import {getObjectFields, getSortFields} from './ObjectFieldsProvider';

Expand All @@ -30,6 +30,10 @@ interface SearchProps {
page: number;
numberElementsByPage?: number;
provider?: 'axios' | 'model';
companyId?: number;
isCompanyM2M?: boolean;
companyFieldName?: string;
companySetFieldName?: string;
}

interface FetchProps {
Expand Down Expand Up @@ -65,6 +69,10 @@ class RequestBuilder {
page = 0,
numberElementsByPage,
provider = 'axios',
companyId,
isCompanyM2M = false,
companyFieldName,
companySetFieldName,
}: SearchProps): Promise<any> => {
if (model == null || fieldKey == null) {
return null;
Expand All @@ -75,18 +83,37 @@ class RequestBuilder {
? numberElementsByPage
: this.requestLimit;

if (companyId && !isCompanyM2M) {
criteria.push(getCompanyCriteria(companyId, companyFieldName));
}

let data: any = {
criteria: [
{
operator: 'and',
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') {
Expand Down Expand Up @@ -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<any> => {
Expand Down

0 comments on commit f8d2d5b

Please sign in to comment.