Skip to content

Commit

Permalink
Feat: Sort map members
Browse files Browse the repository at this point in the history
- MapResponseDTO에서 지도의 멤버들이 정해진 순서대로 정렬되어 response될 수 있는 로직 추가
  • Loading branch information
sally0226 committed Aug 18, 2024
1 parent 5b6b971 commit ba6ec6d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 26 deletions.
26 changes: 25 additions & 1 deletion src/map/dtos/map-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ApiProperty } from '@nestjs/swagger';
import {
GroupMap,
PlaceForMap,
User,
UserMap,
UserMapRole,
UserMapRoleValueType,
Expand Down Expand Up @@ -44,7 +45,7 @@ export class MapResponseDto {
@ApiProperty({ type: UserResponseDto })
createBy: UserResponseDto;

constructor(map: GroupMap, placeForMap: PlaceForMap[]) {
constructor(map: GroupMap, placeForMap: PlaceForMap[] = null) {
this.id = map.id;
this.name = map.name;
this.createdAt = map.createdAt;
Expand All @@ -59,4 +60,27 @@ export class MapResponseDto {
});
if (map.createBy) this.createBy = new UserResponseDto(map.createBy);
}

public sortMembers(currentUser: User = null) {
this.users.sort((a: MapUser, b: MapUser): number => {
// 본인을 최우선으로
if (currentUser) {
const isCurrentUserA = a.id === currentUser.id;
const isCurrentUserB = b.id === currentUser.id;

if (isCurrentUserA && !isCurrentUserB) return -1;
if (!isCurrentUserA && isCurrentUserB) return 1;
}

// 모임장을 다음으로
const isAdminA = a.role === UserMapRole.ADMIN;
const isAdminB = b.role === UserMapRole.ADMIN;

if (isAdminA && !isAdminB) return -1;
if (!isAdminA && isAdminB) return 1;

// 나머지 멤버는 그대로
return 0;
});
}
}
33 changes: 24 additions & 9 deletions src/map/map.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { TagResponseDto } from 'src/map/dtos/tag-response.dto';
import { UseAuthGuard } from '../common/decorators/auth-guard.decorator';
import { UseMapRoleGuard } from '../common/decorators/map-role-guard.decorator';
import { CurrentUser } from '../common/decorators/user.decorator';
import { InviteLink, User, UserMapRole, UserRole } from '../entities';
import { GroupMap, InviteLink, User, UserMapRole, UserRole } from '../entities';
import { InviteLinkService } from '../invite-link/invite-link.service';
import { CreateMapDto } from './dtos/create-map.dto';
import { InviteLinkResponseDto } from './dtos/invite-link-response.dto';
Expand Down Expand Up @@ -53,7 +53,7 @@ export class MapController {
@ApiOperation({ summary: '사용자가 속해있는 지도를 가져옵니다.' })
@ApiOkResponse({ type: [MapItemForUserDto] })
@UseAuthGuard([UserRole.USER])
findAll(@CurrentUser() user: User) {
findAll(@CurrentUser() user: User): Promise<MapItemForUserDto[]> {
return this.mapService.findAll(user);
}

Expand All @@ -62,16 +62,28 @@ export class MapController {
@ApiOkResponse({ type: MapResponseDto })
@UseMapRoleGuard()
@UseAuthGuard([UserRole.USER])
findOne(@Param('id') id: string) {
return this.mapService.findOne({ id });
async findOne(
@Param('id') id: string,
@CurrentUser() user: User,
): Promise<MapResponseDto> {
const dto = await this.mapService.findOne({ id });
dto.sortMembers(user);
return dto;
}

@Patch(':id')
@ApiOkResponse({ type: MapResponseDto })
@UseMapRoleGuard([UserMapRole.ADMIN])
@UseAuthGuard([UserRole.USER])
update(@Param('id') id: string, @Body() updateMapDto: UpdateMapDto) {
return this.mapService.update(id, updateMapDto);
async update(
@Param('id') id: string,
@Body() updateMapDto: UpdateMapDto,
@CurrentUser() user: User,
): Promise<MapResponseDto> {
const map: GroupMap = await this.mapService.update(id, updateMapDto);
const dto = new MapResponseDto(map);
dto.sortMembers(user);
return dto;
}

@Delete(':id')
Expand All @@ -93,7 +105,7 @@ export class MapController {
@CurrentUser() user: User,
@Param('id') id: string,
): Promise<InviteLinkResponseDto> {
const entity = await this.inviteLinkService.create(id, user);
const entity: InviteLink = await this.inviteLinkService.create(id, user);
return new InviteLinkResponseDto(entity);
}

Expand Down Expand Up @@ -140,8 +152,11 @@ export class MapController {
const inviteLink: InviteLink =
await this.inviteLinkService.validate(inviteLinkToken);

const map = await this.mapService.findOne(inviteLink.map);
const previewList = await this.mapService.getPlacesPreview(inviteLink.map);
const map: MapResponseDto = await this.mapService.findOne(inviteLink.map);
map.sortMembers();
const previewList: string[] = await this.mapService.getPlacesPreview(
inviteLink.map,
);

const inviteLinkResponseDto = new InviteLinkResponseDto(inviteLink);
return new CheckInviteLinkResponseDto(
Expand Down
36 changes: 20 additions & 16 deletions src/map/map.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class MapService {
);

return userMapList.map(({ map, role }) => {
const mapItemForUser = new MapItemForUserDto();
const mapItemForUser: MapItemForUserDto = new MapItemForUserDto();
mapItemForUser.id = map.id;
mapItemForUser.name = map.name;
mapItemForUser.createdAt = map.createdAt;
Expand All @@ -110,8 +110,8 @@ export class MapService {
return new MapResponseDto(entity, placeForMap);
}

async update(id: string, updateMapDto: UpdateMapDto) {
const map = await this.mapRepository.findOne(id);
async update(id: string, updateMapDto: UpdateMapDto): Promise<GroupMap> {
const map: GroupMap = await this.mapRepository.findOne(id);
if (!map) {
throw new MapNotFoundException();
}
Expand Down Expand Up @@ -148,12 +148,12 @@ export class MapService {
mapId: string,
createTagDto: CreateTagDto,
): Promise<TagResponseDto> {
const entity = await this.tagRepository.findOne({
const entity: Tag = await this.tagRepository.findOne({
map: rel(GroupMap, mapId),
name: createTagDto.name,
});

const tagIcon = await this.tagIconRepository.findOne({
const tagIcon: TagIcon = await this.tagIconRepository.findOne({
name: createTagDto.name,
});

Expand All @@ -172,7 +172,7 @@ export class MapService {
}

async removeTag(mapId: string, name: string) {
const tag = await this.tagRepository.findOne({
const tag: Tag = await this.tagRepository.findOne({
map: rel(GroupMap, mapId),
name,
});
Expand All @@ -183,7 +183,7 @@ export class MapService {
}

async createUserMap(user: User, map: GroupMap, role?: UserMapRoleValueType) {
const existUserMap = await this.userMapRepository.findOne({
const existUserMap: UserMap = await this.userMapRepository.findOne({
user: user,
map: map,
});
Expand All @@ -205,24 +205,28 @@ export class MapService {
map: { id: mapId },
};

const userMap = await this.userMapRepository.findOne(where);
const userMap: UserMap = await this.userMapRepository.findOne(where);
if (!userMap) {
throw new UserMapNotFoundException();
}
return userMap;
}

async getPlacesPreview(map: GroupMap): Promise<string[]> {
const placesForMapList = await this.placeForMapRepository.find(
{ map },
{
populate: ['place', 'place.kakaoPlace', 'createdBy'],
orderBy: { createdAt: 'desc' },
},
const placesForMapList: PlaceForMap[] =
await this.placeForMapRepository.find(
{ map },
{
populate: ['place', 'place.kakaoPlace', 'createdBy'],
orderBy: { createdAt: 'desc' },
},
);
const subList: PlaceForMap[] = placesForMapList.slice(
0,
INVITE_LINK_PREVIEW_LENGTH,
);
const subList = placesForMapList.slice(0, INVITE_LINK_PREVIEW_LENGTH);
return subList.map((item) => {
const photoList = item.place.kakaoPlace.photoList;
const photoList: string[] = item.place.kakaoPlace.photoList;
if (photoList.length > 0) {
return photoList[0];
}
Expand Down

0 comments on commit ba6ec6d

Please sign in to comment.