From a41e5a93e5592e23729887ada418330e9f6299a8 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 20 Sep 2022 15:30:44 +0200 Subject: [PATCH] (feat) products filtering for admin view --- .rubocop.yml | 2 +- app/controllers/api/products_controller.rb | 1 - app/frontend/src/javascript/api/product.ts | 3 +- .../store/filters/active-filters-tags.tsx | 45 +++++ .../store/filters/categories-filter.tsx | 43 +---- .../store/filters/keyword-filter.tsx | 12 +- .../store/filters/machines-filter.tsx | 7 +- .../components/store/filters/stock-filter.tsx | 16 +- .../javascript/components/store/products.tsx | 150 ++++++++-------- app/frontend/src/javascript/lib/product.ts | 56 +++++- app/frontend/src/javascript/models/api.ts | 11 +- app/frontend/src/javascript/models/order.ts | 11 +- app/frontend/src/javascript/models/product.ts | 24 ++- app/models/machine.rb | 15 +- app/models/machines_product.rb | 7 + app/models/product.rb | 3 +- app/models/projects_machine.rb | 7 + app/models/trainings_machine.rb | 7 + app/services/checkout/payment_service.rb | 10 +- app/services/orders/order_service.rb | 161 +++++++++++------- app/services/product_service.rb | 63 +++++-- app/views/api/orders/index.json.jbuilder | 5 +- app/views/api/products/index.json.jbuilder | 4 +- config/locales/app.shared.en.yml | 3 + ...0220920131912_add_index_on_product_slug.rb | 8 + db/schema.rb | 23 +-- 26 files changed, 425 insertions(+), 272 deletions(-) create mode 100644 app/frontend/src/javascript/components/store/filters/active-filters-tags.tsx create mode 100644 app/models/machines_product.rb create mode 100644 app/models/projects_machine.rb create mode 100644 app/models/trainings_machine.rb create mode 100644 db/migrate/20220920131912_add_index_on_product_slug.rb diff --git a/.rubocop.yml b/.rubocop.yml index f423c323f..987794c3d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -9,7 +9,7 @@ Metrics/MethodLength: Metrics/CyclomaticComplexity: Max: 13 Metrics/PerceivedComplexity: - Max: 11 + Max: 12 Metrics/AbcSize: Max: 45 Metrics/ClassLength: diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index a77d06e67..8068c6f82 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -10,7 +10,6 @@ class API::ProductsController < API::ApiController def index @products = ProductService.list(params) - @pages = ProductService.pages(params) if params[:page].present? end def show diff --git a/app/frontend/src/javascript/api/product.ts b/app/frontend/src/javascript/api/product.ts index d12167b45..f760e1a29 100644 --- a/app/frontend/src/javascript/api/product.ts +++ b/app/frontend/src/javascript/api/product.ts @@ -3,10 +3,11 @@ import { AxiosResponse } from 'axios'; import { serialize } from 'object-to-formdata'; import { Product, ProductIndexFilter, ProductsIndex, ProductStockMovement } from '../models/product'; import ApiLib from '../lib/api'; +import ProductLib from '../lib/product'; export default class ProductAPI { static async index (filters?: ProductIndexFilter): Promise { - const res: AxiosResponse = await apiClient.get(`/api/products${ApiLib.filtersToQuery(filters)}`); + const res: AxiosResponse = await apiClient.get(`/api/products${ApiLib.filtersToQuery(ProductLib.indexFiltersToIds(filters))}`); return res?.data; } diff --git a/app/frontend/src/javascript/components/store/filters/active-filters-tags.tsx b/app/frontend/src/javascript/components/store/filters/active-filters-tags.tsx new file mode 100644 index 000000000..702ad3203 --- /dev/null +++ b/app/frontend/src/javascript/components/store/filters/active-filters-tags.tsx @@ -0,0 +1,45 @@ +import React from 'react'; +import { ProductIndexFilter } from '../../../models/product'; +import { X } from 'phosphor-react'; +import { ProductCategory } from '../../../models/product-category'; +import { Machine } from '../../../models/machine'; +import { useTranslation } from 'react-i18next'; + +interface ActiveFiltersTagsProps { + filters: ProductIndexFilter, + onRemoveCategory: (category: ProductCategory) => void, + onRemoveMachine: (machine: Machine) => void, + onRemoveKeyword: () => void, + onRemoveStock: () => void, +} + +/** + * Some tags listing the currently actives filters for a product list + */ +export const ActiveFiltersTags: React.FC = ({ filters, onRemoveCategory, onRemoveMachine, onRemoveKeyword, onRemoveStock }) => { + const { t } = useTranslation('shared'); + return ( + <> + {filters.categories.map(c => ( +
+

{c.name}

+ +
+ ))} + {filters.machines.map(m => ( +
+

{m.name}

+ +
+ ))} + {filters.keywords[0] &&
+

{filters.keywords[0]}

+ +
} + {(filters.stock_to !== 0 || filters.stock_from !== 0) &&
+

{t(`app.shared.active_filters_tags.stock_${filters.stock_type}`)} [{filters.stock_from || '…'} ⟶ {filters.stock_to || '…'}]

+ +
} + + ); +}; diff --git a/app/frontend/src/javascript/components/store/filters/categories-filter.tsx b/app/frontend/src/javascript/components/store/filters/categories-filter.tsx index a72017e35..967ae51c0 100644 --- a/app/frontend/src/javascript/components/store/filters/categories-filter.tsx +++ b/app/frontend/src/javascript/components/store/filters/categories-filter.tsx @@ -1,6 +1,5 @@ import React, { useEffect, useState } from 'react'; import _ from 'lodash'; -import ProductCategoryAPI from '../../../api/product-category'; import ProductLib from '../../../lib/product'; import { ProductCategory } from '../../../models/product-category'; import { FabButton } from '../../base/fab-button'; @@ -8,7 +7,7 @@ import { AccordionItem } from '../../base/accordion-item'; import { useTranslation } from 'react-i18next'; interface CategoriesFilterProps { - onError: (message: string) => void, + productCategories: Array, onApplyFilters: (categories: Array) => void, currentFilters: Array, openDefault?: boolean, @@ -18,19 +17,12 @@ interface CategoriesFilterProps { /** * Component to filter the products list by categories */ -export const CategoriesFilter: React.FC = ({ onError, onApplyFilters, currentFilters, openDefault = false, instantUpdate = false }) => { +export const CategoriesFilter: React.FC = ({ productCategories, onApplyFilters, currentFilters, openDefault = false, instantUpdate = false }) => { const { t } = useTranslation('admin'); - const [productCategories, setProductCategories] = useState([]); const [openedAccordion, setOpenedAccordion] = useState(openDefault); const [selectedCategories, setSelectedCategories] = useState(currentFilters || []); - useEffect(() => { - ProductCategoryAPI.index().then(data => { - setProductCategories(ProductLib.sortCategories(data)); - }).catch(onError); - }, []); - useEffect(() => { if (currentFilters && !_.isEqual(currentFilters, selectedCategories)) { setSelectedCategories(currentFilters); @@ -49,36 +41,7 @@ export const CategoriesFilter: React.FC = ({ onError, onA * This may cause other categories to be selected or unselected accordingly. */ const handleSelectCategory = (currentCategory: ProductCategory, checked: boolean) => { - let list = [...selectedCategories]; - const children = productCategories - .filter(el => el.parent_id === currentCategory.id); - const siblings = productCategories - .filter(el => el.parent_id === currentCategory.parent_id && el.parent_id !== null); - - if (checked) { - list.push(currentCategory); - if (children.length) { - // if a parent category is selected, we automatically select all its children - list = [...Array.from(new Set([...list, ...children]))]; - } - if (siblings.length && siblings.every(el => list.includes(el))) { - // if a child category is selected, with every sibling of it, we automatically select its parent - list.push(productCategories.find(p => p.id === siblings[0].parent_id)); - } - } else { - list.splice(list.indexOf(currentCategory), 1); - const parent = productCategories.find(p => p.id === currentCategory.parent_id); - if (currentCategory.parent_id && list.includes(parent)) { - // if a child category is unselected, we unselect its parent - list.splice(list.indexOf(parent), 1); - } - if (children.length) { - // if a parent category is unselected, we unselect all its children - children.forEach(child => { - list.splice(list.indexOf(child), 1); - }); - } - } + const list = ProductLib.categoriesSelectionTree(productCategories, selectedCategories, currentCategory, checked ? 'add' : 'remove'); setSelectedCategories(list); if (instantUpdate) { diff --git a/app/frontend/src/javascript/components/store/filters/keyword-filter.tsx b/app/frontend/src/javascript/components/store/filters/keyword-filter.tsx index 63d25daed..87683933d 100644 --- a/app/frontend/src/javascript/components/store/filters/keyword-filter.tsx +++ b/app/frontend/src/javascript/components/store/filters/keyword-filter.tsx @@ -6,7 +6,7 @@ import _ from 'lodash'; interface KeywordFilterProps { onApplyFilters: (keywork: string) => void, - currentFilters: string, + currentFilters?: string, openDefault?: boolean, instantUpdate?: boolean, } @@ -14,14 +14,14 @@ interface KeywordFilterProps { /** * Component to filter the products list by keyword or product reference */ -export const KeywordFilter: React.FC = ({ onApplyFilters, currentFilters, openDefault = false, instantUpdate = false }) => { +export const KeywordFilter: React.FC = ({ onApplyFilters, currentFilters = '', openDefault = false, instantUpdate = false }) => { const { t } = useTranslation('admin'); const [openedAccordion, setOpenedAccordion] = useState(openDefault); - const [keyword, setKeyword] = useState(currentFilters); + const [keyword, setKeyword] = useState(currentFilters || ''); useEffect(() => { - if (currentFilters && !_.isEqual(currentFilters, keyword)) { + if (!_.isEqual(currentFilters, keyword)) { setKeyword(currentFilters); } }, [currentFilters]); @@ -53,8 +53,8 @@ export const KeywordFilter: React.FC = ({ onApplyFilters, cu >
- handleKeywordTyping(event)} /> - onApplyFilters(keyword)} className="is-info">{t('app.admin.store.keyword_filter.filter_apply')} + handleKeywordTyping(event)} value={keyword} /> + onApplyFilters(keyword || undefined)} className="is-info">{t('app.admin.store.keyword_filter.filter_apply')}
diff --git a/app/frontend/src/javascript/components/store/filters/machines-filter.tsx b/app/frontend/src/javascript/components/store/filters/machines-filter.tsx index 2a0d28482..ab4a2bf1f 100644 --- a/app/frontend/src/javascript/components/store/filters/machines-filter.tsx +++ b/app/frontend/src/javascript/components/store/filters/machines-filter.tsx @@ -47,7 +47,7 @@ export const MachinesFilter: React.FC = ({ onError, onApply * Callback triggered when a machine filter is seleced or unselected. */ const handleSelectMachine = (currentMachine: Machine, checked: boolean) => { - const list = [...machines]; + const list = [...selectedMachines]; checked ? list.push(currentMachine) : list.splice(list.indexOf(currentMachine), 1); @@ -63,13 +63,12 @@ export const MachinesFilter: React.FC = ({ onError, onApply + label={t('app.admin.store.machines_filter.filter_machines')}>
{machines.map(m => ( ))} diff --git a/app/frontend/src/javascript/components/store/filters/stock-filter.tsx b/app/frontend/src/javascript/components/store/filters/stock-filter.tsx index 6d7d94283..db210dd43 100644 --- a/app/frontend/src/javascript/components/store/filters/stock-filter.tsx +++ b/app/frontend/src/javascript/components/store/filters/stock-filter.tsx @@ -2,21 +2,15 @@ import React, { useEffect, useState } from 'react'; import { FabButton } from '../../base/fab-button'; import { AccordionItem } from '../../base/accordion-item'; import { useTranslation } from 'react-i18next'; -import { StockType } from '../../../models/product'; +import { ProductIndexFilter, StockType } from '../../../models/product'; import { FormSelect } from '../../form/form-select'; import { FormInput } from '../../form/form-input'; import { useForm } from 'react-hook-form'; import _ from 'lodash'; -export interface StockFilterData { - stock_type: StockType, - stock_from: number, - stock_to: number -} - interface StockFilterProps { - onApplyFilters: (filters: StockFilterData) => void, - currentFilters: StockFilterData, + onApplyFilters: (filters: ProductIndexFilter) => void, + currentFilters: ProductIndexFilter, openDefault?: boolean } @@ -34,7 +28,7 @@ export const StockFilter: React.FC = ({ onApplyFilters, curren const [openedAccordion, setOpenedAccordion] = useState(openDefault); - const { register, control, handleSubmit, getValues, reset } = useForm({ defaultValues: { ...currentFilters } }); + const { register, control, handleSubmit, getValues, reset } = useForm({ defaultValues: { ...currentFilters } }); useEffect(() => { if (currentFilters && !_.isEqual(currentFilters, getValues())) { @@ -52,7 +46,7 @@ export const StockFilter: React.FC = ({ onApplyFilters, curren /** * Callback triggered when the user clicks on "apply" to apply teh current filters. */ - const onSubmit = (data: StockFilterData) => { + const onSubmit = (data: ProductIndexFilter) => { onApplyFilters(data); }; diff --git a/app/frontend/src/javascript/components/store/products.tsx b/app/frontend/src/javascript/components/store/products.tsx index b959c8ee2..591dde8fe 100644 --- a/app/frontend/src/javascript/components/store/products.tsx +++ b/app/frontend/src/javascript/components/store/products.tsx @@ -4,19 +4,21 @@ import { useTranslation } from 'react-i18next'; import { react2angular } from 'react2angular'; import { Loader } from '../base/loader'; import { IApplication } from '../../models/application'; -import { Product } from '../../models/product'; +import { Product, ProductIndexFilter, ProductsIndex } from '../../models/product'; import { ProductCategory } from '../../models/product-category'; import { FabButton } from '../base/fab-button'; import { ProductItem } from './product-item'; import ProductAPI from '../../api/product'; -import { X } from 'phosphor-react'; import { StoreListHeader } from './store-list-header'; import { FabPagination } from '../base/fab-pagination'; import { CategoriesFilter } from './filters/categories-filter'; import { Machine } from '../../models/machine'; import { MachinesFilter } from './filters/machines-filter'; import { KeywordFilter } from './filters/keyword-filter'; -import { StockFilter, StockFilterData } from './filters/stock-filter'; +import { StockFilter } from './filters/stock-filter'; +import ProductCategoryAPI from '../../api/product-category'; +import ProductLib from '../../lib/product'; +import { ActiveFiltersTags } from './filters/active-filters-tags'; declare const Application: IApplication; @@ -34,42 +36,57 @@ interface ProductsProps { const Products: React.FC = ({ onSuccess, onError }) => { const { t } = useTranslation('admin'); - const [filteredProductsList, setFilteredProductList] = useImmer>([]); - const [features, setFeatures] = useImmer(initFilters); - const [filterVisible, setFilterVisible] = useState(false); - const [filters, setFilters] = useImmer(initFilters); - const [clearFilters, setClearFilters] = useState(false); - const [update, setUpdate] = useState(false); + const [productCategories, setProductCategories] = useState>([]); + const [productsList, setProductList] = useState>([]); + const [filters, setFilters] = useImmer(initFilters); const [pageCount, setPageCount] = useState(0); const [currentPage, setCurrentPage] = useState(1); + const [productsCount, setProductsCount] = useState(0); useEffect(() => { - ProductAPI.index({ page: 1, is_active: filterVisible }).then(data => { - setPageCount(data.total_pages); - setFilteredProductList(data.products); - }); + fetchProducts().then(scrollToProducts); + ProductCategoryAPI.index().then(data => { + setProductCategories(ProductLib.sortCategories(data)); + }).catch(onError); }, []); useEffect(() => { - applyFilters(); - setClearFilters(false); - setUpdate(false); - }, [filterVisible, clearFilters, update === true]); + fetchProducts().then(scrollToProducts); + }, [filters]); /** Handle products pagination */ const handlePagination = (page: number) => { if (page !== currentPage) { - ProductAPI.index({ page, is_active: filterVisible }).then(data => { - setCurrentPage(page); - setFilteredProductList(data.products); - setPageCount(data.total_pages); - window.document.getElementById('content-main').scrollTo({ top: 100, behavior: 'smooth' }); - }).catch(() => { - onError(t('app.admin.store.products.unexpected_error_occurred')); + setFilters(draft => { + return { ...draft, page }; }); } }; + /** + * Fetch the products from the API, according to the current filters + */ + const fetchProducts = async (): Promise => { + try { + const data = await ProductAPI.index(filters); + setCurrentPage(data.page); + setProductList(data.data); + setPageCount(data.total_pages); + setProductsCount(data.total_count); + return data; + } catch (error) { + onError(t('app.admin.store.products.unexpected_error_occurred')); + console.error(error); + } + }; + + /** + * Scroll the view to the product list + */ + const scrollToProducts = () => { + window.document.getElementById('content-main').scrollTo({ top: 100, behavior: 'smooth' }); + }; + /** Goto edit product page */ const editProduct = (product: Product) => { window.location.href = `/#!/admin/store/products/${product.id}/edit`; @@ -79,8 +96,8 @@ const Products: React.FC = ({ onSuccess, onError }) => { const deleteProduct = async (productId: number): Promise => { try { await ProductAPI.destroy(productId); - const data = await ProductAPI.index(); - setFilteredProductList(data.products); + await fetchProducts(); + scrollToProducts(); onSuccess(t('app.admin.store.products.successfully_deleted')); } catch (e) { onError(t('app.admin.store.products.unable_to_delete') + e); @@ -94,8 +111,9 @@ const Products: React.FC = ({ onSuccess, onError }) => { /** Filter: toggle non-available products visibility */ const toggleVisible = (checked: boolean) => { - setFilterVisible(!filterVisible); - console.log('Display on the shelf product only:', checked); + setFilters(draft => { + return { ...draft, is_active: checked }; + }); }; /** @@ -107,6 +125,14 @@ const Products: React.FC = ({ onSuccess, onError }) => { }); }; + /** + * Remove the provided category from the filters selection + */ + const handleRemoveCategory = (category: ProductCategory) => { + const list = ProductLib.categoriesSelectionTree(productCategories, filters.categories, category, 'remove'); + handleCategoriesFilterUpdate(list); + }; + /** * Update the list of applied filters with the given machines */ @@ -126,7 +152,7 @@ const Products: React.FC = ({ onSuccess, onError }) => { }; /** Filter: by stock range */ - const handleStockFilterUpdate = (filters: StockFilterData) => { + const handleStockFilterUpdate = (filters: ProductIndexFilter) => { setFilters(draft => { return { ...draft, @@ -140,27 +166,9 @@ const Products: React.FC = ({ onSuccess, onError }) => { console.log('Sort option:', option); }; - /** Apply filters */ - const applyFilters = () => { - let tags = initFilters; - - if (filters.categories.length) { - tags = { ...tags, categories: [...filters.categories] }; - } - - if (filters.machines.length) { - tags = { ...tags, machines: [...filters.machines] }; - } - - setFeatures(tags); - console.log('Apply filters:', filters); - }; - /** Clear filters */ const clearAllFilters = () => { setFilters(initFilters); - setClearFilters(true); - console.log('Clear all filters'); }; /** Creates sorting options to the react-select format */ @@ -189,7 +197,7 @@ const Products: React.FC = ({ onSuccess, onError }) => {
- @@ -197,7 +205,7 @@ const Products: React.FC = ({ onSuccess, onError }) => { onApplyFilters={handleMachinesFilterUpdate} currentFilters={filters.machines} /> - handleKeywordFilterUpdate([...filters.keywords, keyword])} + handleKeywordFilterUpdate([keyword])} currentFilters={filters.keywords[0]} /> = ({ onSuccess, onError }) => {
- {features.categories.map(c => ( -
-

{c.name}

- -
- ))} - {features.machines.map(m => ( -
-

{m.name}

- -
- ))} - {features.keywords.map(k => ( -
-

{k}

- -
- ))} + handleMachinesFilterUpdate(filters.machines.filter(machine => machine !== m))} + onRemoveKeyword={() => handleKeywordFilterUpdate([])} + onRemoveStock={() => handleStockFilterUpdate({ stock_type: 'internal', stock_to: 0, stock_from: 0 })} />
- {filteredProductsList.map((product) => ( + {productsList.map((product) => ( = ({ onSuccess, onError }) => { Application.Components.component('products', react2angular(ProductsWrapper, ['onSuccess', 'onError'])); -interface Filters { - categories: ProductCategory[], - machines: Machine[], - keywords: string[], - stock_type: 'internal' | 'external', - stock_from: number, - stock_to: number -} - -const initFilters: Filters = { +const initFilters: ProductIndexFilter = { categories: [], machines: [], keywords: [], stock_type: 'internal', stock_from: 0, - stock_to: 0 + stock_to: 0, + is_active: false, + page: 1 }; diff --git a/app/frontend/src/javascript/lib/product.ts b/app/frontend/src/javascript/lib/product.ts index af377fede..56fdfce38 100644 --- a/app/frontend/src/javascript/lib/product.ts +++ b/app/frontend/src/javascript/lib/product.ts @@ -1,5 +1,10 @@ import { ProductCategory } from '../models/product-category'; -import { stockMovementInReasons, stockMovementOutReasons, StockMovementReason } from '../models/product'; +import { + ProductIndexFilter, ProductIndexFilterIds, + stockMovementInReasons, + stockMovementOutReasons, + StockMovementReason +} from '../models/product'; export default class ProductLib { /** @@ -48,4 +53,53 @@ export default class ProductLib { return `-${quantity}`; } }; + + /** + * Add or remove the given category from the given list; + * This may cause parent/children categories to be selected or unselected accordingly. + */ + static categoriesSelectionTree = (allCategories: Array, currentSelection: Array, category: ProductCategory, operation: 'add'|'remove'): Array => { + let list = [...currentSelection]; + const children = allCategories + .filter(el => el.parent_id === category.id); + const siblings = allCategories + .filter(el => el.parent_id === category.parent_id && el.parent_id !== null); + + if (operation === 'add') { + list.push(category); + if (children.length) { + // if a parent category is selected, we automatically select all its children + list = [...Array.from(new Set([...list, ...children]))]; + } + if (siblings.length && siblings.every(el => list.includes(el))) { + // if a child category is selected, with every sibling of it, we automatically select its parent + list.push(allCategories.find(p => p.id === siblings[0].parent_id)); + } + } else { + list.splice(list.indexOf(category), 1); + const parent = allCategories.find(p => p.id === category.parent_id); + if (category.parent_id && list.includes(parent)) { + // if a child category is unselected, we unselect its parent + list.splice(list.indexOf(parent), 1); + } + if (children.length) { + // if a parent category is unselected, we unselect all its children + children.forEach(child => { + list.splice(list.indexOf(child), 1); + }); + } + } + return list; + }; + + /** + * Extract the IDS from the filters to pass them to the API + */ + static indexFiltersToIds = (filters: ProductIndexFilter): ProductIndexFilterIds => { + return { + ...filters, + categories: filters.categories.map(c => c.id), + machines: filters.machines.map(m => m.id) + }; + }; } diff --git a/app/frontend/src/javascript/models/api.ts b/app/frontend/src/javascript/models/api.ts index b1fbafd1d..6f531f19d 100644 --- a/app/frontend/src/javascript/models/api.ts +++ b/app/frontend/src/javascript/models/api.ts @@ -1,3 +1,10 @@ // ApiFilter should be extended by an interface listing all the filters allowed for a given API -// eslint-disable-next-line @typescript-eslint/ban-types -export type ApiFilter = {}; +export type ApiFilter = Record; + +export interface PaginatedIndex { + page: number, + total_pages: number, + page_size: number, + total_count: number, + data: Array +} diff --git a/app/frontend/src/javascript/models/order.ts b/app/frontend/src/javascript/models/order.ts index cf986e86a..e1d2fd5e0 100644 --- a/app/frontend/src/javascript/models/order.ts +++ b/app/frontend/src/javascript/models/order.ts @@ -3,6 +3,7 @@ import { PaymentConfirmation } from './payment'; import { CreateTokenResponse } from './payzen'; import { UserRole } from './user'; import { Coupon } from './coupon'; +import { ApiFilter, PaginatedIndex } from './api'; export interface Order { id: number, @@ -45,15 +46,9 @@ export interface OrderPayment { payment?: PaymentConfirmation|CreateTokenResponse } -export interface OrderIndex { - page: number, - total_pages: number, - page_size: number, - total_count: number, - data: Array -} +export type OrderIndex = PaginatedIndex; -export interface OrderIndexFilter { +export interface OrderIndexFilter extends ApiFilter { reference?: string, user_id?: number, user?: { diff --git a/app/frontend/src/javascript/models/product.ts b/app/frontend/src/javascript/models/product.ts index 757918964..62bcf6d27 100644 --- a/app/frontend/src/javascript/models/product.ts +++ b/app/frontend/src/javascript/models/product.ts @@ -1,9 +1,22 @@ import { TDateISO } from '../typings/date-iso'; -import { ApiFilter } from './api'; +import { ApiFilter, PaginatedIndex } from './api'; +import { ProductCategory } from './product-category'; +import { Machine } from './machine'; -export interface ProductIndexFilter extends ApiFilter { +export interface ProductIndexFilter { is_active?: boolean, - page?: number + page?: number, + categories?: ProductCategory[], + machines?: Machine[], + keywords?: string[], + stock_type?: 'internal' | 'external', + stock_from?: number, + stock_to?: number, +} + +export interface ProductIndexFilterIds extends Omit, 'machines'>, ApiFilter { + categories?: Array, + machines?: Array, } export type StockType = 'internal' | 'external' | 'all'; @@ -19,10 +32,7 @@ export interface Stock { external: number, } -export interface ProductsIndex { - total_pages?: number, - products: Array -} +export type ProductsIndex = PaginatedIndex; export interface ProductStockMovement { id?: number, diff --git a/app/models/machine.rb b/app/models/machine.rb index da118eddf..661f37256 100644 --- a/app/models/machine.rb +++ b/app/models/machine.rb @@ -10,12 +10,14 @@ class Machine < ApplicationRecord has_many :machine_files, as: :viewable, dependent: :destroy accepts_nested_attributes_for :machine_files, allow_destroy: true, reject_if: :all_blank - has_and_belongs_to_many :projects, join_table: 'projects_machines' + has_many :projects_machines, dependent: :destroy + has_many :projects, through: :projects_machines has_many :machines_availabilities, dependent: :destroy has_many :availabilities, through: :machines_availabilities - has_and_belongs_to_many :trainings, join_table: 'trainings_machines' + has_many :trainings_machines, dependent: :destroy + has_many :trainings, through: :trainings_machines validates :name, presence: true, length: { maximum: 50 } validates :description, presence: true @@ -27,9 +29,10 @@ class Machine < ApplicationRecord has_many :credits, as: :creditable, dependent: :destroy has_many :plans, through: :credits - has_one :payment_gateway_object, as: :item + has_one :payment_gateway_object, as: :item, dependent: :destroy - has_and_belongs_to_many :products + has_many :machines_products, dependent: :destroy + has_many :products, through: :machines_products after_create :create_statistic_subtype after_create :create_machine_prices @@ -66,11 +69,11 @@ class Machine < ApplicationRecord end def create_machine_prices - Group.all.each do |group| + Group.find_each do |group| Price.create(priceable: self, group: group, amount: 0) end - Plan.all.includes(:group).each do |plan| + Plan.includes(:group).find_each do |plan| Price.create(group: plan.group, plan: plan, priceable: self, amount: 0) end end diff --git a/app/models/machines_product.rb b/app/models/machines_product.rb new file mode 100644 index 000000000..893144834 --- /dev/null +++ b/app/models/machines_product.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +# MachinesAvailability is the relation table between a Machine and a Product. +class MachinesProduct < ApplicationRecord + belongs_to :machine + belongs_to :product +end diff --git a/app/models/product.rb b/app/models/product.rb index 37f5d1a04..6b3df19f0 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -8,7 +8,8 @@ class Product < ApplicationRecord belongs_to :product_category - has_and_belongs_to_many :machines + has_many :machines_products, dependent: :destroy + has_many :machines, through: :machines_products has_many :product_files, as: :viewable, dependent: :destroy accepts_nested_attributes_for :product_files, allow_destroy: true, reject_if: :all_blank diff --git a/app/models/projects_machine.rb b/app/models/projects_machine.rb new file mode 100644 index 000000000..5211d9644 --- /dev/null +++ b/app/models/projects_machine.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +# ProjectsMachine is the relation table between a Machine and a Project. +class ProjectsMachine < ApplicationRecord + belongs_to :machine + belongs_to :project +end diff --git a/app/models/trainings_machine.rb b/app/models/trainings_machine.rb new file mode 100644 index 000000000..feb4b0469 --- /dev/null +++ b/app/models/trainings_machine.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +# TrainingsMachine is the relation table between a Machine and a Training. +class TrainingsMachine < ApplicationRecord + belongs_to :machine + belongs_to :training +end diff --git a/app/services/checkout/payment_service.rb b/app/services/checkout/payment_service.rb index 429870e5f..4dcbc218d 100644 --- a/app/services/checkout/payment_service.rb +++ b/app/services/checkout/payment_service.rb @@ -1,19 +1,19 @@ # frozen_string_literal: true -# Provides methods for pay cart +# Provides methods to pay the cart class Checkout::PaymentService require 'pay_zen/helper' require 'stripe/helper' include Payments::PaymentConcern def payment(order, operator, coupon_code, payment_id = '') - raise Cart::InactiveProductError unless Orders::OrderService.new.all_products_is_active?(order) + raise Cart::InactiveProductError unless Orders::OrderService.all_products_is_active?(order) - raise Cart::OutStockError unless Orders::OrderService.new.in_stock?(order, 'external') + raise Cart::OutStockError unless Orders::OrderService.in_stock?(order, 'external') - raise Cart::QuantityMinError unless Orders::OrderService.new.greater_than_quantity_min?(order) + raise Cart::QuantityMinError unless Orders::OrderService.greater_than_quantity_min?(order) - raise Cart::ItemAmountError unless Orders::OrderService.new.item_amount_not_equal?(order) + raise Cart::ItemAmountError unless Orders::OrderService.item_amount_not_equal?(order) CouponService.new.validate(coupon_code, order.statistic_profile.user.id) diff --git a/app/services/orders/order_service.rb b/app/services/orders/order_service.rb index 4b1209131..b5576f0d0 100644 --- a/app/services/orders/order_service.rb +++ b/app/services/orders/order_service.rb @@ -2,80 +2,109 @@ # Provides methods for Order class Orders::OrderService - ORDERS_PER_PAGE = 20 + class << self + ORDERS_PER_PAGE = 20 - def self.list(filters, current_user) - orders = Order.where(nil) - if filters[:user_id] - statistic_profile_id = current_user.statistic_profile.id - if (current_user.member? && current_user.id == filters[:user_id].to_i) || current_user.privileged? - user = User.find(filters[:user_id]) - statistic_profile_id = user.statistic_profile.id + def list(filters, current_user) + orders = Order.where(nil) + orders = filter_by_user(orders, filters, current_user) + orders = filter_by_reference(orders, filters, current_user) + orders = filter_by_state(orders, filters) + orders = filter_by_period(orders, filters) + + orders = orders.where.not(state: 'cart') if current_user.member? + orders = orders.order(created_at: filters[:sort] || 'DESC') + total_count = orders.count + orders = orders.page(filters[:page] || 1).per(ORDERS_PER_PAGE) + { + data: orders, + page: filters[:page]&.to_i || 1, + total_pages: orders.page(1).per(ORDERS_PER_PAGE).total_pages, + page_size: ORDERS_PER_PAGE, + total_count: total_count + } + end + + def update_state(order, current_user, state, note = nil) + case state + when 'in_progress' + ::Orders::SetInProgressService.new.call(order, current_user) + when 'ready' + ::Orders::OrderReadyService.new.call(order, current_user, note) + when 'canceled' + ::Orders::OrderCanceledService.new.call(order, current_user) + when 'delivered' + ::Orders::OrderDeliveredService.new.call(order, current_user) + when 'refunded' + ::Orders::OrderRefundedService.new.call(order, current_user) + else + nil end - orders = orders.where(statistic_profile_id: statistic_profile_id) - elsif current_user.member? - orders = orders.where(statistic_profile_id: current_user.statistic_profile.id) - else - orders = orders.where.not(statistic_profile_id: nil) end - orders = orders.where(reference: filters[:reference]) if filters[:reference].present? && current_user.privileged? + def in_stock?(order, stock_type = 'external') + order.order_items.each do |item| + return false if item.orderable.stock[stock_type] < item.quantity + end + true + end + + def greater_than_quantity_min?(order) + order.order_items.each do |item| + return false if item.quantity < item.orderable.quantity_min + end + true + end + + def item_amount_not_equal?(order) + order.order_items.each do |item| + return false if item.amount != item.orderable.amount + end + true + end + + def all_products_is_active?(order) + order.order_items.each do |item| + return false unless item.orderable.is_active + end + true + end + + private + + def filter_by_user(orders, filters, current_user) + if filters[:user_id] + statistic_profile_id = current_user.statistic_profile.id + if (current_user.member? && current_user.id == filters[:user_id].to_i) || current_user.privileged? + user = User.find(filters[:user_id]) + statistic_profile_id = user.statistic_profile.id + end + orders = orders.where(statistic_profile_id: statistic_profile_id) + elsif current_user.member? + orders = orders.where(statistic_profile_id: current_user.statistic_profile.id) + else + orders = orders.where.not(statistic_profile_id: nil) + end + orders + end + + def filter_by_reference(orders, filters, current_user) + return orders unless filters[:reference].present? && current_user.privileged? + + orders.where(reference: filters[:reference]) + end + + def filter_by_state(orders, filters) + return orders if filters[:states].blank? - if filters[:states].present? state = filters[:states].split(',') - orders = orders.where(state: state) unless state.empty? + orders.where(state: state) unless state.empty? end - if filters[:period_from].present? && filters[:period_to].present? - orders = orders.where(created_at: DateTime.parse(filters[:period_from])..DateTime.parse(filters[:period_to]).end_of_day) + def filter_by_period(orders, filters) + return orders unless filters[:period_from].present? && filters[:period_to].present? + + orders.where(created_at: DateTime.parse(filters[:period_from])..DateTime.parse(filters[:period_to]).end_of_day) end - - orders = orders.where.not(state: 'cart') if current_user.member? - orders = orders.order(created_at: filters[:sort] || 'DESC') - total_count = orders.count - orders = orders.page(filters[:page] || 1).per(ORDERS_PER_PAGE) - { - data: orders, - page: filters[:page] || 1, - total_pages: orders.page(1).per(ORDERS_PER_PAGE).total_pages, - page_size: ORDERS_PER_PAGE, - total_count: total_count - } - end - - def self.update_state(order, current_user, state, note = nil) - return ::Orders::SetInProgressService.new.call(order, current_user) if state == 'in_progress' - return ::Orders::OrderReadyService.new.call(order, current_user, note) if state == 'ready' - return ::Orders::OrderCanceledService.new.call(order, current_user) if state == 'canceled' - return ::Orders::OrderDeliveredService.new.call(order, current_user) if state == 'delivered' - return ::Orders::OrderRefundedService.new.call(order, current_user) if state == 'refunded' - end - - def in_stock?(order, stock_type = 'external') - order.order_items.each do |item| - return false if item.orderable.stock[stock_type] < item.quantity - end - true - end - - def greater_than_quantity_min?(order) - order.order_items.each do |item| - return false if item.quantity < item.orderable.quantity_min - end - true - end - - def item_amount_not_equal?(order) - order.order_items.each do |item| - return false if item.amount != item.orderable.amount - end - true - end - - def all_products_is_active?(order) - order.order_items.each do |item| - return false unless item.orderable.is_active - end - true end end diff --git a/app/services/product_service.rb b/app/services/product_service.rb index ff7f8432e..70719fb93 100644 --- a/app/services/product_service.rb +++ b/app/services/product_service.rb @@ -7,21 +7,21 @@ class ProductService def list(filters) products = Product.includes(:product_images) - if filters[:is_active].present? - state = filters[:disabled] == 'false' ? [nil, false] : true - products = products.where(is_active: state) - end - products = products.page(filters[:page]).per(PRODUCTS_PER_PAGE) if filters[:page].present? - products - end + products = filter_by_active(products, filters) + products = filter_by_categories(products, filters) + products = filter_by_machines(products, filters) + products = filter_by_keyword_or_reference(products, filters) + products = filter_by_stock(products, filters) - def pages(filters) - products = Product.all - if filters[:is_active].present? - state = filters[:disabled] == 'false' ? [nil, false] : true - products = Product.where(is_active: state) - end - products.page(1).per(PRODUCTS_PER_PAGE).total_pages + total_count = products.count + products = products.page(filters[:page] || 1).per(PRODUCTS_PER_PAGE) + { + data: products, + page: filters[:page]&.to_i || 1, + total_pages: products.page(1).per(PRODUCTS_PER_PAGE).total_pages, + page_size: PRODUCTS_PER_PAGE, + total_count: total_count + } end # amount params multiplied by hundred @@ -80,5 +80,40 @@ class ProductService product.destroy end end + + private + + def filter_by_active(products, filters) + return products if filters[:is_active].blank? + + state = filters[:is_active] == 'false' ? [nil, false, true] : true + products.where(is_active: state) + end + + def filter_by_categories(products, filters) + return products if filters[:categories].blank? + + products.where(product_category_id: filters[:categories].split(',')) + end + + def filter_by_machines(products, filters) + return products if filters[:machines].blank? + + products.includes(:machines_products).where('machines_products.machine_id': filters[:machines].split(',')) + end + + def filter_by_keyword_or_reference(products, filters) + return products if filters[:keywords].blank? + + products.where('sku = :sku OR name ILIKE :query OR description ILIKE :query', + { sku: filters[:keywords], query: "%#{filters[:keywords]}%" }) + end + + def filter_by_stock(products, filters) + products.where("(stock ->> '#{filters[:stock_type]}')::int >= #{filters[:stock_from]}") if filters[:stock_from].to_i.positive? + products.where("(stock ->> '#{filters[:stock_type]}')::int <= #{filters[:stock_to]}") if filters[:stock_to].to_i.positive? + + products + end end end diff --git a/app/views/api/orders/index.json.jbuilder b/app/views/api/orders/index.json.jbuilder index 6bc854380..5f1912cfb 100644 --- a/app/views/api/orders/index.json.jbuilder +++ b/app/views/api/orders/index.json.jbuilder @@ -1,9 +1,6 @@ # frozen_string_literal: true -json.page @result[:page] -json.total_pages @result[:total_pages] -json.page_size @result[:page_size] -json.total_count @result[:total_count] +json.extract! @result, :page, :total_pages, :page_size, :total_count json.data @result[:data] do |order| json.extract! order, :id, :statistic_profile_id, :reference, :state, :created_at, :updated_at json.total order.total / 100.0 if order.total.present? diff --git a/app/views/api/products/index.json.jbuilder b/app/views/api/products/index.json.jbuilder index 0573e95be..33c73329e 100644 --- a/app/views/api/products/index.json.jbuilder +++ b/app/views/api/products/index.json.jbuilder @@ -1,7 +1,7 @@ # frozen_string_literal: true -json.total_pages @pages if @pages.present? -json.products @products do |product| +json.extract! @products, :page, :total_pages, :page_size, :total_count +json.data @products[:data] do |product| json.extract! product, :id, :name, :slug, :sku, :is_active, :product_category_id, :quantity_min, :stock, :machine_ids, :low_stock_threshold json.amount product.amount / 100.0 if product.amount.present? diff --git a/config/locales/app.shared.en.yml b/config/locales/app.shared.en.yml index bfa293431..7d628ae05 100644 --- a/config/locales/app.shared.en.yml +++ b/config/locales/app.shared.en.yml @@ -636,3 +636,6 @@ en: modal_title: "You have some unsaved changes" confirmation_message: "If you leave this page, your changes will be lost. Are you sure you want to continue?" confirmation_button: "Yes, don't save" + active_filters_tags: + stock_internal: "Private stock" + stock_external: "Public stock" diff --git a/db/migrate/20220920131912_add_index_on_product_slug.rb b/db/migrate/20220920131912_add_index_on_product_slug.rb new file mode 100644 index 000000000..4924b4098 --- /dev/null +++ b/db/migrate/20220920131912_add_index_on_product_slug.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +# Products' slugs should validate uniqness in database +class AddIndexOnProductSlug < ActiveRecord::Migration[5.2] + def change + add_index :products, :slug, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 6aaa272cb..62fa5125d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_09_15_133100) do +ActiveRecord::Schema.define(version: 2022_09_20_131912) do # These are extensions that must be enabled in order to support this database enable_extension "fuzzystrmatch" @@ -19,8 +19,8 @@ ActiveRecord::Schema.define(version: 2022_09_15_133100) do enable_extension "unaccent" create_table "abuses", id: :serial, force: :cascade do |t| - t.string "signaled_type" t.integer "signaled_id" + t.string "signaled_type" t.string "first_name" t.string "last_name" t.string "email" @@ -49,8 +49,8 @@ ActiveRecord::Schema.define(version: 2022_09_15_133100) do t.string "locality" t.string "country" t.string "postal_code" - t.string "placeable_type" t.integer "placeable_id" + t.string "placeable_type" t.datetime "created_at" t.datetime "updated_at" end @@ -64,8 +64,8 @@ ActiveRecord::Schema.define(version: 2022_09_15_133100) do end create_table "assets", id: :serial, force: :cascade do |t| - t.string "viewable_type" t.integer "viewable_id" + t.string "viewable_type" t.string "attachment" t.string "type" t.datetime "created_at" @@ -147,8 +147,8 @@ ActiveRecord::Schema.define(version: 2022_09_15_133100) do end create_table "credits", id: :serial, force: :cascade do |t| - t.string "creditable_type" t.integer "creditable_id" + t.string "creditable_type" t.integer "plan_id" t.integer "hours" t.datetime "created_at" @@ -375,15 +375,15 @@ ActiveRecord::Schema.define(version: 2022_09_15_133100) do create_table "notifications", id: :serial, force: :cascade do |t| t.integer "receiver_id" - t.string "attached_object_type" t.integer "attached_object_id" + t.string "attached_object_type" t.integer "notification_type_id" t.boolean "is_read", default: false t.datetime "created_at" t.datetime "updated_at" t.string "receiver_type" t.boolean "is_send", default: false - t.jsonb "meta_data", default: "{}" + t.jsonb "meta_data", default: {} t.index ["notification_type_id"], name: "index_notifications_on_notification_type_id" t.index ["receiver_id"], name: "index_notifications_on_receiver_id" end @@ -623,8 +623,8 @@ ActiveRecord::Schema.define(version: 2022_09_15_133100) do create_table "prices", id: :serial, force: :cascade do |t| t.integer "group_id" t.integer "plan_id" - t.string "priceable_type" t.integer "priceable_id" + t.string "priceable_type" t.integer "amount" t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -672,6 +672,7 @@ ActiveRecord::Schema.define(version: 2022_09_15_133100) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["product_category_id"], name: "index_products_on_product_category_id" + t.index ["slug"], name: "index_products_on_slug", unique: true end create_table "profile_custom_fields", force: :cascade do |t| @@ -822,8 +823,8 @@ ActiveRecord::Schema.define(version: 2022_09_15_133100) do t.text "message" t.datetime "created_at" t.datetime "updated_at" - t.string "reservable_type" t.integer "reservable_id" + t.string "reservable_type" t.integer "nb_reserve_places" t.integer "statistic_profile_id" t.index ["reservable_type", "reservable_id"], name: "index_reservations_on_reservable_type_and_reservable_id" @@ -832,8 +833,8 @@ ActiveRecord::Schema.define(version: 2022_09_15_133100) do create_table "roles", id: :serial, force: :cascade do |t| t.string "name" - t.string "resource_type" t.integer "resource_id" + t.string "resource_type" t.datetime "created_at" t.datetime "updated_at" t.index ["name", "resource_type", "resource_id"], name: "index_roles_on_name_and_resource_type_and_resource_id" @@ -1113,8 +1114,8 @@ ActiveRecord::Schema.define(version: 2022_09_15_133100) do t.boolean "is_allow_newsletter" t.inet "current_sign_in_ip" t.inet "last_sign_in_ip" - t.datetime "validated_at" t.string "mapped_from_sso" + t.datetime "validated_at" t.index ["auth_token"], name: "index_users_on_auth_token" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true