Skip to content

Commit

Permalink
fix(mongodb): 데이터 시간대를 KST로 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
son-daehyeon committed Sep 14, 2024
1 parent 63d587f commit 3d5342e
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 171 deletions.
14 changes: 13 additions & 1 deletion src/common/database/mongo/mongo-model.factory.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ModelDefinition } from '@nestjs/mongoose';
import { Schema } from 'mongoose';
import AutoPopulate from 'mongoose-autopopulate';

const convertToKST = (date: Date): Date => new Date(date.getTime() + 9 * 60 * 60 * 1000);

export class MongoModelFactory {
static generate<T>(name: string, schema: Schema<T>): ModelDefinition {
schema = MongoModelFactory.#setSchemaOptions(schema);
Expand Down Expand Up @@ -32,8 +34,18 @@ export class MongoModelFactory {
}

static #setSchemaOptions(schema: Schema): Schema {
schema.set('timestamps', true);
schema.set('versionKey', false);
schema.set('timestamps', true);

schema.pre('save', function (next) {
if ('createdAt' in this) {
this['createdAt'] = convertToKST(new Date());
}

this['updatedAt'] = convertToKST(new Date());

next();
});

schema.plugin(AutoPopulate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export class ProjectAdminController {
@AuthAdminAccount()
@ApiOperation({ summary: '프로젝트 수정' })
@ApiProperty({ type: UpdateProjectRequestDto })
@ApiCustomErrorResponse([...AuthAdminAccountException, ProjectNotFoundException])
@ApiCustomErrorResponse([
...AuthAdminAccountException,
ProjectNotFoundException,
AlreadyExistsProjectException,
])
async updateProject(@Body() request: UpdateProjectRequestDto): Promise<void> {
return this.projectAdminService.updateProject(request);
}
Expand Down
17 changes: 0 additions & 17 deletions src/domain/activity/project/repository/project.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,6 @@ export class ProjectRepository {
return this.projectModel.findById(id).exec();
}

// Update
async updateTitleById(id: string, title: string): Promise<void> {
await this.projectModel.updateOne({ _id: id }, { title }).exec();
}

async updateContentById(id: string, content: string): Promise<void> {
await this.projectModel.updateOne({ _id: id }, { content }).exec();
}

async updateTagsById(id: string, tags: string[]): Promise<void> {
await this.projectModel.updateOne({ _id: id }, { tags }).exec();
}

async updateImageById(id: string, image: string): Promise<void> {
await this.projectModel.updateOne({ _id: id }, { image }).exec();
}

// Delete
async deleteById(id: string): Promise<void> {
await this.projectModel.deleteOne({ _id: id }).exec();
Expand Down
16 changes: 12 additions & 4 deletions src/domain/activity/project/service/project.admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ export class ProjectAdminService {
throw new ProjectNotFoundException();
}

await this.projectRepository.updateTitleById(projectId, title);
await this.projectRepository.updateContentById(projectId, content);
await this.projectRepository.updateTagsById(projectId, tags);
await this.projectRepository.updateImageById(projectId, image);
const project = (await this.projectRepository.findById(projectId))!;

if (title !== project.title && (await this.projectRepository.existsByTitle(title))) {
throw new AlreadyExistsProjectException();
}

project.title = title;
project.content = content;
project.tags = tags;
project.image = image;

await this.projectRepository.save(project);
}

async deleteProject({ projectId }: DeleteProjectRequestDto): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export class SocialAdminController {
@AuthAdminAccount()
@ApiOperation({ summary: '친목 활동 수정' })
@ApiProperty({ type: UpdateSocialRequestDto })
@ApiCustomErrorResponse([...AuthAdminAccountException, SocialNotFoundException])
@ApiCustomErrorResponse([
...AuthAdminAccountException,
SocialNotFoundException,
AlreadyExistsSocialException,
])
async updateProject(@Body() request: UpdateSocialRequestDto): Promise<void> {
return this.socialAdminService.updateSocial(request);
}
Expand Down
11 changes: 1 addition & 10 deletions src/domain/activity/social/repository/social.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';

import { Content, Social } from '@wink/activity/schema';
import { Social } from '@wink/activity/schema';

import { Model } from 'mongoose';

Expand All @@ -23,15 +23,6 @@ export class SocialRepository {
return this.socialModel.findById(id).exec();
}

// Update
async updateTitleById(id: string, title: string): Promise<void> {
await this.socialModel.updateOne({ _id: id }, { title }).exec();
}

async updateContentsById(id: string, contents: Content[]): Promise<void> {
await this.socialModel.updateOne({ _id: id }, { contents }).exec();
}

// Delete
async deleteById(id: string): Promise<void> {
await this.socialModel.deleteOne({ _id: id }).exec();
Expand Down
12 changes: 10 additions & 2 deletions src/domain/activity/social/service/social.admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ export class SocialAdminService {
throw new SocialNotFoundException();
}

await this.socialRepository.updateTitleById(socialId, title);
await this.socialRepository.updateContentsById(socialId, contents);
const social = (await this.socialRepository.findById(socialId))!;

if (title !== social.title && (await this.socialRepository.existsByTitle(title))) {
throw new AlreadyExistsSocialException();
}

social.title = title;
social.contents = contents;

await this.socialRepository.save(social);
}

async deleteSocial({ socialId }: DeleteSocialRequestDto): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export class StudyAdminController {
@ApiOperation({ summary: '카테고리 수정' })
@ApiProperty({ type: CreateStudyRequestDto })
@ApiCustomResponse(CreateStudyResponseDto)
@ApiCustomErrorResponse([...AuthAdminAccountException, CategoryNotFoundException])
@ApiCustomErrorResponse([
...AuthAdminAccountException,
CategoryNotFoundException,
AlreadyExistsCategoryException,
])
async updateCategory(@Body() request: UpdateCategoryRequestDto): Promise<void> {
return this.studyAdminService.updateCategory(request);
}
Expand Down
5 changes: 2 additions & 3 deletions src/domain/activity/study/repository/category.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ export class CategoryRepository {
return this.categoryModel.findOne({ name }).exec();
}

// Update
async updateNameById(id: string, name: string): Promise<void> {
await this.categoryModel.updateOne({ _id: id }, { name }).exec();
async findById(id: string): Promise<Category | null> {
return this.categoryModel.findById(id).exec();
}

// Delete
Expand Down
10 changes: 9 additions & 1 deletion src/domain/activity/study/service/study.admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ export class StudyAdminService {
throw new CategoryNotFoundException();
}

await this.categoryRepository.updateNameById(categoryId, name);
const category = (await this.categoryRepository.findById(categoryId))!;

if (name !== category.name && (await this.categoryRepository.existsByName(name))) {
throw new AlreadyExistsCategoryException();
}

category.name = name;

await this.categoryRepository.save(category);
}

async deleteCategory({ categoryId }: DeleteCategoryRequestDto): Promise<void> {
Expand Down
39 changes: 1 addition & 38 deletions src/domain/member/repository/member.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';

import { Member, Role } from '@wink/member/schema';
import { Member } from '@wink/member/schema';

import { Model } from 'mongoose';

Expand Down Expand Up @@ -35,43 +35,6 @@ export class MemberRepository {
return this.memberModel.findOne({ email }).select('+password').exec();
}

// Update
async updatePasswordById(id: string, password: string): Promise<void> {
await this.memberModel.updateOne({ _id: id }, { password }).exec();
}

async updateDescriptionById(id: string, description: string | null): Promise<void> {
await this.memberModel.updateOne({ _id: id }, { description }).exec();
}

async updateGithubUrlById(id: string, githubUrl: string | null): Promise<void> {
await this.memberModel.updateOne({ _id: id }, { 'link.github': githubUrl }).exec();
}

async updateInstagramUrlById(id: string, instagramUrl: string | null): Promise<void> {
await this.memberModel.updateOne({ _id: id }, { 'link.instagram': instagramUrl }).exec();
}

async updateBlogById(id: string, blog: string | null): Promise<void> {
await this.memberModel.updateOne({ _id: id }, { 'link.blog': blog }).exec();
}

async updateAvatarById(id: string, avatar: string | null): Promise<void> {
await this.memberModel.updateOne({ _id: id }, { avatar }).exec();
}

async updateRoleById(id: string, role: Role): Promise<void> {
await this.memberModel.updateOne({ _id: id }, { role }).exec();
}

async updateFeeById(id: string, fee: boolean): Promise<void> {
await this.memberModel.updateOne({ _id: id }, { fee }).exec();
}

async updateApprovedById(id: string, approved: boolean): Promise<void> {
await this.memberModel.updateOne({ _id: id }, { approved }).exec();
}

// Delete
async deleteById(id: string): Promise<void> {
await this.memberModel.deleteOne({ _id: id }).exec();
Expand Down
14 changes: 10 additions & 4 deletions src/domain/member/service/member.admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ export class MemberAdminService {
throw new NotWaitingMemberException();
}

await this.memberRepository.updateApprovedById(toId, true);
await this.memberRepository.updateRoleById(toId, Role.MEMBER);
to.approved = true;
to.role = Role.MEMBER;

await this.memberRepository.save(to);

this.mailService.sendTemplate(to.email, new ApproveAccountTemplate(to.name)).then((_) => _);

Expand Down Expand Up @@ -117,7 +119,9 @@ export class MemberAdminService {
throw new SuperRoleException();
}

await this.memberRepository.updateRoleById(to._id, role);
to.role = role;

await this.memberRepository.save(to);

this.eventEmitter.emit(UpdateRoleEvent.EVENT_NAME, new UpdateRoleEvent(from, to, role));
}
Expand All @@ -137,7 +141,9 @@ export class MemberAdminService {
throw new SuperRoleException();
}

await this.memberRepository.updateFeeById(toId, fee);
to.fee = fee;

await this.memberRepository.save(to);

this.eventEmitter.emit(UpdateFeeEvent.EVENT_NAME, new UpdateFeeEvent(from, to, fee));
}
Expand Down
35 changes: 18 additions & 17 deletions src/domain/member/service/member.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ export class MemberService {
member: Member,
{ description, github, instagram, blog }: UpdateMyInfoRequestDto,
): Promise<void> {
const { _id: id } = member;
member.description = description;
member.link.github = github;
member.link.instagram = instagram;
member.link.blog = blog;

await this.memberRepository.updateDescriptionById(id, description);
await this.memberRepository.updateGithubUrlById(id, github);
await this.memberRepository.updateInstagramUrlById(id, instagram);
await this.memberRepository.updateBlogById(id, blog);
await this.memberRepository.save(member);

this.eventEmitter.emit(
UpdateMyInfoEvent.EVENT_NAME,
Expand All @@ -63,17 +63,16 @@ export class MemberService {
member: Member,
{ password, newPassword }: UpdateMyPasswordRequestDto,
): Promise<void> {
const { _id: id } = member;
const fullMember = await this.memberRepository.findByIdWithPassword(id);
const fullMember = (await this.memberRepository.findByIdWithPassword(member._id))!;

if (!(await bcrypt.compare(password, fullMember!.password))) {
if (!(await bcrypt.compare(password, fullMember.password))) {
throw new WrongPasswordException();
}

const salt = await bcrypt.genSalt(10);
const hash = await bcrypt.hash(newPassword, salt);
fullMember.password = await bcrypt.hash(newPassword, salt);

await this.memberRepository.updatePasswordById(id, hash);
await this.memberRepository.save(fullMember);

this.eventEmitter.emit(UpdateMyPasswordEvent.EVENT_NAME, new UpdateMyPasswordEvent(member));
}
Expand All @@ -82,10 +81,12 @@ export class MemberService {
member: Member,
file: Express.Multer.File,
): Promise<UpdateMyAvatarResponseDto> {
const { _id: id, avatar: original } = member;
const { avatar: original } = member;

const avatar = await this.avatarService.upload(file);
await this.memberRepository.updateAvatarById(id, avatar);

member.avatar = avatar;
await this.memberRepository.save(member);

if (original) {
const key = this.avatarService.extractKeyFromUrl(original);
Expand All @@ -99,13 +100,13 @@ export class MemberService {
}

async deleteMyAvatar(member: Member): Promise<void> {
const { _id: id, avatar } = member;

if (avatar) {
const key = this.avatarService.extractKeyFromUrl(avatar);
if (member.avatar) {
const key = this.avatarService.extractKeyFromUrl(member.avatar);

await this.avatarService.delete(key);
await this.memberRepository.updateAvatarById(id, null);

member.avatar = null;
await this.memberRepository.save(member);

this.eventEmitter.emit(DeleteMyAvatarEvent.EVENT_NAME, new DeleteMyAvatarEvent(member));
}
Expand Down
Loading

0 comments on commit 3d5342e

Please sign in to comment.