feat: analytic
This commit is contained in:
@@ -1,52 +1,52 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { CustomerRequest } from '../../types/services/customer'
|
||||
import { api } from '../api'
|
||||
import { toast } from 'react-toastify'
|
||||
import { IngredientRequest } from '../../types/services/ingredient'
|
||||
import { api } from '../api'
|
||||
|
||||
export const useIngredientsMutation = () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const createCustomer = useMutation({
|
||||
mutationFn: async (newCustomer: CustomerRequest) => {
|
||||
const response = await api.post('/customers', newCustomer)
|
||||
const createIngredient = useMutation({
|
||||
mutationFn: async (newIngredient: IngredientRequest) => {
|
||||
const response = await api.post('/ingredients', newIngredient)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Customer created successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['customers'] })
|
||||
toast.success('Ingredient created successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['ingredients'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
|
||||
}
|
||||
})
|
||||
|
||||
const updateCustomer = useMutation({
|
||||
mutationFn: async ({ id, payload }: { id: string; payload: CustomerRequest }) => {
|
||||
const response = await api.put(`/customers/${id}`, payload)
|
||||
const updateIngredient = useMutation({
|
||||
mutationFn: async ({ id, payload }: { id: string; payload: IngredientRequest }) => {
|
||||
const response = await api.put(`/ingredients/${id}`, payload)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Customer updated successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['customers'] })
|
||||
toast.success('Ingredient updated successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['ingredients'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
|
||||
}
|
||||
})
|
||||
|
||||
const deleteCustomer = useMutation({
|
||||
const deleteIngredient = useMutation({
|
||||
mutationFn: async (id: string) => {
|
||||
const response = await api.delete(`/customers/${id}`)
|
||||
const response = await api.delete(`/ingredients/${id}`)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Customer deleted successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['customers'] })
|
||||
toast.success('Ingredient deleted successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['ingredients'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
|
||||
}
|
||||
})
|
||||
|
||||
return { createCustomer, updateCustomer, deleteCustomer }
|
||||
return { createIngredient, updateIngredient, deleteIngredient }
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { ProductSalesReport, SalesReport } from '../../types/services/analytic'
|
||||
import { DashboardReport, PaymentReport, ProductSalesReport, ProfitLossReport, SalesReport } from '../../types/services/analytic'
|
||||
import { api } from '../api'
|
||||
import { formatDateDDMMYYYY } from '../../utils/transform'
|
||||
|
||||
@@ -10,11 +10,11 @@ interface AnalyticQueryParams {
|
||||
|
||||
export function useSalesAnalytics(params: AnalyticQueryParams = {}) {
|
||||
const today = new Date()
|
||||
const sevenDaysAgo = new Date()
|
||||
sevenDaysAgo.setDate(today.getDate() - 30)
|
||||
const monthAgo = new Date()
|
||||
monthAgo.setDate(today.getDate() - 30)
|
||||
|
||||
const defaultDateTo = formatDateDDMMYYYY(today)
|
||||
const defaultDateFrom = formatDateDDMMYYYY(sevenDaysAgo)
|
||||
const defaultDateFrom = formatDateDDMMYYYY(monthAgo)
|
||||
|
||||
const { date_from = defaultDateFrom, date_to = defaultDateTo, ...filters } = params
|
||||
|
||||
@@ -40,11 +40,11 @@ export function useSalesAnalytics(params: AnalyticQueryParams = {}) {
|
||||
|
||||
export function useProductSalesAnalytics(params: AnalyticQueryParams = {}) {
|
||||
const today = new Date()
|
||||
const sevenDaysAgo = new Date()
|
||||
sevenDaysAgo.setDate(today.getDate() - 30)
|
||||
const monthAgo = new Date()
|
||||
monthAgo.setDate(today.getDate() - 30)
|
||||
|
||||
const defaultDateTo = formatDateDDMMYYYY(today)
|
||||
const defaultDateFrom = formatDateDDMMYYYY(sevenDaysAgo)
|
||||
const defaultDateFrom = formatDateDDMMYYYY(monthAgo)
|
||||
|
||||
const { date_from = defaultDateFrom, date_to = defaultDateTo, ...filters } = params
|
||||
|
||||
@@ -67,3 +67,93 @@ export function useProductSalesAnalytics(params: AnalyticQueryParams = {}) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function usePaymentAnalytics(params: AnalyticQueryParams = {}) {
|
||||
const today = new Date()
|
||||
const monthAgo = new Date()
|
||||
monthAgo.setDate(today.getDate() - 30)
|
||||
|
||||
const defaultDateTo = formatDateDDMMYYYY(today)
|
||||
const defaultDateFrom = formatDateDDMMYYYY(monthAgo)
|
||||
|
||||
const { date_from = defaultDateFrom, date_to = defaultDateTo, ...filters } = params
|
||||
|
||||
return useQuery<PaymentReport>({
|
||||
queryKey: ['analytics-payment-methods', { date_from, date_to, ...filters }],
|
||||
queryFn: async () => {
|
||||
const queryParams = new URLSearchParams()
|
||||
|
||||
queryParams.append('date_from', date_from)
|
||||
queryParams.append('date_to', date_to)
|
||||
|
||||
Object.entries(filters).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null && value !== '') {
|
||||
queryParams.append(key, value.toString())
|
||||
}
|
||||
})
|
||||
|
||||
const res = await api.get(`/analytics/payment-methods?${queryParams.toString()}`)
|
||||
return res.data.data
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function useDashboardAnalytics(params: AnalyticQueryParams = {}) {
|
||||
const today = new Date()
|
||||
const monthAgo = new Date()
|
||||
monthAgo.setDate(today.getDate() - 30)
|
||||
|
||||
const defaultDateTo = formatDateDDMMYYYY(today)
|
||||
const defaultDateFrom = formatDateDDMMYYYY(monthAgo)
|
||||
|
||||
const { date_from = defaultDateFrom, date_to = defaultDateTo, ...filters } = params
|
||||
|
||||
return useQuery<DashboardReport>({
|
||||
queryKey: ['analytics-dashboard', { date_from, date_to, ...filters }],
|
||||
queryFn: async () => {
|
||||
const queryParams = new URLSearchParams()
|
||||
|
||||
queryParams.append('date_from', date_from)
|
||||
queryParams.append('date_to', date_to)
|
||||
|
||||
Object.entries(filters).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null && value !== '') {
|
||||
queryParams.append(key, value.toString())
|
||||
}
|
||||
})
|
||||
|
||||
const res = await api.get(`/analytics/dashboard?${queryParams.toString()}`)
|
||||
return res.data.data
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function useProfitLossAnalytics(params: AnalyticQueryParams = {}) {
|
||||
const today = new Date()
|
||||
const monthAgo = new Date()
|
||||
monthAgo.setDate(today.getDate() - 30)
|
||||
|
||||
const defaultDateTo = formatDateDDMMYYYY(today)
|
||||
const defaultDateFrom = formatDateDDMMYYYY(monthAgo)
|
||||
|
||||
const { date_from = defaultDateFrom, date_to = defaultDateTo, ...filters } = params
|
||||
|
||||
return useQuery<ProfitLossReport>({
|
||||
queryKey: ['analytics-profit-loss', { date_from, date_to, ...filters }],
|
||||
queryFn: async () => {
|
||||
const queryParams = new URLSearchParams()
|
||||
|
||||
queryParams.append('date_from', date_from)
|
||||
queryParams.append('date_to', date_to)
|
||||
|
||||
Object.entries(filters).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null && value !== '') {
|
||||
queryParams.append(key, value.toString())
|
||||
}
|
||||
})
|
||||
|
||||
const res = await api.get(`/analytics/profit-loss?${queryParams.toString()}`)
|
||||
return res.data.data
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user