Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create/update payments tables #62

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- CreateEnum
CREATE TYPE "PaymentStatus" AS ENUM ('PAID', 'PROCESSING', 'UNPAID', 'DENIED');

-- CreateTable
CREATE TABLE "Payment" (
"id" SERIAL NOT NULL,
"stripePaymentId" TEXT NOT NULL,
"creationDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updateDate" TIMESTAMP(3) NOT NULL,
"donation_id" INTEGER NOT NULL,
"amount" INTEGER NOT NULL,
"currency" TEXT NOT NULL,
"status" "PaymentStatus" NOT NULL,

CONSTRAINT "Payment_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "Payment_stripePaymentId_key" ON "Payment"("stripePaymentId");
16 changes: 16 additions & 0 deletions backend/typescript/prisma/migrations/20240611002441_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Warnings:

- You are about to drop the column `donation_id` on the `Payment` table. All the data in the column will be lost.
- A unique constraint covering the columns `[payment_id]` on the table `Donation` will be added. If there are existing duplicate values, this will fail.

*/
-- AlterTable
ALTER TABLE "Donation" ADD COLUMN "payment_id" INTEGER;

-- AlterTable
ALTER TABLE "Payment" DROP COLUMN "donation_id",
ALTER COLUMN "amount" SET DATA TYPE DOUBLE PRECISION;

-- CreateIndex
CREATE UNIQUE INDEX "Donation_payment_id_key" ON "Donation"("payment_id");
22 changes: 21 additions & 1 deletion backend/typescript/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ model User {
donations Donation[]
}

enum RoleType{
enum RoleType {
Admin
User
}
Expand All @@ -39,6 +39,8 @@ model Donation {
cause_id Int
is_recurring Recurrence
confirmation_email_sent Boolean
transactions Payment? @relation(fields: [payment_id], references: [id])
payment_id Int? @unique
}

enum Recurrence {
Expand Down Expand Up @@ -75,3 +77,21 @@ model Item {
npo NPO? @relation("NPOItem")
npo_id Int? @unique
}

model Payment {
id Int @id @default(autoincrement())
stripePaymentId String @unique
creationDate DateTime @default(now())
updateDate DateTime @updatedAt
donation Donation?
amount Float
currency String
status PaymentStatus
}

enum PaymentStatus {
PAID
PROCESSING
UNPAID
DENIED
}
26 changes: 26 additions & 0 deletions backend/typescript/rest/paymentRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Router } from "express";
import PaymentService from "../services/implementations/paymentService";
import IPaymentService from "../services/interfaces/paymentService";

const paymentRouter: Router = Router();
const paymentService: IPaymentService = new PaymentService();

paymentRouter.post("/", (req, res) => {
try {
paymentService.createPayment(req.body)
} catch (err) {
console.error(err)
}
res.send("Payment successful!");
});

paymentRouter.put("/", (req, res) => {
const { id, payload } = req.body
try {
paymentService.updatePayment(id, payload)
} catch (err) {
console.error(err)
}

res.send("Payment updated!");
});
35 changes: 35 additions & 0 deletions backend/typescript/services/implementations/paymentService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import IPaymentService from "../interfaces/paymentService";
import prisma from "../../prisma";

class PaymentService implements IPaymentService {
async createPayment(payload: any): Promise<any> {
try {
const newPayment = prisma.payment.create({
data: payload
})

return newPayment;
} catch (error) {
console.error(error)
}

}

async updatePayment(paymentId: number, payload: any): Promise<any> {

try {
const updatedPayment = prisma.payment.update({
where: {
id: paymentId,
},
data: payload
})

return updatedPayment;
} catch (error) {
console.error(error)
}
}
}

export default PaymentService;
32 changes: 32 additions & 0 deletions backend/typescript/services/interfaces/paymentService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


/**
* Interface for the PaymentDTO class
*/
// export interface PaymentDTO {
// id: number;
// stripePaymentId: string;
// creationDate: string;
// updateDate: string;
// // donation?: number;
// amount: number;
// currency: string;
// status: string;
// }

/**
* Interface for the PaymentService class
*/
interface IPaymentService {
/**
* retrieve the SimpleEntity with the given id
* @param id SimpleEntity id
* @returns requested SimpleEntity
* @throws Error if retrieval fails
*/
createPayment(payload: any): Promise<any>;

updatePayment(paymentId: number, payload: any): Promise<any>;
}

export default IPaymentService;