Skip to content

Commit

Permalink
Merge pull request #48 from gsainfoteam/43-sgh
Browse files Browse the repository at this point in the history
modify: add deletedAt to group
  • Loading branch information
GanghyeonSeo authored Aug 27, 2024
2 parents a99c807 + bbcfa02 commit 450a5b8
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 22 deletions.
3 changes: 2 additions & 1 deletion prisma/dbml/schema.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ Table user {

Table group {
uuid String [pk]
name String [unique, not null]
name String [not null]
description String
createdAt DateTime [default: `now()`, not null]
verifiedAt DateTime
presidentUuid String [not null]
deletedAt DateTime
President user [not null]
UserGroup user_group [not null]
Role role [not null]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- DropIndex
DROP INDEX "group_name_key";

-- AlterTable
ALTER TABLE "group" ADD COLUMN "deleted_at" TIMESTAMP(3);
3 changes: 2 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ model User {

model Group {
uuid String @id @default(uuid()) @db.Uuid
name String @unique
name String
description String?
createdAt DateTime @default(now()) @map("created_at")
verifiedAt DateTime? @map("verified_at")
presidentUuid String @map("president_uuid") @db.Uuid
deletedAt DateTime? @map("deleted_at")
President User @relation(fields: [presidentUuid], references: [uuid])
Expand Down
4 changes: 4 additions & 0 deletions src/external/dto/res/externalInfoRes.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { Authority, Role, RoleExternalAuthority } from '@prisma/client';
import { Exclude } from 'class-transformer';
import { GroupWithRole } from 'src/group/types/groupWithRole';

class RoleExternalAuthorityResDto implements RoleExternalAuthority {
Expand Down Expand Up @@ -56,6 +57,9 @@ class GroupWithRoleResDto implements GroupWithRole {

@ApiProperty({ type: RoleResDto, isArray: true })
Role: RoleResDto[];

@Exclude()
deletedAt: Date | null;
}

export class ExternalInfoResDto {
Expand Down
9 changes: 6 additions & 3 deletions src/group/dto/res/ExpandedGroupRes.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export class ExpandedGroupResDto implements ExpandedGroup {
@Exclude()
_count: { UserGroup: number };

@Exclude()
deletedAt: Date | null;

@Exclude()
presidentUuid: string;

@ApiProperty()
uuid: string;

Expand All @@ -36,9 +42,6 @@ export class ExpandedGroupResDto implements ExpandedGroup {
@ApiProperty()
createdAt: Date;

@ApiProperty()
presidentUuid: string;

@ApiProperty({ type: PresidentResDto })
@Expose()
get president(): User {
Expand Down
5 changes: 4 additions & 1 deletion src/group/dto/res/groupRes.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Group } from '@prisma/client';
import { Expose } from 'class-transformer';
import { Exclude, Expose } from 'class-transformer';

export class GroupResDto implements Group {
@ApiProperty()
Expand All @@ -27,6 +27,9 @@ export class GroupResDto implements Group {
return this.verifiedAt !== null;
}

@Exclude()
deletedAt: Date | null;

constructor(partial: Partial<GroupResDto>) {
Object.assign(this, partial);
}
Expand Down
8 changes: 4 additions & 4 deletions src/group/group.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ export class GroupController {
}

@ApiOperation({
summary: 'Get a group',
description: '특정 그룹을 가져오는 API 입니다.',
summary: 'Get a group by uuid',
description: 'uuid를 바탕으로 특정 그룹을 가져오는 API 입니다.',
})
@ApiOkResponse({ type: ExpandedGroupResDto })
@ApiForbiddenResponse()
@ApiInternalServerErrorResponse()
@Get(':uuid')
async getGroup(
async getGroupByUuid(
@Param('uuid') uuid: string,
@GetUser() user: User,
): Promise<ExpandedGroupResDto> {
return new ExpandedGroupResDto(
await this.groupService.getGroup(uuid, user.uuid),
await this.groupService.getGroupByUuid(uuid, user.uuid),
);
}

Expand Down
34 changes: 29 additions & 5 deletions src/group/group.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Injectable,
InternalServerErrorException,
Logger,
NotFoundException,
} from '@nestjs/common';
import { Authority, Group } from '@prisma/client';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library';
Expand All @@ -20,6 +21,7 @@ export class GroupRepository {
this.logger.log(`getGroupList`);
return this.prismaService.group.findMany({
where: {
deletedAt: null,
UserGroup: {
some: {
userUuid,
Expand All @@ -36,6 +38,7 @@ export class GroupRepository {
this.logger.log(`getGroupListWithRole`);
return this.prismaService.group.findMany({
where: {
deletedAt: null,
UserGroup: {
some: {
userUuid,
Expand All @@ -61,11 +64,12 @@ export class GroupRepository {
});
}

async getGroup(uuid: string, userUuid: string): Promise<ExpandedGroup> {
this.logger.log(`getGroup: ${uuid}`);
async getGroupByUuid(uuid: string, userUuid: string): Promise<ExpandedGroup> {
this.logger.log(`getGroupByUuid: ${uuid}`);
return this.prismaService.group
.findUniqueOrThrow({
where: {
deletedAt: null,
uuid,
UserGroup: {
some: {
Expand All @@ -85,14 +89,31 @@ export class GroupRepository {
.catch((error) => {
if (error instanceof PrismaClientKnownRequestError) {
if (error.code === 'P2025') {
throw new ForbiddenException('Group not found');
throw new NotFoundException('Group not found');
}
throw new InternalServerErrorException('unknown database error');
}
throw new InternalServerErrorException('unknown error');
});
}

async getGroupByName(name: string): Promise<Group | null> {
this.logger.log(`getGroupByName ${name}`);
return this.prismaService.group
.findFirst({
where: {
deletedAt: null,
name,
},
})
.catch((error) => {
if (error instanceof PrismaClientKnownRequestError) {
throw new InternalServerErrorException('unknown database error');
}
throw new InternalServerErrorException('unknown error');
});
}

async validateAuthority(
uuid: string,
authorities: Authority[],
Expand Down Expand Up @@ -170,10 +191,13 @@ export class GroupRepository {

async deleteGroup(uuid: string): Promise<void> {
this.logger.log(`deleteGroup: ${uuid}`);
await this.prismaService.group.delete({
await this.prismaService.group.update({
where: {
uuid,
},
data: {
deletedAt: new Date(),
},
});
}

Expand All @@ -189,7 +213,7 @@ export class GroupRepository {
.catch((error) => {
if (error instanceof PrismaClientKnownRequestError) {
if (error.code === 'P2025') {
throw new ForbiddenException('Group not found');
throw new NotFoundException('Group not found');
}
this.logger.log(error);
throw new InternalServerErrorException('unknown database error');
Expand Down
26 changes: 21 additions & 5 deletions src/group/group.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ForbiddenException, Injectable, Logger } from '@nestjs/common';
import {
ConflictException,
ForbiddenException,
Injectable,
Logger,
} from '@nestjs/common';
import { GroupRepository } from './group.repository';
import { CreateGroupDto } from './dto/req/createGroup.dto';
import { InjectRedis } from '@nestjs-modules/ioredis';
Expand All @@ -23,17 +28,28 @@ export class GroupService {
return this.groupRepository.getGroupList(userUuid);
}

async getGroup(uuid: string, userUuid: string): Promise<ExpandedGroup> {
this.logger.log(`getGroup: ${uuid}`);
return this.groupRepository.getGroup(uuid, userUuid);
async getGroupByUuid(uuid: string, userUuid: string): Promise<ExpandedGroup> {
this.logger.log(`getGroupByUuid: ${uuid}`);
return this.groupRepository.getGroupByUuid(uuid, userUuid);
}

async createGroup(
createGroupDto: CreateGroupDto,
userUuid: string,
): Promise<void> {
this.logger.log(`createGroup: ${createGroupDto.name}`);
await this.groupRepository.createGroup(createGroupDto, userUuid);

const checkGroupExistence = await this.groupRepository.getGroupByName(
createGroupDto.name,
);

if (!checkGroupExistence) {
await this.groupRepository.createGroup(createGroupDto, userUuid);
} else {
throw new ConflictException(
`Group with name ${createGroupDto.name} already exists`,
);
}
}

async deleteGroup(uuid: string, userUuid: string): Promise<void> {
Expand Down
5 changes: 3 additions & 2 deletions src/role/role.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Injectable,
InternalServerErrorException,
Logger,
NotFoundException,
} from '@nestjs/common';
import { Authority, Group, Role } from '@prisma/client';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library';
Expand Down Expand Up @@ -84,7 +85,7 @@ export class RoleRepository {
}
if (error.code === 'P2025') {
this.logger.debug(`Group ${groupUuid} not found`);
throw new ForbiddenException('Group not found');
throw new NotFoundException('Group not found');
}
this.logger.error(`Database error: ${error.message}`);
throw new InternalServerErrorException('Database error');
Expand Down Expand Up @@ -135,7 +136,7 @@ export class RoleRepository {
if (error instanceof PrismaClientKnownRequestError) {
if (error.code === 'P2025') {
this.logger.debug(`Group ${groupUuid} not found`);
throw new ForbiddenException('Group not found');
throw new NotFoundException('Group not found');
}
this.logger.error(`Database error: ${error.message}`);
throw new InternalServerErrorException('Database error');
Expand Down

0 comments on commit 450a5b8

Please sign in to comment.