65 lines
1.6 KiB
TypeScript

'use server';
import { getClientWithToken } from '../apollo/client';
import * as GQL from '../types';
export async function createUser(variables: GQL.CreateUserMutationVariables) {
const { query, mutate } = await getClientWithToken();
const res = await query({
query: GQL.GetUserByPhoneDocument,
variables,
});
const customer = res?.data?.customers?.at(0);
if (customer?.phone === variables.phone) {
return mutate({
mutation: GQL.UpdateCustomerProfileDocument,
variables: {
documentId: customer.documentId,
data: variables,
},
});
}
return mutate({
mutation: GQL.CreateUserDocument,
variables,
});
}
export async function createClient(variables: GQL.CreateClientMutationVariables) {
const { mutate } = await getClientWithToken();
const response = mutate({
mutation: GQL.CreateClientDocument,
variables,
}).catch((error) => {
const isContactAlreadyExists = JSON.stringify(error).includes('This attribute must be unique');
if (isContactAlreadyExists) throw new Error('Contact already exists');
throw error;
});
return response;
}
export async function getCustomer(variables: GQL.GetCustomerQueryVariables) {
const { query } = await getClientWithToken();
return query({
query: GQL.GetCustomerDocument,
variables,
});
}
export async function updateCustomerProfile(variables: GQL.UpdateCustomerProfileMutationVariables) {
const { mutate } = await getClientWithToken();
return mutate({
mutation: GQL.UpdateCustomerProfileDocument,
variables,
});
}