- Introduced getCustomers action and corresponding server method to fetch customer data with pagination and sorting. - Updated hooks to support infinite querying of customers, improving data handling in components. - Refactored ContactsList and related components to utilize the new customer fetching logic, enhancing user experience. - Adjusted filter labels in dropdowns for better clarity and user understanding.
137 lines
3.6 KiB
TypeScript
137 lines
3.6 KiB
TypeScript
import { getClientWithToken } from '../apollo/client';
|
|
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 getClientWithToken();
|
|
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>) {
|
|
await this.checkIsBanned();
|
|
|
|
const { query } = await getClientWithToken();
|
|
|
|
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 getClientWithToken();
|
|
|
|
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 getClientWithToken();
|
|
|
|
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 getClientWithToken();
|
|
|
|
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 getClientWithToken();
|
|
|
|
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;
|
|
}
|
|
}
|