Skip to content

Commit

Permalink
refactor: extModule 분리
Browse files Browse the repository at this point in the history
external 분리와 함께 필요한 모듈만 import 하도록 수정했습니다.

- #189
  • Loading branch information
niamu01 committed Dec 8, 2023
1 parent 14d30a2 commit 2a26b21
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 169 deletions.
6 changes: 4 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './auth/auth.module';
import configuration from './configs/configuration';
import TypeOrmConfigService from './configs/typeorm.config';
import { ExtModule } from './ext/ext.module';
import { Cabi42Module } from './external/cabi42/cabi42.module';
import { Where42Module } from './external/where42/where42.module';
import { SessionMiddleware } from './middleware/session-middleware';
import { RedirectModule } from './redirect/redirect.module';
import { ReissueModule } from './reissue/reissue.module';
Expand All @@ -31,7 +32,8 @@ import { UserModule } from './user/user.module';
TagLogModule,
TagLogModule2,
UserModule,
ExtModule,
Where42Module,
Cabi42Module,
ReissueModule,
],
providers: [SessionMiddleware],
Expand Down
151 changes: 0 additions & 151 deletions src/ext/cabi42.controller.ts

This file was deleted.

70 changes: 70 additions & 0 deletions src/external/cabi42/cabi42.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
Controller,
Get,
Logger,
ParseIntPipe,
Query,
UseGuards,
} from '@nestjs/common';
import {
ApiBearerAuth,
ApiOperation,
ApiQuery,
ApiResponse,
ApiTags,
} from '@nestjs/swagger';
import { UserSessionDto } from 'src/auth/dto/user.session.dto';
import { ExtAuthGuard } from 'src/auth/guard/ext-auth.guard';
import { User } from 'src/auth/user.decorator';
import { Cabi42Service } from './cabi42.service';
import { Cabi42ResponseDto } from './dto/cabi42.response.dto';

@ApiTags('Cabi42 전용 API')
@Controller('ext/cabi42')
@ApiBearerAuth()
@UseGuards(ExtAuthGuard)
export class Cabi42Controller {
private logger = new Logger(Cabi42Controller.name);

constructor(private cabi42Service: Cabi42Service) {}

/**
* 모든 이용자의 월별 누적 출입시간을 반환합니다.
*
* @returns Cabi42ResponseDto[]
*/
@ApiOperation({
summary: '모든 이용자의 월별 체류시간 조회',
description: '모든 이용자의 월별 체류시간을 조회합니다.',
})
@ApiResponse({
status: 200,
type: [Cabi42ResponseDto],
description: '조회 성공',
})
@ApiResponse({ status: 400, description: '잘못된 날짜 입력' })
@ApiResponse({ status: 401, description: '접근 권한 없음' })
@ApiResponse({
status: 500,
description: '서버 내부 에러 (백앤드 관리자 문의 필요)',
})
@ApiQuery({
name: 'year',
description: '년도',
required: true,
})
@ApiQuery({
name: 'month',
description: '월',
required: true,
})
@Get('permonth')
async getPerMonth(
@User() user: UserSessionDto,
@Query('year', ParseIntPipe) year: number,
@Query('month', ParseIntPipe) month: number,
): Promise<Cabi42ResponseDto[]> {
this.logger.debug(`@getPerMonth) ${year}-${month} by ${user.login}`);
return await this.cabi42Service.cabi42(year, month);
}
}
23 changes: 23 additions & 0 deletions src/external/cabi42/cabi42.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TagLog } from 'src/entities/tag-log.entity';
import { TagLogRepository } from 'src/tag-log-v1/repository/mysql/tag-log.repository';
import { TagLogModule2 } from 'src/tag-log-v2/tag-log-v2.module';
import { UserModule } from 'src/user/user.module';
import { Cabi42Controller } from './cabi42.controller';
import { Cabi42Service } from './cabi42.service';

const tagLogRepo = {
provide: 'ITagLogRepository',
useClass: TagLogRepository,
};

