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

fix: contact and partner mutually dependent #836

Merged
merged 3 commits into from
Dec 19, 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/88244.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title": "Opportunity form: make contact and partner fields mutually dependent",
"type": "fix",
"packages": "crm"
}
30 changes: 27 additions & 3 deletions packages/apps/crm/src/api/contact-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ import {
RouterProvider,
} from '@axelor/aos-mobile-core';

const createContactCriteria = (searchValue, userId, assigned) => {
const createContactCriteria = (
searchValue,
userId,
assigned,
mainPartnerId,
) => {
const criteria = [
{
operator: 'and',
Expand Down Expand Up @@ -54,6 +59,14 @@ const createContactCriteria = (searchValue, userId, assigned) => {
getSearchCriterias('crm_contact', searchValue),
];

if (mainPartnerId != null) {
criteria.push({
fieldName: 'mainPartner.id',
operator: '=',
value: mainPartnerId,
});
}

if (userId != null && assigned) {
criteria.push({
fieldName: 'user.id',
Expand Down Expand Up @@ -82,10 +95,21 @@ export async function searchContactWithIds(idList) {
});
}

export async function searchContact({searchValue, page = 0, userId, assigned}) {
export async function searchContact({
searchValue,
page = 0,
userId,
assigned,
mainPartnerId,
}) {
return createStandardSearch({
model: 'com.axelor.apps.base.db.Partner',
criteria: createContactCriteria(searchValue, userId, assigned),
criteria: createContactCriteria(
searchValue,
userId,
assigned,
mainPartnerId,
),
fieldKey: 'crm_contact',
sortKey: 'crm_contact',
page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,20 @@ const ContactSearchBarAux = ({
const {contactList, loadingContact, moreLoading, isListEnd} = useSelector(
(state: any) => state.contact,
);
const {partner} = useSelector((state: any) => state.partner);

const fetchContactSearchBarAPI = useCallback(
({page = 0, searchValue}) => {
onFetchDataAction && onFetchDataAction(searchValue);
dispatch((fetchContact as any)({page, searchValue}));
dispatch(
(fetchContact as any)({
page,
searchValue,
mainPartnerId: partner?.id,
}),
);
},
[dispatch, onFetchDataAction],
[dispatch, onFetchDataAction, partner],
);

return (
Expand Down
1 change: 1 addition & 0 deletions packages/apps/crm/src/features/asyncFunctions-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export {
fetchPartner,
searchLinkedPartnersOfContact,
searchPartner,
updatePartner,
} from './partnerSlice';
export {
fetchProspectById,
Expand Down
7 changes: 7 additions & 0 deletions packages/apps/crm/src/features/partnerSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ const initialState = {
const partnerSlice = createSlice({
name: 'partner',
initialState,
reducers: {
updatePartner: (state, action) => {
state.partner = action.payload;
},
},
extraReducers: builder => {
builder.addCase(fetchPartner.pending, state => {
state.loadingPartner = true;
Expand Down Expand Up @@ -127,4 +132,6 @@ const partnerSlice = createSlice({
},
});

export const {updatePartner} = partnerSlice.actions;

export const partnerReducer = partnerSlice.reducer;
23 changes: 22 additions & 1 deletion packages/apps/crm/src/models/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
OpportunityStatusPicker,
PartnerSearchBar,
} from '../components';
import {updatePartner} from '../features/partnerSlice';

const MODELS = {
lead: 'com.axelor.apps.crm.db.Lead',
Expand Down Expand Up @@ -302,6 +303,16 @@ export const crm_formsRegister: FormConfigs = {
widget: 'custom',
customComponent: ClientProspectSearchBar,
required: true,
dependsOn: {
contact: ({newValue, objectState, dispatch}) => {
if (newValue != null) {
dispatch(updatePartner(newValue?.mainPartner));
return newValue?.mainPartner;
} else {
return objectState?.partner;
}
},
},
},
contact: {
titleKey: 'Crm_Contact',
Expand All @@ -311,7 +322,17 @@ export const crm_formsRegister: FormConfigs = {
options: {
showTitle: true,
},
required: true,
requiredIf: ({objectState}) => objectState.partner == null,
dependsOn: {
partner: ({newValue, objectState, dispatch}) => {
dispatch(updatePartner(newValue));
if (objectState.contact?.mainPartner?.id != newValue?.id) {
return null;
} else {
return objectState.contact;
}
},
},
},
expectedCloseDate: {
titleKey: 'Crm_Opportunity_ExpectedCloseDate',
Expand Down
Loading