Skip to content

Commit

Permalink
added feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Apr 14, 2024
1 parent 97877c4 commit a94535b
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 169 deletions.
28 changes: 8 additions & 20 deletions apps/api/src/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,30 +157,18 @@ export class AuthService {
let user = await this.findUserByEmail(email)
// We need to create the user if it doesn't exist yet
if (!user) {
user = await this.createUser(email, name, profilePictureUrl)
user = await createUser(
{
email,
name,
profilePictureUrl
},
this.prisma
)
}
return user
}

private async createUser(
email: string,
name: string,
profilePictureUrl: string
) {
// Create the user
const user = await createUser(
{
email,
name,
profilePictureUrl
},
this.prisma
)
this.logger.log(`User created: ${email}`)

return user
}

private async generateToken(id: string) {
return await this.jwt.signAsync({ id })
}
Expand Down
29 changes: 26 additions & 3 deletions apps/api/src/common/create-user.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { User } from '@prisma/client'
import { User, Workspace } from '@prisma/client'
import { PrismaService } from '../prisma/prisma.service'
import { CreateUserDto } from '../user/dto/create.user/create.user'
import createWorkspace from './create-workspace'
import { Logger } from '@nestjs/common'

const createUser = async (
dto: Partial<CreateUserDto>,
prisma: PrismaService
): Promise<User> => {
): Promise<
User & {
defaultWorkspace: Workspace
}
> => {
const logger = new Logger('createUser')

// Create the user
return await prisma.user.create({
const user = await prisma.user.create({
data: {
email: dto.email,
name: dto.name,
Expand All @@ -17,6 +25,21 @@ const createUser = async (
isOnboardingFinished: dto.isOnboardingFinished ?? false
}
})

// Create the user's default workspace
const workspace = await createWorkspace(
user,
{ name: 'My Workspace' },
prisma,
true
)

logger.log(`Created user ${user.id} with default workspace ${workspace.id}`)

return {
...user,
defaultWorkspace: workspace
}
}

export default createUser
95 changes: 95 additions & 0 deletions apps/api/src/common/create-workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { Authority, EventSource, EventType, User } from '@prisma/client'
import createEvent from './create-event'
import { CreateWorkspace } from '../workspace/dto/create.workspace/create.workspace'
import { v4 } from 'uuid'
import { PrismaService } from '../prisma/prisma.service'
import { Logger } from '@nestjs/common'

export default async function createWorkspace(
user: User,
dto: CreateWorkspace,
prisma: PrismaService,
isDefault?: boolean
) {
const workspaceId = v4()
const logger = new Logger('createWorkspace')

const createNewWorkspace = prisma.workspace.create({
data: {
id: workspaceId,
name: dto.name,
description: dto.description,
approvalEnabled: dto.approvalEnabled,
isFreeTier: true,
ownerId: user.id,
isDefault,
roles: {
createMany: {
data: [
{
name: 'Admin',
authorities: [Authority.WORKSPACE_ADMIN],
hasAdminAuthority: true,
colorCode: '#FF0000'
}
]
}
}
}
})

// Add the owner to the workspace
const assignOwnership = prisma.workspaceMember.create({
data: {
workspace: {
connect: {
id: workspaceId
}
},
user: {
connect: {
id: user.id
}
},
invitationAccepted: true,
roles: {
create: {
role: {
connect: {
workspaceId_name: {
workspaceId: workspaceId,
name: 'Admin'
}
}
}
}
}
}
})

const result = await prisma.$transaction([
createNewWorkspace,
assignOwnership
])
const workspace = result[0]

await createEvent(
{
triggeredBy: user,
entity: workspace,
type: EventType.WORKSPACE_CREATED,
source: EventSource.WORKSPACE,
title: `Workspace created`,
metadata: {
workspaceId: workspace.id,
name: workspace.name
},
workspaceId: workspace.id
},
prisma
)

logger.log(`Created workspace ${dto.name} (${workspaceId})`)

return workspace
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Workspace" ADD COLUMN "isDefault" BOOLEAN NOT NULL DEFAULT false;
1 change: 1 addition & 0 deletions apps/api/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ model Workspace {
updatedAt DateTime @updatedAt
ownerId String
approvalEnabled Boolean @default(false)
isDefault Boolean @default(false)
lastUpdatedBy User? @relation(fields: [lastUpdatedById], references: [id], onUpdate: Cascade, onDelete: SetNull)
lastUpdatedById String?
Expand Down
10 changes: 5 additions & 5 deletions apps/api/src/user/dto/create.user/create.user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class CreateUserDto {
example: 'John Doe',
default: null
})
name: string
name?: string

@IsString()
@IsEmail()
Expand All @@ -37,7 +37,7 @@ export class CreateUserDto {
example: 'https://example.com/profile.jpg',
default: null
})
profilePictureUrl: string
profilePictureUrl?: string

@IsBoolean()
@IsOptional()
Expand All @@ -49,7 +49,7 @@ export class CreateUserDto {
example: true,
default: true
})
isActive: boolean
isActive?: boolean

@IsBoolean()
@IsOptional()
Expand All @@ -61,7 +61,7 @@ export class CreateUserDto {
example: true,
default: false
})
isOnboardingFinished: boolean
isOnboardingFinished?: boolean

@IsBoolean()
@IsOptional()
Expand All @@ -73,5 +73,5 @@ export class CreateUserDto {
example: false,
default: false
})
isAdmin: boolean
isAdmin?: boolean
}
10 changes: 10 additions & 0 deletions apps/api/src/user/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,22 @@ export class UserService {
}

private async deleteUserById(userId: User['id']) {
// Delete the default workspace of this user
await this.prisma.workspace.deleteMany({
where: {
ownerId: userId,
isDefault: true
}
})

// Delete the user
await this.prisma.user.delete({
where: {
id: userId
}
})

this.log.log(`Deleted user ${userId}`)
}

async createUser(user: CreateUserDto) {
Expand Down
Loading

0 comments on commit a94535b

Please sign in to comment.