- Added `isCustomerBanned` function to determine if a customer is banned based on the `bannedUntil` field. - Updated the `BaseService` to throw an error if a banned customer attempts to access certain functionalities. - Enhanced the GraphQL operations to include the `bannedUntil` field in customer queries and mutations, improving data integrity and user experience. - Integrated the `CheckBanned` component in the layout to manage banned customer states effectively.
52 lines
1.4 KiB
TypeScript
52 lines
1.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';
|
|
|
|
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>) {
|
|
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.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;
|
|
}
|
|
}
|