63 lines
1.4 KiB
TypeScript
63 lines
1.4 KiB
TypeScript
/* eslint-disable canonical/id-match */
|
||
import { getClientWithToken } from '../apollo/client';
|
||
import { ERRORS } from '../constants/errors';
|
||
import * as GQL from '../types';
|
||
import { isCustomerBanned } from '@repo/utils/customer';
|
||
|
||
const BASE_ERRORS = {
|
||
MISSING_TELEGRAM_ID: 'Не указан Telegram ID',
|
||
NOT_FOUND_CUSTOMER: 'Пользователь не найден',
|
||
} as const;
|
||
|
||
type UserProfile = {
|
||
telegramId: number;
|
||
};
|
||
|
||
export class BaseService {
|
||
protected _user: UserProfile;
|
||
|
||
constructor(user: UserProfile) {
|
||
if (!user?.telegramId) {
|
||
throw new Error(BASE_ERRORS.MISSING_TELEGRAM_ID);
|
||
}
|
||
|
||
this._user = user;
|
||
}
|
||
|
||
protected async _getUser() {
|
||
const { query } = await getClientWithToken();
|
||
|
||
const result = await query({
|
||
query: GQL.GetCustomerDocument,
|
||
variables: this._user,
|
||
});
|
||
|
||
const customer = result.data.customers.at(0);
|
||
|
||
if (!customer) throw new Error(BASE_ERRORS.NOT_FOUND_CUSTOMER);
|
||
|
||
if (isCustomerBanned(customer)) {
|
||
throw new Error(ERRORS.NO_PERMISSION);
|
||
}
|
||
|
||
return { customer };
|
||
}
|
||
|
||
protected async checkIsBanned() {
|
||
const { query } = await getClientWithToken();
|
||
|
||
const result = await query({
|
||
query: GQL.GetCustomerDocument,
|
||
variables: this._user,
|
||
});
|
||
|
||
const customer = result.data.customers.at(0);
|
||
|
||
if (customer && isCustomerBanned(customer)) {
|
||
throw new Error(ERRORS.NO_PERMISSION);
|
||
}
|
||
|
||
return { customer };
|
||
}
|
||
}
|