-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
326 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ JWT_SECRET="jwt_secret" | |
TZ=Asia/Tokyo | ||
|
||
LINE_CHANNEL_SECRET="" | ||
LINE_CHANNEL_SECRET_FOR_MESSAGING="" | ||
|
||
API_KEY=abcde |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Injectable() | ||
export class ApiKeyGuard implements CanActivate { | ||
constructor(private configService: ConfigService) {} | ||
|
||
canActivate(context: ExecutionContext): boolean { | ||
const request = context.switchToHttp().getRequest(); | ||
const apiKey = request.headers['API-KEY'] || request.headers['api-key']; | ||
return apiKey === this.configService.getOrThrow('API_KEY'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Body, Post, Query } from '@nestjs/common'; | ||
import { Controller, Get, UseGuards } from '@nestjs/common'; | ||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; | ||
import z from 'zod'; | ||
import { JwtAuthGuard } from 'src/auth/guards/jwt.guard'; | ||
import { AuthorizedUser, UserFragment } from 'src/user/user.decorator'; | ||
import { FtftService } from '../ftft.service'; | ||
import { ApiKeyGuard } from 'src/auth/guards/apikey.guard'; | ||
import { ReminderService } from './reminder.service'; | ||
|
||
@ApiTags('ftft/reminder') | ||
@Controller('ftft/reminder') | ||
export class ReminderController { | ||
constructor(private readonly reminderService: ReminderService) {} | ||
|
||
@Get('') | ||
@ApiBearerAuth() | ||
@UseGuards(ApiKeyGuard) | ||
async searchFtfts(@Query() _query: any) { | ||
console.log(_query); | ||
|
||
await this.reminderService.remindToAllLineUsers(); | ||
return { '###': true }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { FtftModule } from '../ftft.module'; | ||
import { ReminderController } from './reminder.controller'; | ||
import { ReminderService } from './reminder.service'; | ||
import { UserModule } from 'src/user/user.module'; | ||
|
||
@Module({ | ||
imports: [FtftModule, UserModule], | ||
controllers: [ReminderController], | ||
providers: [ReminderService], | ||
}) | ||
export class ReminderModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { FtftService } from '../ftft.service'; | ||
import { UserService } from 'src/user/user.service'; | ||
import { isPast, addDays, isFuture } from 'date-fns'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import axios from 'axios'; | ||
|
||
@Module({}) | ||
export class ReminderService { | ||
constructor( | ||
private readonly userService: UserService, | ||
private readonly ftftService: FtftService, | ||
private readonly configService: ConfigService, | ||
) {} | ||
|
||
async remindToAllLineUsers() { | ||
const lineUsers = await this.userService.findAllLineUsers(); | ||
|
||
console.log(lineUsers.length); | ||
for (const lineUser of lineUsers) { | ||
const ftftList = await this.ftftService.searchFtft(lineUser.id, undefined, 1); | ||
console.log(ftftList); | ||
if (ftftList.length === 0) { | ||
continue; | ||
} | ||
|
||
// 3日以上前~4日以内か | ||
const isTarget = isPast(addDays(ftftList[0].createdAt, 3)) && isFuture(addDays(ftftList[0].createdAt, 4)); | ||
if (!isTarget) { | ||
continue; | ||
} | ||
await this.sendRemind(lineUser.lineUserId).catch((error) => { | ||
console.error({ message: 'メッセージが送信できませんでした', error }); | ||
}); | ||
} | ||
} | ||
|
||
async sendRemind(lineUserId: string) { | ||
return axios.post( | ||
'https://api.line.me/v2/bot/message/push', | ||
{ | ||
to: lineUserId, | ||
messages: [ | ||
{ | ||
type: 'text', | ||
text: '最近、何か新しいこと始めた?👀\nもしあればFTFTにメモってみよう🚀\nhttps://ftft.morifuji-is.ninja/', | ||
}, | ||
], | ||
}, | ||
{ | ||
headers: { | ||
Authorization: 'Bearer ' + this.configService.getOrThrow('LINE_CHANNEL_SECRET_FOR_MESSAGING'), | ||
}, | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.