82 lines
1.9 KiB
TypeScript
82 lines
1.9 KiB
TypeScript
'use server';
|
|
import { getClientWithToken } from '../apollo/client';
|
|
import * as GQL from '../types';
|
|
|
|
export async function createOrUpdateUser(variables: GQL.CreateUserMutationVariables) {
|
|
const { query, mutate } = await getClientWithToken();
|
|
|
|
const response = await query({
|
|
query: GQL.GetUserByPhoneDocument,
|
|
variables,
|
|
});
|
|
|
|
const customer = response?.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 createOrUpdateClient(variables: GQL.CreateClientMutationVariables) {
|
|
const { query, mutate } = await getClientWithToken();
|
|
|
|
const response = await query({
|
|
query: GQL.GetUserByPhoneDocument,
|
|
variables,
|
|
});
|
|
|
|
const client = response?.data?.customers?.at(0);
|
|
|
|
if (client?.phone === variables.phone) {
|
|
const masters = [...new Set([...client.masters.map((x) => x?.documentId), variables.masterId])];
|
|
|
|
return mutate({
|
|
mutation: GQL.UpdateCustomerProfileDocument,
|
|
variables: {
|
|
documentId: client.documentId,
|
|
data: {
|
|
masters,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
return mutate({
|
|
mutation: GQL.CreateClientDocument,
|
|
variables,
|
|
});
|
|
}
|
|
|
|
export async function getCustomer(variables: GQL.GetCustomerQueryVariables) {
|
|
const { query } = await getClientWithToken();
|
|
|
|
const response = await query({
|
|
query: GQL.GetCustomerDocument,
|
|
variables,
|
|
});
|
|
|
|
const customer = response?.data?.customers?.at(0);
|
|
|
|
return customer;
|
|
}
|
|
|
|
export async function updateCustomerProfile(variables: GQL.UpdateCustomerProfileMutationVariables) {
|
|
const { mutate } = await getClientWithToken();
|
|
|
|
return mutate({
|
|
mutation: GQL.UpdateCustomerProfileDocument,
|
|
variables,
|
|
});
|
|
}
|