Skip to content

Commit

Permalink
refactor: manage company in data fetching in sale
Browse files Browse the repository at this point in the history
  • Loading branch information
gca-axelor committed Dec 19, 2024
1 parent b2ca802 commit 4e839da
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 33 deletions.
5 changes: 5 additions & 0 deletions changelogs/unreleased/88462.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"title": "Data fetching: add active company management",
"type": "refactor",
"packages": "sale"
}
3 changes: 2 additions & 1 deletion packages/apps/sale/src/api/cart-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ const createCartCriteria = (searchValue, userId) => {
return criteria;
};

export async function searchCart({searchValue, page = 0, userId}) {
export async function searchCart({searchValue, page = 0, userId, companyId}) {
return createStandardSearch({
model: 'com.axelor.apps.sale.db.Cart',
criteria: createCartCriteria(searchValue, userId),
fieldKey: 'sale_cart',
sortKey: 'sale_cart',
page: page,
provider: 'model',
companyId,
});
}

Expand Down
10 changes: 2 additions & 8 deletions packages/apps/sale/src/api/customer-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,8 @@ export async function searchCustomer({
userId,
categoryId,
),
...(companyId != null && {
domain: ':company MEMBER OF self.companySet',
domainContext: {
company: {
id: companyId,
},
},
}),
companyId,
isCompanyM2M: true,
fieldKey: 'sale_customer',
sortKey: 'sale_customer',
page,
Expand Down
3 changes: 2 additions & 1 deletion packages/apps/sale/src/api/customer-delivery-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import {createStandardSearch} from '@axelor/aos-mobile-core';

export async function fetchCustomerDelivery({saleOrderId}) {
export async function fetchCustomerDelivery({saleOrderId, companyId}) {
return createStandardSearch({
model: 'com.axelor.apps.stock.db.StockMove',
domain: ':saleOrder MEMBER OF self.saleOrderSet',
Expand All @@ -30,5 +30,6 @@ export async function fetchCustomerDelivery({saleOrderId}) {
numberElementsByPage: 1,
page: 0,
provider: 'model',
companyId,
});
}
10 changes: 4 additions & 6 deletions packages/apps/sale/src/api/product-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,8 @@ const createProductCategoryCriteria = searchValue => {
return [getSearchCriterias('sale_productCategory', searchValue)];
};

const createProductCompanyCriteria = (companyId, productId) => {
return [
{fieldName: 'company.id', operator: '=', value: companyId},
{fieldName: 'product.id', operator: '=', value: productId},
];
const createProductCompanyCriteria = productId => {
return [{fieldName: 'product.id', operator: '=', value: productId}];
};

const createVariantProductCriteria = (searchValue, parentProductId) => {
Expand Down Expand Up @@ -194,11 +191,12 @@ export async function fetchProductCompanyConfig({companyId, productId}) {

return createStandardSearch({
model: 'com.axelor.apps.base.db.ProductCompany',
criteria: createProductCompanyCriteria(companyId, productId),
criteria: createProductCompanyCriteria(productId),
fieldKey: 'sale_productCompany',
page: 0,
numberElementsByPage: 1,
provider: 'model',
companyId,
});
}

Expand Down
13 changes: 2 additions & 11 deletions packages/apps/sale/src/api/sale-config-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,14 @@

import {createStandardSearch} from '@axelor/aos-mobile-core';

const createSaleConfigCriteria = companyId => {
return [
{
fieldName: 'company.id',
operator: '=',
value: companyId,
},
];
};

export async function fetchSaleConfig({companyId}) {
return createStandardSearch({
model: 'com.axelor.apps.sale.db.SaleConfig',
criteria: createSaleConfigCriteria(companyId),
criteria: [],
fieldKey: 'sale_saleConfig',
sortKey: 'sale_saleConfig',
page: 0,
provider: 'model',
companyId,
});
}
2 changes: 2 additions & 0 deletions packages/apps/sale/src/api/sale-order-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export async function fetchSaleOrder({
statusList,
customerId,
page = 0,
companyId,
}) {
return createStandardSearch({
model: 'com.axelor.apps.sale.db.SaleOrder',
Expand All @@ -62,6 +63,7 @@ export async function fetchSaleOrder({
sortKey: 'sale_saleOrder',
page,
provider: 'model',
companyId,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const SaleOrderBottomButton = ({saleOrder}: SaleOrderBottomButtonProps) => {
const dispatch = useDispatch();
const {checkModule} = useModules();

const {user} = useSelector(state => state.user);
const {customerDelivery} = useSelector(
(state: any) => state.sale_customerDelivery,
);
Expand All @@ -59,8 +60,13 @@ const SaleOrderBottomButton = ({saleOrder}: SaleOrderBottomButtonProps) => {
);

useEffect(() => {
dispatch((fetchCustomerDelivery as any)({saleOrderId: saleOrder.id}));
}, [dispatch, saleOrder]);
dispatch(
(fetchCustomerDelivery as any)({
saleOrderId: saleOrder.id,
companyId: user.activeCompany?.id,
}),
);
}, [dispatch, saleOrder, user.activeCompany?.id]);

if (saleOrder.statusSelect === SaleOrder?.statusSelect.Finalized) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const SaleOrderListView = ({
const {SaleOrder} = useTypes();
const {getSelectionItems} = useTypeHelpers();

const {user} = useSelector(state => state.user);
const {loading, moreLoading, isListEnd, saleOrderList} = useSelector(
(state: any) => state.sale_saleOrder,
);
Expand Down Expand Up @@ -73,8 +74,9 @@ const SaleOrderListView = ({
() => ({
statusList: statusList,
customerId: customer?.id,
companyId: user.activeCompany?.id,
}),
[customer?.id, statusList],
[customer?.id, statusList, user.activeCompany?.id],
);

return (
Expand Down
10 changes: 8 additions & 2 deletions packages/apps/sale/src/screens/cart/ActiveCartScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const ActiveCartScreen = ({}) => {
const isFocused = useIsFocused();

const {userId} = useSelector((state: any) => state.auth);
const {user} = useSelector(state => state.user);
const {mobileSettings} = useSelector((state: any) => state.appConfig);
const {activeCart} = useSelector((state: any) => state.sale_cart);
const {loading, moreLoading, isListEnd, carLineList} = useSelector(
Expand All @@ -64,9 +65,14 @@ const ActiveCartScreen = ({}) => {

useEffect(() => {
if (isFocused) {
dispatch((fetchActiveCart as any)({userId}));
dispatch(
(fetchActiveCart as any)({
userId: userId,
companyId: user.activeCompany?.id,
}),
);
}
}, [dispatch, isFocused, userId]);
}, [dispatch, isFocused, userId, user.activeCompany?.id]);

const handleCartValidation = useCallback(
(cart: any) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/apiProviders/Standard/requests.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class RequestBuilder {
}

if (domain != null && domain !== '') {
if (data._domain !== null) {
if (data._domain != null) {
data._domain += ` AND ${domain}`;
data._domainContext = {
...data._domainContext,
Expand Down

0 comments on commit 4e839da

Please sign in to comment.