-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.ts
157 lines (144 loc) · 4.33 KB
/
keyboard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { type OrderStatus, resetPostDate, updatePostStatus } from "./db.ts";
import { Composer, InlineKeyboard } from "grammy";
import { updateList } from "./list.ts";
import { kv } from "./mod.ts";
// Status names
const statusEnum = {
new_: "🆕 Новый",
work: "🛠️ В работе",
out: "📤 Готовые",
recent: "🕒 Недавно выданный",
};
// Generate keyboard
export const generateKeyboard = (id: number, status: OrderStatus) => {
const keyboard = new InlineKeyboard();
switch (status) {
case "new_":
keyboard.text("В работу →", `work-${id}`).row();
break;
case "work":
keyboard
.text("← В новые", `new-${id}`)
.row()
.text("Готов →", `out-${id}`)
.row();
break;
case "out":
keyboard
.text("← В работу", `work-${id}`)
.row()
.text("Выдать →", `recent-${id}`)
.row();
break;
case "recent":
keyboard.text("← В готовые", `out-${id}`).row();
break;
}
keyboard.text("Сбросить дату", `reset-${id}`);
return keyboard;
};
// Generate text above keyboard XD
export const generateText = (status: OrderStatus) => {
return statusEnum[status];
};
export const keyboardComposer = new Composer();
// Status changes
keyboardComposer.callbackQuery(/new-[0-9]+/, async (ctx) => {
const postId = Number(ctx.callbackQuery.data.split("-")[1]);
await updatePostStatus(postId, "new_");
await ctx.editMessageText(generateText("new_"), {
reply_markup: generateKeyboard(postId, "new_"),
});
await updateList();
if (!ctx.chat) return;
await ctx.api.sendMessage(
ctx.chat.id,
"🆕 Новый (" + ctx.from.first_name + ")",
{
reply_parameters: {
message_id:
ctx.callbackQuery.message?.reply_to_message?.message_id || 0,
},
disable_notification: true,
},
);
await ctx.answerCallbackQuery();
});
keyboardComposer.callbackQuery(/work-[0-9]+/, async (ctx) => {
const postId = Number(ctx.callbackQuery.data.split("-")[1]);
await updatePostStatus(postId, "work");
await ctx.editMessageText(generateText("work"), {
reply_markup: generateKeyboard(postId, "work"),
});
await updateList();
if (!ctx.chat) return;
await ctx.api.sendMessage(
ctx.chat.id,
"🛠️ В работе (" + ctx.from.first_name + ")",
{
reply_parameters: {
message_id:
ctx.callbackQuery.message?.reply_to_message?.message_id || 0,
},
disable_notification: true,
},
);
await ctx.answerCallbackQuery();
});
keyboardComposer.callbackQuery(/out-[0-9]+/, async (ctx) => {
const postId = Number(ctx.callbackQuery.data.split("-")[1]);
await updatePostStatus(postId, "out");
await ctx.editMessageText(generateText("out"), {
reply_markup: generateKeyboard(postId, "out"),
});
await updateList();
if (!ctx.chat) return;
await ctx.api.sendMessage(
ctx.chat.id,
"📤 Готовый (" + ctx.from.first_name + ")",
{
reply_parameters: {
message_id:
ctx.callbackQuery.message?.reply_to_message?.message_id || 0,
},
disable_notification: true,
},
);
await ctx.answerCallbackQuery();
});
keyboardComposer.callbackQuery(/recent-[0-9]+/, async (ctx) => {
const postId = Number(ctx.callbackQuery.data.split("-")[1]);
await updatePostStatus(postId, "recent");
await ctx.editMessageText(generateText("recent"), {
reply_markup: generateKeyboard(postId, "recent"),
});
await updateList();
await requestRecentDelete(postId); // ESPECIALLY FOR RECENT
if (!ctx.chat) return;
await ctx.api.sendMessage(
ctx.chat.id,
"🕒 Недавно выданный (" + ctx.from.first_name + ")",
{
reply_parameters: {
message_id:
ctx.callbackQuery.message?.reply_to_message?.message_id || 0,
},
disable_notification: true,
},
);
await ctx.answerCallbackQuery();
});
keyboardComposer.callbackQuery(/reset-[0-9]+/, async (ctx) => {
const postId = Number(ctx.callbackQuery.data.split("-")[1]);
await resetPostDate(postId);
try {
await updateList();
await ctx.answerCallbackQuery();
} catch {
await ctx.answerCallbackQuery();
}
});
// hehe
const requestRecentDelete = async (postId: number) => {
await kv.enqueue(postId, { delay: 2 * 24 * 60 * 60 * 1000 });
};