refactor(payment): improve input validation and use asyncAndThen

Update validateInput to return Result type and use asyncAndThen for chaining async operations
This commit is contained in:
Yuu Ottosoka
2025-07-21 12:12:36 +03:00
parent d59f0dd794
commit 636f0e7f23

View File

@@ -1,11 +1,13 @@
import { ok, err } from "neverthrow";
import { CreatePaymentInput, CreatePaymentInputSchema } from "./types";
import { paymentAlreadyProcessed } from "./errors";
import { PaymentRepository } from "./repository";
import { Payment, PaymentStatus, PrismaClient } from "@repo/db";
import { createZodValidationError } from "@repo/exceptions";
import { ok, err, Result } from 'neverthrow';
import { CreatePaymentInput, CreatePaymentInputSchema } from './types';
import { paymentAlreadyProcessed } from './errors';
import { PaymentRepository } from './repository';
import { Payment, PaymentStatus, PrismaClient } from '@repo/db';
import { createZodValidationError, ZodValidationError } from '@repo/exceptions';
const validateInput = (data: unknown) => {
const validateInput = (
data: unknown
): Result<CreatePaymentInput, ZodValidationError> => {
const parseResult = CreatePaymentInputSchema.safeParse(data);
return parseResult.success
@@ -15,10 +17,10 @@ const validateInput = (data: unknown) => {
export const createPayment = (
client: PrismaClient,
input: CreatePaymentInput,
input: CreatePaymentInput
) =>
validateInput(input).map((validatedInput) =>
PaymentRepository.create(client, validatedInput),
validateInput(input).asyncAndThen((validatedInput) =>
PaymentRepository.create(client, validatedInput)
);
export const getPaymentById = (client: PrismaClient, id: string) =>