Skip to content

Commit

Permalink
Add view-properties callbackQuery to propertiesController
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantGeekDev committed Jan 6, 2024
1 parent 7adc030 commit bca00ec
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 38 deletions.
3 changes: 2 additions & 1 deletion src/controllers/developments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ developmentsController.callbackQuery(
}
const developmentKeyboard = developerDetailMenuCreator(development);
await ctx.reply(`*${developmentName}*`, {
reply_markup: developmentKeyboard
reply_markup: developmentKeyboard,
parse_mode: "MarkdownV2"
});
}
);
45 changes: 26 additions & 19 deletions src/controllers/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,36 @@ import { Composer } from "grammy";
import type { CustomContext } from "../types/context.js";
import type { PropertyFromDb } from "../types/database.js";
import { displayProperty } from "../services/Property/property.service.js";
import isDevelopmentType from "../helpers/isDevelopmentType.js";

export const propertiesController = new Composer<CustomContext>();

let currentPropertyIndex = 0;

propertiesController.command("properties", async ctx => {
const properties = (await ctx.db.property
.find({})
.toArray()) as PropertyFromDb[];
await displayProperty(ctx, properties, currentPropertyIndex);
propertiesController.callbackQuery(/^view-properties \s*(.+)$/, async ctx => {
const chosenDevelopment = ctx.match[1];
ctx.answerCallbackQuery();
if (isDevelopmentType(chosenDevelopment)) {
const properties = (await ctx.db.property
.find({ development: chosenDevelopment })
.toArray()) as PropertyFromDb[];
await displayProperty(ctx, properties, currentPropertyIndex);

propertiesController.callbackQuery("next-property", async ctx => {
ctx.answerCallbackQuery("");
if (currentPropertyIndex + 1 < properties.length) {
currentPropertyIndex++;
await displayProperty(ctx, properties, currentPropertyIndex);
}
});
propertiesController.callbackQuery("next-property", async ctx => {
ctx.answerCallbackQuery("");
if (currentPropertyIndex + 1 < properties.length) {
currentPropertyIndex++;
await displayProperty(ctx, properties, currentPropertyIndex);
}
});

propertiesController.callbackQuery("previous-property", async ctx => {
ctx.answerCallbackQuery("");
if (currentPropertyIndex + 1 > 0) {
currentPropertyIndex--;
await displayProperty(ctx, properties, currentPropertyIndex);
}
});
propertiesController.callbackQuery("previous-property", async ctx => {
ctx.answerCallbackQuery("");
if (currentPropertyIndex + 1 > 0) {
currentPropertyIndex--;
await displayProperty(ctx, properties, currentPropertyIndex);
}
});
}
return;
});
11 changes: 11 additions & 0 deletions src/helpers/isDevelopmentType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { type DevelopmentType } from "../types/database";

const isDevelopmentType = (
development: string
): development is DevelopmentType => {
return ["SaliSol Hills", "SaliSol Resort", "SaliSol Golf"].includes(
development
);
};

export default isDevelopmentType;
16 changes: 4 additions & 12 deletions src/menus/propertyMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,37 @@ import { InlineKeyboard } from "grammy";
export const fullPropertyControlKeyboard = (
propertyId: string
): InlineKeyboard => {
const salisolHillsDropboxUrl =
"https://www.dropbox.com/sh/t6330gcog0lz7dh/AADqYOkfEnvIQ0RF6Lr0eRBja?dl=0";

const keyboard = new InlineKeyboard()
.text("« Previous Property", "previous-property")
.text("Next Property » ", "next-property")
.row()
.text("📞 Contact me about this property", `contact_property_${propertyId}`)
.row()
.url("🖼️ Our Dropbox", salisolHillsDropboxUrl);
.text("🔙 Back", "view-developments");

return keyboard;
};

export const nextPropertyControlKeyboard = (
propertyId: string
): InlineKeyboard => {
const salisolHillsDropboxUrl =
"https://www.dropbox.com/sh/t6330gcog0lz7dh/AADqYOkfEnvIQ0RF6Lr0eRBja?dl=0";

const keyboard = new InlineKeyboard()
.text("Next Property » ", "next-property")
.row()
.text("📞 Contact me about this property", `contact_property_${propertyId}`)
.row()
.url("🖼️ Our Dropbox", salisolHillsDropboxUrl);
.text("🔙 Back", "view-developments");
return keyboard;
};

export const previousPropertyControlKeyboard = (
propertyId: string
): InlineKeyboard => {
const salisolHillsDropboxUrl =
"https://www.dropbox.com/sh/t6330gcog0lz7dh/AADqYOkfEnvIQ0RF6Lr0eRBja?dl=0";

const keyboard = new InlineKeyboard()
.text("« Previous Property", "previous-property")
.row()
.text("📞 Contact me about this property", `contact_property_${propertyId}`)
.row()
.url("🖼️ Our Dropbox", salisolHillsDropboxUrl);
.text("🔙 Back", "view-developments");
return keyboard;
};
2 changes: 1 addition & 1 deletion src/services/Notifications/notifySalesTeam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const notifySalesTeam = async (

const formattedNotificationMessage = `A new client is requesting information:\nName: ${userName}\nUser: @${userClickable}\n${
propertyOfInterest
? `Interested in ${propertyOfInterest.collection} ${propertyOfInterest.name}`
? `Interested in ${propertyOfInterest.development} ${propertyOfInterest.name}`
: ""
}`;

Expand Down
8 changes: 4 additions & 4 deletions src/services/Property/property.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export const generatePropertyDescription = (
): string => {
const {
name,
collection,
development,
availability,
price,
plotMetersSquared,
builtMetersSquared
} = property;

// Property description uses Telegram's Markdown V2
const propertyDescription = `*🏠 ${collection}: ${name}*\n\nPlot Size: ${plotMetersSquared}m2 \nBuilt Meters: ${builtMetersSquared}m2\nPrice: ${price
const propertyDescription = `*🏠 ${development}: ${name}*\n\nPlot Size: ${plotMetersSquared}m2 \nBuilt Meters: ${builtMetersSquared}m2\nPrice: ${price
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ",")}${
availability ? "" : "Reserved"
Expand Down Expand Up @@ -54,7 +54,7 @@ export const displayProperty = async (
const currentPropertyId = currentProperty._id.toString();

let controlKeyboard;
if (currentPropertyIndex == 0) {
if (currentPropertyIndex == 0 && totalProperties > 1) {
controlKeyboard = nextPropertyControlKeyboard(currentPropertyId);
} else if (currentPropertyIndex + 1 < totalProperties) {
controlKeyboard = fullPropertyControlKeyboard(currentPropertyId);
Expand All @@ -72,6 +72,6 @@ export const displayProperty = async (
await ctx.replyWithVideo(videoFileId);
await ctx.replyWithMediaGroup(propertyPhotoAlbum);
await ctx.reply(`Property ${currentPropertyIndex + 1}/${totalProperties}`, {
reply_markup: controlKeyboard
reply_markup: controlKeyboard ?? undefined
});
};
7 changes: 6 additions & 1 deletion src/types/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ export interface DevelopmentFromDb {
phoneNumber: string;
}

export type DevelopmentType =
| "SaliSol Hills"
| "SaliSol Resort"
| "SaliSol Golf";
export interface PropertyFromDb {
_id: ObjectId;
collection: "SaliSol Hills" | "SaliSol Resort" | "SaliSol Golf";
development: DevelopmentType;
name: string;
price: number;
availability: boolean;
Expand All @@ -45,6 +49,7 @@ export interface PropertyFromDb {
telegramContactUrl: string;
websiteUrl: string;
}

export interface Chat {
chatId: number;
title: string;
Expand Down

0 comments on commit bca00ec

Please sign in to comment.