vchikalkin 5018560f29 feat(customer): implement banned customer check and enhance customer data handling
- 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.
2025-08-25 19:15:00 +03:00

78 lines
2.1 KiB
TypeScript

import { getClientWithToken } from '../apollo/client';
import { ERRORS } from '../constants/errors';
import * as GQL from '../types';
import { BaseService } from './base';
import { type VariablesOf } from '@graphql-typed-document-node/core';
export class ServicesService extends BaseService {
async createService(variables: VariablesOf<typeof GQL.CreateServiceDocument>) {
const { customer } = await this._getUser();
const { mutate } = await getClientWithToken();
const mutationResult = await mutate({
mutation: GQL.CreateServiceDocument,
variables: {
...variables,
data: {
...variables.data,
master: customer?.documentId,
},
},
});
const error = mutationResult.errors?.at(0);
if (error) throw new Error(error.message);
return mutationResult.data;
}
async getService(variables: VariablesOf<typeof GQL.GetServiceDocument>) {
const { query } = await getClientWithToken();
const result = await query({
query: GQL.GetServiceDocument,
variables,
});
return result.data;
}
async getServices(variables: VariablesOf<typeof GQL.GetServicesDocument>) {
const { query } = await getClientWithToken();
const result = await query({
query: GQL.GetServicesDocument,
variables,
});
return result.data;
}
async updateService(variables: VariablesOf<typeof GQL.UpdateServiceDocument>) {
await this.checkPermission(variables);
const { mutate } = await getClientWithToken();
const mutationResult = await mutate({
mutation: GQL.UpdateServiceDocument,
variables,
});
const error = mutationResult.errors?.at(0);
if (error) throw new Error(error.message);
return mutationResult.data;
}
private async checkPermission(
variables: Pick<VariablesOf<typeof GQL.GetServiceDocument>, 'documentId'>,
) {
const { customer } = await this._getUser();
const { service } = await this.getService({ documentId: variables.documentId });
if (service?.master?.documentId !== customer?.documentId) throw new Error(ERRORS.NO_PERMISSION);
}
}