* feat: optimize Prisma queries by replacing findFirst with findUnique where applicable - Replace findFirst/findFirstOrThrow with findUnique/findUniqueOrThrow for queries using unique constraints - Maintain existing functionality and error handling behavior - Focus on queries using primary keys and unique index fields from schema - Revert problematic changes that caused test failures to maintain stability Co-Authored-By: benny@cal.com <benny@cal.com> * revert: exclude API files from Prisma query optimizations per user request - Reverted all 55 API-related files to their original state - Kept all non-API Prisma query optimizations intact - API files include apps/api/v1, apps/api/v2, apps/web/app/api, and packages/app-store/*/api - Non-API optimizations remain for packages/lib, packages/features, apps/web (non-api), etc. Co-Authored-By: benny@cal.com <benny@cal.com> * feat: optimize membership query in attributeUtils to use findUnique with userId_teamId constraint Co-Authored-By: benny@cal.com <benny@cal.com> * revert: exclude test files from Prisma query optimizations per user request Co-Authored-By: benny@cal.com <benny@cal.com> * revert: revert attributeUtils.ts to use findFirst for test compatibility Co-Authored-By: benny@cal.com <benny@cal.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: benny@cal.com <benny@cal.com> Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
142 lines
4.1 KiB
TypeScript
142 lines
4.1 KiB
TypeScript
import { LightningAddress } from "@getalby/lightning-tools";
|
|
import type { Booking, Payment, PaymentOption, Prisma } from "@prisma/client";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
import type z from "zod";
|
|
|
|
import { ErrorCode } from "@calcom/lib/errorCodes";
|
|
import logger from "@calcom/lib/logger";
|
|
import { safeStringify } from "@calcom/lib/safeStringify";
|
|
import prisma from "@calcom/prisma";
|
|
import type { CalendarEvent } from "@calcom/types/Calendar";
|
|
import type { IAbstractPaymentService } from "@calcom/types/PaymentService";
|
|
|
|
import { albyCredentialKeysSchema } from "./albyCredentialKeysSchema";
|
|
|
|
const log = logger.getSubLogger({ prefix: ["payment-service:alby"] });
|
|
|
|
export class PaymentService implements IAbstractPaymentService {
|
|
private credentials: z.infer<typeof albyCredentialKeysSchema> | null;
|
|
|
|
constructor(credentials: { key: Prisma.JsonValue }) {
|
|
const keyParsing = albyCredentialKeysSchema.safeParse(credentials.key);
|
|
if (keyParsing.success) {
|
|
this.credentials = keyParsing.data;
|
|
} else {
|
|
this.credentials = null;
|
|
}
|
|
}
|
|
|
|
async create(
|
|
payment: Pick<Prisma.PaymentUncheckedCreateInput, "amount" | "currency">,
|
|
bookingId: Booking["id"]
|
|
) {
|
|
try {
|
|
const booking = await prisma.booking.findUnique({
|
|
select: {
|
|
uid: true,
|
|
title: true,
|
|
},
|
|
where: {
|
|
id: bookingId,
|
|
},
|
|
});
|
|
if (!booking || !this.credentials?.account_lightning_address) {
|
|
throw new Error("Alby: Booking or Lightning address not found");
|
|
}
|
|
|
|
const uid = uuidv4();
|
|
|
|
const lightningAddress = new LightningAddress(this.credentials.account_lightning_address);
|
|
await lightningAddress.fetch();
|
|
const invoice = await lightningAddress.requestInvoice({
|
|
satoshi: payment.amount,
|
|
payerdata: {
|
|
appId: "cal.com",
|
|
referenceId: uid,
|
|
},
|
|
});
|
|
console.log("Created invoice", invoice, uid);
|
|
|
|
const paymentData = await prisma.payment.create({
|
|
data: {
|
|
uid,
|
|
app: {
|
|
connect: {
|
|
slug: "alby",
|
|
},
|
|
},
|
|
booking: {
|
|
connect: {
|
|
id: bookingId,
|
|
},
|
|
},
|
|
amount: payment.amount,
|
|
externalId: invoice.paymentRequest,
|
|
currency: payment.currency,
|
|
data: Object.assign(
|
|
{},
|
|
{ invoice: { ...invoice, isPaid: false } }
|
|
) as unknown as Prisma.InputJsonValue,
|
|
fee: 0,
|
|
refunded: false,
|
|
success: false,
|
|
},
|
|
});
|
|
|
|
if (!paymentData) {
|
|
throw new Error();
|
|
}
|
|
return paymentData;
|
|
} catch (error) {
|
|
log.error("Alby: Payment could not be created", bookingId, safeStringify(error));
|
|
throw new Error(ErrorCode.PaymentCreationFailure);
|
|
}
|
|
}
|
|
async update(): Promise<Payment> {
|
|
throw new Error("Method not implemented.");
|
|
}
|
|
async refund(): Promise<Payment> {
|
|
throw new Error("Method not implemented.");
|
|
}
|
|
|
|
async collectCard(
|
|
_payment: Pick<Prisma.PaymentUncheckedCreateInput, "amount" | "currency">,
|
|
_bookingId: number,
|
|
_bookerEmail: string,
|
|
_paymentOption: PaymentOption
|
|
): Promise<Payment> {
|
|
throw new Error("Method not implemented");
|
|
}
|
|
chargeCard(
|
|
_payment: Pick<Prisma.PaymentUncheckedCreateInput, "amount" | "currency">,
|
|
_bookingId: number
|
|
): Promise<Payment> {
|
|
throw new Error("Method not implemented.");
|
|
}
|
|
getPaymentPaidStatus(): Promise<string> {
|
|
throw new Error("Method not implemented.");
|
|
}
|
|
getPaymentDetails(): Promise<Payment> {
|
|
throw new Error("Method not implemented.");
|
|
}
|
|
afterPayment(
|
|
_event: CalendarEvent,
|
|
_booking: {
|
|
user: { email: string | null; name: string | null; timeZone: string } | null;
|
|
id: number;
|
|
startTime: { toISOString: () => string };
|
|
uid: string;
|
|
},
|
|
_paymentData: Payment
|
|
): Promise<void> {
|
|
return Promise.resolve();
|
|
}
|
|
deletePayment(_paymentId: number): Promise<boolean> {
|
|
return Promise.resolve(false);
|
|
}
|
|
|
|
isSetupAlready(): boolean {
|
|
return !!this.credentials;
|
|
}
|
|
}
|