-
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.
feat(bot): implement trainings showing and check-in
- Loading branch information
Showing
12 changed files
with
510 additions
and
9 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
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
109 changes: 109 additions & 0 deletions
109
backend/src/bot/handlers/views/trainings_day-trainings.ts
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,109 @@ | ||
import { Composer, InlineKeyboard } from 'grammy' | ||
import type { View } from '.' | ||
import views from '.' | ||
import type { Ctx } from '~/bot/context' | ||
import { Button } from '~/utils/buttons' | ||
import { getDateDayInTimezone, getDayBoundaries } from '~/utils/dates' | ||
import { TIMEZONE } from '~/constants' | ||
|
||
const VIEW_ID = 'trainings/days-list' | ||
|
||
const backButton = new Button({ | ||
id: `${VIEW_ID}:back`, | ||
payloadEncoder: () => '', | ||
payloadDecoder: () => null, | ||
}) | ||
|
||
const trainingButton = new Button<{ trainingId: number }>({ | ||
id: `${VIEW_ID}:training`, | ||
payloadEncoder: ({ trainingId }) => trainingId.toString(), | ||
payloadDecoder: data => ({ trainingId: Number.parseInt(data) }), | ||
}) | ||
|
||
export type Props = { | ||
date: Date | ||
} | ||
|
||
export default { | ||
render: async (ctx, { date }) => { | ||
const { year, month, day } = getDateDayInTimezone(date, TIMEZONE) | ||
const [from, to] = getDayBoundaries({ | ||
year: year, | ||
month: month, | ||
day: day, | ||
timezone: TIMEZONE, | ||
}) | ||
|
||
const trainings = await ctx.domain.getTrainingsForUser({ | ||
telegramId: ctx.from!.id, | ||
from: from, | ||
to: to, | ||
}) | ||
|
||
const keyboard = new InlineKeyboard() | ||
trainings | ||
.sort((a, b) => a.startsAt.getTime() - b.startsAt.getTime()) | ||
.forEach((training) => { | ||
const timeStart = training.startsAt.toLocaleString('en-US', { | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
hour12: false, | ||
timeZone: TIMEZONE, | ||
}) | ||
const timeEnd = training.endsAt.toLocaleString('en-US', { | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
hour12: false, | ||
timeZone: TIMEZONE, | ||
}) | ||
const statusEmoji = training.checkedIn | ||
? '🟢' | ||
: training.checkInAvailable | ||
? '🔵' | ||
: '🔴' | ||
|
||
const data = trainingButton.createCallbackData({ trainingId: training.id }) | ||
keyboard.text(`${statusEmoji} ${timeStart}—${timeEnd}`, data) | ||
keyboard.text(training.title, data) | ||
keyboard.row() | ||
}) | ||
keyboard.text(ctx.t['Buttons.Back'], backButton.createCallbackData(null)) | ||
|
||
return { | ||
type: 'text', | ||
text: ctx.t['Views.DayTrainings.Message'], | ||
keyboard: keyboard, | ||
} | ||
}, | ||
middleware: () => { | ||
const composer = new Composer<Ctx>() | ||
|
||
composer | ||
.filter(trainingButton.filter) | ||
.use(async (ctx) => { | ||
const training = await ctx.domain.getTrainingForUser({ | ||
telegramId: ctx.from!.id, | ||
trainingId: ctx.payload.trainingId, | ||
}) | ||
ctx.answerCallbackQuery() | ||
await ctx.editMessage({ | ||
chatId: ctx.chat!.id, | ||
messageId: ctx.callbackQuery.message!.message_id, | ||
content: await views.trainingsTraining.render(ctx, { training }), | ||
}) | ||
}) | ||
|
||
composer | ||
.filter(backButton.filter) | ||
.use(async (ctx) => { | ||
await ctx.editMessage({ | ||
chatId: ctx.chat!.id, | ||
messageId: ctx.callbackQuery.message!.message_id, | ||
content: await views.trainingsDaysList.render(ctx, {}), | ||
}) | ||
ctx.answerCallbackQuery() | ||
}) | ||
|
||
return composer.middleware() | ||
}, | ||
} satisfies View<Ctx, Props> as View<Ctx, Props> |
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,82 @@ | ||
import { Composer, InlineKeyboard } from 'grammy' | ||
import type { View } from '.' | ||
import views from '.' | ||
import { TIMEZONE, TRAININGS_DAYS_LIST_COUNT } from '~/constants' | ||
import type { Ctx } from '~/bot/context' | ||
import { Button } from '~/utils/buttons' | ||
import { getDateDayInTimezone, getDayBoundaries } from '~/utils/dates' | ||
|
||
const VIEW_ID = 'trainings/day-trainings' | ||
|
||
const backButton = new Button({ | ||
id: `${VIEW_ID}:back`, | ||
payloadEncoder: () => '', | ||
payloadDecoder: () => null, | ||
}) | ||
|
||
const dayButton = new Button<Date>({ | ||
id: `${VIEW_ID}:day`, | ||
payloadEncoder: payload => payload.toISOString(), | ||
payloadDecoder: data => new Date(data), | ||
}) | ||
|
||
export default { | ||
render: async (ctx) => { | ||
const keyboard = new InlineKeyboard() | ||
const now = new Date() | ||
|
||
for (let i = 0; i < TRAININGS_DAYS_LIST_COUNT; i++) { | ||
const actualDate = new Date(now.getTime()) | ||
actualDate.setDate(now.getDate() + i) | ||
|
||
const day = getDateDayInTimezone(actualDate, TIMEZONE) | ||
const [timezoneDate, _] = getDayBoundaries({ | ||
...day, | ||
timezone: TIMEZONE, | ||
}) | ||
|
||
keyboard | ||
.text( | ||
ctx.t['Views.TrainingsDaysList.Buttons.Day'](timezoneDate), | ||
dayButton.createCallbackData(actualDate), | ||
) | ||
.row() | ||
} | ||
|
||
keyboard.row() | ||
keyboard.text(ctx.t['Buttons.Back'], backButton.createCallbackData(null)) | ||
|
||
return { | ||
type: 'text', | ||
text: ctx.t['Views.TrainingsDaysList.Message'], | ||
keyboard: keyboard, | ||
} | ||
}, | ||
middleware: () => { | ||
const composer = new Composer<Ctx>() | ||
|
||
composer | ||
.filter(dayButton.filter) | ||
.use(async (ctx) => { | ||
await ctx.editMessage({ | ||
chatId: ctx.chat!.id, | ||
messageId: ctx.callbackQuery.message!.message_id, | ||
content: await views.trainingsDayTrainings.render(ctx, { date: ctx.payload }), | ||
}) | ||
ctx.answerCallbackQuery() | ||
}) | ||
|
||
composer | ||
.filter(backButton.filter) | ||
.use(async (ctx) => { | ||
ctx.answerCallbackQuery() | ||
await ctx.editMessage({ | ||
chatId: ctx.chat!.id, | ||
messageId: ctx.callbackQuery.message!.message_id, | ||
content: await views.main.render(ctx, {}), | ||
}) | ||
}) | ||
|
||
return composer.middleware() | ||
}, | ||
} satisfies View<Ctx> as View<Ctx> |
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,106 @@ | ||
import { Composer, InlineKeyboard } from 'grammy' | ||
import type { View } from '.' | ||
import views from '.' | ||
import type { Ctx } from '~/bot/context' | ||
import { Button } from '~/utils/buttons' | ||
import type { TrainingDetailed } from '~/services/sport/types' | ||
|
||
const VIEW_ID = 'trainings/training' | ||
|
||
const backButton = new Button<{ date: Date }>({ | ||
id: `${VIEW_ID}:back`, | ||
payloadEncoder: payload => payload.date.toISOString(), | ||
payloadDecoder: data => ({ date: new Date(data) }), | ||
}) | ||
|
||
const checkInButton = new Button<{ trainingId: number }>({ | ||
id: `${VIEW_ID}:check-in`, | ||
payloadEncoder: ({ trainingId }) => trainingId.toString(), | ||
payloadDecoder: data => ({ trainingId: Number.parseInt(data) }), | ||
}) | ||
const cancelCheckInButton = new Button<{ trainingId: number }>({ | ||
id: `${VIEW_ID}:cancel-check-in`, | ||
payloadEncoder: ({ trainingId }) => trainingId.toString(), | ||
payloadDecoder: data => ({ trainingId: Number.parseInt(data) }), | ||
}) | ||
|
||
export type Props = { | ||
training: TrainingDetailed | ||
} | ||
|
||
export default { | ||
render: async (ctx, { training }) => { | ||
const keyboard = new InlineKeyboard() | ||
if (training.checkedIn) { | ||
keyboard.text( | ||
ctx.t['Views.Training.Buttons.CancelCheckIn'], | ||
cancelCheckInButton.createCallbackData({ trainingId: training.id }), | ||
) | ||
} else if (training.checkInAvailable) { | ||
keyboard.text( | ||
ctx.t['Views.Training.Buttons.CheckIn'], | ||
checkInButton.createCallbackData({ trainingId: training.id }), | ||
) | ||
} | ||
keyboard.row() | ||
keyboard.text(ctx.t['Buttons.Back'], backButton.createCallbackData({ date: training.startsAt })) | ||
|
||
return { | ||
type: 'text', | ||
text: ctx.t['Views.Training.Message'](training), | ||
keyboard: keyboard, | ||
} | ||
}, | ||
middleware: () => { | ||
const composer = new Composer<Ctx>() | ||
|
||
composer | ||
.filter(checkInButton.filter) | ||
.use(async (ctx) => { | ||
await ctx.domain.checkInUserForTraining({ | ||
telegramId: ctx.from!.id, | ||
trainingId: ctx.payload.trainingId, | ||
}) | ||
ctx.answerCallbackQuery({ | ||
text: 'Checked-in', | ||
show_alert: true, | ||
}) | ||
await ctx.editMessage({ | ||
chatId: ctx.chat!.id, | ||
messageId: ctx.callbackQuery.message!.message_id, | ||
content: await views.main.render(ctx, {}), | ||
}) | ||
}) | ||
|
||
composer | ||
.filter(cancelCheckInButton.filter) | ||
.use(async (ctx) => { | ||
await ctx.domain.cancelCheckInUserForTraining({ | ||
telegramId: ctx.from!.id, | ||
trainingId: ctx.payload.trainingId, | ||
}) | ||
ctx.answerCallbackQuery({ | ||
text: 'Canceled check-in', | ||
show_alert: true, | ||
}) | ||
await ctx.editMessage({ | ||
chatId: ctx.chat!.id, | ||
messageId: ctx.callbackQuery.message!.message_id, | ||
content: await views.main.render(ctx, {}), | ||
}) | ||
}) | ||
|
||
composer | ||
.filter(backButton.filter) | ||
.use(async (ctx) => { | ||
await ctx.editMessage({ | ||
chatId: ctx.chat!.id, | ||
messageId: ctx.callbackQuery.message!.message_id, | ||
content: await views.trainingsDayTrainings.render(ctx, ctx.payload), | ||
}) | ||
ctx.answerCallbackQuery() | ||
}) | ||
|
||
return composer.middleware() | ||
}, | ||
} satisfies View<Ctx, Props> as View<Ctx, Props> |
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,2 @@ | ||
export const TIMEZONE = 'Europe/Moscow' | ||
export const TRAININGS_DAYS_LIST_COUNT = 7 |
Oops, something went wrong.