refactor(api): streamline customer, order, service, and slot actions by wrapping server functions with client action utility to rethrow error messages to client
This commit is contained in:
parent
57cab6209d
commit
f1da2c834a
@ -1,36 +1,8 @@
|
||||
'use server';
|
||||
import * as customers from './server/customers';
|
||||
import { wrapClientAction } from '@/utils/actions';
|
||||
|
||||
import { useService } from './lib/service';
|
||||
import { CustomersService } from '@repo/graphql/api/customers';
|
||||
|
||||
const getService = useService(CustomersService);
|
||||
|
||||
export async function addMasters(...variables: Parameters<CustomersService['addMasters']>) {
|
||||
const service = await getService();
|
||||
|
||||
return service.addMasters(...variables);
|
||||
}
|
||||
|
||||
export async function getClients(...variables: Parameters<CustomersService['getClients']>) {
|
||||
const service = await getService();
|
||||
|
||||
return service.getClients(...variables);
|
||||
}
|
||||
|
||||
export async function getCustomer(...variables: Parameters<CustomersService['getCustomer']>) {
|
||||
const service = await getService();
|
||||
|
||||
return service.getCustomer(...variables);
|
||||
}
|
||||
|
||||
export async function getMasters(...variables: Parameters<CustomersService['getMasters']>) {
|
||||
const service = await getService();
|
||||
|
||||
return service.getMasters(...variables);
|
||||
}
|
||||
|
||||
export async function updateCustomer(...variables: Parameters<CustomersService['updateCustomer']>) {
|
||||
const service = await getService();
|
||||
|
||||
return service.updateCustomer(...variables);
|
||||
}
|
||||
export const addMasters = wrapClientAction(customers.addMasters);
|
||||
export const getClients = wrapClientAction(customers.getClients);
|
||||
export const getCustomer = wrapClientAction(customers.getCustomer);
|
||||
export const getMasters = wrapClientAction(customers.getMasters);
|
||||
export const updateCustomer = wrapClientAction(customers.updateCustomer);
|
||||
|
||||
@ -1,30 +1,7 @@
|
||||
'use server';
|
||||
import * as orders from './server/orders';
|
||||
import { wrapClientAction } from '@/utils/actions';
|
||||
|
||||
import { useService } from './lib/service';
|
||||
import { OrdersService } from '@repo/graphql/api/orders';
|
||||
|
||||
const getServicesService = useService(OrdersService);
|
||||
|
||||
export async function createOrder(...variables: Parameters<OrdersService['createOrder']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return service.createOrder(...variables);
|
||||
}
|
||||
|
||||
export async function getOrder(...variables: Parameters<OrdersService['getOrder']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return service.getOrder(...variables);
|
||||
}
|
||||
|
||||
export async function getOrders(...variables: Parameters<OrdersService['getOrders']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return service.getOrders(...variables);
|
||||
}
|
||||
|
||||
export async function updateOrder(...variables: Parameters<OrdersService['updateOrder']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return service.updateOrder(...variables);
|
||||
}
|
||||
export const createOrder = wrapClientAction(orders.createOrder);
|
||||
export const getOrder = wrapClientAction(orders.getOrder);
|
||||
export const getOrders = wrapClientAction(orders.getOrders);
|
||||
export const updateOrder = wrapClientAction(orders.updateOrder);
|
||||
|
||||
37
apps/web/actions/api/server/customers.ts
Normal file
37
apps/web/actions/api/server/customers.ts
Normal file
@ -0,0 +1,37 @@
|
||||
'use server';
|
||||
|
||||
import { useService } from '../lib/service';
|
||||
import { wrapServerAction } from '@/utils/actions';
|
||||
import { CustomersService } from '@repo/graphql/api/customers';
|
||||
|
||||
const getService = useService(CustomersService);
|
||||
|
||||
export async function addMasters(...variables: Parameters<CustomersService['addMasters']>) {
|
||||
const service = await getService();
|
||||
|
||||
return wrapServerAction(() => service.addMasters(...variables));
|
||||
}
|
||||
|
||||
export async function getClients(...variables: Parameters<CustomersService['getClients']>) {
|
||||
const service = await getService();
|
||||
|
||||
return wrapServerAction(() => service.getClients(...variables));
|
||||
}
|
||||
|
||||
export async function getCustomer(...variables: Parameters<CustomersService['getCustomer']>) {
|
||||
const service = await getService();
|
||||
|
||||
return wrapServerAction(() => service.getCustomer(...variables));
|
||||
}
|
||||
|
||||
export async function getMasters(...variables: Parameters<CustomersService['getMasters']>) {
|
||||
const service = await getService();
|
||||
|
||||
return wrapServerAction(() => service.getMasters(...variables));
|
||||
}
|
||||
|
||||
export async function updateCustomer(...variables: Parameters<CustomersService['updateCustomer']>) {
|
||||
const service = await getService();
|
||||
|
||||
return wrapServerAction(() => service.updateCustomer(...variables));
|
||||
}
|
||||
31
apps/web/actions/api/server/orders.ts
Normal file
31
apps/web/actions/api/server/orders.ts
Normal file
@ -0,0 +1,31 @@
|
||||
'use server';
|
||||
|
||||
import { useService } from '../lib/service';
|
||||
import { wrapServerAction } from '@/utils/actions';
|
||||
import { OrdersService } from '@repo/graphql/api/orders';
|
||||
|
||||
const getServicesService = useService(OrdersService);
|
||||
|
||||
export async function createOrder(...variables: Parameters<OrdersService['createOrder']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return wrapServerAction(() => service.createOrder(...variables));
|
||||
}
|
||||
|
||||
export async function getOrder(...variables: Parameters<OrdersService['getOrder']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return wrapServerAction(() => service.getOrder(...variables));
|
||||
}
|
||||
|
||||
export async function getOrders(...variables: Parameters<OrdersService['getOrders']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return wrapServerAction(() => service.getOrders(...variables));
|
||||
}
|
||||
|
||||
export async function updateOrder(...variables: Parameters<OrdersService['updateOrder']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return wrapServerAction(() => service.updateOrder(...variables));
|
||||
}
|
||||
19
apps/web/actions/api/server/services.ts
Normal file
19
apps/web/actions/api/server/services.ts
Normal file
@ -0,0 +1,19 @@
|
||||
'use server';
|
||||
|
||||
import { useService } from '../lib/service';
|
||||
import { wrapServerAction } from '@/utils/actions';
|
||||
import { ServicesService } from '@repo/graphql/api/services';
|
||||
|
||||
const getServicesService = useService(ServicesService);
|
||||
|
||||
export async function getService(...variables: Parameters<ServicesService['getService']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return wrapServerAction(() => service.getService(...variables));
|
||||
}
|
||||
|
||||
export async function getServices(...variables: Parameters<ServicesService['getServices']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return wrapServerAction(() => service.getServices(...variables));
|
||||
}
|
||||
45
apps/web/actions/api/server/slots.ts
Normal file
45
apps/web/actions/api/server/slots.ts
Normal file
@ -0,0 +1,45 @@
|
||||
'use server';
|
||||
|
||||
import { useService } from '../lib/service';
|
||||
import { wrapServerAction } from '@/utils/actions';
|
||||
import { SlotsService } from '@repo/graphql/api/slots';
|
||||
|
||||
const getService = useService(SlotsService);
|
||||
|
||||
export async function createSlot(...variables: Parameters<SlotsService['createSlot']>) {
|
||||
const service = await getService();
|
||||
|
||||
return wrapServerAction(() => service.createSlot(...variables));
|
||||
}
|
||||
|
||||
export async function deleteSlot(...variables: Parameters<SlotsService['deleteSlot']>) {
|
||||
const service = await getService();
|
||||
|
||||
return wrapServerAction(() => service.deleteSlot(...variables));
|
||||
}
|
||||
|
||||
export async function getAvailableTimeSlots(
|
||||
...variables: Parameters<SlotsService['getAvailableTimeSlots']>
|
||||
) {
|
||||
const service = await getService();
|
||||
|
||||
return wrapServerAction(() => service.getAvailableTimeSlots(...variables));
|
||||
}
|
||||
|
||||
export async function getSlot(...variables: Parameters<SlotsService['getSlot']>) {
|
||||
const service = await getService();
|
||||
|
||||
return wrapServerAction(() => service.getSlot(...variables));
|
||||
}
|
||||
|
||||
export async function getSlots(...variables: Parameters<SlotsService['getSlots']>) {
|
||||
const service = await getService();
|
||||
|
||||
return wrapServerAction(() => service.getSlots(...variables));
|
||||
}
|
||||
|
||||
export async function updateSlot(...variables: Parameters<SlotsService['updateSlot']>) {
|
||||
const service = await getService();
|
||||
|
||||
return wrapServerAction(() => service.updateSlot(...variables));
|
||||
}
|
||||
@ -1,18 +1,5 @@
|
||||
'use server';
|
||||
import * as services from './server/services';
|
||||
import { wrapClientAction } from '@/utils/actions';
|
||||
|
||||
import { useService } from './lib/service';
|
||||
import { ServicesService } from '@repo/graphql/api/services';
|
||||
|
||||
const getServicesService = useService(ServicesService);
|
||||
|
||||
export async function getService(...variables: Parameters<ServicesService['getService']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return service.getService(...variables);
|
||||
}
|
||||
|
||||
export async function getServices(...variables: Parameters<ServicesService['getServices']>) {
|
||||
const service = await getServicesService();
|
||||
|
||||
return service.getServices(...variables);
|
||||
}
|
||||
export const getServices = wrapClientAction(services.getServices);
|
||||
export const getService = wrapClientAction(services.getService);
|
||||
|
||||
@ -1,44 +1,9 @@
|
||||
'use server';
|
||||
import * as slots from './server/slots';
|
||||
import { wrapClientAction } from '@/utils/actions';
|
||||
|
||||
import { useService } from './lib/service';
|
||||
import { SlotsService } from '@repo/graphql/api/slots';
|
||||
|
||||
const getService = useService(SlotsService);
|
||||
|
||||
export async function createSlot(...variables: Parameters<SlotsService['createSlot']>) {
|
||||
const service = await getService();
|
||||
|
||||
return service.createSlot(...variables);
|
||||
}
|
||||
|
||||
export async function deleteSlot(...variables: Parameters<SlotsService['deleteSlot']>) {
|
||||
const service = await getService();
|
||||
|
||||
return service.deleteSlot(...variables);
|
||||
}
|
||||
|
||||
export async function getAvailableTimeSlots(
|
||||
...variables: Parameters<SlotsService['getAvailableTimeSlots']>
|
||||
) {
|
||||
const service = await getService();
|
||||
|
||||
return service.getAvailableTimeSlots(...variables);
|
||||
}
|
||||
|
||||
export async function getSlot(...variables: Parameters<SlotsService['getSlot']>) {
|
||||
const service = await getService();
|
||||
|
||||
return service.getSlot(...variables);
|
||||
}
|
||||
|
||||
export async function getSlots(...variables: Parameters<SlotsService['getSlots']>) {
|
||||
const service = await getService();
|
||||
|
||||
return service.getSlots(...variables);
|
||||
}
|
||||
|
||||
export async function updateSlot(...variables: Parameters<SlotsService['updateSlot']>) {
|
||||
const service = await getService();
|
||||
|
||||
return service.updateSlot(...variables);
|
||||
}
|
||||
export const getSlot = wrapClientAction(slots.getSlot);
|
||||
export const getSlots = wrapClientAction(slots.getSlots);
|
||||
export const createSlot = wrapClientAction(slots.createSlot);
|
||||
export const updateSlot = wrapClientAction(slots.updateSlot);
|
||||
export const deleteSlot = wrapClientAction(slots.deleteSlot);
|
||||
export const getAvailableTimeSlots = wrapClientAction(slots.getAvailableTimeSlots);
|
||||
|
||||
23
apps/web/utils/actions.ts
Normal file
23
apps/web/utils/actions.ts
Normal file
@ -0,0 +1,23 @@
|
||||
/* eslint-disable unicorn/prevent-abbreviations */
|
||||
|
||||
export function wrapClientAction<Args extends unknown[], Return>(
|
||||
fn: (...args: Args) => Promise<{ data?: Return; error?: string; ok: boolean }>,
|
||||
): (...args: Args) => Promise<Return> {
|
||||
return async (...args: Args): Promise<Return> => {
|
||||
const res = await fn(...args);
|
||||
if (!res.ok) throw new Error(res.error ?? 'Неизвестная ошибка');
|
||||
return res.data as Return;
|
||||
};
|
||||
}
|
||||
|
||||
export async function wrapServerAction<T>(
|
||||
action: () => Promise<T>,
|
||||
): Promise<{ data: T; ok: true } | { error: string; ok: false }> {
|
||||
try {
|
||||
const data = await action();
|
||||
return { data, ok: true };
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Неизвестная ошибка сервера';
|
||||
return { error: message, ok: false };
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user