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) { await this.checkIsBanned(); const { customer } = await this._getUser(); const { mutate } = await this.getGraphQLClient(); 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) { await this.checkIsBanned(); const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetServiceDocument, variables, }); return result.data; } async getServices(variables: VariablesOf) { await this.checkIsBanned(); const { query } = await this.getGraphQLClient(); const result = await query({ query: GQL.GetServicesDocument, variables, }); return result.data; } async updateService(variables: VariablesOf) { await this.checkIsBanned(); await this.checkPermission(variables); const { mutate } = await this.getGraphQLClient(); 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, '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); } }