Open KazeroG opened 8 months ago
The documentation comparison between the old and new source code focuses on the Prisma schema for a database application. This comparison highlights the structural changes made to the database schema and their implications.
Both versions of the source code define a database schema using Prisma, aimed at a PostgreSQL database. The schema includes definitions for various models such as Account
, Session
, VerificationToken
, User
, Team
, TeamMember
, Invitation
, PasswordReset
, ApiKey
, Subscription
, Service
, and Price
. These models are designed to manage user accounts, authentication sessions, user roles, team memberships, and service subscriptions.
Addition of stripeCustomerId
in the User
Model:
stripeCustomerId
field in the User
model. This addition suggests an integration with Stripe to manage customer information for billing and subscriptions directly within the user records.Association of Subscription
with User
:
Subscription
model explicitly includes a userId
field along with a relation to the User
model. This change solidifies the relationship between users and their subscriptions, allowing for direct queries of a user's subscriptions and vice versa.Stripe Integration:
stripeCustomerId
to the User
model, the application can now directly link Stripe customer records with application user records. This facilitates easier management of subscription billing and payments, enhancing the application's e-commerce capabilities.Enhanced Subscription Management:
userId
field in the Subscription
model simplifies the management of user subscriptions. It allows for easier tracking of which services a user is subscribed to, improving the application's ability to manage access to paid features or content.Account
, Session
, VerificationToken
, Team
, TeamMember
, Invitation
, PasswordReset
, ApiKey
, Service
, and Price
) remain unchanged. This indicates that the core functionality related to account management, session handling, and team collaboration remains stable.prisma-client-js
client, remains consistent. This stability ensures that the changes are backward compatible and do not require alterations to the database setup or connection logic.The modifications from the old to the new source code represent a focused enhancement of the application's user and subscription management capabilities, specifically through the integration of Stripe and a clearer association between users and their subscriptions. These changes are indicative of an application's evolution to incorporate more complex billing and subscription management features directly within its data model, improving overall functionality and user experience.
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
enum Role {
ADMIN
OWNER
MEMBER
}
model Account {
id String @id @default(uuid())
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String? @db.Text
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
}
model Session {
id String @id @default(uuid())
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
model VerificationToken {
identifier String
token String @unique
expires DateTime
@@unique([identifier, token])
}
model User {
id String @id @default(uuid())
name String
email String @unique
emailVerified DateTime?
password String?
image String?
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
invalid_login_attempts Int @default(0)
lockedAt DateTime?
stripeCustomerId String? // Ajoutez cette ligne
teamMembers TeamMember[]
accounts Account[]
sessions Session[]
invitations Invitation[]
subscriptions Subscription[]
}
model Team {
id String @id @default(uuid())
name String
slug String @unique
domain String? @unique
defaultRole Role @default(MEMBER)
billingId String?
billingProvider String?
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
members TeamMember[]
invitations Invitation[]
apiKeys ApiKey[]
}
model TeamMember {
id String @id @default(uuid())
teamId String
userId String
role Role @default(MEMBER)
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([teamId, userId])
@@index([userId])
}
model Invitation {
id String @id @default(uuid())
teamId String
email String?
role Role @default(MEMBER)
token String @unique
expires DateTime
invitedBy String
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
sentViaEmail Boolean @default(true)
allowedDomains String[] @default([])
user User @relation(fields: [invitedBy], references: [id], onDelete: Cascade)
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
@@unique([teamId, email])
}
model PasswordReset {
id Int @id @default(autoincrement())
email String
token String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
expiresAt DateTime
}
model ApiKey {
id String @id @default(uuid())
name String
teamId String
hashedKey String @unique
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
expiresAt DateTime?
lastUsedAt DateTime?
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
}
model Subscription {
id String @id
customerId String
priceId String
active Boolean @default(false)
startDate DateTime
endDate DateTime
cancelAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
userId String
User User? @relation(fields: [userId], references: [id])
@@index([customerId])
}
model Service {
id String @id @default(uuid())
description String
features String[]
image String
name String
created DateTime
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
Price Price[]
}
model Price {
id String @id @default(uuid())
billingScheme String
currency String
serviceId String
amount Int?
metadata Json
type String
created DateTime
service Service @relation(fields: [serviceId], references: [id], onDelete: Cascade)
}
Thank you @KazeroG We will review and revert on your suggestions.
+1 much needed update
Any update on this?
This source code demonstrates how to synchronize data from Stripe, a payment processing platform, with a local database using Prisma, an ORM (Object Relational Mapping) tool. The script performs several key operations, including fetching data from Stripe, mapping Stripe customers to local users, updating user information with Stripe customer IDs, and seeding the local database with Stripe products, prices, and subscriptions. Below is a detailed breakdown of the code, including its structure, functions, and purpose.
Overview
sync
asynchronous function orchestrates the entire synchronization process.Detailed Documentation
Prisma Client Initialization
@prisma/client
package.Stripe Instance Creation
STRIPE_SECRET_KEY
and sets the API version to'2023-10-16'
.Synchronization Function (
sync
)Helper Functions
getStripeInstance
: Ensures that the Stripe secret key is set and returns the Stripe instance.fetchStripeData
: Fetches active products, prices, subscriptions, and customers from Stripe.mapUsersToCustomers
: Maps local user records to Stripe customers based on email.updateUsersWithStripeCustomerId
: Updates local users with their corresponding Stripe customer IDs.performDatabaseOperations
: Orchestrates database operations including cleaning up old data and seeding with new data from Stripe.cleanup
,seedServices
,seedPrices
,seedSubscriptions
: Helper functions to perform specific database seeding operations.printStats
: Logs the count of products, prices, and subscriptions synced to the database.Error Handling
Complete New Source Code