60 lines
1.6 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';
const DEFAULT_CUSTOMER_ROLE = GQL.Enum_Customer_Role.Client;
export class RegistrationService {
async _NOCACHE_GetCustomer(variables: VariablesOf<typeof GQL._Nocache_GetCustomerDocument>) {
const { query } = await getClientWithToken();
const result = await query({
query: GQL._Nocache_GetCustomerDocument,
variables,
});
const customer = result.data.customers.at(0);
return { customer };
}
async createCustomer(variables: VariablesOf<typeof GQL.CreateCustomerDocument>) {
const { mutate } = await getClientWithToken();
const mutationResult = await mutate({
mutation: GQL.CreateCustomerDocument,
variables: {
...variables,
data: {
...variables.data,
role: DEFAULT_CUSTOMER_ROLE,
},
},
});
const error = mutationResult.errors?.at(0);
if (error) throw new Error(error.message);
return mutationResult.data;
}
async updateCustomer(variables: VariablesOf<typeof GQL.UpdateCustomerDocument>) {
if (variables.data.bannedUntil || variables.data.phone) {
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;
}
}