Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 프로젝트 외부 링크 추가 API 구현 #296

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { ErrorExceptionFilter } from './common/exception-filter/exception.filter
import { AuthenticationGuard } from './common/guard/authentication.guard';
import { MemberRepository } from './member/repository/member.repository';
import { Memo } from './project/entity/memo.entity';
import { Link } from './project/entity/link.entity.';

@Module({
imports: [
Expand All @@ -47,6 +48,7 @@ import { Memo } from './project/entity/memo.entity';
Project,
ProjectToMember,
Memo,
Link,
],
synchronize: ConfigService.get('NODE_ENV') == 'PROD' ? false : true,
}),
Expand Down
20 changes: 20 additions & 0 deletions backend/src/project/dto/LinkCreateRequest.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Type } from 'class-transformer';
import { IsNotEmpty, IsString, Matches, ValidateNested } from 'class-validator';

class Link {
@IsString()
url: string;

@IsString()
description: string;
}

export class LinkCreateRequestDto {
@Matches(/^create$/)
action: string;

@IsNotEmpty()
@ValidateNested()
@Type(() => Link)
content: Link;
}
43 changes: 43 additions & 0 deletions backend/src/project/entity/link.entity..ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
import { Project } from './project.entity';

@Entity()
export class Link {
@PrimaryGeneratedColumn('increment', { type: 'int' })
id: number;

@Column({ type: 'int', name: 'project_id' })
projectId: number;

@ManyToOne(() => Project, (project) => project.id, { nullable: false })
@JoinColumn({ name: 'project_id' })
project: Project;

@Column({ type: 'varchar', length: 255, nullable: false })
url: string;

@Column({ type: 'varchar', length: 255, nullable: false })
description: string;

@CreateDateColumn({ type: 'timestamp' })
created_at: Date;

@UpdateDateColumn({ type: 'timestamp' })
updated_at: Date;

static of(project: Project, url: string, description: string) {
const newLink = new Link();
newLink.project = project;
newLink.url = url;
newLink.description = description;
return newLink;
}
}
4 changes: 4 additions & 0 deletions backend/src/project/entity/project.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Generated,
JoinColumn,
} from 'typeorm';
import { Link } from './link.entity.';
import { Memo } from './memo.entity';
import { ProjectToMember } from './project-member.entity';

Expand Down Expand Up @@ -41,6 +42,9 @@ export class Project {
@OneToMany(() => Memo, (memo) => memo.id)
memoList: Memo[];

@OneToMany(() => Link, (link) => link.id)
linkList: Link[];

static of(title: string, subject: string) {
const newProject = new Project();
newProject.title = title;
Expand Down
3 changes: 2 additions & 1 deletion backend/src/project/project.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import { MemberRepository } from 'src/member/repository/member.repository';
import { ProjectWebsocketGateway } from './websocket.gateway';
import { Memo } from './entity/memo.entity';
import { MemberService } from 'src/member/service/member.service';
import { Link } from './entity/link.entity.';

@Module({
imports: [
LesserJwtModule,
TypeOrmModule.forFeature([Project, ProjectToMember, Member, Memo]),
TypeOrmModule.forFeature([Project, ProjectToMember, Member, Memo, Link]),
],
controllers: [ProjectController],
providers: [
Expand Down
7 changes: 7 additions & 0 deletions backend/src/project/project.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ProjectToMember } from './entity/project-member.entity';
import { Member } from 'src/member/entity/member.entity';
import { Memo, memoColor } from './entity/memo.entity';
import { MemberRepository } from 'src/member/repository/member.repository';
import { Link } from './entity/link.entity.';

@Injectable()
export class ProjectRepository {
Expand All @@ -18,6 +19,8 @@ export class ProjectRepository {
private readonly memberRepository: Repository<Member>,
@InjectRepository(Memo)
private readonly memoRepository: Repository<Memo>,
@InjectRepository(Link)
private readonly linkRepository: Repository<Link>,
) {}

create(project: Project): Promise<Project> {
Expand Down Expand Up @@ -89,4 +92,8 @@ export class ProjectRepository {
where: { id },
});
}

createLink(link: Link): Promise<Link> {
return this.linkRepository.save(link);
}
}
22 changes: 22 additions & 0 deletions backend/src/project/service/project.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Member } from 'src/member/entity/member.entity';
import { Project } from '../entity/project.entity';
import { ProjectToMember } from '../entity/project-member.entity';
import { Memo, memoColor } from '../entity/memo.entity';
import { Link } from '../entity/link.entity.';

describe('ProjectService', () => {
let projectService: ProjectService;
Expand All @@ -28,6 +29,7 @@ describe('ProjectService', () => {
updateMemoColor: jest.fn(),
findMemoById: jest.fn(),
getProjectMemberList: jest.fn(),
createLink: jest.fn(),
},
},
],
Expand Down Expand Up @@ -263,4 +265,24 @@ describe('ProjectService', () => {
).rejects.toThrow('project does not have this memo');
});
});

