Skip to content

Commit

Permalink
Refactor code to avoid duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantGeekDev committed Jan 5, 2024
1 parent 7b90b5a commit 808cd55
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 70 deletions.
76 changes: 6 additions & 70 deletions src/controllers/properties.ts
Original file line number Diff line number Diff line change
@@ -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<CustomContext>();
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);
}
});
});
31 changes: 31 additions & 0 deletions src/services/Property/property.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
});
};

0 comments on commit 808cd55

Please sign in to comment.