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