Skip to content

Commit e4eb5fb

Browse files
committed
feat: add checkTomorrowLibrarian
1 parent 8dd9d89 commit e4eb5fb

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

Diff for: src/rotation/rotations.service.ts

+42-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ import { RotationEntity } from './entity/rotation.entity';
1414
import { RotationAttendeeEntity } from './entity/rotation-attendee.entity';
1515
import { UserService } from 'src/user/user.service';
1616
import { RotationRepository } from './repository/rotations.repository';
17-
import { getFourthWeekdaysOfMonth, getNextYearAndMonth, getTodayDate } from './utils/date';
17+
import {
18+
getFourthWeekdaysOfMonth,
19+
getNextYearAndMonth,
20+
getTodayDay,
21+
getTomorrowDate,
22+
} from './utils/date';
1823
import { RotationAttendeeRepository } from './repository/rotation-attendees.repository';
1924
import { DayObject, RotationAttendeeInfo } from './utils/types';
2025
import { HolidayService } from 'src/holiday/holiday.service';
2126
import { createRotation } from './utils/rotation';
27+
import { SlackService } from 'src/slack/slack.service';
2228

2329
function getRotationCronTime() {
2430
if (process.env.NODE_ENV === 'production') {
@@ -36,8 +42,41 @@ export class RotationsService {
3642
private rotationAttendeeRepository: RotationAttendeeRepository,
3743
@Inject(forwardRef(() => UserService)) private userService: UserService,
3844
private holidayService: HolidayService,
45+
private slackService: SlackService,
3946
) {}
4047

48+
@Cron('0 0 * * *', {
49+
name: 'checkTomorrowLibrarian',
50+
timeZone: 'Asia/Seoul',
51+
})
52+
async checkTomorrowLibrarian(): Promise<void> {
53+
const tomorrow = getTomorrowDate();
54+
const tomorrowYear = tomorrow.getFullYear();
55+
const tomorrowMonth = tomorrow.getMonth() + 1;
56+
const tomorrowDay = tomorrow.getDate();
57+
58+
const tomorrowLibrarian = await this.rotationRepository.find({
59+
where: {
60+
year: tomorrowYear,
61+
month: tomorrowMonth,
62+
day: tomorrowDay,
63+
},
64+
relations: ['user'],
65+
});
66+
67+
if (tomorrowLibrarian.length === 0) {
68+
return;
69+
}
70+
71+
const tomorrowLibrarianOne = tomorrowLibrarian[0].user;
72+
const tomorrowLibrarianTwo = tomorrowLibrarian[1].user;
73+
74+
const message = `[알림] 내일은 ${tomorrowLibrarianOne.nickname}님과 ${tomorrowLibrarianTwo.nickname}님이 사서입니다!`;
75+
76+
await this.slackService.sendDirectMessage(tomorrowLibrarianOne.slackMemberId, message);
77+
await this.slackService.sendDirectMessage(tomorrowLibrarianTwo.slackMemberId, message);
78+
}
79+
4180
/*
4281
* 4주차 월요일에 유저를 모두 DB에 담아놓는 작업 필요
4382
* [update 20231219] - 매 달 1일에 유저를 모두 DB에 담아놓는 작업으로 변경
@@ -80,7 +119,7 @@ export class RotationsService {
80119
timeZone: 'Asia/Seoul',
81120
})
82121
async setRotation(): Promise<void> {
83-
if (getFourthWeekdaysOfMonth().indexOf(getTodayDate()) > 0) {
122+
if (getFourthWeekdaysOfMonth().indexOf(getTodayDay()) > 0) {
84123
try {
85124
this.logger.log('Setting rotation...');
86125

@@ -279,7 +318,7 @@ export class RotationsService {
279318
const { year, month } = getNextYearAndMonth();
280319

281320
/* 4주차인지 확인 */
282-
// if (getFourthWeekdaysOfMonth().indexOf(getTodayDate()) < 0) {
321+
// if (getFourthWeekdaysOfMonth().indexOfDay()) < 0) {
283322
// throw new BadRequestException(
284323
// 'Invalid date: Today is not a fourth weekday of the month.',
285324
// );

Diff for: src/rotation/utils/date.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ export function getNextYearAndMonth(): { year: number; month: number } {
1111
return { year, month };
1212
}
1313

14-
export function getTodayDate(): number {
14+
export function getTomorrowDate(): Date {
15+
return new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
16+
}
17+
18+
export function getTodayDay(): number {
1519
return new Date().getDate();
1620
}
1721

@@ -33,8 +37,7 @@ const getFourthWeekPeriod = (date = new Date()): number[] => {
3337
dateOfThursdayOnFirstWeek = 1 + DAY_IN_WEEK + DAY_OF_THURSDAY - firstDay;
3438
}
3539

36-
const dateOfThursdayOfFourthWeek =
37-
dateOfThursdayOnFirstWeek + 3 * DAY_IN_WEEK;
40+
const dateOfThursdayOfFourthWeek = dateOfThursdayOnFirstWeek + 3 * DAY_IN_WEEK;
3841
const dateOfMondayOnFourthWeek = dateOfThursdayOfFourthWeek - 3;
3942
const dateOfSundayOnFourthWeek = dateOfThursdayOfFourthWeek + 3;
4043

@@ -43,8 +46,7 @@ const getFourthWeekPeriod = (date = new Date()): number[] => {
4346

4447
export const getFourthWeekdaysOfMonth = (date = new Date()): number[] => {
4548
// eslint-disable-next-line @typescript-eslint/no-unused-vars
46-
const [dateOfMondayOnFourthWeek, dateOfSundayOnFourthWeek] =
47-
getFourthWeekPeriod(date);
49+
const [dateOfMondayOnFourthWeek, dateOfSundayOnFourthWeek] = getFourthWeekPeriod(date);
4850
const fourthWeekdays: number[] = [];
4951

5052
for (let i = 0; i < 5; i++) {
@@ -57,8 +59,7 @@ export const getFourthWeekdaysOfMonth = (date = new Date()): number[] => {
5759

5860
export const getFourthFridayOfMonth = (date = new Date()): number => {
5961
// eslint-disable-next-line @typescript-eslint/no-unused-vars
60-
const [dateOfMondayOnFourthWeek, dateOfSundayOnFourthWeek] =
61-
getFourthWeekPeriod(date);
62+
const [dateOfMondayOnFourthWeek, dateOfSundayOnFourthWeek] = getFourthWeekPeriod(date);
6263
const dateOfFridayOnFourthWeek = dateOfSundayOnFourthWeek - 2;
6364

6465
return dateOfFridayOnFourthWeek;

0 commit comments

Comments
 (0)