Skip to content

Commit

Permalink
feat: add transaction entity to store payment information (#45)
Browse files Browse the repository at this point in the history
feat: add transaction entity to store payment information
  • Loading branch information
kodiakhq[bot] authored Dec 26, 2021
2 parents 4eb7538 + f9f097c commit ea128b9
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 24 deletions.
10 changes: 10 additions & 0 deletions app/models/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@ export const ROLES = {
AUTHOR: 'AUTHOR',
MEMBER: 'MEMBER',
}

export const SUBSCRIPTION_STATUS = {
INACTIVE: 'INACTIVE',
ACTIVE: 'ACTIVE',
}

export const TRANSACTION_STATUS = {
SUBMITTED: 'SUBMITTED',
VERIFIED: 'VERIFIED',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- CreateTable
CREATE TABLE "Transaction" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"userId" TEXT NOT NULL,
"subscriptionId" TEXT NOT NULL,
"bankName" TEXT NOT NULL,
"bankAccountNumber" TEXT NOT NULL,
"bankAccountName" TEXT NOT NULL,
"amount" INTEGER NOT NULL,
"status" TEXT NOT NULL,
CONSTRAINT "Transaction_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "Transaction_subscriptionId_fkey" FOREIGN KEY ("subscriptionId") REFERENCES "Subscription" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
33 changes: 25 additions & 8 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ model User {
role String
courses Course[]
subscriptions Subscription[]
Transaction Transaction[]
}

model Subscription {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String
courseId String
status String
course Course @relation(fields: [courseId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String
courseId String
status String
course Course @relation(fields: [courseId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
Transaction Transaction[]
}

model Course {
Expand All @@ -45,3 +47,18 @@ model Course {
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
subscriptions Subscription[]
}

model Transaction {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId String
subscriptionId String
bankName String
bankAccountNumber String
bankAccountName String
amount Int
status String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
subscription Subscription @relation(fields: [subscriptionId], references: [id], onDelete: Cascade)
}
126 changes: 110 additions & 16 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,102 @@
import { PrismaClient } from '@prisma/client'
import { ROLES } from '../app/models/enum'
import {
ROLES,
SUBSCRIPTION_STATUS,
TRANSACTION_STATUS,
} from '../app/models/enum'

const prisma = new PrismaClient()

async function main() {
await Promise.all(
getUsers().map((user) =>
prisma.user.create({
data: user,
// update or insert user with admin role
await prisma.user.upsert({
where: { email: '[email protected]' },
update: {},
create: {
email: '[email protected]',
name: 'Zain',
phoneNumber: '+6512345678',
role: ROLES.ADMIN,
},
})

// update or insert user with author role
const author = await prisma.user.upsert({
where: { email: '[email protected]' },
update: {},
create: {
email: '[email protected]',
name: 'Vika',
role: ROLES.AUTHOR,
},
})

// update or insert user with member role
const member = await prisma.user.upsert({
where: { email: '[email protected]' },
update: {},
create: {
email: '[email protected]',
name: 'Pejuang Kode',
role: ROLES.MEMBER,
},
})

// create courses
const courses = await Promise.all(
getCourse().map((course) =>
prisma.course.create({
data: {
authorId: author.id,
...course,
},
})
)
)

// create subscription
const subscription1 = await prisma.subscription.create({
data: {
userId: member.id,
courseId: courses[0].id,
status: SUBSCRIPTION_STATUS.ACTIVE,
},
})

// create subscription
const subscription2 = await prisma.subscription.create({
data: {
userId: member.id,
courseId: courses[1].id,
status: SUBSCRIPTION_STATUS.ACTIVE,
},
})

// create transaction with status SUBMITTED
await prisma.transaction.create({
data: {
userId: member.id,
subscriptionId: subscription1.id,
bankName: 'Bank Mandiri',
bankAccountName: 'Pejuang Kode',
bankAccountNumber: '123456789',
amount: 25000,
status: TRANSACTION_STATUS.SUBMITTED,
},
})

// create transaction with status VERIFIED
await prisma.transaction.create({
data: {
userId: member.id,
subscriptionId: subscription2.id,
bankName: 'Bank Mandiri',
bankAccountName: 'Pejuang Kode',
bankAccountNumber: '123456789',
amount: 50000,
status: TRANSACTION_STATUS.VERIFIED,
},
})
}

main()
Expand All @@ -22,23 +108,31 @@ main()
await prisma.$disconnect()
})

function getUsers() {
function getCourse() {
return [
{
email: '[email protected]',
name: 'Zain',
phoneNumber: '+6512345678',
role: ROLES.ADMIN,
name: 'Menumbuhkan Minat Baca Anak',
description:
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
price: 25000,
image: 'https://picsum.photos/id/1073/1200/800',
category: 'Parenting',
},
{
email: '[email protected]',
name: 'Vika',
role: ROLES.AUTHOR,
name: 'Mengelola Konflik dalam Keluarga',
description:
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
price: 50000,
image: 'https://picsum.photos/id/146/1200/800',
category: 'Parenting',
},
{
email: '[email protected]',
name: 'Pejuang Kode',
role: ROLES.MEMBER,
name: 'Menumbuhkan Rasa Percaya Diri Sejak Dini',
description:
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
price: 100000,
image: 'https://picsum.photos/id/1001/1200/800',
category: 'Parenting',
},
]
}

0 comments on commit ea128b9

Please sign in to comment.