From 49715531d95093ae1e54dcbbd3428e2437960a9a Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Sat, 17 Feb 2024 16:57:15 +0300 Subject: [PATCH] apps/web: use dates w/ zero time --- apps/web/process/add-product/reactions.ts | 13 +++++-------- apps/web/process/bonuses/lib/helper.ts | 4 ++-- apps/web/process/bonuses/reactions/common.ts | 4 ---- apps/web/process/configurator/lib/helper.ts | 5 +++-- .../process/configurator/reactions/filters.ts | 7 ++----- .../web/process/configurator/reactions/values.ts | 4 ---- apps/web/process/fingap/lib/helper.ts | 4 ++-- apps/web/process/fingap/reactions/common.ts | 4 ---- apps/web/process/gibdd/reactions.ts | 8 ++------ apps/web/process/hooks/init/get-initial-data.ts | 4 ++-- apps/web/process/hooks/init/get-main-data.js | 6 +----- .../process/leasing-without-kasko/reactions.ts | 7 ++----- .../supplier-agent/lib/create-reactions.ts | 7 ++----- .../process/supplier-agent/reactions/agents.ts | 4 ---- .../process/supplier-agent/reactions/supplier.ts | 4 ---- apps/web/process/supplier-agent/validation.ts | 7 ++----- apps/web/server/routers/calculate/lib/request.ts | 3 ++- apps/web/utils/currency.ts | 7 ++----- apps/web/utils/date.ts | 16 ++++++++++++++++ 19 files changed, 45 insertions(+), 73 deletions(-) create mode 100644 apps/web/utils/date.ts diff --git a/apps/web/process/add-product/reactions.ts b/apps/web/process/add-product/reactions.ts index 80003ad..37a9087 100644 --- a/apps/web/process/add-product/reactions.ts +++ b/apps/web/process/add-product/reactions.ts @@ -4,14 +4,11 @@ import type { ProcessContext } from '../types'; import { createValidationSchema } from './validation'; import { selectRequirementTelematic } from '@/config/default-options'; import * as CRMTypes from '@/graphql/crm.types'; +import { getCurrentISODate } from '@/utils/date'; import { normalizeOptions } from '@/utils/entity'; import { debouncedReaction } from '@/utils/mobx'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; import { reaction, toJS } from 'mobx'; -dayjs.extend(utc); - export default function reactions({ store, apolloClient }: ProcessContext) { const { $calculation, $tables } = store; @@ -23,7 +20,7 @@ export default function reactions({ store, apolloClient }: ProcessContext) { return; } - const currentDate = dayjs().utc(false).toISOString(); + const currentDate = getCurrentISODate(); const { data: { evo_addproduct_types }, @@ -80,7 +77,7 @@ export default function reactions({ store, apolloClient }: ProcessContext) { reaction( () => $calculation.$values.getValues(['leasingPeriod', 'leaseObjectType']), async ({ leasingPeriod, leaseObjectType }) => { - const currentDate = dayjs().utc(false).toISOString(); + const currentDate = getCurrentISODate(); const { data: { evo_addproduct_types }, @@ -164,7 +161,7 @@ export default function reactions({ store, apolloClient }: ProcessContext) { leasingPeriod, plPriceRub, }) => { - const currentDate = dayjs().utc(false).toISOString(); + const currentDate = getCurrentISODate(); const { data: { evo_addproduct_types }, @@ -277,7 +274,7 @@ export default function reactions({ store, apolloClient }: ProcessContext) { leaseObjectType: leaseObjectTypeId, engineType, }) => { - const currentDate = dayjs().utc(false).toISOString(); + const currentDate = getCurrentISODate(); const { data: { evo_addproduct_types: trackerTypes }, } = await apolloClient.query({ diff --git a/apps/web/process/bonuses/lib/helper.ts b/apps/web/process/bonuses/lib/helper.ts index 902cc99..ae1c09e 100644 --- a/apps/web/process/bonuses/lib/helper.ts +++ b/apps/web/process/bonuses/lib/helper.ts @@ -1,7 +1,7 @@ import type { ValidationContext } from '../../types'; import type { ElementsTypes } from '@/Components/Calculation/config/map/values'; import * as CRMTypes from '@/graphql/crm.types'; -import dayjs from 'dayjs'; +import { getCurrentISODate } from '@/utils/date'; export type ProductId = ElementsTypes['selectProduct']; @@ -25,7 +25,7 @@ export default function helper({ apolloClient, user }: ValidationContext) { return null; } - const currentDate = dayjs().utc(false).toISOString(); + const currentDate = getCurrentISODate(); const { data: { evo_coefficients }, diff --git a/apps/web/process/bonuses/reactions/common.ts b/apps/web/process/bonuses/reactions/common.ts index dae1f5e..da6f546 100644 --- a/apps/web/process/bonuses/reactions/common.ts +++ b/apps/web/process/bonuses/reactions/common.ts @@ -2,13 +2,9 @@ import type { ProcessContext } from '../../types'; import helper from '../lib/helper'; import * as CRMTypes from '@/graphql/crm.types'; import { disposableReaction } from '@/utils/mobx'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; import { reaction } from 'mobx'; import { round } from 'tools'; -dayjs.extend(utc); - export default function reactions(context: ProcessContext) { const { store, apolloClient } = context; const { $calculation, $process } = store; diff --git a/apps/web/process/configurator/lib/helper.ts b/apps/web/process/configurator/lib/helper.ts index f773a2d..e8b7307 100644 --- a/apps/web/process/configurator/lib/helper.ts +++ b/apps/web/process/configurator/lib/helper.ts @@ -3,6 +3,7 @@ import defaultValues from '@/config/default-values'; import * as CRMTypes from '@/graphql/crm.types'; import type { ProcessContext } from '@/process/types'; import type { CalculationValues } from '@/stores/calculation/values/types'; +import { getCurrentISODate } from '@/utils/date'; import dayjs from 'dayjs'; import { first, sort } from 'radash'; @@ -79,7 +80,7 @@ export default function helper({ apolloClient }: Pick $calculation.element('selectQuote').getValue(), async (quoteId) => { - const currentDate = dayjs().utc(false).toISOString(); + const currentDate = getCurrentISODate(); let { data: { evo_baseproducts }, diff --git a/apps/web/process/configurator/reactions/values.ts b/apps/web/process/configurator/reactions/values.ts index a7b22f9..bc8f10b 100644 --- a/apps/web/process/configurator/reactions/values.ts +++ b/apps/web/process/configurator/reactions/values.ts @@ -4,12 +4,8 @@ import * as CRMTypes from '@/graphql/crm.types'; import type { ProcessContext } from '@/process/types'; import { normalizeOptions } from '@/utils/entity'; import { disposableDebouncedReaction, disposableReaction } from '@/utils/mobx'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; import { comparer, reaction } from 'mobx'; -dayjs.extend(utc); - export default function valuesReactions({ store, apolloClient, trpcClient }: ProcessContext) { let abortController = new AbortController(); const { $calculation, $process } = store; diff --git a/apps/web/process/fingap/lib/helper.ts b/apps/web/process/fingap/lib/helper.ts index db3156a..a6ddb66 100644 --- a/apps/web/process/fingap/lib/helper.ts +++ b/apps/web/process/fingap/lib/helper.ts @@ -6,8 +6,8 @@ import { STALE_TIME } from '@/constants/request'; import * as CRMTypes from '@/graphql/crm.types'; import type { ProcessContext } from '@/process/types'; import type { CalculationValues } from '@/stores/calculation/values/types'; +import { getCurrentISODate } from '@/utils/date'; import type { QueryFunctionContext } from '@tanstack/react-query'; -import dayjs from 'dayjs'; import { flatten } from 'tools/object'; type GetFingapRisksInputValues = Pick< @@ -38,7 +38,7 @@ export default function helper({ } = await apolloClient.query({ query: CRMTypes.GetAddproductTypesDocument, variables: { - currentDate: dayjs().utc(false).toISOString(), + currentDate: getCurrentISODate(), }, }); diff --git a/apps/web/process/fingap/reactions/common.ts b/apps/web/process/fingap/reactions/common.ts index 5012196..11d2fe2 100644 --- a/apps/web/process/fingap/reactions/common.ts +++ b/apps/web/process/fingap/reactions/common.ts @@ -1,12 +1,8 @@ import helper from '../lib/helper'; import type { ProcessContext } from '@/process/types'; import { disposableReaction } from '@/utils/mobx'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; import { comparer, reaction } from 'mobx'; -dayjs.extend(utc); - export default function reactions({ store, apolloClient, queryClient }: ProcessContext) { const { getFingapRisks } = helper({ apolloClient, queryClient }); diff --git a/apps/web/process/gibdd/reactions.ts b/apps/web/process/gibdd/reactions.ts index b41a24e..7bd56af 100644 --- a/apps/web/process/gibdd/reactions.ts +++ b/apps/web/process/gibdd/reactions.ts @@ -6,14 +6,12 @@ import { getTransTax } from '@/api/1c/query'; import { selectObjectCategoryTax } from '@/config/default-options'; import { STALE_TIME } from '@/constants/request'; import * as CRMTypes from '@/graphql/crm.types'; +import { getCurrentISODate } from '@/utils/date'; import { normalizeOptions } from '@/utils/entity'; import { disposableReaction } from '@/utils/mobx'; import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; import { reaction } from 'mobx'; -dayjs.extend(utc); - export function common({ store, apolloClient, queryClient }: ProcessContext) { const { $calculation, $process } = store; @@ -239,8 +237,6 @@ export function common({ store, apolloClient, queryClient }: ProcessContext) { leaseObjectType, // eslint-disable-next-line sonarjs/cognitive-complexity }) => { - const currentDate = dayjs().utc(false).toISOString(); - let evo_region: CRMTypes.GetRegionQuery['evo_region']; if (objectRegionRegistrationId) { const { data } = await apolloClient.query({ @@ -254,7 +250,7 @@ export function common({ store, apolloClient, queryClient }: ProcessContext) { data: { evo_addproduct_types }, } = await apolloClient.query({ query: CRMTypes.GetRegistrationTypesDocument, - variables: { currentDate }, + variables: { currentDate: getCurrentISODate() }, }); const options = evo_addproduct_types?.filter((x) => { diff --git a/apps/web/process/hooks/init/get-initial-data.ts b/apps/web/process/hooks/init/get-initial-data.ts index ad75012..e929acb 100644 --- a/apps/web/process/hooks/init/get-initial-data.ts +++ b/apps/web/process/hooks/init/get-initial-data.ts @@ -1,8 +1,8 @@ import type { User } from '@/api/user/types'; import { crmTools } from '@/graphql/crm.tools'; import * as CRMTypes from '@/graphql/crm.types'; +import { getCurrentISODate } from '@/utils/date'; import type { ApolloClient } from '@apollo/client'; -import dayjs from 'dayjs'; export async function getInitialData({ query }: ApolloClient, user: User) { const leadsQuery = query({ @@ -17,7 +17,7 @@ export async function getInitialData({ query }: ApolloClient, user: User const productsQuery = query({ fetchPolicy: 'network-only', query: CRMTypes.GetProductsDocument, - variables: { currentDate: dayjs().utc(false).toISOString() }, + variables: { currentDate: getCurrentISODate() }, }); const systemUserQuery = query({ fetchPolicy: 'network-only', diff --git a/apps/web/process/hooks/init/get-main-data.js b/apps/web/process/hooks/init/get-main-data.js index fa00b16..ffd71c7 100644 --- a/apps/web/process/hooks/init/get-main-data.js +++ b/apps/web/process/hooks/init/get-main-data.js @@ -3,13 +3,9 @@ import * as CRMTypes from '@/graphql/crm.types'; import { useStore } from '@/stores/hooks'; import { normalizeOptions } from '@/utils/entity'; import { useApolloClient } from '@apollo/client'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; import { useEffect } from 'react'; -dayjs.extend(utc); - -const currentDate = dayjs().utc(false).toISOString(); +const currentDate = getCurrentISODate(); /** * diff --git a/apps/web/process/leasing-without-kasko/reactions.ts b/apps/web/process/leasing-without-kasko/reactions.ts index b92d78d..35f33c0 100644 --- a/apps/web/process/leasing-without-kasko/reactions.ts +++ b/apps/web/process/leasing-without-kasko/reactions.ts @@ -3,14 +3,11 @@ import { notification } from '@/Components/Common/Notification'; import * as CRMTypes from '@/graphql/crm.types'; import type { ProcessContext } from '@/process/types'; +import { getCurrentISODate } from '@/utils/date'; import { normalizeOptions } from '@/utils/entity'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; import { reaction } from 'mobx'; import { uid } from 'radash'; -dayjs.extend(utc); - const NOTIFICATION_KEY = uid(7); export function common({ store, apolloClient }: ProcessContext) { @@ -67,7 +64,7 @@ export function common({ store, apolloClient }: ProcessContext) { firstPaymentPerc, model: modelId, }) => { - const currentDate = dayjs().utc(false).toISOString(); + const currentDate = getCurrentISODate(); const { data: { evo_addproduct_types }, diff --git a/apps/web/process/supplier-agent/lib/create-reactions.ts b/apps/web/process/supplier-agent/lib/create-reactions.ts index 9e8076d..2856bc6 100644 --- a/apps/web/process/supplier-agent/lib/create-reactions.ts +++ b/apps/web/process/supplier-agent/lib/create-reactions.ts @@ -1,15 +1,12 @@ import type { AgentsFields, AgentsRewardConditionsFields, AgentsSumFields } from './types'; import * as CRMTypes from '@/graphql/crm.types'; import type RootStore from '@/stores/root'; +import { getCurrentISODate } from '@/utils/date'; import { normalizeOptions } from '@/utils/entity'; import { disposableReaction } from '@/utils/mobx'; import type { ApolloClient } from '@apollo/client'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; import { comparer, reaction } from 'mobx'; -dayjs.extend(utc); - export function fillAgentRewardReaction( store: RootStore, apolloClient: ApolloClient, @@ -44,7 +41,7 @@ export function fillAgentRewardReaction( query: CRMTypes.GetRewardConditionsDocument, variables: { agentid, - currentDate: dayjs().utc(false).toISOString(), + currentDate: getCurrentISODate(), }, }); diff --git a/apps/web/process/supplier-agent/reactions/agents.ts b/apps/web/process/supplier-agent/reactions/agents.ts index f0a661f..a3f0bea 100644 --- a/apps/web/process/supplier-agent/reactions/agents.ts +++ b/apps/web/process/supplier-agent/reactions/agents.ts @@ -4,15 +4,11 @@ import * as fillAgentsFromLead from '../lib/fill-agents-from-lead'; import * as CRMTypes from '@/graphql/crm.types'; import type { ProcessContext } from '@/process/types'; import { disposableReaction } from '@/utils/mobx'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; import { reaction } from 'mobx'; const { fillIndAgent, fillCalcBroker, fillCalcDoubleAgent, fillFinDepartment } = fillAgentsFromLead; const { fillAgentRewardReaction, fillAgentRewardSummReaction } = createReactions; -dayjs.extend(utc); - class Helper { public enabled: boolean; /** diff --git a/apps/web/process/supplier-agent/reactions/supplier.ts b/apps/web/process/supplier-agent/reactions/supplier.ts index 37a92df..85b1424 100644 --- a/apps/web/process/supplier-agent/reactions/supplier.ts +++ b/apps/web/process/supplier-agent/reactions/supplier.ts @@ -2,13 +2,9 @@ import * as createReactions from '../lib/create-reactions'; import * as CRMTypes from '@/graphql/crm.types'; import type { ProcessContext } from '@/process/types'; import { normalizeOptions } from '@/utils/entity'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; import { reaction } from 'mobx'; import { sift } from 'radash'; -dayjs.extend(utc); - export function common({ store, apolloClient }: ProcessContext) { const { $calculation, $process } = store; diff --git a/apps/web/process/supplier-agent/validation.ts b/apps/web/process/supplier-agent/validation.ts index 08c6123..0c94122 100644 --- a/apps/web/process/supplier-agent/validation.ts +++ b/apps/web/process/supplier-agent/validation.ts @@ -4,14 +4,11 @@ import type { ValidationContext } from '../types'; import type { Elements } from '@/Components/Calculation/config/map/values'; import ValuesSchema from '@/config/schema/values'; import * as CRMTypes from '@/graphql/crm.types'; +import { getCurrentISODate } from '@/utils/date'; import { normalizeOptions } from '@/utils/entity'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; import type { RefinementCtx } from 'zod'; import { z } from 'zod'; -dayjs.extend(utc); - function helper({ apolloClient, ctx }: ValidationContext & { ctx: RefinementCtx }) { return { async validateRewardSum({ @@ -32,7 +29,7 @@ function helper({ apolloClient, ctx }: ValidationContext & { ctx: RefinementCtx query: CRMTypes.GetRewardConditionsDocument, variables: { agentid, - currentDate: dayjs().utc(false).toISOString(), + currentDate: getCurrentISODate(), }, }); diff --git a/apps/web/server/routers/calculate/lib/request.ts b/apps/web/server/routers/calculate/lib/request.ts index 12abd10..3d261ab 100644 --- a/apps/web/server/routers/calculate/lib/request.ts +++ b/apps/web/server/routers/calculate/lib/request.ts @@ -5,6 +5,7 @@ import { ESN, NSIB_MAX, VAT } from '@/constants/values'; import * as CRMTypes from '@/graphql/crm.types'; import helper from '@/process/calculate/lib/helper'; import { createCurrencyUtility } from '@/utils/currency'; +import { getCurrentISODate } from '@/utils/date'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import { min, sum } from 'radash'; @@ -126,7 +127,7 @@ export async function createRequestData({ await apolloClient.query({ query: CRMTypes.GetCoefficientsDocument, variables: { - currentDate: currentDate.toISOString(), + currentDate: getCurrentISODate(), }, }) ).data.evo_coefficients diff --git a/apps/web/utils/currency.ts b/apps/web/utils/currency.ts index 9f5a887..68f6e20 100644 --- a/apps/web/utils/currency.ts +++ b/apps/web/utils/currency.ts @@ -1,9 +1,6 @@ import * as CRMTypes from '@/graphql/crm.types'; import type { ApolloClient } from '@apollo/client'; -import dayjs from 'dayjs'; -import utc from 'dayjs/plugin/utc'; - -dayjs.extend(utc); +import { getCurrentISODate } from './date'; type Context = { apolloClient: ApolloClient; @@ -36,7 +33,7 @@ export function createCurrencyUtility({ apolloClient }: Context) { fetchPolicy: 'network-only', query: CRMTypes.GetCurrencyChangesDocument, variables: { - currentDate: dayjs().utc(false).toISOString(), + currentDate: getCurrentISODate(), }, }); diff --git a/apps/web/utils/date.ts b/apps/web/utils/date.ts new file mode 100644 index 0000000..bd43afb --- /dev/null +++ b/apps/web/utils/date.ts @@ -0,0 +1,16 @@ +import dayjs from 'dayjs'; +import utc from 'dayjs/plugin/utc'; + +dayjs.extend(utc); + +function _currentDate() { + return dayjs().utc(false).hour(0).minute(0).second(0).millisecond(0); +} + +export function getCurrentISODate() { + return _currentDate().toISOString(); +} + +export function getCurrentDate() { + return _currentDate().toDate(); +}