Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create generic criteria to manage company #833

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading