From 303f20809218cd1cc372239b7faaaf565859e51f Mon Sep 17 00:00:00 2001 From: Guntoro Yudhy Kusuma Date: Sat, 25 Dec 2021 23:32:19 +0700 Subject: [PATCH 1/2] feat: add transaction entity to store payment information --- .../migration.sql | 15 +++++++ prisma/schema.prisma | 33 +++++++++++---- prisma/seed.ts | 42 +++++++++++++++++++ 3 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 prisma/migrations/20211225043237_add_transaction_table/migration.sql diff --git a/prisma/migrations/20211225043237_add_transaction_table/migration.sql b/prisma/migrations/20211225043237_add_transaction_table/migration.sql new file mode 100644 index 0000000..ed42acb --- /dev/null +++ b/prisma/migrations/20211225043237_add_transaction_table/migration.sql @@ -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 +); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 72cb7de..be684a8 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -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 { @@ -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) +} diff --git a/prisma/seed.ts b/prisma/seed.ts index 49727f7..0c2ca47 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -11,6 +11,13 @@ async function main() { }) ) ) + await Promise.all( + getCourse().map((course) => + prisma.course.create({ + data: course, + }) + ) + ) } main() @@ -25,20 +32,55 @@ main() function getUsers() { return [ { + id: '82bf2c53-a9eb-4029-8d6d-17f514e8be7b', email: 'me@zainf.dev', name: 'Zain', phoneNumber: '+6512345678', role: ROLES.ADMIN, }, { + id: '76ca6998-da6a-4523-bb71-397f7472cd49', email: 'vika@rbagi.id', name: 'Vika', role: ROLES.AUTHOR, }, { + id: 'ace81e7d-371a-42f4-99d5-2477237cc23d', email: 'pk@zainf.dev', name: 'Pejuang Kode', role: ROLES.MEMBER, }, ] } + +function getCourse() { + return [ + { + authorId: '76ca6998-da6a-4523-bb71-397f7472cd49', + 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', + }, + { + authorId: '76ca6998-da6a-4523-bb71-397f7472cd49', + 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', + }, + { + authorId: '76ca6998-da6a-4523-bb71-397f7472cd49', + 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', + }, + ] +} From 37f945b1aa725808eeb1d679e58eca18683734ac Mon Sep 17 00:00:00 2001 From: Guntoro Yudhy Kusuma Date: Sun, 26 Dec 2021 11:06:25 +0700 Subject: [PATCH 2/2] feat: update seed.ts and enum.ts --- app/models/enum.ts | 10 ++++ prisma/seed.ts | 126 ++++++++++++++++++++++++++++++++------------- 2 files changed, 99 insertions(+), 37 deletions(-) diff --git a/app/models/enum.ts b/app/models/enum.ts index 3379a37..2ad5a98 100644 --- a/app/models/enum.ts +++ b/app/models/enum.ts @@ -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', +} diff --git a/prisma/seed.ts b/prisma/seed.ts index 0c2ca47..63cb118 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -1,23 +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, - }) - ) - ) - await Promise.all( + // update or insert user with admin role + await prisma.user.upsert({ + where: { email: 'me@zain.dev' }, + update: {}, + create: { + email: 'me@zainf.dev', + name: 'Zain', + phoneNumber: '+6512345678', + role: ROLES.ADMIN, + }, + }) + + // update or insert user with author role + const author = await prisma.user.upsert({ + where: { email: 'vika@rbagi.id' }, + update: {}, + create: { + email: 'vika@rbagi.id', + name: 'Vika', + role: ROLES.AUTHOR, + }, + }) + + // update or insert user with member role + const member = await prisma.user.upsert({ + where: { email: 'pk@zainf.dev' }, + update: {}, + create: { + email: 'pk@zainf.dev', + name: 'Pejuang Kode', + role: ROLES.MEMBER, + }, + }) + + // create courses + const courses = await Promise.all( getCourse().map((course) => prisma.course.create({ - data: course, + 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() @@ -29,34 +108,9 @@ main() await prisma.$disconnect() }) -function getUsers() { - return [ - { - id: '82bf2c53-a9eb-4029-8d6d-17f514e8be7b', - email: 'me@zainf.dev', - name: 'Zain', - phoneNumber: '+6512345678', - role: ROLES.ADMIN, - }, - { - id: '76ca6998-da6a-4523-bb71-397f7472cd49', - email: 'vika@rbagi.id', - name: 'Vika', - role: ROLES.AUTHOR, - }, - { - id: 'ace81e7d-371a-42f4-99d5-2477237cc23d', - email: 'pk@zainf.dev', - name: 'Pejuang Kode', - role: ROLES.MEMBER, - }, - ] -} - function getCourse() { return [ { - authorId: '76ca6998-da6a-4523-bb71-397f7472cd49', 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.", @@ -65,7 +119,6 @@ function getCourse() { category: 'Parenting', }, { - authorId: '76ca6998-da6a-4523-bb71-397f7472cd49', 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.", @@ -74,7 +127,6 @@ function getCourse() { category: 'Parenting', }, { - authorId: '76ca6998-da6a-4523-bb71-397f7472cd49', 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.",