2
0

feat(db): 🗃️ Add createdAt and updatedAt

This commit is contained in:
Baptiste Arnaud
2022-02-24 14:52:35 +01:00
parent b9dafa611e
commit a499d85c98
6 changed files with 54 additions and 18 deletions

View File

@ -41,7 +41,6 @@ export const CollaborationList = () => {
const [invitationEmail, setInvitationEmail] = useState('') const [invitationEmail, setInvitationEmail] = useState('')
const [isSendingInvitation, setIsSendingInvitation] = useState(false) const [isSendingInvitation, setIsSendingInvitation] = useState(false)
console.log(user, owner)
const isOwner = user?.email === owner?.email const isOwner = user?.email === owner?.email
const toast = useToast({ const toast = useToast({

View File

@ -8,6 +8,7 @@ import { Provider } from 'next-auth/providers'
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
import { withSentry } from '@sentry/nextjs' import { withSentry } from '@sentry/nextjs'
import { CustomAdapter } from './adapter' import { CustomAdapter } from './adapter'
import { User } from 'db'
const providers: Provider[] = [ const providers: Provider[] = [
EmailProvider({ EmailProvider({
@ -56,12 +57,29 @@ const handler = (req: NextApiRequest, res: NextApiResponse) => {
strategy: 'database', strategy: 'database',
}, },
callbacks: { callbacks: {
session: async ({ session, user }) => ({ session: async ({ session, user }) => {
...session, const userFromDb = user as User
user, await updateLastActivityDate(userFromDb)
}), return {
...session,
userFromDb,
}
},
}, },
}) })
} }
const updateLastActivityDate = async (user: User) => {
const datesAreOnSameDay = (first: Date, second: Date) =>
first.getFullYear() === second.getFullYear() &&
first.getMonth() === second.getMonth() &&
first.getDate() === second.getDate()
if (!datesAreOnSameDay(user.lastActivityAt, new Date()))
await prisma.user.update({
where: { id: user.id },
data: { lastActivityAt: new Date() },
})
}
export default withSentry(handler) export default withSentry(handler)

View File

@ -24,7 +24,7 @@ export const useCredentials = ({
export const createCredentials = async ( export const createCredentials = async (
userId: string, userId: string,
credentials: Omit<Credentials, 'ownerId' | 'id' | 'iv'> credentials: Omit<Credentials, 'ownerId' | 'id' | 'iv' | 'createdAt'>
) => ) =>
sendRequest<{ sendRequest<{
credentials: Credentials credentials: Credentials

View File

@ -12,7 +12,7 @@ export const useCustomDomains = ({
onError: (error: Error) => void onError: (error: Error) => void
}) => { }) => {
const { data, error, mutate } = useSWR< const { data, error, mutate } = useSWR<
{ customDomains: CustomDomain[] }, { customDomains: Omit<CustomDomain, 'createdAt'>[] },
Error Error
>(userId ? `/api/users/${userId}/customDomains` : null, fetcher) >(userId ? `/api/users/${userId}/customDomains` : null, fetcher)
if (error) onError(error) if (error) onError(error)
@ -25,7 +25,7 @@ export const useCustomDomains = ({
export const createCustomDomain = async ( export const createCustomDomain = async (
userId: string, userId: string,
customDomain: Omit<CustomDomain, 'ownerId'> customDomain: Omit<CustomDomain, 'ownerId' | 'createdAt'>
) => ) =>
sendRequest<{ sendRequest<{
credentials: Credentials credentials: Credentials

View File

@ -0,0 +1,13 @@
-- AlterTable
ALTER TABLE "Credentials" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
-- AlterTable
ALTER TABLE "CustomDomain" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
-- AlterTable
ALTER TABLE "Invitation" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
-- AlterTable
ALTER TABLE "User" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "lastActivityAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;

View File

@ -39,6 +39,9 @@ model Session {
model User { model User {
id String @id @default(cuid()) id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
lastActivityAt DateTime @default(now())
name String? name String?
email String? @unique email String? @unique
emailVerified DateTime? emailVerified DateTime?
@ -56,19 +59,21 @@ model User {
} }
model CustomDomain { model CustomDomain {
ownerId String createdAt DateTime @default(now())
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade) ownerId String
name String @unique owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
name String @unique
} }
model Credentials { model Credentials {
id String @id @default(cuid()) id String @id @default(cuid())
ownerId String createdAt DateTime @default(now())
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade) ownerId String
data String // Encrypted data owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
name String data String // Encrypted data
type String name String
iv String type String
iv String
@@unique([name, type, ownerId]) @@unique([name, type, ownerId])
} }
@ -129,6 +134,7 @@ model Typebot {
} }
model Invitation { model Invitation {
createdAt DateTime @default(now())
email String email String
typebotId String typebotId String
typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade) typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)