All files / src/services lunchPlans.ts

100% Statements 34/34
100% Branches 8/8
100% Functions 3/3
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 431x 1x     1x         1x 1x 5x 5x 5x 5x 5x 5x   5x 1x 1x 1x 1x 1x   1x 5x 5x 3x 3x 3x 3x 3x 1x 2x 3x 3x 5x 5x 5x 5x 1x  
import { Effect, pipe } from 'effect';
import { createOrpcClient } from '@/libs/orpcClient';
 
// Constants
import { ERROR_MESSAGES } from '@/constants/messages';
 
// Types
import { ApiError, ApiResponse, LunchPlansResponse } from '@/types';
 
export const lunchPlansService = {
  list: async (token?: string): Promise<ApiResponse<LunchPlansResponse[]>> =>
    Effect.runPromise(
      pipe(
        Effect.tryPromise({
          try: async () => {
            const orpcClient = createOrpcClient(token);
            const { data } = await orpcClient.lunchPlans?.list({});
 
            if (!data) {
              return {
                data: [],
                error: { message: ERROR_MESSAGES.FETCH_LUNCH_PLANS },
              };
            }
 
            return { data };
          },
          catch: (error) => ({
            data: [],
            error: {
              ...(error as ApiError),
              message:
                error instanceof Error
                  ? error.message
                  : ERROR_MESSAGES.FETCH_LUNCH_PLANS,
            },
          }),
        }),
        Effect.catchAll((error) => Effect.succeed(error)),
      ),
    ),
};