Vlad Chikalkin 2836153887
Feature/caching (#117)
* Refactor GraphQL client usage in services to improve consistency

- Replaced direct calls to `getClientWithToken` with a new method `getGraphQLClient` across multiple services, ensuring a unified approach to obtaining the GraphQL client.
- Updated the `BaseService` to manage the GraphQL client instance, enhancing performance by reusing the client when available.

* Refactor UpdateProfile component to use useClientOnce for initial profile update

- Replaced useEffect with useClientOnce to handle the first login profile update more efficiently.
- Removed local state management for hasUpdated, simplifying the component logic.
- Updated localStorage handling to ensure the profile update occurs only on the first login.

* Refactor Apollo Client setup to improve modularity and maintainability

- Introduced a new `createLink` function to encapsulate the link creation logic for Apollo Client.
- Updated `createApolloClient` to utilize the new `createLink` function, enhancing code organization.
- Simplified the handling of authorization headers by moving it to the link configuration.

* Add cache-proxy application with initial setup and configuration

- Created a new cache-proxy application using NestJS, including essential files such as Dockerfile, .gitignore, and .eslintrc.js.
- Implemented core application structure with AppModule, ProxyModule, and ProxyController for handling GraphQL requests.
- Configured caching with Redis and established environment variable management using Zod for validation.
- Added utility functions for query handling and time management, enhancing the application's functionality.
- Included README.md for project documentation and setup instructions.

* Add cache-proxy service to Docker configurations and update deployment workflow</message>

<message>
- Introduced a new cache-proxy service in both `docker-compose.dev.yml` and `docker-compose.yml`, with dependencies on Redis and integration into the web and bot services.
- Updated GitHub Actions workflow to include build and push steps for the cache-proxy image, ensuring it is deployed alongside web and bot services.
- Modified environment variable management to accommodate the cache-proxy, enhancing the overall deployment process.
- Adjusted GraphQL cached URL to point to the cache-proxy service for improved request handling.

* Add health check endpoint and controller to cache-proxy service

- Implemented a new HealthController in the cache-proxy application to provide a health check endpoint at `/api/health`, returning a simple status response.
- Updated the AppModule to include the HealthController, ensuring it is registered within the application.
- Configured a health check in the Docker Compose file for the cache-proxy service, allowing for automated health monitoring.

* Update proxy controller and environment variable for cache-proxy service

- Changed the route prefix of the ProxyController from `/proxy` to `/api` to align with the new API structure.
- Updated the default value of the `URL_GRAPHQL_CACHED` environment variable to reflect the new route, ensuring proper integration with the GraphQL service.

* Update cache proxy configuration for query time-to-live settings

- Increased the time-to-live for `GetCustomer`, `GetOrder`, `GetService`, and `GetSlot` queries to 24 hours, enhancing cache efficiency and performance.
- Maintained the existing setting for `GetSubscriptions`, which remains at 12 hours.

* Enhance subscription management and configuration settings

- Added new query time-to-live settings for `GetSlotsOrders`, `GetSubscriptionPrices`, and `GetSubscriptions` to improve caching strategy.
- Implemented `hasTrialSubscription` method in `SubscriptionsService` to check for trial subscriptions based on user history, enhancing subscription management capabilities.
- Updated GraphQL operations to reflect the change from `getSubscriptionSettings` to `GetSubscriptionSettings`, ensuring consistency in naming conventions.

* fix build

* Refactor subscription settings naming

- Updated the naming of `getSubscriptionSettings` to `GetSubscriptionSettings` in the cache-proxy configuration for consistency with other settings.
2025-10-06 23:26:03 +03:00

134 lines
3.5 KiB
TypeScript

import { ERRORS } from '../constants/errors';
import * as GQL from '../types';
import { BaseService } from './base';
import { type VariablesOf } from '@graphql-typed-document-node/core';
const DEFAULT_CUSTOMERS_SORT = ['name:asc'];
export class CustomersService extends BaseService {
async addInvitedBy(variables: VariablesOf<typeof GQL.UpdateCustomerDocument>) {
await this.checkIsBanned();
const newInvitedByIds = variables.data.invitedBy;
// Проверяем, что пользователь не пытается изменить поле bannedUntil
if (variables.data.bannedUntil !== undefined) {
throw new Error(ERRORS.NO_PERMISSION);
}
const { mutate, query } = await this.getGraphQLClient();
const getInvitedByResult = await query({
query: GQL.GetInvitedByDocument,
variables,
});
const existingInvitedByIds = getInvitedByResult?.data?.customers
?.at(0)
?.invitedBy.map((x) => x?.documentId);
const newInvitedByIdsList = [
...new Set([...(existingInvitedByIds || []), ...(newInvitedByIds || [])]),
];
const mutationResult = await mutate({
mutation: GQL.UpdateCustomerDocument,
variables: {
data: { invitedBy: newInvitedByIdsList },
documentId: variables.documentId,
},
});
const error = mutationResult.errors?.at(0);
if (error) throw new Error(error.message);
return mutationResult.data;
}
async getCustomer(variables: VariablesOf<typeof GQL.GetCustomerDocument>) {
const { query } = await this.getGraphQLClient();
const result = await query({
query: GQL.GetCustomerDocument,
variables,
});
const customer = result.data.customers.at(0);
return { customer };
}
async getCustomers(variables: VariablesOf<typeof GQL.GetCustomersDocument>) {
await this.checkIsBanned();
const { query } = await this.getGraphQLClient();
const result = await query({
query: GQL.GetCustomersDocument,
variables: {
sort: DEFAULT_CUSTOMERS_SORT,
...variables,
},
});
return result.data;
}
async getInvited(variables?: VariablesOf<typeof GQL.GetInvitedDocument>) {
await this.checkIsBanned();
const { query } = await this.getGraphQLClient();
const result = await query({
query: GQL.GetInvitedDocument,
variables,
});
const customer = result.data.customers.at(0);
return customer;
}
async getInvitedBy(variables?: VariablesOf<typeof GQL.GetInvitedByDocument>) {
await this.checkIsBanned();
const { query } = await this.getGraphQLClient();
const result = await query({
query: GQL.GetInvitedByDocument,
variables,
});
const customer = result.data.customers.at(0);
return customer;
}
async updateCustomer(
variables: Omit<VariablesOf<typeof GQL.UpdateCustomerDocument>, 'documentId'>,
) {
await this.checkIsBanned();
const { customer } = await this._getUser();
// Пров еряем, что пользователь не пытается изменить поле bannedUntil
if (variables.data.bannedUntil !== undefined) {
throw new Error(ERRORS.NO_PERMISSION);
}
const { mutate } = await this.getGraphQLClient();
const mutationResult = await mutate({
mutation: GQL.UpdateCustomerDocument,
variables: {
data: variables.data,
documentId: customer.documentId,
},
});
const error = mutationResult.errors?.at(0);
if (error) throw new Error(error.message);
return mutationResult.data;
}
}