-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
365 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
2 changes: 2 additions & 0 deletions
2
apps/api/src/prisma/migrations/20240413155927_add_default_workspace/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.