40 lines
834 B
TypeScript
40 lines
834 B
TypeScript
/* eslint-disable canonical/id-match */
|
|
import { getClientWithToken } from '../apollo/client';
|
|
import * as GQL from '../types';
|
|
|
|
const ERRORS = {
|
|
MISSING_TELEGRAM_ID: 'Missing telegram id',
|
|
NOT_FOUND_CUSTOMER: 'Customer not found',
|
|
};
|
|
|
|
type UserProfile = {
|
|
telegramId: number;
|
|
};
|
|
|
|
export class BaseService {
|
|
protected _user: UserProfile;
|
|
|
|
constructor(user: UserProfile) {
|
|
if (!user?.telegramId) {
|
|
throw new Error(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(ERRORS.NOT_FOUND_CUSTOMER);
|
|
|
|
return { customer };
|
|
}
|
|
}
|