/**
* 24Hane 기능 외 다른 서비스에서 사용하는 외부 API 모듈
*/
@Module({
imports: [UserModule, TagLogModule2, TypeOrmModule.forFeature([TagLog])],
controllers: [Cabi42Controller],
providers: [tagLogRepo, Cabi42Service],
})
export class Cabi42Module {}
58 changes: 58 additions & 0 deletions src/external/cabi42/cabi42.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Injectable, Logger } from '@nestjs/common';
import { TagLogService } from 'src/tag-log-v2/tag-log-v2.service';
import { UserService } from 'src/user/user.service';
import { Cabi42ResponseDto } from './dto/cabi42.response.dto';

@Injectable()
export class Cabi42Service {
private logger = new Logger(Cabi42Service.name);

constructor(
private userService: UserService,
private tagLogService: TagLogService,
) {}

async cabi42(year: number, month: number): Promise<Cabi42ResponseDto[]> {
this.logger.debug(`@cabi42) check all user of ${year}-${month}`);
// admin을 제외한 모든 유저
const cadets = await this.userService.getAllIds(false);

return await Promise.all(
cadets.map((cadet) =>
this.getAccumulationInMonthById(
cadet.user_id,
cadet.login,
year,
month,
),
),
);
}

async findIdByLogin(login: string): Promise<number> {
this.logger.debug(`@findIdByLogin) ${login}`);
return this.userService.findIdByLogin(login);
}

async getAccumulationInMonthById(
id: number,
login: string,
year: number,
month: number,
): Promise<Cabi42ResponseDto> {
this.logger.debug(
`@cabi42) check user: ${login}(${id}) of ${year}-${month}`,
);
const date = new Date(`${year}-${month}`);
const resultMonth = await this.tagLogService.getAllTagPerMonth(id, date);
const monthAccumationTime = resultMonth.reduce(
(prev, result) => result.durationSecond + prev,
0,
);
return {
id,
login,
monthAccumationTime,
};
}
}
10 changes: 10 additions & 0 deletions src/external/cabi42/dto/cabi42.response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';
import { UserIdType } from 'src/tag-log-v2/dto/admin/user-id.type';

export class Cabi42ResponseDto extends UserIdType {
@ApiProperty({
description: '월별 누적시간 (초 단위)',
example: 12345,
})
monthAccumationTime: number;
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeviceDto } from 'src/ext/dto/device.dto';
import { DeviceDto } from 'src/external/where42/dto/device.dto';

export interface IDeviceInfoRepository {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { InjectRepository } from '@nestjs/typeorm';
import { DeviceInfo } from 'src/entities/device-info.entity';
import { DeviceDto } from 'src/ext/dto/device.dto';
import { IDeviceInfoRepository } from '../interface/device-info.repository.interface';
import { DeviceDto } from 'src/external/where42/dto/device.dto';
import { Repository } from 'typeorm';
import { IDeviceInfoRepository } from '../interface/device-info.repository.interface';

export class DeviceInfoRepository implements IDeviceInfoRepository {
constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Controller, Get, Logger, Param, UseGuards } from '@nestjs/common';
import {
ApiTags,
ApiBearerAuth,
ApiOperation,
ApiParam,
ApiResponse,
ApiTags,
} from '@nestjs/swagger';
import { ExtAuthGuard } from 'src/auth/guard/ext-auth.guard';
import { Where42ResponseDto } from './dto/where42.response.dto';
import { ExtService } from './ext.service';
import { Where42Service } from './where42.service';

@ApiTags('Where42 전용 API')
@Controller('ext/where42')
Expand All @@ -17,7 +17,7 @@ import { ExtService } from './ext.service';
export class Where42Controller {
private logger = new Logger(Where42Controller.name);

constructor(private extService: ExtService) {}
constructor(private where42Service: Where42Service) {}

/**
* 특정 사용자가 클러스터에 체류중인지 확인합니다.
Expand Down Expand Up @@ -48,6 +48,6 @@ export class Where42Controller {
@Get('where42/:login')
async where42(@Param('login') login: string): Promise<Where42ResponseDto> {
this.logger.debug(`@islogin) where42: ${login}`);
return this.extService.where42(login);
return this.where42Service.where42(login);
}
}
Loading

0 comments on commit 2a26b21

Please sign in to comment.