Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
YellaCRE committed Feb 14, 2024
2 parents b2abc41 + b56446e commit 78099a4
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 13 deletions.
10 changes: 10 additions & 0 deletions libs/grasstracking/src/grasstracking.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Global, Module } from '@nestjs/common';
// import { TeamTrackingService } from '@app/teamtracking/teamtracking.service';
import { GrassTrackingService } from './grasstracking.service';

@Global()
@Module({
providers: [GrassTrackingService],
exports: [GrassTrackingService],
})
export class GrasstrackingModule {}
29 changes: 29 additions & 0 deletions libs/grasstracking/src/grasstracking.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Injectable } from '@nestjs/common';
import { Subject, Observable } from 'rxjs';

@Injectable()
export class GrassTrackingService {
private teamStatusUpdates: Record<string, Subject<string>> = {};

getTeamStatusUpdateObservable(teamId: string): Observable<string> {
const statusKey = `team_${teamId}_status`;
if (!this.teamStatusUpdates[statusKey]) {
this.teamStatusUpdates[statusKey] = new Subject<string>();
}
console.log(Object.keys(this.teamStatusUpdates));
return this.teamStatusUpdates[statusKey].asObservable();
}

pushTeamStatusUpdate(teamId: string, status: string): void {
const statusKey = `team_${teamId}_status`;
if (!this.teamStatusUpdates[statusKey]) {
this.teamStatusUpdates[statusKey] = new Subject<string>();
}
// console.log(statusKey);
this.teamStatusUpdates[statusKey].next(status);
}

streamTeamStatus(teamId: string): Observable<string> {
return this.getTeamStatusUpdateObservable(`${teamId}`);
}
}
2 changes: 2 additions & 0 deletions libs/grasstracking/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './grasstracking.service';
export * from './grasstracking.module';
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { EventEmitterModule } from '@nestjs/event-emitter';
import { eventEmitterConfig } from '../configs/event-emitter.config';
import { TeamtrackingModule } from '@app/teamtracking';
import { GrassModule } from './grass/grass.module';
import { GrasstrackingModule } from 'libs/grasstracking/src';

