Skip to content

Commit

Permalink
Add export command
Browse files Browse the repository at this point in the history
  • Loading branch information
simon renlund committed Jan 25, 2024
1 parent e3f5290 commit b7e5b87
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions backend/.env.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BOT_TOKEN=""
CHAT_ID=""
ADMIN_CHAT_ID=""
PGDATABASE="spiken"
PGHOST="localhost"
PGUSER="spiken"
Expand Down
2 changes: 2 additions & 0 deletions backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import 'dotenv/config'
const processEnvSchema = z.object({
BOT_TOKEN: z.string(),
CHAT_ID: z.string().transform((val) => Number(val)),
ADMIN_CHAT_ID: z.string().transform((val) => Number(val)),
})
const typedProcessEnv = processEnvSchema.parse(process.env)
export const config = {
botToken: typedProcessEnv.BOT_TOKEN,
chatId: typedProcessEnv.CHAT_ID,
adminChatId: typedProcessEnv.ADMIN_CHAT_ID,
}
37 changes: 31 additions & 6 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Context, Telegraf } from 'telegraf'
import { config } from './config.js'
import { getBalanceForMember, purchaseItemForMember } from './transactions.js'
import {
exportTransactions,
getBalanceForMember,
purchaseItemForMember,
} from './transactions.js'
import { Message, Update } from '@telegraf/types'

/*
Toiveiden tynnyri:
- Admin interface, lägg till/ta bort produkter, csv transaction dump, lägg till/ta bort saldo
- Admin interface, lägg till/ta bort produkter, lägg till/ta bort saldo
- Skamlistan, posta alla med negativt i chatten med en @
- En 'vapaa myynti' command med description och summa
- "Undo" funktionalitet
Expand All @@ -24,7 +28,7 @@ bot.use(async (ctx, next) => {
if (!ctx.from) {
return
}
if (!(await isChatMember(ctx.from.id))) {
if (!(await isChatMember(ctx.from.id, config.chatId))) {
return ctx.reply('sii dej i reven!')
}
await next()
Expand Down Expand Up @@ -110,6 +114,27 @@ bot.command('start', async (ctx) => {
return ctx.reply(info_message)
})

bot.command('exportera', async (ctx) => {
if (!(await isChatMember(ctx.from.id, config.adminChatId))) {
return ctx.reply('sii dej i reven, pleb!')
}
const res = await exportTransactions()
const headers = res.fields.map((field) => field.name)
const rows = res.rows.map((row) => {
return headers
.map((header) => {
return (String(row[header]) ?? '').replace(',', '')
})
.join(', ')
})
const csv = `${headers.join(', ')}
${rows.join('\n')}`
ctx.replyWithDocument({
source: Buffer.from(csv, 'utf-8'),
filename: `spiken-dump-${new Date().toISOString()}.csv`,
})
})

bot.telegram.setMyCommands([
...commands.map(({ command, description, priceCents }) => ({
command,
Expand All @@ -118,7 +143,7 @@ bot.telegram.setMyCommands([
).toFixed(2)}€`,
})),
{ command: 'saldo', description: 'Kontrollera saldo' },
{ command: 'info', description: 'Visar information om bottens användning'}
{ command: 'info', description: 'Visar information om bottens användning' },
])

bot.launch()
Expand All @@ -127,10 +152,10 @@ bot.launch()
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))

const isChatMember = async (userId: number) => {
const isChatMember = async (userId: number, chatId: number) => {
const acceptedStatuses = ['creator', 'administrator', 'member', 'owner']
try {
const member = await bot.telegram.getChatMember(config.chatId, userId)
const member = await bot.telegram.getChatMember(chatId, userId)
return acceptedStatuses.includes(member.status)
} catch (e) {
console.log('Error checking group membership:', e)
Expand Down
5 changes: 5 additions & 0 deletions backend/src/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export const getBalanceForMember = async (userId: number) => {
}
}

export const exportTransactions = async () => {
const res = await pool.query(`SELECT * FROM transactions`)
return res
}

const BalanceResponseSchema = z
.array(
z.object({
Expand Down

0 comments on commit b7e5b87

Please sign in to comment.