Skip to content

Commit 0668dcc

Browse files
Merge pull request #314 from boostcampwm-2024/bhg-be-#313
초기 메인 워크스페이스 생성
2 parents 8f513b2 + 3285140 commit 0668dcc

File tree

3 files changed

+161
-131
lines changed

3 files changed

+161
-131
lines changed

apps/backend/src/workspace/workspace.service.ts

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { Injectable, Logger } from '@nestjs/common';
22
import { UserRepository } from '../user/user.repository';
33
import { WorkspaceRepository } from './workspace.repository';
44
import { RoleRepository } from '../role/role.repository';
@@ -8,14 +8,30 @@ import { UserNotFoundException } from '../exception/user.exception';
88
import { Workspace } from './workspace.entity';
99
import { WorkspaceNotFoundException } from '../exception/workspace.exception';
1010
import { NotWorkspaceOwnerException } from '../exception/workspace-auth.exception';
11-
11+
enum MainWorkspace {
12+
OWNER_SNOWFLAKEID = 'admin',
13+
OWNER_PROVIDER_ID = 'adminProviderId',
14+
OWNER_PROVIDER = 'adminProvider',
15+
OWNER_EMAIL = '[email protected]',
16+
WORKSPACE_SNOWFLAKEID = 'main',
17+
WORKSPACE_TITLE = 'main workspace',
18+
WORKSPACE_DESCRIPTION = '모든 유저가 접근 가능한 메인 workspace',
19+
WORKSPACE_VISIBILITY = 'public',
20+
}
1221
@Injectable()
1322
export class WorkspaceService {
23+
private readonly logger = new Logger(WorkspaceService.name); // 클래스 이름을 context로 설정
24+
1425
constructor(
1526
private readonly workspaceRepository: WorkspaceRepository,
1627
private readonly userRepository: UserRepository,
1728
private readonly roleRepository: RoleRepository,
18-
) {}
29+
) {
30+
console.log('환경 : ', process.env.NODE_ENV);
31+
if (process.env.NODE_ENV !== 'test') {
32+
this.initializeMainWorkspace();
33+
}
34+
}
1935

2036
async createWorkspace(
2137
userId: number,
@@ -89,4 +105,62 @@ export class WorkspaceService {
89105
role: role.role as 'owner' | 'guest',
90106
}));
91107
}
108+
109+
// 가장 처음에 모두가 접속할 수 있는 main workspace를 생성한다.
110+
async initializeMainWorkspace() {
111+
let findOwner = await this.userRepository.findOneBy({
112+
snowflakeId: MainWorkspace.OWNER_SNOWFLAKEID,
113+
});
114+
115+
// 존재하지 않을 때만 생성한다.
116+
if (!findOwner) {
117+
// main workspace owner를 생성한다.
118+
const owner = await this.userRepository.save({
119+
snowflakeId: MainWorkspace.OWNER_SNOWFLAKEID,
120+
providerId: MainWorkspace.OWNER_PROVIDER_ID,
121+
provider: MainWorkspace.OWNER_PROVIDER,
122+
email: MainWorkspace.OWNER_EMAIL,
123+
});
124+
125+
findOwner = owner;
126+
}
127+
this.logger.log('main workspace owner가 존재합니다.');
128+
129+
// main workspace를 찾는다.
130+
let findWorkspace = await this.workspaceRepository.findOneBy({
131+
snowflakeId: MainWorkspace.WORKSPACE_SNOWFLAKEID,
132+
});
133+
134+
// owner는 존재하지만 워크스페이스가 없으면 생성한다.
135+
if (!findWorkspace) {
136+
findWorkspace = await this.workspaceRepository.save({
137+
snowflakeId: MainWorkspace.WORKSPACE_SNOWFLAKEID,
138+
owner: findOwner,
139+
title: MainWorkspace.WORKSPACE_TITLE,
140+
description: MainWorkspace.WORKSPACE_DESCRIPTION,
141+
visibility: MainWorkspace.WORKSPACE_VISIBILITY,
142+
});
143+
this.logger.log('main workspace를 생성했습니다.');
144+
}
145+
this.logger.log('main workspace가 존재합니다.');
146+
147+
// owner의 role을 찾는다.
148+
const role = await this.roleRepository.findOne({
149+
where: {
150+
workspaceId: findWorkspace.id,
151+
userId: findOwner.id,
152+
},
153+
});
154+
155+
// owner의 role이 없으면 생성한다.
156+
if (!role) {
157+
await this.roleRepository.save({
158+
workspaceId: findWorkspace.id,
159+
userId: findOwner.id,
160+
workspace: findWorkspace,
161+
user: findOwner,
162+
role: 'owner',
163+
});
164+
}
165+
}
92166
}

apps/backend/src/yjs/yjs.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export class YjsService
4444
private readonly pageService: PageService,
4545
private readonly edgeService: EdgeService,
4646
private readonly redisService: RedisService,
47-
) {}
47+
) {
48+
49+
}
4850

4951
@WebSocketServer()
5052
server: Server;

0 commit comments

Comments
 (0)