Skip to content

Commit 9c9dd97

Browse files
committed
Набросать отправку уведомлений об оценках в телеграм
1 parent a08bdb4 commit 9c9dd97

File tree

23 files changed

+353
-23
lines changed

23 files changed

+353
-23
lines changed

apps/api/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ DATABASE_PORT=5432
1212
DATABASE_NAME='databaseName'
1313
DATABASE_USERNAME='user'
1414
DATABASE_PASSWORD='password'
15+
BOT_TOKEN=''
1516

1617
POSTGRES_USER=postgres
1718
POSTGRES_PW=postgres

apps/api/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
"dependencies": {
1818
"@diary-spo/crypto": "workspace:*",
1919
"@elysiajs/cors": "^1.1.1",
20+
"@types/node-telegram-bot-api": "^0.64.7",
2021
"cache-manager": "^5.7.6",
2122
"elysia": "1.1.21",
2223
"elysia-compression": "^0.0.7",
2324
"elysia-helmet": "^2.0.0",
2425
"ky": "^1.7.2",
2526
"node-rsa": "^1.1.1",
27+
"node-telegram-bot-api": "^0.66.0",
2628
"pg": "^8.13.1",
2729
"pg-hstore": "^2.3.4",
2830
"rand-token": "^1.0.1",

apps/api/src/config/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ export const {
2222
DATABASE_USERNAME,
2323
DATABASE_PASSWORD,
2424
TIMEZONE,
25-
KEY_SCAN_PATH
25+
KEY_SCAN_PATH,
26+
BOT_TOKEN
2627
} = PARAMS_INIT

apps/api/src/config/params.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export const PARAMS_INIT: ParamsInit = {
1010
DATABASE_USERNAME: '',
1111
DATABASE_PASSWORD: '',
1212
TIMEZONE: getTimezone(),
13-
KEY_SCAN_PATH: './'
13+
KEY_SCAN_PATH: './',
14+
BOT_TOKEN: ''
1415
}

apps/api/src/config/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface StringParams {
2929
DATABASE_PASSWORD: string
3030
TIMEZONE: string
3131
KEY_SCAN_PATH: string
32+
BOT_TOKEN: string
3233
}
3334

3435
export type ParamsKeys = StringKeys<ParamsInit>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { MarkKeys, Task } from '@diary-spo/shared'
2+
3+
export interface IMarkEvent {
4+
diaryUserId: bigint
5+
task: Task
6+
mark: MarkKeys
7+
previousMarkId: number | null
8+
status: 'ADD' | 'DELETE' | 'UPDATE'
9+
eventDatetime: Date
10+
}
11+
12+
const ids_marks: IMarkEvent[] = [] // Идентификаторы оценок
13+
14+
/**
15+
* Регистрирует событие взаимодействия с оценкой
16+
* @param event
17+
*/
18+
export const addNewMarkEvent = (event: IMarkEvent): void => {
19+
ids_marks.push(event)
20+
console.log(event)
21+
}
22+
23+
export const getMarkEvent = () =>
24+
ids_marks.length > 0 ? ids_marks.splice(0, 1)[0] : null

apps/api/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Elysia } from 'elysia'
44
import { compression } from 'elysia-compression'
55
import { helmet } from 'elysia-helmet'
66

7-
import { TIMEZONE } from '@config'
7+
import { BOT_TOKEN, TIMEZONE } from '@config'
88
import { sequelize } from '@db'
99

1010
import { getTimezone } from './config/getTimeZone'

apps/api/src/models/Mark/actions/save/markSaveOrGet.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import type { MarkKeys } from '@diary-spo/shared'
1+
import type { MarkKeys, Task } from '@diary-spo/shared'
22
import { type ICacheData, retriesForError } from '@helpers'
33
import { formatDate } from '@utils'
4+
import { addNewMarkEvent } from '../../../../helpers/notificationController'
45
import { markValueSaveOrGet } from '../../../MarkValue'
56
import type { IScheduleModel } from '../../../Schedule'
67
import { markSaveOrGetUnSafe } from './markSaveOrGetUnSafe'
@@ -10,7 +11,9 @@ export const markSaveOrGet = async (
1011
schedule: IScheduleModel,
1112
taskId: bigint,
1213
termId: bigint | null,
13-
authData: ICacheData
14+
authData: ICacheData,
15+
task: Task | null,
16+
systemInitiator = false
1417
) => {
1518
// Не сохраняем текущий семестр для предыдущих семестров
1619
if (authData.termStartDate && authData.termLastUpdate) {
@@ -35,11 +38,26 @@ export const markSaveOrGet = async (
3538
authData
3639
])
3740

41+
const previousMarkId = markDB[0].markValueId
42+
3843
// Если есть изменения, то он потом сохранит
3944
markDB[0].markValueId = markValueId
4045
if (termId) {
4146
markDB[0].termId = termId
4247
}
4348

49+
// Сохраняем событие добавления/обновления оценки
50+
if (task && systemInitiator)
51+
if (markDB[0].markValueId !== previousMarkId || markDB[1])
52+
addNewMarkEvent({
53+
mark,
54+
previousMarkId:
55+
markDB[0].markValueId !== previousMarkId ? previousMarkId : null,
56+
task,
57+
diaryUserId: authData.localUserId,
58+
status: markDB[1] ? 'ADD' : 'UPDATE',
59+
eventDatetime: new Date()
60+
})
61+
4462
return markDB[0].save()
4563
}

apps/api/src/models/Schedule/actions/save/daySave.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { lessonSave } from './lessonSave'
88
export const daySave = async (
99
day: Day,
1010
authData: ICacheData,
11-
termPromise?: ITermDetectP
11+
termPromise?: ITermDetectP,
12+
systemInitiator = false
1213
) => {
1314
if (!day.lessons) {
1415
return
@@ -19,7 +20,7 @@ export const daySave = async (
1920
for (const lesson of day.lessons) {
2021
const promise = retriesForError(
2122
lessonSave,
22-
[new Date(day.date), lesson, authData, termPromise],
23+
[new Date(day.date), lesson, authData, termPromise, systemInitiator],
2324
3,
2425
1000
2526
) //lessonSave(day.date, lesson, authData, termPromise)

apps/api/src/models/Schedule/actions/save/lessonSave.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ import type { ScheduleWhere } from '../types'
2727
* @param lesson - Занятие для сохранения
2828
* @param authData - Данные, предоставленные клиентом для авторизации
2929
* @param termPromise
30+
* @param systemInitiator
3031
* @returns Promise<IScheduleModel | null>
3132
*/
3233
export const lessonSave = async (
3334
date: Date,
3435
lesson: Lesson,
3536
authData: ICacheData,
36-
termPromise?: ITermDetectP
37+
termPromise?: ITermDetectP,
38+
systemInitiator = false
3739
): Promise<IScheduleModel | null> => {
3840
/**
3941
* Пропускаем пустые занятия типа:
@@ -124,7 +126,7 @@ export const lessonSave = async (
124126

125127
/**
126128
* Если расписание есть в базе, то
127-
* пробуем поменять поля и если они поменялись, то
129+
* пробуем сравнить поля и если они поменялись, то
128130
* он (метод save) сделает запрос для сохранения
129131
*/
130132
if (!isCreated) {
@@ -165,7 +167,7 @@ export const lessonSave = async (
165167
const schedule = await promiseToReturn
166168
retriesForError(
167169
tasksSaveOrGet,
168-
[gradebook.tasks, schedule, authData, termPromise],
170+
[gradebook.tasks, schedule, authData, termPromise, systemInitiator],
169171
2
170172
)
171173
}

0 commit comments

Comments
 (0)