All files / src/services comment.ts

100% Statements 89/89
100% Branches 21/21
100% Functions 5/5
100% Lines 89/89

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 1161x     1x 1x     1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                               1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x   9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x   7x   9x 3x 3x 3x 3x 3x 3x 3x 2x 3x 3x 3x   4x 9x 9x 9x 9x 9x 9x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 9x 9x 9x 9x      
import { Effect, pipe, Schema } from 'effect';
 
// Constants
import { API_ROUTES } from '@/constants';
import { STATUS_CODE, STATUS_MESSAGES } from '@shared/constants';
 
// Types
import { ApiError, HttpMethod } from '@shared/types';
import { CommentAction, FeedbackResponse } from '@/types';
 
const CommentMutationSchema = Schema.Struct({
  lunchMenuId: Schema.String.pipe(
    Schema.minLength(1, {
      message: () => STATUS_MESSAGES[STATUS_CODE.BAD_REQUEST]!,
    }),
  ),
  token: Schema.String.pipe(
    Schema.minLength(1, {
      message: () => STATUS_MESSAGES[STATUS_CODE.UNAUTHORIZED]!,
    }),
  ),
  type: Schema.Union(
    Schema.Literal(CommentAction.CREATE),
    Schema.Literal(CommentAction.UPDATE),
    Schema.Literal(CommentAction.DELETE),
  ),
  content: Schema.optional(Schema.String),
  isAnonymous: Schema.optional(Schema.Boolean),
});
 
interface CommentMutationParams {
  lunchMenuId: string;
  token: string;
  type: CommentAction;
  content?: string;
  isAnonymous?: boolean;
}
 
interface CommentMutationResponse {
  data?: FeedbackResponse | null;
  success?: boolean;
  error?: ApiError | null;
}
 
const commentService = async ({
  lunchMenuId,
  token,
  type,
  content,
  isAnonymous,
}: CommentMutationParams): Promise<CommentMutationResponse> =>
  Effect.runPromise(
    pipe(
      Effect.tryPromise({
        try: async () => {
          await Schema.decodeUnknown(CommentMutationSchema)({
            lunchMenuId,
            token,
            type,
            content,
            isAnonymous,
          });
 
          const response = await fetch(API_ROUTES.COMMENT, {
            method: HttpMethod.POST,
            headers: {
              'Content-Type': 'application/json',
              Authorization: `Bearer ${token}`,
            },
            body: JSON.stringify({
              lunchMenuId,
              type,
              content,
              isAnonymous,
            }),
          });
 
          const data = await response.json();
 
          if (!data || data.error) {
            return {
              data: data?.data ?? null,
              success: data?.success ?? false,
              error: {
                status: data?.error?.status ?? STATUS_CODE.BAD_REQUEST,
                message:
                  data?.error?.message ??
                  STATUS_MESSAGES[STATUS_CODE.BAD_REQUEST],
              },
            };
          }
 
          return {
            data: data.data ?? null,
            success: data.success ?? true,
            error: null,
          };
        },
        catch: (error) => ({
          data: null,
          success: false,
          error: {
            status: STATUS_CODE.INTERNAL_SERVER_ERROR,
            message:
              error instanceof Error
                ? error.message
                : STATUS_MESSAGES[STATUS_CODE.INTERNAL_SERVER_ERROR],
          },
        }),
      }),
      Effect.catchAll((error) => Effect.succeed(error)),
    ),
  );
 
export { commentService, CommentMutationSchema };