-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.ts
58 lines (50 loc) · 1.58 KB
/
list.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
import { Composer } from "grammy";
import { listPosts, setList } from "./db.ts";
import { channelId } from "./mod.ts";
import { getList } from "./db.ts";
import { bot } from "./mod.ts";
export const listComposer = new Composer();
// List init command
listComposer.chatType("channel").command("list", async (ctx) => {
await setList(ctx.msgId);
await ctx.editMessageText(await generateListText(), { parse_mode: "HTML" });
});
// List names
const listEnum = {
new_: "🆕 Новые",
work: "🛠️ В работе",
out: "📤 Готовые",
recent: "🕒 Недавно выданные",
};
// Generate list names
const generateListText = async () => {
const posts = await listPosts();
const { new_, work, out, recent } = Object.groupBy(posts, (x) => x.status);
const text = [new_, work, out, recent].map((list) =>
list
? listEnum[list[0].status] + "\n" +
list.map((e, i) =>
`${i + 1}. ${
e.createdAt.toLocaleDateString("ru", {
timeZone: "Asia/Yekaterinburg",
day: "numeric",
month: "short",
})
} <a href="https://t.me/c/${
channelId.toString().slice(3)
}/${e.id}">${e.caption}</a>`
).join("\n")
: ""
)
.filter((text) => text.length)
.join("\n\n");
return text.length ? text : "Заказ-нарядов нет.";
};
// Update the list
export const updateList = async () => {
const msgId = await getList();
if (!msgId) return;
await bot.api.editMessageText(channelId, msgId, await generateListText(), {
parse_mode: "HTML",
});
};