82 lines
2.3 KiB
TypeScript
82 lines
2.3 KiB
TypeScript
import { getClientWithToken } from '../apollo/client';
|
|
import * as GQL from '../types';
|
|
import { BaseService } from './base';
|
|
import { CustomersService } from './customers';
|
|
import { type VariablesOf } from '@graphql-typed-document-node/core';
|
|
|
|
export class ServicesService extends BaseService {
|
|
async createService(variables: VariablesOf<typeof GQL.CreateServiceDocument>) {
|
|
const customerService = new CustomersService(this.customer);
|
|
|
|
const { customer } = await customerService.getCustomer(this.customer);
|
|
|
|
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 customerService = new CustomersService(this.customer);
|
|
|
|
const { customer } = await customerService.getCustomer(this.customer);
|
|
|
|
const { service } = await this.getService({ documentId: variables.documentId });
|
|
|
|
if (service?.master?.documentId !== customer?.documentId) throw new Error('No permission');
|
|
}
|
|
}
|