diff --git a/src/controllers/properties.ts b/src/controllers/properties.ts index d35de22..295b292 100644 --- a/src/controllers/properties.ts +++ b/src/controllers/properties.ts @@ -1,92 +1,28 @@ import { Composer } from "grammy"; import type { CustomContext } from "../types/context.js"; import type { Property } from "../types/database.js"; -import { - generatePropertyDescription, - generatePropertyPhotoAlbum -} from "../services/Property/property.service.js"; -import { - fullPropertyControlKeyboard, - nextPropertyControlKeyboard, - previousPropertyControlKeyboard -} from "../menus/propertyMenu.js"; +import { displayProperty } from "../services/Property/property.service.js"; export const propertiesController = new Composer(); let currentPropertyIndex = 0; propertiesController.command("properties", async ctx => { const properties = (await ctx.db.property.find({}).toArray()) as Property[]; - const currentProperty = properties[currentPropertyIndex]; - const totalProperties = properties.length; - const { videoFileId, albumUrls } = currentProperty; - - const propertyDescription = generatePropertyDescription(currentProperty); - const propertyPhotoAlbum = generatePropertyPhotoAlbum(albumUrls); - await ctx.reply(propertyDescription, { - parse_mode: "MarkdownV2" - }); - await ctx.replyWithVideo(videoFileId); - await ctx.replyWithMediaGroup(propertyPhotoAlbum); - await ctx.reply(`Property ${currentPropertyIndex + 1}/${totalProperties}`, { - reply_markup: nextPropertyControlKeyboard - }); + await displayProperty(ctx, properties, currentPropertyIndex); propertiesController.callbackQuery("next-property", async ctx => { ctx.answerCallbackQuery(""); - if (currentPropertyIndex + 1 < totalProperties) { + if (currentPropertyIndex + 1 < properties.length) { currentPropertyIndex++; - - const currentProperty = properties[currentPropertyIndex]; - const totalProperties = properties.length; - const { videoFileId, albumUrls } = currentProperty; - const propertyDescription = generatePropertyDescription(currentProperty); - const propertyPhotoAlbum = generatePropertyPhotoAlbum(albumUrls); - await ctx.reply( - `Property ${currentPropertyIndex + 1}/${totalProperties}` - ); - await ctx.reply(propertyDescription, { - parse_mode: "MarkdownV2" - }); - await ctx.replyWithVideo(videoFileId); - await ctx.replyWithMediaGroup(propertyPhotoAlbum); - await ctx.reply( - `Property ${currentPropertyIndex + 1}/${totalProperties}`, - { - reply_markup: - currentPropertyIndex + 1 < totalProperties - ? fullPropertyControlKeyboard - : previousPropertyControlKeyboard - } - ); + await displayProperty(ctx, properties, currentPropertyIndex); } }); propertiesController.callbackQuery("previous-property", async ctx => { ctx.answerCallbackQuery(""); - if (currentPropertyIndex > 1) { + if (currentPropertyIndex + 1 > 0) { currentPropertyIndex--; - const currentProperty = properties[currentPropertyIndex]; - const totalProperties = properties.length; - const { videoFileId, albumUrls } = currentProperty; - const propertyDescription = generatePropertyDescription(currentProperty); - const propertyPhotoAlbum = generatePropertyPhotoAlbum(albumUrls); - await ctx.reply( - `Property ${currentPropertyIndex + 1}/${totalProperties}` - ); - await ctx.reply(propertyDescription, { - parse_mode: "MarkdownV2" - }); - await ctx.replyWithVideo(videoFileId); - await ctx.replyWithMediaGroup(propertyPhotoAlbum); - await ctx.reply( - `Property ${currentPropertyIndex + 1}/${totalProperties}`, - { - reply_markup: - currentPropertyIndex + 1 > 1 - ? fullPropertyControlKeyboard - : nextPropertyControlKeyboard - } - ); + await displayProperty(ctx, properties, currentPropertyIndex); } }); }); diff --git a/src/services/Property/property.service.ts b/src/services/Property/property.service.ts index 338d9f8..62ad5bf 100644 --- a/src/services/Property/property.service.ts +++ b/src/services/Property/property.service.ts @@ -1,6 +1,11 @@ import { type InputMediaPhoto } from "grammy/types"; import { InputMediaBuilder } from "grammy"; import type { Property } from "../../types/database.js"; +import { + fullPropertyControlKeyboard, + nextPropertyControlKeyboard, + previousPropertyControlKeyboard +} from "../../menus/propertyMenu.js"; export const generatePropertyDescription = (property: Property): string => { const { @@ -34,3 +39,29 @@ export const generatePropertyPhotoAlbum = ( return photoAlbum; }; + +export const displayProperty = async ( + ctx: CustomContext, + properties: Property[], + currentPropertyIndex: number +) => { + const totalProperties = properties.length; + const currentProperty = properties[currentPropertyIndex]; + const { videoFileId, albumUrls } = currentProperty; + + const propertyDescription = generatePropertyDescription(currentProperty); + const propertyPhotoAlbum = generatePropertyPhotoAlbum(albumUrls); + + await ctx.reply(`Property ${currentPropertyIndex + 1}/${totalProperties}`); + await ctx.reply(propertyDescription, { parse_mode: "MarkdownV2" }); + await ctx.replyWithVideo(videoFileId); + await ctx.replyWithMediaGroup(propertyPhotoAlbum); + await ctx.reply(`Property ${currentPropertyIndex + 1}/${totalProperties}`, { + reply_markup: + currentPropertyIndex == 0 + ? nextPropertyControlKeyboard + : currentPropertyIndex + 1 < totalProperties + ? fullPropertyControlKeyboard + : previousPropertyControlKeyboard + }); +};