- Added a comprehensive ban checking system to prevent access for banned users at multiple levels, including database, API, and client-side. - Introduced `bannedUntil` field in the customer model to manage temporary and permanent bans effectively. - Enhanced `BaseService` and various service classes to include ban checks, ensuring that banned users cannot perform actions or access data. - Updated error handling to provide consistent feedback for banned users across the application. - Improved user experience with a dedicated ban check component and a user-friendly ban notification page.
87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
import { getClientWithToken } from '../apollo/client';
|
|
import { ERRORS } from '../constants/errors';
|
|
import * as GQL from '../types';
|
|
import { type VariablesOf } from '@graphql-typed-document-node/core';
|
|
import { isCustomerBanned } from '@repo/utils/customer';
|
|
|
|
export class RegistrationService {
|
|
async createCustomer(variables: VariablesOf<typeof GQL.CreateCustomerDocument>) {
|
|
const { mutate } = await getClientWithToken();
|
|
|
|
const mutationResult = await mutate({
|
|
mutation: GQL.CreateCustomerDocument,
|
|
variables,
|
|
});
|
|
|
|
const error = mutationResult.errors?.at(0);
|
|
if (error) throw new Error(error.message);
|
|
|
|
return mutationResult.data;
|
|
}
|
|
|
|
async getCustomer(variables: VariablesOf<typeof GQL.GetCustomerDocument>) {
|
|
if (variables.telegramId) {
|
|
await this.checkBanStatus(variables.telegramId);
|
|
}
|
|
|
|
const { query } = await getClientWithToken();
|
|
|
|
const result = await query({
|
|
query: GQL.GetCustomerDocument,
|
|
variables,
|
|
});
|
|
|
|
const customer = result.data.customers.at(0);
|
|
|
|
return { customer };
|
|
}
|
|
|
|
async updateCustomer(variables: VariablesOf<typeof GQL.UpdateCustomerDocument>) {
|
|
// Проверяем бан для существующего пользователя
|
|
if (variables.documentId) {
|
|
const { query } = await getClientWithToken();
|
|
const result = await query({
|
|
query: GQL.GetCustomerDocument,
|
|
variables: { documentId: variables.documentId },
|
|
});
|
|
const customer = result.data.customers.at(0);
|
|
if (customer && isCustomerBanned(customer)) {
|
|
throw new Error(ERRORS.NO_PERMISSION);
|
|
}
|
|
}
|
|
|
|
if (variables.data.bannedUntil) {
|
|
throw new Error(ERRORS.NO_PERMISSION);
|
|
}
|
|
|
|
const { mutate } = await getClientWithToken();
|
|
|
|
const mutationResult = await mutate({
|
|
mutation: GQL.UpdateCustomerDocument,
|
|
variables,
|
|
});
|
|
|
|
const error = mutationResult.errors?.at(0);
|
|
if (error) throw new Error(error.message);
|
|
|
|
return mutationResult.data;
|
|
}
|
|
|
|
private async checkBanStatus(telegramId: number) {
|
|
const { query } = await getClientWithToken();
|
|
|
|
const result = await query({
|
|
query: GQL.GetCustomerDocument,
|
|
variables: { telegramId },
|
|
});
|
|
|
|
const customer = result.data.customers.at(0);
|
|
|
|
if (customer && isCustomerBanned(customer)) {
|
|
throw new Error(ERRORS.NO_PERMISSION);
|
|
}
|
|
|
|
return { customer };
|
|
}
|
|
}
|