@Module({
imports: [
Expand All @@ -37,6 +38,7 @@ import { GrassModule } from './grass/grass.module';
FirebaseModule,
TeamtrackingModule,
GrassModule,
GrasstrackingModule,
],
controllers: [],
providers: [],
Expand Down
3 changes: 2 additions & 1 deletion src/board/board.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { BoardController } from './board.controller';
import { BoardService } from './board.service';
import { BoardRepository } from './board.repository';
import { UsersModule } from 'src/users/users.module';
import { TeamModule } from 'src/team/team.module';

@Module({
imports: [TypeOrmModule.forFeature([Board]), UsersModule],
imports: [TypeOrmModule.forFeature([Board]), UsersModule, TeamModule],
controllers: [BoardController],
providers: [BoardService, BoardRepository],
exports: [BoardService],
Expand Down
19 changes: 17 additions & 2 deletions src/board/board.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
import { TeamStatusEvent } from '../team/events/team-status.event';
import { TeamStreamDto } from '../team/dto/team.dto';
import { UsersRepository } from '../users/users.repository';
import { GrassStreamDto } from 'src/grass/dto/grass.dto';
import { GrassStatusEvent } from 'src/grass/events/grass-status.event';
import { TeamRepository } from 'src/team/team.repository';

@Injectable()
export class BoardService {
constructor(
private boardRepository: BoardRepository,
private usersRepository: UsersRepository,
private teamRepository: TeamRepository,
private readonly eventEmitter: EventEmitter2,
) {}

Expand All @@ -31,13 +35,24 @@ export class BoardService {
const score = (await this.findByMonth(userId, today)).length;
await this.usersRepository.updateTilScore(userId, score);

const streamDTO: TeamStreamDto = {
const teamStreamDTO: TeamStreamDto = {
userId,
status: 'written', // 또는 상태를 업데이트합니다.
teamCode: teamCode,
};

this.eventEmitter.emit(TeamStatusEvent.EVENT_NAME, streamDTO);
this.eventEmitter.emit(TeamStatusEvent.EVENT_NAME, teamStreamDTO);

const grassStreamDTO: GrassStreamDto = {
// userId,

grass:
await this.teamRepository.getWrittenUserIdsPercentageByTeamCode(
teamCode,
),
teamCode: teamCode,
};
this.eventEmitter.emit(GrassStatusEvent.EVENT_NAME, grassStreamDTO);
}

async findAll(): Promise<Board[]> {
Expand Down
2 changes: 1 addition & 1 deletion src/extension/history.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class HistoryController {
// console.log('USER : ', user);
// console.log('EXTENSION HISTORY : ', extensionHistoryDto);
extensionHistoryDto.userId = user.id.toString();
await this.service.handleExtensionHistory(extensionHistoryDto);
return await this.service.handleExtensionHistory(extensionHistoryDto);
}

@Get('/history')
Expand Down
4 changes: 3 additions & 1 deletion src/extension/history.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class HistoryService {
)
.getOne();
if (checkDup) {
return;
return { result: 'duplicate' };
}

const aiData = await this.chatService.processExtenstionData(dto);
Expand All @@ -42,6 +42,8 @@ export class HistoryService {
processedTitle: processTitle,
processedData: processData,
});

return { result: 'ok' };
}

preprocess(tags: string): string {
Expand Down
6 changes: 6 additions & 0 deletions src/grass/dto/grass.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ export class GrassDto {
@IsNotEmpty()
date: Date;
}

export class GrassStreamDto {
// userId: number;
grass: number;
teamCode: string;
}
13 changes: 13 additions & 0 deletions src/grass/events/grass-status.event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// import { Team } from '../entities/team.entity';

import { Team } from 'src/team/entities/team.entity';

export class GrassStatusEvent {
static readonly EVENT_NAME = 'grass.status';

readonly team: Team;

constructor(team: Team) {
this.team = team;
}
}
26 changes: 21 additions & 5 deletions src/grass/grass.controller.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { Body, Controller, Get, Param, Post, UseGuards } from '@nestjs/common';
import { UsersService } from 'src/users/users.service';
import { Body, Controller, Post, Sse, UseGuards } from '@nestjs/common';
import { TeamService } from 'src/team/team.service';
import { BoardService } from 'src/board/board.service';
import { JwtAuthGuard } from 'src/auth/guards/jwtAuth.guard';
import { User } from 'src/users/utils/user.decorator';
import { User as UserEntity } from '../users/entity/user.entity';
import { GrassDto } from './dto/grass.dto';
import { GrassStatusEvent } from './events/grass-status.event';
// import { TeamTrackingService } from '@app/teamtracking';
// import { map, startWith } from 'rxjs';
import { GrassTrackingService } from 'libs/grasstracking/src';
import { TeamRepository } from 'src/team/team.repository';

@Controller('grass')
export class GrassController {
constructor(
private readonly userService: UsersService,
private readonly teamService: TeamService,
private readonly teamRepository: TeamRepository,
private readonly boardService: BoardService,
private readonly grassTrackingService: GrassTrackingService,
) {}

@Post('/users') // 개인 해당월 잔디용
Expand All @@ -31,7 +36,6 @@ export class GrassController {
@Post('/team') // 팀 해당월 잔디용
@UseGuards(JwtAuthGuard)
async findTeamGrass(@Body() grassDto: GrassDto, @User() user: UserEntity) {
// const user = await this.userService.findUserById(userId);
const teamMember = await this.teamService.findMemberList(user.teamCode);
const teamBoard = [];
for (const user of teamMember) {
Expand All @@ -42,8 +46,20 @@ export class GrassController {
userId,
new Date(grassDto.date),
);

teamBoard.push({ user: { profileImage, name }, boards });
}
return teamBoard;
const totalMember = teamMember.length;
const teamBoardWithCount = {
teamBoard: teamBoard,
totalMember: totalMember,
};
return teamBoardWithCount;
}

@UseGuards(JwtAuthGuard)
@Sse(GrassStatusEvent.EVENT_NAME)
async sseTeamGrassStatus(@User() user: UserEntity) {
return this.grassTrackingService.streamTeamStatus(user.teamCode);
}
}
3 changes: 2 additions & 1 deletion src/grass/grass.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { GrassController } from './grass.controller';
import { UsersModule } from 'src/users/users.module';
import { BoardModule } from 'src/board/board.module';
import { TeamModule } from 'src/team/team.module';
import { GrassStatusListener } from './listeners/grass-status.listener';

@Module({
imports: [UsersModule, BoardModule, TeamModule],
controllers: [GrassController],
providers: [GrassService],
providers: [GrassService, GrassStatusListener],
})
export class GrassModule {}
27 changes: 27 additions & 0 deletions src/grass/listeners/grass-status.listener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Injectable } from '@nestjs/common';
import { GrassStatusEvent } from '../events/grass-status.event';
import { OnEvent } from '@nestjs/event-emitter';
// import { TeamRepository } from '../team.repository';
// import { TeamTrackingService } from '@app/teamtracking';
// import { TeamStreamDto } from '../dto/team.dto';
import { GrassStreamDto } from '../dto/grass.dto';
import { TeamRepository } from 'src/team/team.repository';
import { GrassTrackingService } from 'libs/grasstracking/src';

@Injectable()
export class GrassStatusListener {
constructor(
private readonly teamRepository: TeamRepository,
private readonly grassTrackingService: GrassTrackingService,
) {}

@OnEvent(GrassStatusEvent.EVENT_NAME, { async: true })
async handleTeamStatusUpdate(grassStreamDTO: GrassStreamDto) {
const teamCode = grassStreamDTO.teamCode.toString();

this.grassTrackingService.pushTeamStatusUpdate(
teamCode,
JSON.stringify({ data: grassStreamDTO }),
);
}
}
2 changes: 1 addition & 1 deletion src/team/team.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ import { Team } from './entities/team.entity';
imports: [TypeOrmModule.forFeature([Team]), UsersModule],
controllers: [TeamController],
providers: [TeamService, TeamStatusListener, TeamRepository],
exports: [TeamService],
exports: [TeamService, TeamRepository],
})
export class TeamModule {}
47 changes: 46 additions & 1 deletion src/team/team.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,52 @@ export class TeamRepository extends Repository<Team> {
.distinct(true)
.getMany();

return writtenUsers.map((user) => user.userId);
return Array.from(new Set(writtenUsers.map((user) => user.userId)));
}

async getWrittenUserIdsPercentageByTeamCode(
teamCode: string,
): Promise<number> {
const today = new Date();
const todayStart = new Date(today.setHours(0, 0, 0, 0));
const todayEnd = new Date(today.setHours(23, 59, 59, 999));

const totalUsersCount = await this.dataSource
.getRepository(User)
.createQueryBuilder('user')
.where('user.teamCode = :teamCode', { teamCode })
.getCount();

// const writtenUsersCount = await this.dataSource
// .getRepository(Board)
// .createQueryBuilder('board')
// .leftJoin('board.user', 'user')
// .where('user.teamCode = :teamCode', { teamCode })
// .andWhere('board.timestamp BETWEEN :start AND :end', {
// start: todayStart,
// end: todayEnd,
// })
// .select('DISTINCT board.userId')
// .getCount();
const writtenUsers = await this.dataSource
.getRepository(Board)
.createQueryBuilder('board')
.leftJoinAndSelect('board.user', 'user')
.where('user.teamCode = :teamCode', { teamCode })
.andWhere('board.timestamp BETWEEN :start AND :end', {
start: todayStart,
end: todayEnd,
})
.select('board.userId')
.distinct(true)
.getMany();

const writtenUsersCount = Array.from(
new Set(writtenUsers.map((user) => user.userId)),
).length;

const writtenUserPercentage = (writtenUsersCount / totalUsersCount) * 100;
return writtenUserPercentage;
}

async getMyTeamUsers(teamCode: string) {
Expand Down

0 comments on commit 78099a4

Please sign in to comment.