describe('Create link', () => {
const [title, subject] = ['title', 'subject'];
const project = Project.of(title, subject);
it('should return created link', async () => {
const url = 'url';
const description = 'description';
jest
.spyOn(projectRepository, 'createLink')
.mockResolvedValue(Link.of(project, url, description));

const link: Link = await projectService.createLink(
project,
url,
description,
);
expect(link.url).toBe(url);
expect(link.description).toBe(description);
});
});
});
6 changes: 6 additions & 0 deletions backend/src/project/service/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Member } from 'src/member/entity/member.entity';
import { Project } from '../entity/project.entity';
import { ProjectToMember } from '../entity/project-member.entity';
import { Memo, memoColor } from '../entity/memo.entity';
import { Link } from '../entity/link.entity.';

@Injectable()
export class ProjectService {
Expand Down Expand Up @@ -76,4 +77,9 @@ export class ProjectService {
await this.projectRepository.updateMemoColor(id, color);
return true;
}

createLink(project: Project, url: string, description: string) {
const newLink = Link.of(project, url, description);
return this.projectRepository.createLink(newLink);
}
}
31 changes: 31 additions & 0 deletions backend/src/project/websocket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { InitLandingResponseDto } from './dto/InitLandingResponse.dto';
import { MemoColorUpdateRequestDto } from './dto/MemoColorUpdateRequest.dto';
import { MemberUpdateRequestDto } from './dto/MemberUpdateRequest.dto';
import { MemberStatus } from './enum/MemberStatus.enum';
import { LinkCreateRequestDto } from './dto/LinkCreateRequest.dto';

export interface ClientSocket extends Socket {
projectId?: number;
Expand Down Expand Up @@ -252,6 +253,36 @@ export class ProjectWebsocketGateway
this.sendMemberStatusUpdate(client);
}

@SubscribeMessage('link')
async handleLinkEvent(
@ConnectedSocket() client: ClientSocket,
@MessageBody() data: LinkCreateRequestDto,
) {
if (data.action === 'create') {
const errors = await validate(plainToClass(LinkCreateRequestDto, data));
if (errors.length > 0) {
const errorList = this.getRecursiveErrorMsgList(errors);
client.emit('error', { errorList });
return;
}
const { content } = data as LinkCreateRequestDto;
const createLink = await this.projectService.createLink(
client.project,
content.url,
content.description,
);
client.nsp.to('landing').emit('landing', {
domain: 'link',
action: 'create',
content: {
id: createLink.id,
url: createLink.url,
description: createLink.description,
},
});
}
}

notifyJoinToConnectedMembers(projectId: number, member: Member) {
const projectNamespace = this.namespaceMap.get(projectId);
if (!projectNamespace) return;
Expand Down
91 changes: 91 additions & 0 deletions backend/test/project/ws-link.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {
app,
appInit,
connectServer,
createMember,
createProject,
getProjectLinkId,
joinProject,
memberFixture,
memberFixture2,
projectPayload,
} from 'test/setup';
import {
emitJoinLanding,
expectUpdatedMemberStatus,
handleConnectErrorWithReject,
handleErrorWithReject,
initLanding,
initLandingAndReturnId,
} from './ws-common';

describe('WS link', () => {
beforeEach(async () => {
await app.close();
await appInit();
await app.listen(3000);
});
describe('link create', () => {
it('should return created link data when received create link request', async () => {
let socket1;
let socket2;
return new Promise<void>(async (resolve, reject) => {
// 회원1 회원가입 + 프로젝트 생성
const accessToken = (await createMember(memberFixture, app))
.accessToken;
const project = await createProject(accessToken, projectPayload, app);
const projectLinkId = await getProjectLinkId(accessToken, project.id);

// 회원2 회원가입 + 프로젝트 참여
const accessToken2 = (await createMember(memberFixture2, app))
.accessToken;
await joinProject(accessToken2, projectLinkId);

socket1 = connectServer(project.id, accessToken);
handleConnectErrorWithReject(socket1, reject);
handleErrorWithReject(socket1, reject);
await emitJoinLanding(socket1);
await initLanding(socket1);

socket2 = connectServer(project.id, accessToken2);
handleConnectErrorWithReject(socket2, reject);
handleErrorWithReject(socket2, reject);
await emitJoinLanding(socket2);
const memberId2 = await initLandingAndReturnId(socket2);
await expectUpdatedMemberStatus(socket1, 'on', memberId2);

const url = '유효한 url';
const description = '피그마';
const requestData = {
action: 'create',
content: { url, description },
};
socket1.emit('link', requestData);
await Promise.all([
expectCreateLink(socket1, url, description),
expectCreateLink(socket2, url, description),
]);
resolve();
}).finally(() => {
socket1.close();
socket2.close();
});
});

const expectCreateLink = (socket, url, description) => {
return new Promise<void>((resolve, reject) => {
socket.on('landing', async (data) => {
const { content, action, domain } = data;
expect(domain).toBe('link');
expect(action).toBe('create');
expect(content?.description).toBe(description);
expect(content?.id).toBeDefined();
expect(content?.url).toBe(url);
expect(content?.description).toBe(description);
socket.off('landing');
resolve();
});
});
};
});
});
Loading